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;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertNull;
018
019import java.util.List;
020import java.util.Map;
021
022import org.junit.Ignore;
023import org.junit.Test;
024
025import ch.qos.logback.classic.control.ControlLogger;
026import ch.qos.logback.classic.control.ControlLoggerContext;
027import ch.qos.logback.classic.control.CreateLogger;
028import ch.qos.logback.classic.control.ScenarioAction;
029import ch.qos.logback.classic.control.ScenarioMaker;
030import ch.qos.logback.classic.control.SetLevel;
031import ch.qos.logback.classic.control.Scenario;
032
033public class ScenarioBasedLoggerContextTest {
034    LoggerContext lc;
035
036    @Test
037    public void testLen3() {
038        doScenarioedTest(3);
039    }
040
041    @Test
042    public void testLength_30() {
043        doScenarioedTest(30);
044    }
045
046    @Test
047    public void testLength_20000() {
048        doScenarioedTest(20 * 1000);
049    }
050
051    @Test
052    @Ignore
053    public void testLengthLong() {
054        doScenarioedTest(100 * 1000);
055    }
056
057    private void doScenarioedTest(int len) {
058        LoggerContext lc = new LoggerContext();
059        ControlLoggerContext controlContext = new ControlLoggerContext();
060        Scenario s = ScenarioMaker.makeRealisticCreationScenario(len);
061        List<ScenarioAction> actionList = s.getActionList();
062        int size = actionList.size();
063        for (int i = 0; i < size; i++) {
064            ScenarioAction action = (ScenarioAction) actionList.get(i);
065            if (action instanceof CreateLogger) {
066                CreateLogger cl = (CreateLogger) action;
067                lc.getLogger(cl.getLoggerName());
068                controlContext.getLogger(cl.getLoggerName());
069            } else if (action instanceof SetLevel) {
070                SetLevel sl = (SetLevel) action;
071                Logger l = lc.getLogger(sl.getLoggerName());
072                ControlLogger controlLogger = controlContext.getLogger(sl.getLoggerName());
073                l.setLevel(sl.getLevel());
074                controlLogger.setLevel(sl.getLevel());
075            }
076        }
077
078        compareLoggerContexts(controlContext, lc);
079    }
080
081    void compareLoggerContexts(ControlLoggerContext controlLC, LoggerContext lc) {
082        Map<String, ControlLogger> controlLoggerMap = controlLC.getLoggerMap();
083
084        assertEquals(controlLoggerMap.size() + 1, lc.size());
085
086        for (String loggerName : controlLoggerMap.keySet()) {
087
088            Logger logger = lc.exists(loggerName);
089            ControlLogger controlLogger = (ControlLogger) controlLoggerMap.get(loggerName);
090            if (logger == null) {
091                throw new IllegalStateException("logger" + loggerName + " should exist");
092            }
093            assertEquals(loggerName, logger.getName());
094            assertEquals(loggerName, controlLogger.getName());
095
096            compareLoggers(controlLogger, logger);
097        }
098    }
099
100    void compareLoggers(ControlLogger controlLogger, Logger logger) {
101        assertEquals(controlLogger.getName(), logger.getName());
102        assertEquals(controlLogger.getEffectiveLevel(), logger.getEffectiveLevel());
103
104        Level controlLevel = controlLogger.getLevel();
105        Level level = logger.getLevel();
106
107        if (controlLevel == null) {
108            assertNull(level);
109        } else {
110            assertEquals(controlLevel, level);
111        }
112    }
113}