1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, 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  
15  package ch.qos.logback.classic.joran;
16  
17  import ch.qos.logback.classic.ClassicConstants;
18  import ch.qos.logback.classic.Level;
19  import ch.qos.logback.classic.Logger;
20  import ch.qos.logback.classic.LoggerContext;
21  import ch.qos.logback.core.joran.JoranConstants;
22  import ch.qos.logback.core.util.StatusPrinter2;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  import java.util.Properties;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  
30  class PropertiesConfiguratorTest {
31  
32      LoggerContext lc = new LoggerContext();
33      Properties props = new Properties();
34      PropertiesConfigurator pc = new PropertiesConfigurator();
35      StatusPrinter2 statusPrinter2 = new StatusPrinter2();
36      @BeforeEach
37      public void setup() throws Exception {
38          pc.setContext(lc);
39      }
40  
41      @Test
42      public void smoke() {
43          String TOTO_STR = "toto";
44          props.setProperty(PropertiesConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, Level.INFO.levelStr);
45          props.setProperty(PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, Level.ERROR.levelStr);
46          pc.doConfigure(props);
47  
48          Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
49          Logger totoLogger = lc.getLogger(TOTO_STR);
50  
51          assertEquals(Level.INFO, rootLogger.getLevel());
52  
53          assertEquals(Level.ERROR, totoLogger.getLevel());
54  
55      }
56  
57      @Test
58      public void withVariables() {
59          String TOTO_STR = "toto";
60          String ROOT_LEVEL_STR = "rootLevel";
61          String TOTO_LEVEL_STR = "totoLevel";
62  
63          props.setProperty(ROOT_LEVEL_STR, Level.INFO.levelStr);
64          System.setProperty("totoLevel", Level.ERROR.levelStr);
65          props.setProperty(PropertiesConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, asVar(ROOT_LEVEL_STR));
66          props.setProperty(PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, asVar(TOTO_LEVEL_STR));
67          pc.doConfigure(props);
68  
69          Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
70          Logger totoLogger = lc.getLogger(TOTO_STR);
71          statusPrinter2.print(lc);
72          assertEquals(Level.INFO, rootLogger.getLevel());
73          assertEquals(Level.ERROR, totoLogger.getLevel());
74  
75      }
76  
77      @Test
78      void inheritedLevelString() {
79          String loggerName0 = "com.abc.some0";
80          String loggerName1 = "com.abc.some1";
81  
82          Logger aLogger0 = lc.getLogger(loggerName0);
83          aLogger0.setLevel(Level.ERROR);
84  
85          Logger aLogger1 = lc.getLogger(loggerName1);
86          aLogger1.setLevel(Level.WARN);
87  
88  
89          props.setProperty(PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + loggerName0, JoranConstants.INHERITED);
90          props.setProperty(PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + loggerName1, JoranConstants.NULL);
91          pc.doConfigure(props);
92  
93          //statusPrinter2.print(lc);
94          assertEquals(null, aLogger0.getLevel());
95          assertEquals(null, aLogger1.getLevel());
96      }
97  
98      String asVar(String v) {
99          return "${"+v+"}";
100     }
101 }