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.selector;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import org.junit.After;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.slf4j.LoggerFactory;
22  import org.slf4j.LoggerFactoryFriend;
23  
24  import ch.qos.logback.classic.ClassicConstants;
25  import ch.qos.logback.classic.selector.servlet.ContextDetachingSCL;
26  import ch.qos.logback.classic.util.ContextSelectorStaticBinder;
27  import ch.qos.logback.classic.util.MockInitialContext;
28  import ch.qos.logback.classic.util.MockInitialContextFactory;
29  
30  public class ContextDetachingSCLTest {
31  
32      static String INITIAL_CONTEXT_KEY = "java.naming.factory.initial";
33  
34      ContextDetachingSCL contextDetachingSCL;
35  
36      @Before
37      public void setUp() throws Exception {
38  
39          System.setProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR, "JNDI");
40  
41          contextDetachingSCL = new ContextDetachingSCL();
42  
43          MockInitialContextFactory.initialize();
44          MockInitialContext mic = MockInitialContextFactory.getContext();
45          mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "toto");
46  
47          // The property must be set after we setup the Mock
48          System.setProperty(INITIAL_CONTEXT_KEY, MockInitialContextFactory.class.getName());
49  
50          // reinitialize the LoggerFactory, These reset methods are reserved for internal use
51          LoggerFactoryFriend.reset();
52  
53          // this call will create the context "toto"
54          LoggerFactory.getLogger(ContextDetachingSCLTest.class);
55      }
56  
57      @After
58      public void tearDown() throws Exception {
59          System.clearProperty(INITIAL_CONTEXT_KEY);
60          // reinitialize the LoggerFactory, These resets method are reserved for internal use
61          LoggerFactoryFriend.reset();
62      }
63  
64      @Test
65      public void testDetach() {
66          ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton().getContextSelector();
67          contextDetachingSCL.contextDestroyed(null);
68          assertEquals(0, selector.getCount());
69      }
70  
71      @Test
72      public void testDetachWithMissingContext() {
73          MockInitialContext mic = MockInitialContextFactory.getContext();
74          mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "tata");
75          ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton().getContextSelector();
76          assertEquals("tata", selector.getLoggerContext().getName());
77  
78          mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "titi");
79          assertEquals("titi", selector.getLoggerContext().getName());
80          contextDetachingSCL.contextDestroyed(null);
81  
82          assertEquals(2, selector.getCount());
83      }
84  
85  }