Source code

    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.impl;
    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
    053
    054    @After
    055    public void tearDown() throws Exception {
    056        removeHandlers(Logger.getLogger(loggerName));
    057    }
    058
    059    @Test
    060    public void testLoggerNameUsingJdkLogging() throws Exception {
    061        logger.info("test message");
    062        assertCorrectLoggerName();
    063    }
    064
    065    @Test
    066    public void testLoggerNameUsingSlf4j() throws Exception {
    067        JDK14LoggerFactory factory = new JDK14LoggerFactory();
    068        org.slf4j.Logger logger = factory.getLogger(loggerName);
    069        logger.info("test message");
    070        assertCorrectLoggerName();
    071    }
    072
    073    private void addMockHandler(Logger logger) {
    074        mockHandler = new MockHandler();
    075        removeHandlers(logger);
    076        logger.addHandler(mockHandler);
    077    }
    078
    079    private void removeHandlers(Logger logger) {
    080        logger.setUseParentHandlers(false);
    081        Handler[] handlers = logger.getHandlers();
    082        for (int i = 0; i < handlers.length; i++) {
    083            logger.removeHandler(handlers[i]);
    084        }
    085    }
    086
    087    private void assertCorrectLoggerName() {
    088        assertNotNull("no log record", mockHandler.record);
    089        assertNotNull("missing logger name", mockHandler.record.getLoggerName());
    090    }
    091
    092    private class MockHandler extends java.util.logging.Handler {
    093        public LogRecord record;
    094
    095        public void close() throws SecurityException {
    096        }
    097
    098        public void flush() {
    099        }
    100
    101        public void publish(LogRecord record) {
    102            this.record = record;
    103        }
    104
    105    }
    106}