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.util.ContextSelectorStaticBinder;
026import ch.qos.logback.classic.util.MockInitialContext;
027import ch.qos.logback.classic.util.MockInitialContextFactory;
028import ch.qos.logback.core.Context;
029
030public class ContextJNDISelectorTest {
031
032    static String INITIAL_CONTEXT_KEY = "java.naming.factory.initial";
033
034    @Before
035    public void setUp() throws Exception {
036
037        System.setProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR, "JNDI");
038        LoggerFactoryFriend.reset();
039
040        MockInitialContextFactory.initialize();
041        MockInitialContext mic = MockInitialContextFactory.getContext();
042        mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "toto");
043
044        // The property must be set after we setup the Mock
045        System.setProperty(INITIAL_CONTEXT_KEY, MockInitialContextFactory.class.getName());
046
047        // this call will create the context "toto"
048        LoggerFactory.getLogger(ContextDetachingSCLTest.class);
049    }
050
051    @After
052    public void tearDown() throws Exception {
053        System.clearProperty(INITIAL_CONTEXT_KEY);
054    }
055
056    @Test
057    public void testGetExistingContext() {
058        ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector();
059        Context context = selector.getLoggerContext();
060        assertEquals("toto", context.getName());
061    }
062
063    @Test
064    public void testCreateContext() {
065        MockInitialContext mic = MockInitialContextFactory.getContext();
066        mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "tata");
067
068        LoggerFactory.getLogger(ContextDetachingSCLTest.class);
069
070        ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton().getContextSelector();
071        Context context = selector.getLoggerContext();
072        assertEquals("tata", context.getName());
073        System.out.println(selector.getContextNames());
074        assertEquals(2, selector.getCount());
075    }
076
077    @Test
078    public void defaultContext() {
079        MockInitialContext mic = MockInitialContextFactory.getContext();
080        mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, null);
081
082        ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton().getContextSelector();
083        Context context = selector.getLoggerContext();
084
085        assertEquals("default", context.getName());
086    }
087
088}