KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > slf4j > impl > JDK14AdapterLoggerNameTest


1 /*
2  * Copyright (c) 2004-2005 SLF4J.ORG
3  * Copyright (c) 2004-2005 QOS.ch
4  *
5  * All rights reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, and/or sell copies of the Software, and to permit persons
12  * to whom the Software is furnished to do so, provided that the above
13  * copyright notice(s) and this permission notice appear in all copies of
14  * the Software and that both the above copyright notice(s) and this
15  * permission notice appear in supporting documentation.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
20  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
22  * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
23  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
24  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
25  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26  *
27  * Except as contained in this notice, the name of a copyright holder
28  * shall not be used in advertising or otherwise to promote the sale, use
29  * or other dealings in this Software without prior written authorization
30  * of the copyright holder.
31  *
32  */

33
34 package org.slf4j.impl;
35
36 import java.util.logging.Handler JavaDoc;
37 import java.util.logging.LogRecord JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 import junit.framework.TestCase;
41
42 public class JDK14AdapterLoggerNameTest extends TestCase {
43   private MockHandler mockHandler;
44
45   protected void setUp() throws Exception JavaDoc {
46     super.setUp();
47     Logger JavaDoc logger = Logger.getLogger("TEST");
48     mockHandler = new MockHandler();
49     removeHandlers(logger);
50     logger.addHandler(mockHandler);
51   }
52
53   protected void tearDown() throws Exception JavaDoc {
54     removeHandlers(Logger.getLogger("TEST"));
55     super.tearDown();
56   }
57
58   public void testLoggerNameusingJdkLogging() throws Exception JavaDoc {
59     Logger.getLogger("TEST").info("test message");
60     assertCorrectLoggerName();
61
62   }
63
64   public void testLoggerNameUsingSlf4j() throws Exception JavaDoc {
65     JDK14LoggerFactory factory = new JDK14LoggerFactory();
66     org.slf4j.Logger logger = factory.getLogger("TEST");
67     logger.info("test message");
68     assertCorrectLoggerName();
69   }
70
71   private void removeHandlers(Logger JavaDoc logger) {
72     logger.setUseParentHandlers(false);
73     Handler JavaDoc[] handlers = logger.getHandlers();
74     for (int i = 0; i < handlers.length; i++) {
75       logger.removeHandler(handlers[i]);
76     }
77   }
78
79   private void assertCorrectLoggerName() {
80     assertNotNull("no log record", mockHandler.record);
81     assertNotNull("missing logger name", mockHandler.record.getLoggerName());
82   }
83
84   private class MockHandler extends java.util.logging.Handler JavaDoc {
85     public LogRecord JavaDoc record;
86
87     public void close() throws SecurityException JavaDoc {
88     }
89
90     public void flush() {
91     }
92
93     public void publish(LogRecord JavaDoc record) {
94       this.record = record;
95     }
96
97   }
98 }
Popular Tags