View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
3    * reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
6    * v1.0 as published by the Eclipse Foundation
7    *
8    * or (per the licensee's choosing)
9    *
10   * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
11   */
12  package ch.qos.logback.classic.util;
13  
14  import ch.qos.logback.classic.ClassicConstants;
15  import ch.qos.logback.classic.ClassicTestConstants;
16  import ch.qos.logback.classic.Logger;
17  import ch.qos.logback.classic.LoggerContext;
18  import ch.qos.logback.classic.spi.ILoggingEvent;
19  import ch.qos.logback.core.Appender;
20  import ch.qos.logback.core.ConsoleAppender;
21  import ch.qos.logback.core.CoreConstants;
22  import ch.qos.logback.core.LogbackException;
23  import ch.qos.logback.core.joran.spi.JoranException;
24  import ch.qos.logback.core.status.StatusListener;
25  import ch.qos.logback.core.testUtil.TrivialStatusListener;
26  import ch.qos.logback.core.util.Loader;
27  import org.junit.jupiter.api.AfterEach;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Disabled;
31  import org.junit.jupiter.api.Test;
32  
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.net.MalformedURLException;
36  import java.net.URL;
37  import java.util.Enumeration;
38  import java.util.List;
39  import java.util.Vector;
40  
41  import static org.junit.jupiter.api.Assertions.assertEquals;
42  import static org.junit.jupiter.api.Assertions.assertNotNull;
43  import static org.junit.jupiter.api.Assertions.assertNull;
44  import static org.junit.jupiter.api.Assertions.assertSame;
45  import static org.junit.jupiter.api.Assertions.assertTrue;
46  import static org.junit.jupiter.api.Assertions.fail;
47  import static org.junit.jupiter.api.Assumptions.assumeTrue;
48  
49  public class ContextInitializerTest {
50  
51      static final String PATH_TO_META_INF_CONF_SERVICE = "META-INF/services/ch.qos.logback.classic.spi.Configurator";
52      static final String FAKE_META_INF_SERVICES = "FAKE_META_INF_SERVICES_ch_qos_logback_classic_spi_Configurator";
53      LoggerContext loggerContext = new LoggerContext();
54      Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
55  
56      @BeforeEach
57      public void setUp() throws Exception {
58      }
59  
60      @AfterEach
61      public void tearDown() throws Exception {
62          System.clearProperty(ClassicConstants.CONFIG_FILE_PROPERTY);
63          System.clearProperty(CoreConstants.STATUS_LISTENER_CLASS_KEY);
64          //ClassicEnvUtil.testServiceLoaderClassLoader = null;
65          MockConfigurator.context = null;
66      }
67  
68      @Test
69      @Disabled
70      // this test works only if logback-test.xml or logback.xml files are on the
71      // classpath.
72      // However, this is something we try to avoid in order to simplify the life
73      // of users trying to follow the manual and logback-examples from an IDE
74      public void reset() throws JoranException {
75          {
76              new ContextInitializer(loggerContext).autoConfig();
77              Appender<ILoggingEvent> appender = root.getAppender("STDOUT");
78              assertNotNull(appender);
79              assertTrue(appender instanceof ConsoleAppender);
80          }
81          {
82              loggerContext.stop();
83              Appender<ILoggingEvent> appender = root.getAppender("STDOUT");
84              assertNull(appender);
85          }
86      }
87  
88      @Test
89      public void autoConfigFromSystemProperties() throws JoranException {
90          doAutoConfigFromSystemProperties(ClassicTestConstants.INPUT_PREFIX + "autoConfig.xml");
91          doAutoConfigFromSystemProperties("autoConfigAsResource.xml");
92          // test passing a URL. note the relative path syntax with file:src/test/...
93          doAutoConfigFromSystemProperties("file://./" + ClassicTestConstants.INPUT_PREFIX + "autoConfig.xml");
94      }
95  
96      public void doAutoConfigFromSystemProperties(String val) throws JoranException {
97          // lc.reset();
98          System.setProperty(ClassicConstants.CONFIG_FILE_PROPERTY, val);
99          new ContextInitializer(loggerContext).autoConfig();
100         Appender<ILoggingEvent> appender = root.getAppender("AUTO_BY_SYSTEM_PROPERTY");
101         assertNotNull(appender);
102     }
103 
104     // this test as constructed cannot run in a modular environment since
105     // ServiceLoader will not honor providers specified in a provider-configuration file (META-INF/..)
106     // if module-info.java in the same module declares a provider
107     //
108     // https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/ServiceLoader.html#
109     //
110     //In a provider-configuration file, any mention of a service provider that is deployed
111     // in a named module is ignored. This is to avoid duplicates that would otherwise arise
112     // when a named module has both a provides directive and a provider-configuration file
113     // that mention the same service provider.
114     @Disabled
115     @Test
116     public void autoConfigFromServiceLoaderJDK6andAbove() throws Exception {
117         assumeTrue(!isJDK5());
118         ClassLoader mockClassLoader = buildMockServiceLoader(this.getClass().getClassLoader());
119         assertNull(MockConfigurator.context);
120         new ContextInitializer(loggerContext).autoConfig(mockClassLoader);
121         assertNotNull(MockConfigurator.context);
122         assertSame(loggerContext, MockConfigurator.context);
123     }
124 
125     @Test
126     public void autoConfigFromServiceLoaderJDK5() throws Exception {
127         assumeTrue(isJDK5());
128         ClassLoader mockClassLoader = buildMockServiceLoader(this.getClass().getClassLoader());
129         assertNull(MockConfigurator.context);
130         new ContextInitializer(loggerContext).autoConfig(mockClassLoader);
131         assertNull(MockConfigurator.context);
132     }
133 
134     @Test
135     public void autoStatusListener() throws JoranException {
136         System.setProperty(CoreConstants.STATUS_LISTENER_CLASS_KEY, TrivialStatusListener.class.getName());
137         List<StatusListener> statusListenerList = loggerContext.getStatusManager().getCopyOfStatusListenerList();
138         assertEquals(0, statusListenerList.size());
139         doAutoConfigFromSystemProperties(ClassicTestConstants.INPUT_PREFIX + "autoConfig.xml");
140         statusListenerList = loggerContext.getStatusManager().getCopyOfStatusListenerList();
141         assertTrue(statusListenerList.size() == 1, statusListenerList.size() + " should be 1");
142         // LOGBACK-767
143         TrivialStatusListener tsl = (TrivialStatusListener) statusListenerList.get(0);
144         assertTrue(tsl.list.size() > 0, "expecting at least one event in list");
145     }
146 
147     @Test
148     public void autoOnConsoleStatusListener() throws JoranException {
149         System.setProperty(CoreConstants.STATUS_LISTENER_CLASS_KEY, CoreConstants.SYSOUT);
150         List<StatusListener> sll = loggerContext.getStatusManager().getCopyOfStatusListenerList();
151         assertEquals(0, sll.size());
152         doAutoConfigFromSystemProperties(ClassicTestConstants.INPUT_PREFIX + "autoConfig.xml");
153         sll = loggerContext.getStatusManager().getCopyOfStatusListenerList();
154         assertTrue(sll.size() == 1, sll.size() + " should be 1");
155     }
156 
157     @Test
158     public void shouldConfigureFromXmlFile() throws MalformedURLException, JoranException {
159         assertNull(loggerContext.getObject(CoreConstants.SAFE_JORAN_CONFIGURATION));
160 
161         URL configurationFileUrl = Loader.getResource("BOO_logback-test.xml",
162                 Thread.currentThread().getContextClassLoader());
163         DefaultJoranConfigurator joranConfigurator = new DefaultJoranConfigurator();
164         joranConfigurator.setContext(loggerContext);
165         joranConfigurator.configureByResource(configurationFileUrl);
166 
167         assertNotNull(loggerContext.getObject(CoreConstants.SAFE_JORAN_CONFIGURATION));
168     }
169 
170 //    @Test
171 //    public void shouldConfigureFromGroovyScript() throws MalformedURLException, JoranException {
172 //        LoggerContext loggerContext = new LoggerContext();
173 //        ContextInitializer initializer = new ContextInitializer(loggerContext);
174 //        assertNull(loggerContext.getObject(CoreConstants.CONFIGURATION_WATCH_LIST));
175 //
176 //        URL configurationFileUrl = Loader.getResource("test.groovy", Thread.currentThread().getContextClassLoader());
177 //        initializer.configureByResource(configurationFileUrl);
178 //
179 //        assertNotNull(loggerContext.getObject(CoreConstants.CONFIGURATION_WATCH_LIST));
180 //    }
181 
182     @Test
183     public void shouldThrowExceptionIfUnexpectedConfigurationFileExtension() throws JoranException {
184         URL configurationFileUrl = Loader.getResource("README.txt", Thread.currentThread().getContextClassLoader());
185         try {
186             DefaultJoranConfigurator joranConfigurator = new DefaultJoranConfigurator();
187             joranConfigurator.setContext(loggerContext);
188             joranConfigurator.configureByResource(configurationFileUrl);
189             fail("Should throw LogbackException");
190         } catch (LogbackException expectedException) {
191             // pass
192         }
193     }
194 
195     private static boolean isJDK5() {
196         String ver = System.getProperty("java.version");
197         boolean jdk5 = ver.startsWith("1.5.") || ver.equals("1.5");
198         return jdk5;
199     }
200 
201     private ClassLoader buildMockServiceLoader(ClassLoader realLoader) {
202 
203         //final ClassLoader realLoader = ClassicEnvUtil.class.getClassLoader();
204         ClassLoader wrapperClassLoader = new WrappedClassLoader(realLoader) {
205 
206             @Override
207             public String toString() {
208                 return "wrapperClassLoader: " + super.toString();
209             }
210 
211             @Override
212             public Enumeration<URL> getResources(String name) throws IOException {
213                 final Enumeration<URL> r;
214                 if (name.endsWith(PATH_TO_META_INF_CONF_SERVICE)) {
215                     System.out.println("Hit on " + PATH_TO_META_INF_CONF_SERVICE);
216                     Vector<URL> vs = new Vector<URL>();
217                     URL u = super.getResource(FAKE_META_INF_SERVICES);
218                     Assertions.assertNotNull(u);
219                     System.out.println("Found url: " + u);
220                     vs.add(u);
221                     return vs.elements();
222                 } else {
223                     r = super.getResources(name);
224                 }
225                 return r;
226             }
227         };
228 
229         return wrapperClassLoader;
230     }
231 
232     static class WrappedClassLoader extends ClassLoader {
233         final ClassLoader delegate;
234 
235         public WrappedClassLoader(ClassLoader delegate) {
236             super();
237             this.delegate = delegate;
238         }
239 
240         public Class<?> loadClass(String name) throws ClassNotFoundException {
241             return delegate.loadClass(name);
242         }
243 
244         public URL getResource(String name) {
245             return delegate.getResource(name);
246         }
247 
248         public Enumeration<URL> getResources(String name) throws IOException {
249             return delegate.getResources(name);
250         }
251 
252         public InputStream getResourceAsStream(String name) {
253             return delegate.getResourceAsStream(name);
254         }
255 
256         public void setDefaultAssertionStatus(boolean enabled) {
257             delegate.setDefaultAssertionStatus(enabled);
258         }
259 
260         public void setPackageAssertionStatus(String packageName, boolean enabled) {
261             delegate.setPackageAssertionStatus(packageName, enabled);
262         }
263 
264         public void setClassAssertionStatus(String className, boolean enabled) {
265             delegate.setClassAssertionStatus(className, enabled);
266         }
267 
268         public void clearAssertionStatus() {
269             delegate.clearAssertionStatus();
270         }
271     }
272 
273 }