1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.util;
11  
12  
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertNull;
15  import static org.junit.Assert.assertTrue;
16  import static org.junit.Assert.assertEquals;
17  
18  import java.util.List;
19  
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.Ignore;
23  import org.junit.Test;
24  import org.slf4j.LoggerFactory;
25  
26  import ch.qos.logback.classic.Logger;
27  import ch.qos.logback.classic.LoggerContext;
28  import ch.qos.logback.core.Appender;
29  import ch.qos.logback.core.ConsoleAppender;
30  import ch.qos.logback.core.joran.spi.JoranException;
31  import ch.qos.logback.core.status.StatusListener;
32  import ch.qos.logback.core.status.TrivialStatusListener;
33  
34  public class InitializationTest {
35  
36    org.slf4j.Logger logger = LoggerFactory.getLogger(InitializationTest.class);
37    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
38    Logger root = (Logger) LoggerFactory.getLogger("root");
39  
40    @Before
41    public void setUp() throws Exception {
42      logger.debug("Hello-didily-odily");
43    }
44  
45    @After
46    public void tearDown() throws Exception {
47      System.clearProperty(ContextInitializer.CONFIG_FILE_PROPERTY);
48      System.clearProperty(ContextInitializer.STATUS_LISTENER_CLASS);
49      lc.reset(); // we are going to need this context
50    }
51  
52    @Test
53    @Ignore
54    // this test works only if logback-test.xml or logback.xml files are on the classpath. 
55    // However, this is something we try to avoid in order to simplify the life
56    // of users trying to follows the manual and logback-examples from an IDE
57    public void atoconfig() {
58      Appender appender = root.getAppender("STDOUT");
59      assertNotNull(appender);
60      assertTrue(appender instanceof ConsoleAppender);
61    }
62  
63    @Test
64    @Ignore  
65    // this test works only if logback-test.xml or logback.xml files are on the classpath. 
66    // However, this is something we try to avoid in order to simplify the life
67    // of users trying to follows the manual and logback-examples from an IDE
68    public void reset() throws JoranException {
69      {
70        new ContextInitializer(lc).autoConfig();
71        Appender appender = root.getAppender("STDOUT");
72        assertNotNull(appender);
73        assertTrue(appender instanceof ConsoleAppender);
74      }
75      {
76        lc.stop();
77        Appender appender = root.getAppender("STDOUT");
78        assertNull(appender);
79      }
80    }
81  
82    @Test
83    public void autoConfigFromSystemProperties() throws JoranException  {
84      doAutoConfigFromSystemProperties(TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml");
85      doAutoConfigFromSystemProperties("autoConfigAsResource.xml");
86      // test passing a URL. note the relative path syntax with file:src/test/...
87      doAutoConfigFromSystemProperties("file:"+TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml"); 
88    }
89    
90    public void doAutoConfigFromSystemProperties(String val) throws JoranException {
91      //lc.reset();
92      System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, val);
93      new ContextInitializer(lc).autoConfig();
94      Appender appender = root.getAppender("AUTO_BY_SYSTEM_PROPERTY");
95      assertNotNull(appender);
96    }
97    
98    @Test
99    public void autoStatusListener() throws JoranException {
100     System.setProperty(ContextInitializer.STATUS_LISTENER_CLASS, TrivialStatusListener.class.getName());
101     List<StatusListener> sll = lc.getStatusManager().getCopyOfStatusListenerList();
102     assertEquals(0, sll.size());
103     doAutoConfigFromSystemProperties(TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml");
104     sll = lc.getStatusManager().getCopyOfStatusListenerList();
105     assertTrue(sll.size() +" should be 1", sll.size() == 1);
106   }
107   
108   @Test
109   public void autoOnConsoleStatusListener() throws JoranException {
110     System.setProperty(ContextInitializer.STATUS_LISTENER_CLASS,  ContextInitializer.SYSOUT);
111     List<StatusListener> sll = lc.getStatusManager().getCopyOfStatusListenerList();
112     assertEquals(0, sll.size());
113     doAutoConfigFromSystemProperties(TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml");
114     sll = lc.getStatusManager().getCopyOfStatusListenerList();
115     assertTrue(sll.size() +" should be 1", sll.size() == 1);
116   }
117 }