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.spi;
015
016import static org.junit.Assert.assertEquals;
017
018import ch.qos.logback.classic.Level;
019import ch.qos.logback.classic.Logger;
020import org.junit.Before;
021import org.junit.Test;
022
023import ch.qos.logback.classic.LoggerContext;
024import ch.qos.logback.classic.spi.BasicContextListener.UpdateType;
025
026public class ContextListenerTest {
027
028    LoggerContext context;
029    BasicContextListener listener;
030
031    @Before
032    public void setUp() throws Exception {
033        context = new LoggerContext();
034        listener = new BasicContextListener();
035        context.addListener(listener);
036    }
037
038    @Test
039    public void testNotifyOnReset() {
040        context.reset();
041        assertEquals(UpdateType.RESET, listener.updateType);
042        assertEquals(listener.context, context);
043    }
044
045    @Test
046    public void testNotifyOnStopResistant() {
047        listener.setResetResistant(true);
048        context.stop();
049        assertEquals(UpdateType.STOP, listener.updateType);
050        assertEquals(listener.context, context);
051    }
052
053    @Test
054    public void testNotifyOnStopNotResistant() {
055        context.stop();
056        assertEquals(UpdateType.RESET, listener.updateType);
057        assertEquals(listener.context, context);
058    }
059
060    @Test
061    public void testNotifyOnStart() {
062        context.start();
063        assertEquals(UpdateType.START, listener.updateType);
064        assertEquals(listener.context, context);
065    }
066
067    void checkLevelChange(String loggerName, Level level) {
068        Logger logger = context.getLogger(loggerName);
069        logger.setLevel(level);
070
071        assertEquals(UpdateType.LEVEL_CHANGE, listener.updateType);
072        assertEquals(listener.logger, logger);
073        assertEquals(listener.level, level);
074
075    }
076
077    @Test
078    public void testLevelChange() {
079        checkLevelChange("a", Level.INFO);
080        checkLevelChange("a.b", Level.ERROR);
081        checkLevelChange("a.b.c", Level.DEBUG);
082    }
083}