001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.jul;
026
027import static org.junit.Assert.assertNotNull;
028
029import java.util.Random;
030import java.util.logging.Handler;
031import java.util.logging.LogRecord;
032import java.util.logging.Logger;
033
034import org.junit.After;
035import org.junit.Before;
036import org.junit.Test;
037
038public class JDK14AdapterLoggerNameTest {
039    private MockHandler mockHandler;
040    static Random random = new Random(System.currentTimeMillis());
041    long diff = random.nextInt(10000);
042    String loggerName = "JDK14AdapterLoggerNameTest" + diff;
043
044    Logger logger = Logger.getLogger(loggerName);
045
046    @Before
047    public void setUp() throws Exception {
048        Logger logger = Logger.getLogger(loggerName);
049        addMockHandler(logger);
050    }
051
052    @After
053    public void tearDown() throws Exception {
054        removeHandlers(Logger.getLogger(loggerName));
055    }
056
057    @Test
058    public void testLoggerNameUsingJdkLogging() throws Exception {
059        logger.info("test message");
060        assertCorrectLoggerName();
061    }
062
063    @Test
064    public void testLoggerNameUsingSlf4j() throws Exception {
065        JDK14LoggerFactory factory = new JDK14LoggerFactory();
066        org.slf4j.Logger logger = factory.getLogger(loggerName);
067        logger.info("test message");
068        assertCorrectLoggerName();
069    }
070
071    private void addMockHandler(Logger logger) {
072        mockHandler = new MockHandler();
073        removeHandlers(logger);
074        logger.addHandler(mockHandler);
075    }
076
077    private void removeHandlers(Logger logger) {
078        logger.setUseParentHandlers(false);
079        Handler[] handlers = logger.getHandlers();
080        for (Handler handler : handlers) {
081            logger.removeHandler(handler);
082        }
083    }
084
085    private void assertCorrectLoggerName() {
086        assertNotNull("no log record", mockHandler.record);
087        assertNotNull("missing logger name", mockHandler.record.getLoggerName());
088    }
089
090    private static class MockHandler extends java.util.logging.Handler {
091        public LogRecord record;
092
093        public void close() throws SecurityException {
094        }
095
096        public void flush() {
097        }
098
099        public void publish(LogRecord record) {
100            this.record = record;
101        }
102
103    }
104}