001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, 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.jul;
015
016import ch.qos.logback.classic.Level;
017import ch.qos.logback.classic.Logger;
018import ch.qos.logback.classic.LoggerContext;
019import ch.qos.logback.core.testUtil.RandomUtil;
020
021import org.junit.Before;
022import org.junit.Rule;
023import org.junit.Test;
024import org.junit.rules.ExpectedException;
025
026import static org.junit.Assert.assertEquals;
027
028public class LevelChangePropagatorTest {
029    int rand = RandomUtil.getPositiveInt();
030    LoggerContext loggerContext = new LoggerContext();
031    LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
032
033    @Rule
034    public ExpectedException exception = ExpectedException.none();
035
036    @Before
037    public void setUp() {
038        levelChangePropagator.setContext(loggerContext);
039        loggerContext.addListener(levelChangePropagator);
040    }
041
042    void checkLevelChange(String loggerName, Level level) {
043        Logger logger = loggerContext.getLogger(loggerName);
044        logger.setLevel(level);
045        java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
046        java.util.logging.Level julLevel = JULHelper.asJULLevel(level);
047
048        assertEquals(julLevel, julLogger.getLevel());
049    }
050
051    @Test
052    public void smoke() {
053        checkLevelChange("a", Level.INFO);
054        checkLevelChange("a.b", Level.DEBUG);
055    }
056
057    @Test
058    public void root() {
059        checkLevelChange(Logger.ROOT_LOGGER_NAME, Level.TRACE);
060    }
061
062    // see http://jira.qos.ch/browse/LBCLASSIC-256
063    @Test
064    public void gc() {
065        Logger logger = loggerContext.getLogger("gc" + rand);
066        logger.setLevel(Level.INFO);
067        // invoke GC so that the relevant julLogger can be garbage collected.
068        System.gc();
069        java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
070        java.util.logging.Level julLevel = JULHelper.asJULLevel(Level.INFO);
071
072        assertEquals(julLevel, julLogger.getLevel());
073    }
074
075    @Test
076    public void julHelperAsJulLevelRejectsNull() {
077        exception.expect(IllegalArgumentException.class);
078        exception.expectMessage("Unexpected level [null]");
079        JULHelper.asJULLevel(null);
080    }
081
082    @Test
083    public void settingLevelToNullGetsParentLevel() {
084        // first set level of "a" (the parent) to DEBUG
085        Logger parent = loggerContext.getLogger("a");
086        parent.setLevel(Level.DEBUG);
087
088        // then set level of "a.b" (child logger of a) to null
089        // for b to inherit its parent's level
090        Logger child = loggerContext.getLogger("a.b");
091        child.setLevel(Level.INFO);
092        child.setLevel(null);
093
094        assertEquals(parent.getEffectiveLevel(), child.getEffectiveLevel());
095        assertEquals(Level.DEBUG, child.getEffectiveLevel());
096    }
097}