KickJava   Java API By Example, From Geeks To Geeks.

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


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

4
5 package org.objectweb.util.monolog;
6
7 import junit.framework.Assert;
8 import junit.framework.TestCase;
9 import junit.textui.TestRunner;
10
11 import org.objectweb.util.monolog.api.Handler;
12 import org.objectweb.util.monolog.api.HandlerFactory;
13
14 /**
15  * This test verifies an implementation of the Handler interface and an
16  * implementation of the HandlerFactory interface.
17  *
18  * @author Sebastien Chassande-Barrioz
19  */

20 public class TestHandler extends TestCase {
21
22     /**
23      * This constant field contains the list of the setter name needed to
24      * initialize this class.
25      */

26     public static final String JavaDoc[] SETTER_METHODS = {"setHandlerFactoryClassName"};
27
28     protected HandlerFactory hf = null;
29
30     private Handler hc = null;
31
32     public TestHandler() {
33         super("");
34     }
35
36     /**
37      * It assigns the HandlerFactory class name. This method tries to get an
38      * instance with this class name.
39      */

40     public void setHandlerFactoryClassName(String JavaDoc lfcn) {
41         try {
42             hf = (HandlerFactory) Class.forName(lfcn).newInstance();
43         }
44         catch (ClassCastException JavaDoc e) {
45             throw new UnsupportedOperationException JavaDoc(
46                 "The specified class is not a Handler factory: " + lfcn);
47         }
48         catch (Exception JavaDoc e) {
49             throw new UnsupportedOperationException JavaDoc(
50                 "Handler factory class is not availlable: " + lfcn);
51         }
52     }
53
54     /**
55      * For running the TestHandler suite standalone. A particular TestSuite is
56      * used to initialized the class instance by setter methods.
57      * One parameter is required:
58      * <ul>
59      * <li>a HandlerFactory class name</li>
60      * </ul>
61      */

62     public static void main(String JavaDoc args[]) {
63         if (args.length < 1) {
64             System.out.println("Syntax error !");
65             System.out.println("java TestHandler <handler factory class name>");
66             System.exit(12);
67         }
68         Object JavaDoc[] params = {args[0]};
69         try {
70             TestSuite suite =
71                 new TestSuite(TestHandler.class, SETTER_METHODS, params);
72             TestRunner.run(suite);
73         }
74         catch (Exception JavaDoc e) {
75             e.printStackTrace();
76         }
77     }
78
79     public static TestSuite getTestSuite(String JavaDoc lfcn) {
80         Object JavaDoc[] params = {lfcn};
81         try {
82             return new TestSuite(TestHandler.class, SETTER_METHODS, params);
83         }
84         catch (Exception JavaDoc e) {
85             e.printStackTrace();
86             return null;
87         }
88     }
89
90     // ------ TEST METHODS ------ //
91
//----------------------------//
92

93     /**
94      * It tests to define and configure a console handler.
95      * see testHandlerDef(byte) method
96      */

97     public void testConsoleHandlerDef() {
98         testHandlerDef("console");
99     }
100
101     /**
102      * It tests to define and configure a file handler.
103      * see testHandlerDef(byte) method
104      */

105     public void testFileHandlerDef() {
106         testHandlerDef("file");
107     }
108
109     /**
110      * It tests to define and configure a rolling file handler.
111      * see testHandlerDef(byte) method
112      */

113     public void testRollingFileHandlerDef() {
114         testHandlerDef("rollingfile");
115     }
116
117     /**
118      * It creates one handler. Then it fetches it by its name with the
119      * getHandler(String) method. It checks the name, properties, the
120      * number of handler in the HandlerFactory.
121      */

122     protected void testHandlerDef(String JavaDoc handlerType) {
123         hc = hf.createHandler("MyHandler", handlerType);
124         hc.setAttribute("toto_key", "toto_value");
125         hc.setAttribute("titi.key", "titi.value");
126         // do not activate the handler because it is not properly configured
127

128         assertEquals("assigns property",
129             "toto_value", hc.getAttribute("toto_key"));
130         assertEquals("assigns property with dot",
131             "titi.value", hc.getAttribute("titi.key"));
132         assertEquals("assigns a handler name",
133             "MyHandler", hc.getName());
134
135
136         assertEquals("Handler not correctly registered into MonologConf",
137             hc, hf.getHandler("MyHandler"));
138         assertEquals("Handler not correctly registered into MonologConf (time 2)",
139             hc, hf.getHandler("MyHandler"));
140         hc = hf.getHandler("MyHandler");
141
142         assertEquals("assigns property after getHandlerConf(String)",
143             "toto_value", hc.getAttribute("toto_key"));
144         assertEquals("assigns property with dot after getHandlerConf(String)",
145             "titi.value", hc.getAttribute("titi.key"));
146         assertEquals("assigns a handler name after getHandlerConf(String)",
147             "MyHandler", hc.getName());
148
149         assertEquals("getHandler() with one HandlerConf",
150             1, hf.getHandlers().length);
151
152         hf.removeHandler("MyHandler");
153
154         assertEquals("removeHandler with one HandlerConf",
155             0, hf.getHandlers().length);
156     }
157
158     /**
159      * It tests to fetch several console handlers by their name.
160      * see the testgetHandlerByName(byte) method
161      */

162     public void testgetConsoleHandlerByName() {
163         testgetHandlerByName("console");
164     }
165
166     /**
167      * It tests to fetch several file handlers by their name.
168      * see testgetHandlerByName(byte) method
169      */

170     public void testgetFileHandlerByName() {
171         testgetHandlerByName("file");
172     }
173
174     /**
175      * It tests to fetch several rolling file handlers by their name.
176      * see testgetHandlerByName(byte) method
177      */

178     public void testgetRollingFileHandlerByName() {
179         testgetHandlerByName("rollingfile");
180     }
181
182     /**
183      * It creates 20 handlers. Then it fetches them by their name with the
184      * getHandler(String) method.
185      */

186     protected void testgetHandlerByName(String JavaDoc handlerType) {
187         int iternumber = 20;
188         for (int i = 0; i < iternumber; i++) {
189             hc = hf.createHandler("MyHandler" + i, handlerType);
190             hc.setAttribute("toto_key" + i, "toto_value" + i);
191             hc.setAttribute("titi.key" + i, "titi.value" + i);
192             // do not activate the handler because it is not properly configured
193
}
194         for (int i = 0; i < iternumber; i++) {
195             String JavaDoc name = "MyHandler" + i;
196             hc = hf.getHandler(name);
197             assertNotNull("fetch " + name, hc);
198             assertEquals("name value by fetch " + name, name, hc.getName());
199
200             assertEquals("assigns property by fetch " + name,
201                 "toto_value" + i, hc.getAttribute("toto_key" + i));
202             assertEquals("assigns property with dot by fetch " + name,
203                 "titi.value" + i, hc.getAttribute("titi.key" + i));
204         }
205     }
206
207     /**
208      * It tests to fetch several console handlers by the getHandler() method.
209      * see testGetAllHandler(byte) method
210      */

211     public void testGetAllHandlerConsole() {
212         testGetAllHandler("console");
213     }
214
215     /**
216      * It tests to fetch several file handlers by the getHandler() method.
217      * see testGetAllHandler(byte) method
218      */

219     public void testGetAllHandlerFile() {
220         testGetAllHandler("file");
221     }
222
223     /**
224      * It tests to fetch several rolling file handlers by the getHandler()
225      * method.
226      * see testGetAllHandler(byte) method
227      */

228     public void testGetAllHandlerRollingFile() {
229         testGetAllHandler("rollingfile");
230     }
231
232     /**
233      * It creates 20 handlers. Then it fetches them by the
234      * getHandler() method.
235      */

236     protected void testGetAllHandler(String JavaDoc handlerType) {
237         int iternumber = 20;
238         for (int i = 0; i < iternumber; i++) {
239             hc = hf.createHandler("MyHandler" + i, handlerType);
240             hc.setAttribute("toto_key" + i, "toto_value" + i);
241             hc.setAttribute("titi.key" + i, "titi.value" + i);
242             // do not activate the handler because it is not properly configured
243
}
244         Handler[] hcs = hf.getHandlers();
245         assertEquals("getHandler with " + iternumber + " HandlerConf",
246             iternumber, hcs.length);
247
248         Handler[] hcs2 = new Handler[hcs.length];
249         for (int i = 0; i < hcs.length; i++) {
250             hc = hcs[i];
251             int j = Integer.parseInt(
252                 hc.getName().substring(9,hc.getName().length()));
253             assertNull("Duplicate HandlerConf", hcs2[j]);
254             hcs2[j] = hc;
255             String JavaDoc name = "MyHandler" + j;
256             assertNotNull("fetch " + name, hc);
257             assertEquals("name value by fetch " + name, name, hc.getName());
258
259             assertEquals("assigns property by fetch " + name,
260                 "toto_value" + j, hc.getAttribute("toto_key" + j));
261             assertEquals("assigns property with dot by fetch " + name,
262                 "titi.value" + j, hc.getAttribute("titi.key" + j));
263         }
264     }
265
266     /**
267      * It tests to remove several console handlers.
268      * see testRemoveHandler(byte) method
269      */

270     public void testRemoveConsoleHandler() {
271         testRemoveHandler("console");
272     }
273
274     /**
275      * It tests to remove several file handlers.
276      * see testRemoveHandler(byte) method
277      */

278     public void testRemoveFileHandler() {
279         testRemoveHandler("file");
280     }
281
282     /**
283      * It tests to remove several rolling file handlers.
284      * see testRemoveHandler(byte) method
285      */

286     public void testRemoveRollingFileHandler() {
287         testRemoveHandler("rollingfile");
288     }
289
290     /**
291      * It creates 20 handlers. Then it removes them.
292      */

293     public void testRemoveHandler(String JavaDoc handlerType) {
294         int iternumber = 20;
295         for (int i = 0; i < iternumber; i++) {
296             hc = hf.createHandler("MyHandler" + i, handlerType);
297             hc.setAttribute("toto_key" + i, "toto_value" + i);
298             hc.setAttribute("titi.key" + i, "titi.value" + i);
299             // do not activate the handler because it is not properly configured
300
}
301
302         Handler[] hcs = hf.getHandlers();
303         for (int i = 0; i < hcs.length; i++) {
304             String JavaDoc name = "MyHandler" + i;
305             hf.removeHandler(name);
306             Assert.assertNull("Remove " + name, hf.getHandler(name));
307         }
308     }
309
310     /**
311      * It creates 20 handlers. Then it fetches them by their name with the
312      * getHandler(String) method.
313      */

314     protected void testgetHandlerByName() {
315         int iternumber = 20;
316         for (int i = 0; i < iternumber; i++) {
317             if (i<(iternumber/3)) {
318                 hc = hf.createHandler("MyHandler" + i, "console");
319             }
320             else if (i<((2*iternumber)/3)) {
321                 hc = hf.createHandler("MyHandler" + i,
322                     "file");
323             }
324             else {
325                 hc = hf.createHandler("MyHandler" + i, "rollingfile");
326             }
327             hc.setAttribute("toto_key" + i, "toto_value" + i);
328             hc.setAttribute("titi.key" + i, "titi.value" + i);
329             // do not activate the handler because it is not properly configured
330
}
331         for (int i = 0; i < iternumber; i++) {
332             String JavaDoc name = "MyHandler" + i;
333             hc = hf.getHandler(name);
334             assertNotNull("fetch " + name, hc);
335             assertEquals("name value by fetch " + name,
336                 name, hc.getName());
337
338             assertEquals("assigns property by fetch " + name,
339                 "toto_value" + i, hc.getAttribute("toto_key" + i));
340             assertEquals("assigns property with dot by fetch " + name,
341                 "titi.value" + i, hc.getAttribute("titi.key" + i));
342         }
343     }
344 }
345
Popular Tags