KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > TestLogger


1 /**
2  * Copyright (C) 2002
3  */

4
5 package org.objectweb.util.monolog;
6
7 import junit.framework.Assert;
8 import org.objectweb.util.monolog.api.BasicLevel;
9 import org.objectweb.util.monolog.api.Handler;
10 import org.objectweb.util.monolog.api.TopicalLogger;
11
12 /**
13  * It verifies a TopicalLogger implementation and a TopicalFactory
14  * implementation.
15  *
16  * @author Sebastien Chassande-Barrioz
17  */

18 public class TestLogger extends TestHelper {
19
20     public static final String JavaDoc LOG_FILE_NAME = "test.log";
21     public static final String JavaDoc LOG_PATTERN = "%m%n";
22
23     TopicalLogger l = null;
24
25     public TestLogger() {
26     }
27
28     public TestLogger(String JavaDoc s) {
29         super(s);
30     }
31
32     /**
33      * For running the TestLogger suite standalone.
34      */

35     public static void main(String JavaDoc args[]) {
36         if (args.length < 1) {
37             System.out.println("Syntax error !");
38             System.out.println("java TestLogger <logger factory class name>");
39             System.exit(1);
40         }
41         TestHelper.run(TestLogger.class, new String JavaDoc[0],
42             new String JavaDoc[0], args[0]);
43     }
44
45     public static TestSuite _getTestSuite(String JavaDoc lfcn) {
46         return TestHelper.getTestSuite(TestLogger.class, new String JavaDoc[0],
47             new String JavaDoc[0], lfcn);
48     }
49
50     // ------ TEST METHODS ------ //
51
//----------------------------//
52
public void testNbLogger() {
53         assertEquals("nb initial loggers", 1, lf.getLoggers().length);
54     }
55
56     public void testRootLoggerConf() {
57         l = (TopicalLogger) lf.getLogger("");
58         assertNotNull("Root logger undefined", l);
59         String JavaDoc[] ts = l.getTopic();
60         assertNotNull("Topic list is null", ts);
61         assertEquals("several topic", 1, ts.length);
62         assertNotNull("Topic list is null", ts[0]);
63         assertEquals("name 'root' not equals to ''", l, lf.getLogger("root"));
64         assertTrue("several topic", ts[0].length() == 0 || "root".equals(ts[0]));
65
66     }
67
68     public void testGetOneLoggerByName() {
69         l = (TopicalLogger) lf.getLogger("foo");
70
71         assertEquals("same name but 2 instances",
72             l, lf.getLogger("foo"));
73         String JavaDoc[] topics = ((TopicalLogger) lf.getLogger("foo")).getTopic();
74         assertNotNull("Topic list is null", topics);
75         assertEquals("several topic", 1, topics.length);
76         assertNotNull("Topic elem is null", topics[0]);
77         assertTrue("bad name", topics[0].equals("foo"));
78     }
79
80     public void testGetAllLoggerConf() {
81         int iternumber = 20;
82         for (int i = 0; i < iternumber; i++) {
83             l = (TopicalLogger) lf.getLogger("foo" + i);
84         }
85         TopicalLogger[] locs = (TopicalLogger[]) lf.getLoggers();
86         assertNotNull("LoggerConf list is null", locs);
87         TopicalLogger[] locs2 = new TopicalLogger[iternumber];
88         for (int i = 0; i < locs.length; i++) {
89             assertNotNull("LoggerConf list element is null", locs[i]);
90             String JavaDoc[] ts = locs[i].getTopic();
91             assertNotNull("topic list is null", ts);
92             assertEquals("bad Topic list size", 1, ts.length);
93             assertNotNull("Null topic", ts[0]);
94             int j = 0;
95             if ((ts[0].length() > 3) && ts[0].startsWith("foo", 0)) {
96                 try {
97                     j = Integer.parseInt(ts[0].substring(3, ts[0].length()));
98                 }
99                 catch (NumberFormatException JavaDoc e) {
100                     fail("Bad topic name: " + ts[0]);
101                 }
102                 assertNull("duplicate LoggerConf", locs2[j]);
103                 locs2[j] = locs[i];
104                 assertEquals("bad topic", "foo" + j, ts[0]);
105             }
106         }
107         boolean allset = true;
108
109         for (int i = 0; i < iternumber && allset; i++) {
110             allset = locs2[i] != null;
111         }
112         if (!allset) {
113             for (int i = 0; i < iternumber; i++) {
114                 if (locs2[i]==null)
115                     System.out.println("losc2[" + i + "]=null value");
116                 else
117                     System.out.println("losc2[" + i + "]=" + locs2[i].getTopic()[0]);
118             }
119             assertTrue("Some logger has not been found", allset);
120         }
121     }
122
123     public void testMultipleTopic() {
124         l = (TopicalLogger) lf.getLogger("foo");
125
126         assertEquals("same name but 2 instances",
127             l, lf.getLogger("foo"));
128         try {
129             l.addTopic("bar");
130             l.addTopic("azerty");
131             l.addTopic("querty");
132         }
133         catch (Exception JavaDoc e) {
134             fail("does not support multiple topic");
135         }
136         /*assertEquals("additionnal (1) name but 2 instances",
137             l, lf.getLogger("bar"));
138         assertEquals("additionnal (2) name but 2 instances",
139             l, lf.getLogger("azerty"));
140         assertEquals("additionnal (3) name but 2 instances",
141             l, lf.getLogger("querty")); */

142
143         String JavaDoc[] tcs = l.getTopic();
144         assertNotNull("Topic list is null", tcs);
145         assertEquals("Wrong topic number", 4, tcs.length);
146         String JavaDoc[] tcs2 = new String JavaDoc[4];
147         for (int i = 0; i < tcs.length; i++) {
148             assertNotNull("Null topic", tcs[i]);
149             if (tcs[i].equals("foo")) {
150                 assertNull("duplicate first name", tcs2[0]);
151                 tcs2[0] = tcs[i];
152             }
153             else if (tcs[i].equals("bar")) {
154                 assertNull("duplicate name", tcs2[1]);
155                 tcs2[1] = tcs[i];
156             }
157             else if (tcs[i].equals("azerty")) {
158                 assertNull("duplicate name", tcs2[2]);
159                 tcs2[2] = tcs[i];
160             }
161             else if (tcs[i].equals("querty")) {
162                 assertNull("duplicate name", tcs2[3]);
163                 tcs2[3] = tcs[i];
164             }
165         }
166     }
167
168     public void testSimpleInheritanceLevel() {
169         l = (TopicalLogger) lf.getLogger("test.simple.inheritance.level.toto");
170         l.setIntLevel(BasicLevel.WARN);
171         l = (TopicalLogger) lf.getLogger("test.simple.inheritance.level.toto.titi");
172         assertTrue("wrong isLoggable return 1", l.isLoggable(BasicLevel.WARN));
173         assertTrue("wrong isLoggable return 2", !l.isLoggable(BasicLevel.DEBUG));
174
175         l.setIntLevel(BasicLevel.DEBUG);
176         assertTrue("wrong isLoggable return 3", l.isLoggable(BasicLevel.WARN));
177         assertTrue("wrong isLoggable return 4", l.isLoggable(BasicLevel.DEBUG));
178     }
179
180     public void testTopicsInheritanceLevel() {
181         l = (TopicalLogger) lf.getLogger("test.topic.inheritance.level.toto");
182         l.setIntLevel(BasicLevel.WARN);
183         l = (TopicalLogger) lf.getLogger("test.topic.inheritance.level.toto.titi");
184         assertTrue("wrong isLoggable return 1", l.isLoggable(BasicLevel.WARN));
185         assertTrue("wrong isLoggable return 2",
186             !l.isLoggable(BasicLevel.DEBUG));
187
188         try {
189             l.addTopic("test.topic.inheritance.level.tutu.titi");
190         }
191         catch (Exception JavaDoc e) {
192             Assert.fail("Multiple topic error: " + e.getMessage());
193         }
194         l = (TopicalLogger) lf.getLogger("test.topic.inheritance.level.tutu");
195         l.setIntLevel(BasicLevel.DEBUG);
196         assertTrue("wrong isLoggable return 3", l.isLoggable(BasicLevel.WARN));
197         assertTrue("wrong isLoggable return 4", l.isLoggable(BasicLevel.DEBUG));
198     }
199
200     public void testLogInCollocatedHandler() {
201         quietRootLogger();
202         l = (TopicalLogger) lf.getLogger("test.simple.log");
203         Handler hc =
204             hf.createHandler("myhandler", "file");
205         hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
206         hc.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
207         hc.setAttribute("activation", lf);
208         try {
209             l.addHandler(hc);
210         }
211         catch (Exception JavaDoc e) {
212             fail(e.getMessage());
213         }
214         l.setIntLevel(BasicLevel.DEBUG);
215         l.log(BasicLevel.DEBUG, "collocated Handler bar");
216         String JavaDoc[] found = getFirstLines(LOG_FILE_NAME, 1);
217         assertNotNull("TestHelper error", found);
218         assertNotNull("TestHelper error", found[0]);
219         assertTrue("no log in collocated Handler", found[0].endsWith("collocated Handler bar"));
220     }
221
222     public void testLogInSeveralCollocatedHandler() {
223         quietRootLogger();
224         l = (TopicalLogger) lf.getLogger("test.simple.log");
225         Handler hc1 = hf.createHandler("myhandler", "file");
226         hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + "1");
227         hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
228         hc1.setAttribute("activation", hf);
229
230         Handler hc2 = hf.createHandler("myhandler2", "file");
231         hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + "2");
232         hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
233         hc2.setAttribute("activation", hf);
234
235         try {
236             l.addHandler(hc1);
237             l.addHandler(hc2);
238         }
239         catch (Exception JavaDoc e) {
240             fail(e.getMessage());
241         }
242
243         l.setIntLevel(BasicLevel.DEBUG);
244         l.log(BasicLevel.DEBUG, "several collocated Handler bar");
245
246         String JavaDoc[] found = getFirstLines(LOG_FILE_NAME + "1", 1);
247         assertNotNull("TestHelper error", found);
248         assertNotNull("TestHelper error", found[0]);
249         assertTrue("no log in collocated Handler",
250             found[0].endsWith("several collocated Handler bar"));
251
252         found = getFirstLines(LOG_FILE_NAME + "2", 1);
253         assertNotNull("TestHelper error", found);
254         assertNotNull("TestHelper error", found[0]);
255         assertTrue("no log in collocated Handler",
256             found[0].endsWith("several collocated Handler bar"));
257     }
258
259     public void testLogInInheritedHandler() {
260         quietRootLogger();
261         l = (TopicalLogger) lf.getLogger("test.simple.log");
262         Handler hc = hf.createHandler("myhandler", "file");
263         hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
264         hc.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
265         hc.setAttribute("activation", hf);
266         try {
267             l.addHandler(hc);
268         }
269         catch (Exception JavaDoc e) {
270             fail(e.getMessage());
271         }
272         l.setIntLevel(BasicLevel.DEBUG);
273         l = (TopicalLogger) lf.getLogger("test.simple.log.foo");
274         l.log(BasicLevel.DEBUG, "inherited Handler bar");
275         String JavaDoc[] found = getLastLines(LOG_FILE_NAME, 1);
276         assertNotNull("TestHelper error", found);
277         assertNotNull("TestHelper error", found[0]);
278         assertTrue("no log in inherited Handler", found[0].endsWith("inherited Handler bar"));
279     }
280
281     public void testLogInSeveralInheritedHandler() {
282         quietRootLogger();
283         l = (TopicalLogger) lf.getLogger("test.simple.log");
284         Handler hc1 = hf.createHandler("myhandler", "file");
285         hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + "1");
286         hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
287         hc1.setAttribute("activation", hf);
288
289         Handler hc2 = hf.createHandler("myhandler2", "file");
290         hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + "2");
291         hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
292         hc2.setAttribute("activation", hf);
293
294         try {
295             l.addHandler(hc1);
296             l = (TopicalLogger) lf.getLogger("test.simple.log.foo");
297             l.addHandler(hc2);
298         }
299         catch (Exception JavaDoc e) {
300             fail(e.getMessage());
301         }
302
303         l = (TopicalLogger) lf.getLogger("test.simple.log.foo.bar");
304         l.setIntLevel(BasicLevel.DEBUG);
305         l.log(BasicLevel.DEBUG, "several collocated Handler bar");
306
307         String JavaDoc[] found = getFirstLines(LOG_FILE_NAME + "1", 1);
308         assertNotNull("TestHelper error", found);
309         assertNotNull("TestHelper error", found[0]);
310         assertTrue("no log in collocated Handler",
311             found[0].endsWith("several collocated Handler bar"));
312
313         found = getFirstLines(LOG_FILE_NAME + "2", 1);
314         assertNotNull("TestHelper error", found);
315         assertNotNull("TestHelper error", found[0]);
316         assertTrue("no log in collocated Handler",
317             found[0].endsWith("several collocated Handler bar"));
318     }
319 }
320
Popular Tags