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