001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.selector;
015
016import static org.junit.Assert.assertEquals;
017
018import org.junit.After;
019import org.junit.Before;
020import org.junit.Test;
021import org.slf4j.LoggerFactory;
022import org.slf4j.LoggerFactoryFriend;
023
024import ch.qos.logback.classic.ClassicConstants;
025import ch.qos.logback.classic.selector.servlet.ContextDetachingSCL;
026import ch.qos.logback.classic.util.ContextSelectorStaticBinder;
027import ch.qos.logback.classic.util.MockInitialContext;
028import ch.qos.logback.classic.util.MockInitialContextFactory;
029
030public class ContextDetachingSCLTest {
031
032    static String INITIAL_CONTEXT_KEY = "java.naming.factory.initial";
033
034    ContextDetachingSCL contextDetachingSCL;
035
036    @Before
037    public void setUp() throws Exception {
038
039        System.setProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR, "JNDI");
040
041        contextDetachingSCL = new ContextDetachingSCL();
042
043        MockInitialContextFactory.initialize();
044        MockInitialContext mic = MockInitialContextFactory.getContext();
045        mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "toto");
046
047        // The property must be set after we setup the Mock
048        System.setProperty(INITIAL_CONTEXT_KEY, MockInitialContextFactory.class.getName());
049
050        // reinitialize the LoggerFactory, These reset methods are reserved for internal use
051        LoggerFactoryFriend.reset();
052
053        // this call will create the context "toto"
054        LoggerFactory.getLogger(ContextDetachingSCLTest.class);
055    }
056
057    @After
058    public void tearDown() throws Exception {
059        System.clearProperty(INITIAL_CONTEXT_KEY);
060        // reinitialize the LoggerFactory, These resets method are reserved for internal use
061        LoggerFactoryFriend.reset();
062    }
063
064    @Test
065    public void testDetach() {
066        ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton().getContextSelector();
067        contextDetachingSCL.contextDestroyed(null);
068        assertEquals(0, selector.getCount());
069    }
070
071    @Test
072    public void testDetachWithMissingContext() {
073        MockInitialContext mic = MockInitialContextFactory.getContext();
074        mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "tata");
075        ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton().getContextSelector();
076        assertEquals("tata", selector.getLoggerContext().getName());
077
078        mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "titi");
079        assertEquals("titi", selector.getLoggerContext().getName());
080        contextDetachingSCL.contextDestroyed(null);
081
082        assertEquals(2, selector.getCount());
083    }
084
085}