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.jul;
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      @After
53      public void tearDown() throws Exception {
54          removeHandlers(Logger.getLogger(loggerName));
55      }
56  
57      @Test
58      public void testLoggerNameUsingJdkLogging() throws Exception {
59          logger.info("test message");
60          assertCorrectLoggerName();
61      }
62  
63      @Test
64      public void testLoggerNameUsingSlf4j() throws Exception {
65          JDK14LoggerFactory factory = new JDK14LoggerFactory();
66          org.slf4j.Logger logger = factory.getLogger(loggerName);
67          logger.info("test message");
68          assertCorrectLoggerName();
69      }
70  
71      private void addMockHandler(Logger logger) {
72          mockHandler = new MockHandler();
73          removeHandlers(logger);
74          logger.addHandler(mockHandler);
75      }
76  
77      private void removeHandlers(Logger logger) {
78          logger.setUseParentHandlers(false);
79          Handler[] handlers = logger.getHandlers();
80          for (Handler handler : handlers) {
81              logger.removeHandler(handler);
82          }
83      }
84  
85      private void assertCorrectLoggerName() {
86          assertNotNull("no log record", mockHandler.record);
87          assertNotNull("missing logger name", mockHandler.record.getLoggerName());
88      }
89  
90      private static class MockHandler extends java.util.logging.Handler {
91          public LogRecord record;
92  
93          public void close() throws SecurityException {
94          }
95  
96          public void flush() {
97          }
98  
99          public void publish(LogRecord record) {
100             this.record = record;
101         }
102 
103     }
104 }