View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2016, 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.jmx;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.lang.management.ManagementFactory;
23  import java.util.List;
24  
25  import javax.management.MBeanServer;
26  import javax.management.ObjectName;
27  
28  import org.junit.After;
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  import ch.qos.logback.classic.Level;
33  import ch.qos.logback.classic.Logger;
34  import ch.qos.logback.classic.LoggerContext;
35  import ch.qos.logback.classic.spi.LoggerContextListener;
36  import ch.qos.logback.core.testUtil.RandomUtil;
37  
38  import static org.slf4j.Logger.ROOT_LOGGER_NAME;
39  
40  public class JMXConfiguratorTest {
41  
42      MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
43      LoggerContext lc = new LoggerContext();
44      Logger testLogger = lc.getLogger(this.getClass());
45  
46      List<LoggerContextListener> listenerList;
47      int diff = RandomUtil.getPositiveInt();
48  
49      @Before
50      public void setUp() throws Exception {
51          lc.setName("context-" + diff);
52          assertNotNull(mbs);
53      }
54  
55      @After
56      public void tearDown() throws Exception {
57          lc.stop();
58      }
59  
60      @Override
61      public String toString() {
62          return this.getClass().getName() + "(" + lc.getName() + ")";
63      }
64  
65      @Test
66      public void contextReset() throws Exception {
67          String randomizedObjectNameAsStr = "ch.qos.logback." + diff + ":Name=" + lc.getName() + ",Type=" + this.getClass().getName();
68  
69          ObjectName objectName = MBeanUtil.string2ObjectName(lc, this, randomizedObjectNameAsStr);
70          JMXConfigurator jmxConfigurator = new JMXConfigurator(lc, mbs, objectName);
71          mbs.registerMBean(jmxConfigurator, objectName);
72  
73          listenerList = lc.getCopyOfListenerList();
74          assertEquals(1, listenerList.size());
75  
76          lc.reset();
77  
78          // check that after lc.reset, jmxConfigurator should still be
79          // registered as a listener in the loggerContext and also as an
80          // MBean in mbs
81          listenerList = lc.getCopyOfListenerList();
82          assertEquals(1, listenerList.size());
83          assertTrue(listenerList.contains(jmxConfigurator));
84  
85          assertTrue(mbs.isRegistered(objectName));
86      }
87  
88      @Test
89      public void contextStop() throws Exception {
90          String randomizedObjectNameAsStr = "ch.qos.logback." + diff + ":Name=" + lc.getName() + ",Type=" + this.getClass().getName();
91  
92          ObjectName objectName = MBeanUtil.string2ObjectName(lc, this, randomizedObjectNameAsStr);
93          JMXConfigurator jmxConfigurator = new JMXConfigurator(lc, mbs, objectName);
94          mbs.registerMBean(jmxConfigurator, objectName);
95  
96          listenerList = lc.getCopyOfListenerList();
97          assertEquals(1, listenerList.size());
98  
99          lc.stop();
100 
101         // check that after lc.processPriorToRemoval, jmxConfigurator is no longer
102         // registered as a listener in the loggerContext nor as an
103         // MBean in mbs
104         listenerList = lc.getCopyOfListenerList();
105         assertEquals(0, listenerList.size());
106 
107         assertFalse(mbs.isRegistered(objectName));
108     }
109 
110     @Test
111     public void testNonRemovalOfPreviousIntanceFromTheContextListenerList() {
112         String objectNameAsStr = "ch.qos.logback.toto" + ":Name=" + lc.getName() + ",Type=" + this.getClass().getName();
113         ObjectName objectName = MBeanUtil.string2ObjectName(lc, this, objectNameAsStr);
114         JMXConfigurator jmxConfigurator0 = new JMXConfigurator(lc, mbs, objectName);
115 
116         listenerList = lc.getCopyOfListenerList();
117         assertTrue(listenerList.contains(jmxConfigurator0));
118 
119         JMXConfigurator jmxConfigurator1 = new JMXConfigurator(lc, mbs, objectName);
120         listenerList = lc.getCopyOfListenerList();
121         assertEquals(1, listenerList.size());
122         assertTrue("old configurator should be present", listenerList.contains(jmxConfigurator0));
123         assertFalse("new configurator should be absent", listenerList.contains(jmxConfigurator1));
124     }
125 
126     @Test
127     public void getLoggerLevel_LBCLASSIC_78() {
128         String objectNameAsStr = "ch.qos" + diff + ":Name=" + lc.getName() + ",Type=" + this.getClass().getName();
129 
130         ObjectName on = MBeanUtil.string2ObjectName(lc, this, objectNameAsStr);
131         JMXConfigurator configurator = new JMXConfigurator(lc, mbs, on);
132         assertEquals("", configurator.getLoggerLevel(testLogger.getName()));
133         MBeanUtil.unregister(lc, mbs, on, this);
134     }
135 
136     @Test
137     public void setLoggerLevel_LBCLASSIC_79() {
138         String objectNameAsStr = "ch.qos" + diff + ":Name=" + lc.getName() + ",Type=" + this.getClass().getName();
139 
140         ObjectName on = MBeanUtil.string2ObjectName(lc, this, objectNameAsStr);
141         JMXConfigurator configurator = new JMXConfigurator(lc, mbs, on);
142         configurator.setLoggerLevel(testLogger.getName(), "DEBUG");
143         assertEquals(Level.DEBUG, testLogger.getLevel());
144 
145         configurator.setLoggerLevel(testLogger.getName(), "null");
146         assertNull(testLogger.getLevel());
147 
148         MBeanUtil.unregister(lc, mbs, on, this);
149     }
150 
151     @Test
152     public void testReloadDefaultConfiguration() throws Exception {
153         String objectNameAsStr = "ch.qos" + diff + ":Name=" + lc.getName() + ",Type=" + this.getClass().getName();
154 
155         ObjectName on = MBeanUtil.string2ObjectName(lc, this, objectNameAsStr);
156         JMXConfigurator configurator = new JMXConfigurator(lc, mbs, on);
157         configurator.setLoggerLevel(testLogger.getName(), "DEBUG");
158         assertEquals(Level.DEBUG, testLogger.getLevel());
159 
160         configurator.reloadDefaultConfiguration();
161         assertNull(testLogger.getLevel());
162         assertEquals(Level.DEBUG, lc.getLogger(ROOT_LOGGER_NAME).getLevel());
163         MBeanUtil.unregister(lc, mbs, on, this);
164     }
165 
166 }