View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 static org.junit.Assert.assertEquals;
17  
18  import org.junit.After;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.slf4j.IMarkerFactory;
22  import org.slf4j.Marker;
23  import org.slf4j.helpers.BasicMarkerFactory;
24  
25  import ch.qos.logback.classic.Level;
26  import ch.qos.logback.classic.Logger;
27  import ch.qos.logback.classic.LoggerContext;
28  import ch.qos.logback.classic.spi.ILoggingEvent;
29  import ch.qos.logback.classic.spi.LoggingEvent;
30  
31  public class MarkerConverterTest {
32  
33      LoggerContext lc;
34      MarkerConverter converter;
35      // use a different facotry for each test so that they are independent
36      IMarkerFactory markerFactory = new BasicMarkerFactory();
37  
38      @Before
39      public void setUp() throws Exception {
40          lc = new LoggerContext();
41          converter = new MarkerConverter();
42          converter.start();
43      }
44  
45      @After
46      public void tearDown() throws Exception {
47          lc = null;
48          converter.stop();
49          converter = null;
50      }
51  
52      @Test
53      public void testWithNullMarker() {
54          String result = converter.convert(createLoggingEvent(null));
55          assertEquals("", result);
56      }
57  
58      @Test
59      public void testWithMarker() {
60          String name = "test";
61          Marker marker = markerFactory.getMarker(name);
62          String result = converter.convert(createLoggingEvent(marker));
63          assertEquals(name, result);
64      }
65  
66      @Test
67      public void testWithOneChildMarker() {
68          Marker marker = markerFactory.getMarker("test");
69          marker.add(markerFactory.getMarker("child"));
70  
71          String result = converter.convert(createLoggingEvent(marker));
72  
73          assertEquals("test [ child ]", result);
74      }
75  
76      @Test
77      public void testWithSeveralChildMarker() {
78          Marker marker = markerFactory.getMarker("testParent");
79          marker.add(markerFactory.getMarker("child1"));
80          marker.add(markerFactory.getMarker("child2"));
81          marker.add(markerFactory.getMarker("child3"));
82  
83          String result = converter.convert(createLoggingEvent(marker));
84  
85          assertEquals("testParent [ child1, child2, child3 ]", result);
86      }
87  
88      private ILoggingEvent createLoggingEvent(Marker marker) {
89          LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG, "test message", null, null);
90          le.setMarker(marker);
91          return le;
92      }
93  }