KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > TestConfigLoggerTest


1 /*
2  * LoggerTest.java
3  * JUnit based test
4  *
5  * Created on 19. Januar 2003, 18:09
6  */

7
8 package org.jzonic.jlo;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.jzonic.jlo.handler.MockHandler;
14
15 import java.util.Vector JavaDoc;
16 /**
17  *
18  * @author Administrator
19  */

20 public class TestConfigLoggerTest extends TestCase {
21         
22     private static final LogManager manager = LogManager.getInstance();
23     
24     public TestConfigLoggerTest(java.lang.String JavaDoc testName) {
25         super(testName);
26     }
27     
28     public void setUp() throws Exception JavaDoc {
29     }
30     
31     public static void main(java.lang.String JavaDoc[] args) {
32         junit.textui.TestRunner.run(suite());
33     }
34     
35     public static Test suite() {
36         TestSuite suite = new TestSuite(TestConfigLoggerTest.class);
37         return suite;
38     }
39     
40     public void testSimpleLogging() {
41         MockHandler mockHandler = getFreshHandler("TestGenerator");
42         Logger logger = LogManager.getLogger("de.xcom","test");
43         logger.debug("debug message");
44         Vector JavaDoc entries = mockHandler.getMessages();
45         assertEquals(1,entries.size());
46     }
47     
48     public void testSimpleLoggingTargetOff() {
49         MockHandler mockHandler = getFreshHandler("TestGenerator");
50         Logger logger = LogManager.getLogger("de.xcom","test");
51         int targets = logger.getTargets();
52         logger.setTargets(0);
53         logger.debug("debug message");
54         Vector JavaDoc entries = mockHandler.getMessages();
55         assertEquals(0,entries.size());
56         logger.setTargets(targets);
57     }
58     
59     public void testChannel() {
60         MockHandler mockHandler = getFreshHandler("TestGenerator");
61         Channel channel = LogManager.getChannel("timing","test");
62         channel.log("debug message");
63         Vector JavaDoc entries = mockHandler.getMessages();
64         assertEquals(1,entries.size());
65     }
66         
67     public void testLogger2() {
68         MockHandler mockHandler = getFreshHandler("TestGenerator");
69         Logger testLogger = LogManager.getLogger("de.xcom","test");
70         testLogger.trace("Hello world");
71         assertEquals(1,mockHandler.getMessages().size());
72         testLogger.info("Hello world");
73         assertEquals(2,mockHandler.getMessages().size());
74         testLogger.debug("Hello world");
75         assertEquals(3,mockHandler.getMessages().size());
76         testLogger.warn("Hello world");
77         assertEquals(4,mockHandler.getMessages().size());
78         testLogger.error("Hello world");
79         assertEquals(5,mockHandler.getMessages().size());
80         testLogger.fatal("Hello world");
81         assertEquals(6,mockHandler.getMessages().size());
82     }
83     
84     public void testLogger2Loggable() {
85         MockHandler mockHandler = getFreshHandler("TestGenerator");
86         Logger testLogger = LogManager.getLogger("de.xcom.test","test");
87         boolean test = testLogger.isTraceEnabled();
88         assertEquals(true, test);
89         test = testLogger.isInfoEnabled();
90         assertEquals(false, test);
91         test = testLogger.isDebugEnabled();
92         assertEquals(false, test);
93         test = testLogger.isWarnEnabled();
94         assertEquals(true, test);
95         test = testLogger.isErrorEnabled();
96         assertEquals(true, test);
97         test = testLogger.isFatalEnabled();
98         assertEquals(true, test);
99     }
100     
101     private MockHandler getFreshHandler(String JavaDoc handlerName) {
102         LogConfiguration config = manager.getLogConfiguration("test");
103         assertNotNull(config);
104         LogGenerator lg = config.getLogGenerator(handlerName);
105         assertNotNull(lg);
106         MockHandler mockHandler = (MockHandler)lg.getHandler();
107         assertNotNull(mockHandler);
108         mockHandler.clear();
109         return mockHandler;
110     }
111 }
112
Popular Tags