1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.spi;
15
16
17 import ch.qos.logback.classic.Level;
18 import ch.qos.logback.classic.Logger;
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Test;
21
22 import ch.qos.logback.classic.LoggerContext;
23 import ch.qos.logback.classic.spi.BasicContextListener.UpdateType;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26
27 public class ContextListenerTest {
28
29 LoggerContext context;
30 BasicContextListener listener;
31
32 @BeforeEach
33 public void setUp() throws Exception {
34 context = new LoggerContext();
35 listener = new BasicContextListener();
36 context.addListener(listener);
37 }
38
39 @Test
40 public void testNotifyOnReset() {
41 context.reset();
42 assertEquals(UpdateType.RESET, listener.updateType);
43 assertEquals(listener.context, context);
44 }
45
46 @Test
47 public void testNotifyOnStopResistant() {
48 listener.setResetResistant(true);
49 context.stop();
50 assertEquals(UpdateType.STOP, listener.updateType);
51 assertEquals(listener.context, context);
52 }
53
54 @Test
55 public void testNotifyOnStopNotResistant() {
56 context.stop();
57 assertEquals(UpdateType.RESET, listener.updateType);
58 assertEquals(listener.context, context);
59 }
60
61 @Test
62 public void testNotifyOnStart() {
63 context.start();
64 assertEquals(UpdateType.START, listener.updateType);
65 assertEquals(listener.context, context);
66 }
67
68 void checkLevelChange(String loggerName, Level level) {
69 Logger logger = context.getLogger(loggerName);
70 logger.setLevel(level);
71
72 assertEquals(UpdateType.LEVEL_CHANGE, listener.updateType);
73 assertEquals(listener.logger, logger);
74 assertEquals(listener.level, level);
75
76 }
77
78 @Test
79 public void testLevelChange() {
80 checkLevelChange("a", Level.INFO);
81 checkLevelChange("a.b", Level.ERROR);
82 checkLevelChange("a.b.c", Level.DEBUG);
83 }
84 }