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