View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic;
15  
16  import java.util.List;
17  import java.util.Map;
18  
19  import org.junit.jupiter.api.Disabled;
20  import org.junit.jupiter.api.Test;
21  
22  import ch.qos.logback.classic.control.ControlLogger;
23  import ch.qos.logback.classic.control.ControlLoggerContext;
24  import ch.qos.logback.classic.control.CreateLogger;
25  import ch.qos.logback.classic.control.ScenarioAction;
26  import ch.qos.logback.classic.control.ScenarioMaker;
27  import ch.qos.logback.classic.control.SetLevel;
28  import ch.qos.logback.classic.control.Scenario;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertNull;
32  
33  public class ScenarioBasedLoggerContextTest {
34      LoggerContext lc;
35  
36      @Test
37      public void testLen3() {
38          doScenarioedTest(3);
39      }
40  
41      @Test
42      public void testLength_30() {
43          doScenarioedTest(30);
44      }
45  
46      @Test
47      public void testLength_20000() {
48          doScenarioedTest(20 * 1000);
49      }
50  
51      @Test
52      @Disabled
53      public void testLengthLong() {
54          doScenarioedTest(100 * 1000);
55      }
56  
57      private void doScenarioedTest(int len) {
58          LoggerContext lc = new LoggerContext();
59          ControlLoggerContext controlContext = new ControlLoggerContext();
60          Scenario s = ScenarioMaker.makeRealisticCreationScenario(len);
61          List<ScenarioAction> actionList = s.getActionList();
62          int size = actionList.size();
63          for (int i = 0; i < size; i++) {
64              ScenarioAction action = (ScenarioAction) actionList.get(i);
65              if (action instanceof CreateLogger) {
66                  CreateLogger cl = (CreateLogger) action;
67                  lc.getLogger(cl.getLoggerName());
68                  controlContext.getLogger(cl.getLoggerName());
69              } else if (action instanceof SetLevel) {
70                  SetLevel sl = (SetLevel) action;
71                  Logger l = lc.getLogger(sl.getLoggerName());
72                  ControlLogger controlLogger = controlContext.getLogger(sl.getLoggerName());
73                  l.setLevel(sl.getLevel());
74                  controlLogger.setLevel(sl.getLevel());
75              }
76          }
77  
78          compareLoggerContexts(controlContext, lc);
79      }
80  
81      void compareLoggerContexts(ControlLoggerContext controlLC, LoggerContext lc) {
82          Map<String, ControlLogger> controlLoggerMap = controlLC.getLoggerMap();
83  
84          assertEquals(controlLoggerMap.size() + 1, lc.size());
85  
86          for (String loggerName : controlLoggerMap.keySet()) {
87  
88              Logger logger = lc.exists(loggerName);
89              ControlLogger controlLogger = (ControlLogger) controlLoggerMap.get(loggerName);
90              if (logger == null) {
91                  throw new IllegalStateException("logger" + loggerName + " should exist");
92              }
93              assertEquals(loggerName, logger.getName());
94              assertEquals(loggerName, controlLogger.getName());
95  
96              compareLoggers(controlLogger, logger);
97          }
98      }
99  
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 }