1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic;
15
16 import ch.qos.logback.classic.turbo.NOPTurboFilter;
17 import ch.qos.logback.core.CoreConstants;
18 import ch.qos.logback.core.rolling.helper.FileNamePattern;
19 import ch.qos.logback.core.status.StatusManager;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22
23 import java.util.Map;
24
25 import static ch.qos.logback.core.CoreConstants.FA_FILENAME_COLLISION_MAP;
26 import static org.junit.jupiter.api.Assertions.*;
27
28 public class LoggerContextTest {
29 LoggerContext lc;
30
31 @BeforeEach
32 public void setUp() throws Exception {
33 lc = new LoggerContext();
34 lc.setName("x");
35 }
36
37 @Test
38 public void testRootGetLogger() {
39 Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
40 assertEquals(Level.DEBUG, root.getLevel());
41 assertEquals(Level.DEBUG, root.getEffectiveLevel());
42 }
43
44 @Test
45 public void testLoggerX() {
46 Logger x = lc.getLogger("x");
47 assertNotNull(x);
48 assertEquals("x", x.getName());
49 assertNull(x.getLevel());
50 assertEquals(Level.DEBUG, x.getEffectiveLevel());
51 }
52
53 @Test
54 public void testNull() {
55 try {
56 lc.getLogger((String) null);
57 fail("null should cause an exception");
58 } catch (IllegalArgumentException e) {
59 }
60 }
61
62 @Test
63 public void testEmpty() {
64 Logger empty = lc.getLogger("");
65 LoggerTestHelper.assertNameEquals(empty, "");
66 LoggerTestHelper.assertLevels(null, empty, Level.DEBUG);
67
68 Logger dot = lc.getLogger(".");
69 LoggerTestHelper.assertNameEquals(dot, ".");
70
71
72
73
74 LoggerTestHelper.assertLevels(null, dot, Level.DEBUG);
75
76 assertEquals(3, lc.getLoggerList().size());
77 }
78
79 @Test
80 public void testDotDot() {
81 Logger dotdot = lc.getLogger("..");
82 assertEquals(4, lc.getLoggerList().size());
83 LoggerTestHelper.assertNameEquals(dotdot, "..");
84
85
86
87 }
88
89 int instanceCount() {
90 return lc.getLoggerList().size();
91 }
92
93 @Test
94 public void testLoggerXY() {
95 assertEquals(1, lc.getLoggerList().size());
96
97 Logger xy = lc.getLogger("x.y");
98 assertEquals(3, instanceCount());
99 LoggerTestHelper.assertNameEquals(xy, "x.y");
100 LoggerTestHelper.assertLevels(null, xy, Level.DEBUG);
101
102 Logger x = lc.getLogger("x");
103 assertEquals(3, instanceCount());
104
105 Logger xy2 = lc.getLogger("x.y");
106 assertEquals(xy, xy2);
107
108 Logger x2 = lc.getLogger("x");
109 assertEquals(x, x2);
110 assertEquals(3, instanceCount());
111 }
112
113 @Test
114 public void testLoggerMultipleChildren() {
115 assertEquals(1, instanceCount());
116 Logger xy0 = lc.getLogger("x.y0");
117 LoggerTestHelper.assertNameEquals(xy0, "x.y0");
118
119 Logger xy1 = lc.getLogger("x.y1");
120 LoggerTestHelper.assertNameEquals(xy1, "x.y1");
121
122 LoggerTestHelper.assertLevels(null, xy0, Level.DEBUG);
123 LoggerTestHelper.assertLevels(null, xy1, Level.DEBUG);
124 assertEquals(4, instanceCount());
125
126 for (int i = 0; i < 100; i++) {
127 Logger xy_i = lc.getLogger("x.y" + i);
128 LoggerTestHelper.assertNameEquals(xy_i, "x.y" + i);
129 LoggerTestHelper.assertLevels(null, xy_i, Level.DEBUG);
130 }
131 assertEquals(102, instanceCount());
132 }
133
134 @Test
135 public void testMultiLevel() {
136 Logger wxyz = lc.getLogger("w.x.y.z");
137 LoggerTestHelper.assertNameEquals(wxyz, "w.x.y.z");
138 LoggerTestHelper.assertLevels(null, wxyz, Level.DEBUG);
139
140 Logger wx = lc.getLogger("w.x");
141 wx.setLevel(Level.INFO);
142 LoggerTestHelper.assertNameEquals(wx, "w.x");
143 LoggerTestHelper.assertLevels(Level.INFO, wx, Level.INFO);
144 LoggerTestHelper.assertLevels(null, lc.getLogger("w.x.y"), Level.INFO);
145 LoggerTestHelper.assertLevels(null, wxyz, Level.INFO);
146 }
147
148 @Test
149 public void testStatusWithUnconfiguredContext() {
150 Logger logger = lc.getLogger(LoggerContextTest.class);
151
152 for (int i = 0; i < 3; i++) {
153 logger.debug("test");
154 }
155
156 logger = lc.getLogger("x.y.z");
157
158 for (int i = 0; i < 3; i++) {
159 logger.debug("test");
160 }
161
162 StatusManager sm = lc.getStatusManager();
163 assertTrue(sm.getCount() == 1, "StatusManager has recieved too many messages");
164 }
165
166 @Test
167 public void resetTest() {
168
169 Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
170 Logger a = lc.getLogger("a");
171 Logger ab = lc.getLogger("a.b");
172
173 ab.setLevel(Level.WARN);
174 root.setLevel(Level.INFO);
175 lc.reset();
176 assertEquals(Level.DEBUG, root.getEffectiveLevel());
177 assertTrue(root.isDebugEnabled());
178 assertEquals(Level.DEBUG, a.getEffectiveLevel());
179 assertEquals(Level.DEBUG, ab.getEffectiveLevel());
180
181 assertEquals(Level.DEBUG, root.getLevel());
182 assertNull(a.getLevel());
183 assertNull(ab.getLevel());
184 }
185
186
187 @Test
188 public void turboFilterStopOnReset() {
189 NOPTurboFilter nopTF = new NOPTurboFilter();
190 nopTF.start();
191 lc.addTurboFilter(nopTF);
192 assertTrue(nopTF.isStarted());
193 lc.reset();
194 assertFalse(nopTF.isStarted());
195 }
196
197 @Test
198 public void resetTest_LBCORE_104() {
199 lc.putProperty("keyA", "valA");
200 lc.putObject("keyA", "valA");
201 assertEquals("valA", lc.getProperty("keyA"));
202 assertEquals("valA", lc.getObject("keyA"));
203 lc.reset();
204 assertNull(lc.getProperty("keyA"));
205 assertNull(lc.getObject("keyA"));
206 }
207
208 @Test
209 public void loggerNameEndingInDotOrDollarShouldWork() {
210 {
211 String loggerName = "toto.x.";
212 Logger logger = lc.getLogger(loggerName);
213 assertEquals(loggerName, logger.getName());
214 }
215
216 {
217 String loggerName = "toto.x$";
218 Logger logger = lc.getLogger(loggerName);
219 assertEquals(loggerName, logger.getName());
220 }
221 }
222
223 @Test
224 public void levelResetTest() {
225 Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
226 root.setLevel(Level.TRACE);
227 assertTrue(root.isTraceEnabled());
228 lc.reset();
229 assertFalse(root.isTraceEnabled());
230 assertTrue(root.isDebugEnabled());
231 }
232
233 @Test
234 public void evaluatorMapPostReset() {
235 lc.reset();
236 assertNotNull(lc.getObject(CoreConstants.EVALUATOR_MAP));
237 }
238
239 @SuppressWarnings("unchecked")
240 @Test
241 public void collisionMapsPostReset() {
242 lc.reset();
243
244 Map<String, String> fileCollisions = (Map<String, String>) lc.getObject(FA_FILENAME_COLLISION_MAP);
245 assertNotNull(fileCollisions);
246 assertTrue(fileCollisions.isEmpty());
247
248 Map<String, FileNamePattern> filenamePatternCollisionMap = (Map<String, FileNamePattern>) lc.getObject(
249 CoreConstants.RFA_FILENAME_PATTERN_COLLISION_MAP);
250 assertNotNull(filenamePatternCollisionMap);
251 assertTrue(filenamePatternCollisionMap.isEmpty());
252 }
253
254
255 @Test
256 public void concurrentModification() {
257 final int runLen = 100;
258 Thread thread = new Thread(new Runnable() {
259 public void run() {
260 for (int i = 0; i < runLen; i++) {
261 lc.getLogger("a" + i);
262 Thread.yield();
263 }
264 }
265 });
266 thread.start();
267
268 for (int i = 0; i < runLen; i++) {
269 lc.putProperty("a" + i, "val");
270 Thread.yield();
271 }
272 }
273
274 }