KickJava   Java API By Example, From Geeks To Geeks.

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


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 CompleteLoggerTest extends TestCase {
21         
22     private static final LogManager manager = LogManager.getInstance();
23     
24     public CompleteLoggerTest(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(CompleteLoggerTest.class);
37         return suite;
38     }
39     
40     public void testSimpleLogging() {
41         MockHandler mockHandler = getFreshHandler("TestGenerator");
42         Logger logger = LogManager.getLogger("org.jzonic.jlo");
43         logger.debug("debug message");
44         Vector JavaDoc entries = mockHandler.getMessages();
45         assertEquals(2,entries.size());
46     }
47     
48     public void testSimpleLoggingTargetOff() {
49         MockHandler mockHandler = getFreshHandler("TestGenerator");
50         Logger logger = LogManager.getLogger("org.jzonic.jlo");
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("debug");
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("org.jzonic.jlo");
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(4,mockHandler.getMessages().size());
76         testLogger.warn("Hello world");
77         assertEquals(5,mockHandler.getMessages().size());
78         testLogger.error("Hello world");
79         assertEquals(6,mockHandler.getMessages().size());
80         testLogger.fatal("Hello world");
81         assertEquals(7,mockHandler.getMessages().size());
82     }
83     
84     public void testLogger2Loggable() {
85         Logger testLogger = LogManager.getLogger("org.jzonic.jlo");
86         boolean test = testLogger.isTraceEnabled();
87         assertEquals(true, test);
88         test = testLogger.isInfoEnabled();
89         assertEquals(true, test);
90         test = testLogger.isDebugEnabled();
91         assertEquals(true, test);
92         test = testLogger.isWarnEnabled();
93         assertEquals(true, test);
94         test = testLogger.isErrorEnabled();
95         assertEquals(true, test);
96         test = testLogger.isFatalEnabled();
97         assertEquals(true, test);
98     }
99     
100     public void testLoggerWithFilter() {
101         MockHandler mockHandler = getFreshHandler("TestGenerator2");
102         Logger logger = LogManager.getLogger("org.jzonic");
103         logger.debug("hello world");
104         assertEquals(0,mockHandler.getMessages().size());
105         logger.debug("Exception");
106         assertEquals(1,mockHandler.getMessages().size());
107     }
108     
109     public void testLoggerWithRegExFilter() {
110         MockHandler mockHandler = getFreshHandler("TestGenerator");
111         Logger logger = LogManager.getLogger("org.jzonic.filter");
112         logger.debug("hello world");
113         assertEquals(0,mockHandler.getMessages().size());
114         logger.debug("This is 1.2 text");
115         assertEquals(2,mockHandler.getMessages().size());
116     }
117     
118     private MockHandler getFreshHandler(String JavaDoc handlerName) {
119         LogConfiguration config = manager.getLogConfiguration();
120         assertNotNull(config);
121         LogGenerator lg = config.getLogGenerator(handlerName);
122         assertNotNull(lg);
123         MockHandler mockHandler = (MockHandler)lg.getHandler();
124         mockHandler.clear();
125         return mockHandler;
126     }
127     
128 }
129
Popular Tags