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