KickJava   Java API By Example, From Geeks To Geeks.

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


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

4
5 package org.objectweb.util.monolog;
6
7 import org.objectweb.util.monolog.api.TopicalLogger;
8 import org.objectweb.util.monolog.api.Handler;
9 import org.objectweb.util.monolog.api.BasicLevel;
10
11 /**
12  * This test check the additivity flag of the logger.
13  *
14  * @author Sebastien Chassande-Barrioz
15  */

16 public class TestAdditivity extends TestHelper {
17
18     public static final String JavaDoc LOG_FILE_NAME = "test.log";
19     public static final String JavaDoc LOG_PATTERN = "%m%n";
20
21     TopicalLogger l = null;
22
23     /**
24      * For running the TestLogger suite standalone.
25      */

26     public static void main(String JavaDoc args[]) {
27         if (args.length < 1) {
28             System.out.println("Syntax error !");
29             System.out.println("java TestAdditivity <logger factory class name>");
30             System.exit(1);
31         }
32         TestHelper.run(TestAdditivity.class, new String JavaDoc[0],
33             new String JavaDoc[0], args[0]);
34     }
35
36     public static TestSuite getTestSuite(String JavaDoc lfcn) {
37         return TestHelper.getTestSuite(TestAdditivity.class, new String JavaDoc[0],
38             new String JavaDoc[0], lfcn);
39     }
40
41     // ------ TEST METHODS ------ //
42
//----------------------------//
43

44     public void testA() {
45         quietRootLogger();
46         TopicalLogger l1 = (TopicalLogger) lf.getLogger("test.additivity");
47         TopicalLogger l2 = (TopicalLogger) lf.getLogger("test.additivity.foo");
48         Handler hc1 = hf.createHandler("myhandler", "file");
49         hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '1');
50         hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
51         hc1.setAttribute("activation", lf);
52         Handler hc2 = hf.createHandler("myhandler2", "file");
53         hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '2');
54         hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
55         hc2.setAttribute("activation", lf);
56         try {
57             l1.addHandler(hc1);
58             l2.addHandler(hc2);
59         }
60         catch (Exception JavaDoc e) {
61             fail(e.getMessage());
62         }
63         l2.setAdditivity(false);
64         l2.setIntLevel(BasicLevel.DEBUG);
65         l2.log(BasicLevel.DEBUG, "simple additivity");
66         // The log message must be found in the file 2
67
String JavaDoc[] found2 = getLastLines(LOG_FILE_NAME + '2', 1);
68         assertNotNull("TestHelper error", found2);
69         assertNotNull("TestHelper error", found2[0]);
70         assertTrue("A.1",
71             found2[0].endsWith("simple additivity"));
72
73         // The log message must NOT be found in the file 1
74
String JavaDoc[] found1 = getLastLines(LOG_FILE_NAME + '1', 1);
75         if (found1 !=null && found1.length>0 && found1[0] !=null
76             && found1[0].endsWith("simple additivity")) {
77             fail("A.2");
78         }
79
80         l2 = (TopicalLogger) lf.getLogger("test.additivity.foo.bar");
81         l2.log(BasicLevel.DEBUG, "simple additivity2");
82         // The log message must be found in the file 2
83
found2 = getLastLines(LOG_FILE_NAME + '2', 1);
84         assertNotNull("TestHelper error", found2);
85         assertNotNull("TestHelper error", found2[0]);
86         assertTrue("A.3",
87             found2[0].endsWith("simple additivity2"));
88
89         // The log message must NOT be found in the file 1
90
found1 = getLastLines(LOG_FILE_NAME + '1', 1);
91         if (found1 !=null && found1.length>0 && found1[0] !=null
92             && found1[0].endsWith("simple additivity2")) {
93             fail("A.4");
94         }
95     }
96     public void testB() {
97         quietRootLogger();
98         TopicalLogger l1 = (TopicalLogger) lf.getLogger("test.additivity");
99         TopicalLogger l2 = (TopicalLogger) lf.getLogger("test.additivity.foo");
100         Handler hc1 =
101             hf.createHandler("myhandler", "file");
102         hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '1');
103         hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
104         hc1.setAttribute("activation", lf);
105         Handler hc2 = hf.createHandler("myhandler2", "file");
106         hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '2');
107         hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
108         hc2.setAttribute("activation", lf);
109         try {
110             l1.addHandler(hc1);
111             l2.addHandler(hc2);
112         }
113         catch (Exception JavaDoc e) {
114             fail(e.getMessage());
115         }
116         l2.setAdditivity(false);
117         l2.log(BasicLevel.DEBUG, "simple additivity B");
118         // The log message must be found in the file 2
119
String JavaDoc[] found2 = getLastLines(LOG_FILE_NAME + '2', 1);
120         assertNotNull("TestHelper error", found2);
121         assertNotNull("TestHelper error", found2[0]);
122         assertTrue("B.1",
123             found2[0].endsWith("simple additivity B"));
124
125         // The log message must NOT be found in the file 1
126
String JavaDoc[] found1 = getLastLines(LOG_FILE_NAME + '1', 1);
127         if (found1 !=null && found1.length>0 && found1[0] !=null
128             && found1[0].endsWith("simple additivity B")) {
129             fail("B.2");
130         }
131
132         l2 = (TopicalLogger) lf.getLogger("test.additivity.foo.bar");
133         l2.setIntLevel(BasicLevel.DEBUG);
134         l2.log(BasicLevel.DEBUG, "simple additivity B 2");
135         // The log message must be found in the file 2
136
found2 = getLastLines(LOG_FILE_NAME + '2', 1);
137         assertNotNull("TestHelper error", found2);
138         assertNotNull("TestHelper error", found2[0]);
139         assertTrue("B.3",
140             found2[0].endsWith("simple additivity B 2"));
141
142         // The log message must NOT be found in the file 1
143
found1 = getFirstLines(LOG_FILE_NAME + '1', 1);
144         if (found1 !=null && found1.length>0 && found1[0] !=null
145             && found1[0].endsWith("simple additivity B 2")) {
146             fail("B.4");
147         }
148     }
149 }
150
Popular Tags