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