JDK14AdapterLoggerNameTest xref

View Javadoc

    1   /**
    2    * Copyright (c) 2004-2011 QOS.ch
    3    * All rights reserved.
    4    *
    5    * Permission is hereby granted, free  of charge, to any person obtaining
    6    * a  copy  of this  software  and  associated  documentation files  (the
    7    * "Software"), to  deal in  the Software without  restriction, including
    8    * without limitation  the rights to  use, copy, modify,  merge, publish,
    9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    10   * permit persons to whom the Software  is furnished to do so, subject to
    11   * the following conditions:
    12   *
    13   * The  above  copyright  notice  and  this permission  notice  shall  be
    14   * included in all copies or substantial portions of the Software.
    15   *
    16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23   *
    24   */
    25  package org.slf4j.impl;
    26
    27  import java.util.logging.Handler;
    28  import java.util.logging.LogRecord;
    29  import java.util.logging.Logger;
    30
    31  import junit.framework.TestCase;
    32
    33  public class JDK14AdapterLoggerNameTest extends TestCase {
    34    private MockHandler mockHandler;
    35
    36    protected void setUp() throws Exception {
    37      super.setUp();
    38      Logger logger = Logger.getLogger("TEST");
    39      mockHandler = new MockHandler();
    40      removeHandlers(logger);
    41      logger.addHandler(mockHandler);
    42    }
    43
    44    protected void tearDown() throws Exception {
    45      removeHandlers(Logger.getLogger("TEST"));
    46      super.tearDown();
    47    }
    48
    49    public void testLoggerNameusingJdkLogging() throws Exception {
    50      Logger.getLogger("TEST").info("test message");
    51      assertCorrectLoggerName();
    52
    53    }
    54
    55    public void testLoggerNameUsingSlf4j() throws Exception {
    56      JDK14LoggerFactory factory = new JDK14LoggerFactory();
    57      org.slf4j.Logger logger = factory.getLogger("TEST");
    58      logger.info("test message");
    59      assertCorrectLoggerName();
    60    }
    61
    62    private void removeHandlers(Logger logger) {
    63      logger.setUseParentHandlers(false);
    64      Handler[] handlers = logger.getHandlers();
    65      for (int i = 0; i < handlers.length; i++) {
    66        logger.removeHandler(handlers[i]);
    67      }
    68    }
    69
    70    private void assertCorrectLoggerName() {
    71      assertNotNull("no log record", mockHandler.record);
    72      assertNotNull("missing logger name", mockHandler.record.getLoggerName());
    73    }
    74
    75    private class MockHandler extends java.util.logging.Handler {
    76      public LogRecord record;
    77
    78      public void close() throws SecurityException {
    79      }
    80
    81      public void flush() {
    82      }
    83
    84      public void publish(LogRecord record) {
    85        this.record = record;
    86      }
    87
    88    }
    89  }