1
2
3
4
5
6
7
8
9
10
11
12
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.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27
28 public class LevelChangePropagatorTest {
29 int rand = RandomUtil.getPositiveInt();
30 LoggerContext loggerContext = new LoggerContext();
31 LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
32
33 @BeforeEach
34 public void setUp() {
35 levelChangePropagator.setContext(loggerContext);
36 loggerContext.addListener(levelChangePropagator);
37 }
38
39 void checkLevelChange(String loggerName, Level level) {
40 Logger logger = loggerContext.getLogger(loggerName);
41 logger.setLevel(level);
42 java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
43 java.util.logging.Level julLevel = JULHelper.asJULLevel(level);
44
45 assertEquals(julLevel, julLogger.getLevel());
46 }
47
48
49
50 @Test
51 public void smoke() {
52 checkLevelChange("a", Level.INFO);
53 checkLevelChange("a.b", Level.DEBUG);
54 }
55
56 @Test
57 public void root() {
58 checkLevelChange(Logger.ROOT_LOGGER_NAME, Level.TRACE);
59 }
60
61
62 @Test
63 public void gc() {
64 Logger logger = loggerContext.getLogger("gc" + rand);
65 logger.setLevel(Level.INFO);
66
67 System.gc();
68 java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
69 java.util.logging.Level julLevel = JULHelper.asJULLevel(Level.INFO);
70
71 assertEquals(julLevel, julLogger.getLevel());
72 }
73
74
75 @Test
76 public void jonathan() {
77 Level level = Level.INFO;
78 Logger logger = loggerContext.getLogger("aaa");
79 logger.setLevel(level);
80
81 java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
82 java.util.logging.Level julLevel = JULHelper.asJULLevel(level);
83
84 assertFalse(julLogger.isLoggable(java.util.logging.Level.CONFIG));
85 }
86
87 @Test
88 public void julHelperAsJulLevelRejectsNull() {
89 Exception e = assertThrows(IllegalArgumentException.class, () -> {
90 JULHelper.asJULLevel(null);
91 });
92 assertEquals("Unexpected level [null]", e.getMessage());
93 }
94
95 @Test
96 public void settingLevelToNullGetsParentLevel() {
97
98 Logger parent = loggerContext.getLogger("a");
99 parent.setLevel(Level.DEBUG);
100
101
102
103 Logger child = loggerContext.getLogger("a.b");
104 child.setLevel(Level.INFO);
105 child.setLevel(null);
106
107 assertEquals(parent.getEffectiveLevel(), child.getEffectiveLevel());
108 assertEquals(Level.DEBUG, child.getEffectiveLevel());
109 }
110 }