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.jul;
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.core.testUtil.RandomUtil;
20  
21  import org.junit.Before;
22  import org.junit.Rule;
23  import org.junit.Test;
24  import org.junit.rules.ExpectedException;
25  
26  import static org.junit.Assert.assertEquals;
27  
28  public class LevelChangePropagatorTest {
29      int rand = RandomUtil.getPositiveInt();
30      LoggerContext loggerContext = new LoggerContext();
31      LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
32  
33      @Rule
34      public ExpectedException exception = ExpectedException.none();
35  
36      @Before
37      public void setUp() {
38          levelChangePropagator.setContext(loggerContext);
39          loggerContext.addListener(levelChangePropagator);
40      }
41  
42      void checkLevelChange(String loggerName, Level level) {
43          Logger logger = loggerContext.getLogger(loggerName);
44          logger.setLevel(level);
45          java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
46          java.util.logging.Level julLevel = JULHelper.asJULLevel(level);
47  
48          assertEquals(julLevel, julLogger.getLevel());
49      }
50  
51      @Test
52      public void smoke() {
53          checkLevelChange("a", Level.INFO);
54          checkLevelChange("a.b", Level.DEBUG);
55      }
56  
57      @Test
58      public void root() {
59          checkLevelChange(Logger.ROOT_LOGGER_NAME, Level.TRACE);
60      }
61  
62      // see http://jira.qos.ch/browse/LBCLASSIC-256
63      @Test
64      public void gc() {
65          Logger logger = loggerContext.getLogger("gc" + rand);
66          logger.setLevel(Level.INFO);
67          // invoke GC so that the relevant julLogger can be garbage collected.
68          System.gc();
69          java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
70          java.util.logging.Level julLevel = JULHelper.asJULLevel(Level.INFO);
71  
72          assertEquals(julLevel, julLogger.getLevel());
73      }
74  
75      @Test
76      public void julHelperAsJulLevelRejectsNull() {
77          exception.expect(IllegalArgumentException.class);
78          exception.expectMessage("Unexpected level [null]");
79          JULHelper.asJULLevel(null);
80      }
81  
82      @Test
83      public void settingLevelToNullGetsParentLevel() {
84          // first set level of "a" (the parent) to DEBUG
85          Logger parent = loggerContext.getLogger("a");
86          parent.setLevel(Level.DEBUG);
87  
88          // then set level of "a.b" (child logger of a) to null
89          // for b to inherit its parent's level
90          Logger child = loggerContext.getLogger("a.b");
91          child.setLevel(Level.INFO);
92          child.setLevel(null);
93  
94          assertEquals(parent.getEffectiveLevel(), child.getEffectiveLevel());
95          assertEquals(Level.DEBUG, child.getEffectiveLevel());
96      }
97  }