001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2016, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.jmx;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertFalse;
018import static org.junit.Assert.assertNotNull;
019import static org.junit.Assert.assertNull;
020import static org.junit.Assert.assertTrue;
021
022import java.lang.management.ManagementFactory;
023import java.util.List;
024
025import javax.management.MBeanServer;
026import javax.management.ObjectName;
027
028import org.junit.After;
029import org.junit.Before;
030import org.junit.Test;
031
032import ch.qos.logback.classic.Level;
033import ch.qos.logback.classic.Logger;
034import ch.qos.logback.classic.LoggerContext;
035import ch.qos.logback.classic.spi.LoggerContextListener;
036import ch.qos.logback.core.testUtil.RandomUtil;
037
038import static org.slf4j.Logger.ROOT_LOGGER_NAME;
039
040public class JMXConfiguratorTest {
041
042    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
043    LoggerContext lc = new LoggerContext();
044    Logger testLogger = lc.getLogger(this.getClass());
045
046    List<LoggerContextListener> listenerList;
047    int diff = RandomUtil.getPositiveInt();
048
049    @Before
050    public void setUp() throws Exception {
051        lc.setName("context-" + diff);
052        assertNotNull(mbs);
053    }
054
055    @After
056    public void tearDown() throws Exception {
057        lc.stop();
058    }
059
060    @Override
061    public String toString() {
062        return this.getClass().getName() + "(" + lc.getName() + ")";
063    }
064
065    @Test
066    public void contextReset() throws Exception {
067        String randomizedObjectNameAsStr = "ch.qos.logback." + diff + ":Name=" + lc.getName() + ",Type=" + this.getClass().getName();
068
069        ObjectName objectName = MBeanUtil.string2ObjectName(lc, this, randomizedObjectNameAsStr);
070        JMXConfigurator jmxConfigurator = new JMXConfigurator(lc, mbs, objectName);
071        mbs.registerMBean(jmxConfigurator, objectName);
072
073        listenerList = lc.getCopyOfListenerList();
074        assertEquals(1, listenerList.size());
075
076        lc.reset();
077
078        // check that after lc.reset, jmxConfigurator should still be
079        // registered as a listener in the loggerContext and also as an
080        // MBean in mbs
081        listenerList = lc.getCopyOfListenerList();
082        assertEquals(1, listenerList.size());
083        assertTrue(listenerList.contains(jmxConfigurator));
084
085        assertTrue(mbs.isRegistered(objectName));
086    }
087
088    @Test
089    public void contextStop() throws Exception {
090        String randomizedObjectNameAsStr = "ch.qos.logback." + diff + ":Name=" + lc.getName() + ",Type=" + this.getClass().getName();
091
092        ObjectName objectName = MBeanUtil.string2ObjectName(lc, this, randomizedObjectNameAsStr);
093        JMXConfigurator jmxConfigurator = new JMXConfigurator(lc, mbs, objectName);
094        mbs.registerMBean(jmxConfigurator, objectName);
095
096        listenerList = lc.getCopyOfListenerList();
097        assertEquals(1, listenerList.size());
098
099        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}