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.util.ContextSelectorStaticBinder;
18  import ch.qos.logback.core.Context;
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 ContextJNDISelectorTest {
32  
33      static String INITIAL_CONTEXT_KEY = "java.naming.factory.initial";
34  
35      @BeforeEach
36      public void setUp() throws Exception {
37  
38          System.setProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR, "JNDI");
39          LoggerFactoryFriend.reset();
40  
41          MockInitialContextFactory.initialize();
42          MockInitialContext mic = MockInitialContextFactory.getContext();
43          mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "toto");
44  
45          // The property must be set after we set up the Mock
46          System.setProperty(INITIAL_CONTEXT_KEY, MockInitialContextFactory.class.getName());
47  
48          // this call will create the context "toto"
49          LoggerFactory.getLogger(ContextDetachingSCLTest.class);
50      }
51  
52      @AfterEach
53      public void tearDown() throws Exception {
54          System.clearProperty(INITIAL_CONTEXT_KEY);
55      }
56  
57      @Test
58      public void testGetExistingContext() {
59          ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector();
60          Context context = selector.getLoggerContext();
61          assertEquals("toto", context.getName());
62      }
63  
64      @Test
65      public void testCreateContext() {
66          MockInitialContext mic = MockInitialContextFactory.getContext();
67          mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "tata");
68  
69          LoggerFactory.getLogger(ContextDetachingSCLTest.class);
70  
71          ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton()
72                  .getContextSelector();
73          Context context = selector.getLoggerContext();
74          assertEquals("tata", context.getName());
75          System.out.println(selector.getContextNames());
76          assertEquals(2, selector.getCount());
77      }
78  
79      @Test
80      public void defaultContext() {
81          MockInitialContext mic = MockInitialContextFactory.getContext();
82          mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, null);
83  
84          ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton()
85                  .getContextSelector();
86          Context context = selector.getLoggerContext();
87  
88          assertEquals("default", context.getName());
89      }
90  
91  }