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