1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.pattern;
15  
16  import ch.qos.logback.classic.Level;
17  import ch.qos.logback.classic.Logger;
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.spi.LoggingEvent;
20  import org.junit.jupiter.api.AfterEach;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.slf4j.event.KeyValuePair;
24  
25  import static org.junit.jupiter.api.Assertions.assertEquals;
26  
27  public class KeyValuePairConverterTest {
28      LoggerContext lc;
29      KeyValuePairConverter converter;
30      LoggingEvent event;
31  
32      @BeforeEach
33      public void setUp() throws Exception {
34          lc = new LoggerContext();
35          converter = new KeyValuePairConverter();
36          converter.start();
37          event = createLoggingEvent();
38      }
39  
40      @AfterEach
41      public void tearDown() throws Exception {
42          lc = null;
43          converter.stop();
44          converter = null;
45      }
46  
47      @Test
48      public void smoke() {
49          event.addKeyValuePair(new KeyValuePair("a", "b"));
50          event.addKeyValuePair(new KeyValuePair("k", "v"));
51          String result = converter.convert(event);
52          assertEquals("a=\"b\" k=\"v\"", result);
53      }
54  
55      @Test
56      public void testWithNullKVPList() {
57          // event.getKeyValuePairs().add(new KeyValuePair("k", "v"));
58          String result = converter.convert(event);
59          assertEquals("", result);
60      }
61  
62      @Test
63      public void testWithOnelKVP() {
64          event.addKeyValuePair(new KeyValuePair("k", "v"));
65          String result = converter.convert(event);
66          assertEquals("k=\"v\"", result);
67      }
68  
69      private LoggingEvent createLoggingEvent() {
70          LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME),
71                  Level.DEBUG, "test message", null, null);
72          return le;
73      }
74  }