1
2
3
4
5
6
7
8
9
10
11
12
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.ILoggingEvent;
20 import ch.qos.logback.classic.spi.LoggingEvent;
21 import org.junit.jupiter.api.AfterEach;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.slf4j.IMarkerFactory;
25 import org.slf4j.Marker;
26 import org.slf4j.helpers.BasicMarkerFactory;
27
28 import static org.junit.jupiter.api.Assertions.assertEquals;
29
30 public class MarkerConverterTest {
31
32 LoggerContext lc;
33 MarkerConverter converter;
34
35 IMarkerFactory markerFactory = new BasicMarkerFactory();
36
37 @BeforeEach
38 public void setUp() throws Exception {
39 lc = new LoggerContext();
40 converter = new MarkerConverter();
41 converter.start();
42 }
43
44 @AfterEach
45 public void tearDown() throws Exception {
46 lc = null;
47 converter.stop();
48 converter = null;
49 }
50
51 @Test
52 public void testWithNullMarker() {
53 String result = converter.convert(createLoggingEvent(null));
54 assertEquals("", result);
55 }
56
57 @Test
58 public void testWithMarker() {
59 String name = "test";
60 Marker marker = markerFactory.getMarker(name);
61 String result = converter.convert(createLoggingEvent(marker));
62 assertEquals(name, result);
63 }
64
65 @Test
66 public void testWithOneChildMarker() {
67 Marker marker = markerFactory.getMarker("test");
68 marker.add(markerFactory.getMarker("child"));
69
70 String result = converter.convert(createLoggingEvent(marker));
71
72 assertEquals("test [ child ]", result);
73 }
74
75 @Test
76 public void testWithSeveralChildMarker() {
77 Marker marker = markerFactory.getMarker("testParent");
78 marker.add(markerFactory.getMarker("child1"));
79 marker.add(markerFactory.getMarker("child2"));
80 marker.add(markerFactory.getMarker("child3"));
81
82 String result = converter.convert(createLoggingEvent(marker));
83
84 assertEquals("testParent [ child1, child2, child3 ]", result);
85 }
86
87 private ILoggingEvent createLoggingEvent(Marker marker) {
88 LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME),
89 Level.DEBUG, "test message", null, null);
90 le.addMarker(marker);
91 return le;
92 }
93 }