1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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  
15  package ch.qos.logback.classic.spi;
16  
17  import ch.qos.logback.classic.ClassicTestConstants;
18  import ch.qos.logback.classic.Logger;
19  import ch.qos.logback.classic.LoggerContext;
20  import ch.qos.logback.classic.joran.JoranConfigurator;
21  import ch.qos.logback.core.joran.spi.JoranException;
22  import ch.qos.logback.core.testUtil.RandomUtil;
23  import ch.qos.logback.core.status.testUtil.StatusChecker;
24  import org.junit.jupiter.api.Test;
25  
26  import java.util.List;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  
30  public class LoggerContextLifeCycleTest {
31  
32      LoggerContext loggerContext = new LoggerContext();
33      Logger logger = loggerContext.getLogger(this.getClass().getName());
34      Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
35      StatusChecker checker = new StatusChecker(loggerContext);
36      int diff = RandomUtil.getPositiveInt();
37  
38      void configure(String file) throws JoranException {
39          JoranConfigurator jc = new JoranConfigurator();
40          jc.setContext(loggerContext);
41          loggerContext.putProperty("diff", "" + diff);
42          jc.doConfigure(file);
43          loggerContext.start();
44      }
45  
46      @Test
47      public void smoke() throws JoranException {
48          configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "spi/contextListener.xml");
49  
50          List<LoggerContextListener> listenerList = loggerContext.getCopyOfListenerList();
51          assertEquals(1, listenerList.size());
52  
53          ListContextListener lcl = (ListContextListener) listenerList.get(0);
54          // lcl.updateList.stream().forEach(System.out::println);
55          assertEquals(BasicContextListener.UpdateType.START, lcl.updateList.get(1));
56      }
57  
58      @Test
59      public void smokeWithImports() throws JoranException {
60          configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "spi/contextListenerWithImports.xml");
61  
62          List<LoggerContextListener> listenerList = loggerContext.getCopyOfListenerList();
63          assertEquals(1, listenerList.size());
64  
65          ListContextListener lcl = (ListContextListener) listenerList.get(0);
66          // lcl.updateList.stream().forEach(System.out::println);
67          assertEquals(BasicContextListener.UpdateType.START, lcl.updateList.get(1));
68      }
69  
70  }