KickJava   Java API By Example, From Geeks To Geeks.

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


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.formatter.SimpleFormatter;
14 import org.jzonic.jlo.handler.MockHandler;
15
16 import java.util.Vector JavaDoc;
17 /**
18  *
19  * @author Administrator
20  */

21 public class LoggerTest extends TestCase {
22     
23     private Logger logger;
24     private MockHandler mockHandler;
25     private Channel channel;
26     private Logger testLogger;
27     
28     public LoggerTest(java.lang.String JavaDoc testName) {
29         super(testName);
30     }
31     
32     public void setUp() throws Exception JavaDoc {
33         mockHandler = new MockHandler("Test");
34         LogGenerator lg = new LogGenerator("test", mockHandler, new SimpleFormatter("Test"));
35         logger = new Logger("test",255,"Test");
36         logger.addLogGenerator(lg);
37         channel = new Channel("debug",lg,true);
38         testLogger = new Logger("test2",Target.warn.intValue(),"Test");
39         testLogger.addLogGenerator(lg);
40     }
41     
42     public static void main(java.lang.String JavaDoc[] args) {
43         junit.textui.TestRunner.run(suite());
44     }
45     
46     public static Test suite() {
47         TestSuite suite = new TestSuite(LoggerTest.class);
48         return suite;
49     }
50     
51     public void testSimpleLogging() {
52         logger.debug("debug message");
53         Vector JavaDoc entries = mockHandler.getMessages();
54         assertEquals(1,entries.size());
55     }
56     
57     public void testSimpleLoggingTargetOff() {
58         logger.setTargets(0);
59         logger.debug("debug message");
60         Vector JavaDoc entries = mockHandler.getMessages();
61         assertEquals(0,entries.size());
62     }
63     
64     public void testChannel() {
65         channel.log("debug message");
66         Vector JavaDoc entries = mockHandler.getMessages();
67         assertEquals(1,entries.size());
68     }
69     
70     public void testNELogger() {
71         Logger myLogger = LogManager.getLogger("org.foo","hello");
72         int counter = 0;
73         try {
74             Vector JavaDoc empty = null;
75             int size = empty.size();
76         }
77         catch (Exception JavaDoc e) {
78             myLogger.fatal("hello",e);
79         }
80         assertNotNull(myLogger);
81     }
82     
83     public void testLogger2() {
84         testLogger.trace("Hello world");
85         assertEquals(0,mockHandler.getMessages().size());
86         testLogger.info("Hello world");
87         assertEquals(0,mockHandler.getMessages().size());
88         testLogger.debug("Hello world");
89         assertEquals(0,mockHandler.getMessages().size());
90         testLogger.warn("Hello world");
91         assertEquals(1,mockHandler.getMessages().size());
92         testLogger.error("Hello world");
93         assertEquals(1,mockHandler.getMessages().size());
94         testLogger.fatal("Hello world");
95         assertEquals(1,mockHandler.getMessages().size());
96     }
97     
98     public void testLogger2Loggable() {
99         boolean test = testLogger.isTraceEnabled();
100         assertEquals(false, test);
101         test = testLogger.isInfoEnabled();
102         assertEquals(false, test);
103         test = testLogger.isDebugEnabled();
104         assertEquals(false, test);
105         test = testLogger.isWarnEnabled();
106         assertEquals(true, test);
107         test = testLogger.isErrorEnabled();
108         assertEquals(false, test);
109         test = testLogger.isFatalEnabled();
110         assertEquals(false, test);
111     }
112     
113 }
114
Popular Tags