001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic; 015 016import static ch.qos.logback.core.CoreConstants.FA_FILENAME_COLLISION_MAP; 017import static org.junit.Assert.assertEquals; 018import static org.junit.Assert.assertFalse; 019import static org.junit.Assert.assertNotNull; 020import static org.junit.Assert.assertNull; 021import static org.junit.Assert.assertTrue; 022import static org.junit.Assert.fail; 023 024import java.util.Map; 025 026import org.junit.Before; 027import org.junit.Test; 028 029import ch.qos.logback.classic.turbo.NOPTurboFilter; 030import ch.qos.logback.core.CoreConstants; 031import ch.qos.logback.core.rolling.helper.FileNamePattern; 032import ch.qos.logback.core.status.StatusManager; 033 034public class LoggerContextTest { 035 LoggerContext lc; 036 037 @Before 038 public void setUp() throws Exception { 039 lc = new LoggerContext(); 040 lc.setName("x"); 041 } 042 043 @Test 044 public void testRootGetLogger() { 045 Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME); 046 assertEquals(Level.DEBUG, root.getLevel()); 047 assertEquals(Level.DEBUG, root.getEffectiveLevel()); 048 } 049 050 @Test 051 public void testLoggerX() { 052 Logger x = lc.getLogger("x"); 053 assertNotNull(x); 054 assertEquals("x", x.getName()); 055 assertNull(x.getLevel()); 056 assertEquals(Level.DEBUG, x.getEffectiveLevel()); 057 } 058 059 @Test 060 public void testNull() { 061 try { 062 lc.getLogger((String) null); 063 fail("null should cause an exception"); 064 } catch (IllegalArgumentException e) { 065 } 066 } 067 068 @Test 069 public void testEmpty() { 070 Logger empty = lc.getLogger(""); 071 LoggerTestHelper.assertNameEquals(empty, ""); 072 LoggerTestHelper.assertLevels(null, empty, Level.DEBUG); 073 074 Logger dot = lc.getLogger("."); 075 LoggerTestHelper.assertNameEquals(dot, "."); 076 // LoggerTestHelper.assertNameEquals(dot.parent, ""); 077 // LoggerTestHelper.assertNameEquals(dot.parent.parent, "root"); 078 079 // assertNull(dot.parent.parent.parent); 080 LoggerTestHelper.assertLevels(null, dot, Level.DEBUG); 081 082 assertEquals(3, lc.getLoggerList().size()); 083 } 084 085 @Test 086 public void testDotDot() { 087 Logger dotdot = lc.getLogger(".."); 088 assertEquals(4, lc.getLoggerList().size()); 089 LoggerTestHelper.assertNameEquals(dotdot, ".."); 090 // LoggerTestHelper.assertNameEquals(dotdot.parent, "."); 091 // LoggerTestHelper.assertNameEquals(dotdot.parent.parent, ""); 092 // LoggerTestHelper.assertNameEquals(dotdot.parent.parent.parent, "root"); 093 } 094 095 int instanceCount() { 096 return lc.getLoggerList().size(); 097 } 098 099 @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("StatusManager has recieved too many messages", sm.getCount() == 1); 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.getObject(CoreConstants.RFA_FILENAME_PATTERN_COLLISION_MAP); 255 assertNotNull(filenamePatternCollisionMap); 256 assertTrue(filenamePatternCollisionMap.isEmpty()); 257 } 258 259 // http://jira.qos.ch/browse/LOGBACK-142 260 @Test 261 public void concurrentModification() { 262 final int runLen = 100; 263 Thread thread = new Thread(new Runnable() { 264 public void run() { 265 for (int i = 0; i < runLen; i++) { 266 lc.getLogger("a" + i); 267 Thread.yield(); 268 } 269 } 270 }); 271 thread.start(); 272 273 for (int i = 0; i < runLen; i++) { 274 lc.putProperty("a" + i, "val"); 275 Thread.yield(); 276 } 277 } 278 279}