1
2
3
4
5
6
7
8
9
10
11
12
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
49 System.setProperty(INITIAL_CONTEXT_KEY, MockInitialContextFactory.class.getName());
50
51
52
53 LoggerFactoryFriend.reset();
54
55
56 LoggerFactory.getLogger(ContextDetachingSCLTest.class);
57 }
58
59 @AfterEach
60 public void tearDown() throws Exception {
61 System.clearProperty(INITIAL_CONTEXT_KEY);
62
63
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 }