View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2021, 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 testWithNullKVPList() {
49          // event.getKeyValuePairs().add(new KeyValuePair("k", "v"));
50          String result = converter.convert(event);
51          assertEquals("", result);
52      }
53  
54      @Test
55      public void testWithOnelKVP() {
56          event.addKeyValuePair(new KeyValuePair("k", "v"));
57          String result = converter.convert(event);
58          assertEquals("k=\"v\"", result);
59      }
60  
61      private LoggingEvent createLoggingEvent() {
62          LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME),
63                  Level.DEBUG, "test message", null, null);
64          return le;
65      }
66  }