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 static org.junit.Assert.assertNotNull;
    28
    29  import java.util.Random;
    30  import java.util.logging.Handler;
    31  import java.util.logging.LogRecord;
    32  import java.util.logging.Logger;
    33
    34  import org.junit.After;
    35  import org.junit.Before;
    36  import org.junit.Test;
    37
    38  public class JDK14AdapterLoggerNameTest {
    39      private MockHandler mockHandler;
    40      static Random random = new Random(System.currentTimeMillis());
    41      long diff = random.nextInt(10000);
    42      String loggerName = "JDK14AdapterLoggerNameTest"+diff;
    43
    44      Logger logger = Logger.getLogger(loggerName);
    45
    46      @Before
    47      public void setUp() throws Exception {
    48          Logger logger = Logger.getLogger(loggerName);
    49          addMockHandler(logger);
    50      }
    51
    52
    53
    54      @After
    55      public void tearDown() throws Exception {
    56          removeHandlers(Logger.getLogger(loggerName));
    57      }
    58
    59      @Test
    60      public void testLoggerNameUsingJdkLogging() throws Exception {
    61          logger.info("test message");
    62          assertCorrectLoggerName();
    63      }
    64
    65      @Test
    66      public void testLoggerNameUsingSlf4j() throws Exception {
    67          JDK14LoggerFactory factory = new JDK14LoggerFactory();
    68          org.slf4j.Logger logger = factory.getLogger(loggerName);
    69          logger.info("test message");
    70          assertCorrectLoggerName();
    71      }
    72
    73      private void addMockHandler(Logger logger) {
    74          mockHandler = new MockHandler();
    75          removeHandlers(logger);
    76          logger.addHandler(mockHandler);
    77      }
    78
    79      private void removeHandlers(Logger logger) {
    80          logger.setUseParentHandlers(false);
    81          Handler[] handlers = logger.getHandlers();
    82          for (int i = 0; i < handlers.length; i++) {
    83              logger.removeHandler(handlers[i]);
    84          }
    85      }
    86
    87      private void assertCorrectLoggerName() {
    88          assertNotNull("no log record", mockHandler.record);
    89          assertNotNull("missing logger name", mockHandler.record.getLoggerName());
    90      }
    91
    92      private class MockHandler extends java.util.logging.Handler {
    93          public LogRecord record;
    94
    95          public void close() throws SecurityException {
    96          }
    97
    98          public void flush() {
    99          }
    100
    101         public void publish(LogRecord record) {
    102             this.record = record;
    103         }
    104
    105     }
    106 }