KickJava   Java API By Example, From Geeks To Geeks.

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


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

4
5 package org.objectweb.util.monolog;
6
7 import org.objectweb.util.monolog.api.BasicLevel;
8 import org.objectweb.util.monolog.api.Handler;
9 import org.objectweb.util.monolog.api.TopicalLogger;
10 import org.objectweb.util.monolog.api.MonologFactory;
11
12 import java.util.Properties JavaDoc;
13
14 /**
15  * This class test the configurability of the monolog wrappers via the
16  * Configurable interface.
17  *
18  * @author Sebastien Chassande-Barrioz
19  */

20 public class TestConfigurability extends TestHelper {
21
22     /**
23      * For running the TestConfigurability suite standalone.
24      * arguments:
25      * <logger factory class name> <mode> [<config file name> [ true | false ]]
26      */

27     public static void main(String JavaDoc args[]) {
28         if (args.length < 2) {
29             System.out.println("Syntax error !");
30             System.out.println(
31                 "java TestConfigurability <logger factory class name> <mode> "
32                 + "[<config file name> [ true | false ]]");
33             System.exit(1);
34         }
35         int size = args.length;
36         String JavaDoc[] params = new String JavaDoc[size];
37         String JavaDoc[] methods = new String JavaDoc[size];
38         params[0] = args[1];
39         methods[0] = "setConfigMode";
40         if (args.length > 2) {
41             params[1] = args[2];
42             methods[1] = "setConfigFile";
43         }
44         if (args.length > 3) {
45             params[2] = args[3];
46             methods[2] = "setUseClassPath";
47         }
48         params[params.length - 1] = "";
49         methods[methods.length - 1] = "init";
50         TestHelper.run(TestConfigurability.class, methods, params, args[0]);
51     }
52
53     /**
54      * Return the test suite associated to this class.
55      */

56     public static TestSuite getTestSuite(String JavaDoc lfcn, String JavaDoc mode,
57                                          String JavaDoc configFile, String JavaDoc useCP) {
58         int size = 1 + (mode != null ? 1 : 0) + (configFile != null ? 1 : 0)
59             + (useCP != null ? 1 : 0);
60
61         String JavaDoc[] params = new String JavaDoc[size];
62         String JavaDoc[] methods = new String JavaDoc[size];
63         params[0] = mode;
64         methods[0] = "setConfigMode";
65         if (configFile != null) {
66             params[1] = configFile;
67             methods[1] = "setConfigFile";
68         }
69         if (useCP != null) {
70             params[2] = useCP;
71             methods[2] = "setUseClassPath";
72         }
73         params[params.length - 1] = "";
74         methods[methods.length - 1] = "init";
75
76         return TestHelper.getTestSuite(TestLogger.class, methods, params, lfcn);
77     }
78
79     String JavaDoc mode = null;
80     String JavaDoc configFileName = null;
81     String JavaDoc useClassPath = null;
82
83     public void setConfigMode(String JavaDoc configMode) {
84         mode = configMode;
85     }
86
87     public void setConfigFile(String JavaDoc configFile) {
88         configFileName = configFile;
89     }
90
91     public void setUseClassPath(String JavaDoc useclasspath) {
92         useClassPath = useclasspath;
93     }
94
95     /**
96      * Call the configure method on the wrapper
97      */

98     public void init(String JavaDoc unused) {
99
100         Properties JavaDoc prop = null;
101
102         if (mode != null && !mode.equalsIgnoreCase("null")) {
103             prop = new Properties JavaDoc();
104             prop.put(MonologFactory.LOG_CONFIGURATION_TYPE, mode);
105             if (configFileName != null) {
106                 prop.put(MonologFactory.LOG_CONFIGURATION_FILE, configFileName);
107             }
108
109             if (useClassPath != null) {
110                 prop.put(MonologFactory.LOG_CONFIGURATION_FILE_USE_CLASSPATH,
111                     useClassPath);
112             }
113             debug("Test the configurability with in " + mode + " mode"
114                 + (useClassPath != null ?
115                 ", use of the classpath: " + useClassPath
116                 : ""));
117         }
118         try {
119             ((MonologFactory) lf).configure(prop);
120         }
121         catch (Exception JavaDoc e) {
122             fail("Impossible to configure in " + mode + " mode: "
123                 + e.getMessage());
124         }
125     }
126
127     public void testSimple() {
128         quietRootLogger();
129         TopicalLogger l = (TopicalLogger)
130             lf.getLogger("test.configurability.simple");
131         Handler hc =
132             hf.createHandler("myhandler_configurability", "file");
133         hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, "test.log");
134         hc.setAttribute(Handler.PATTERN_ATTRIBUTE, "%m%n");
135         hc.setAttribute("activation", lf);
136         try {
137             l.addHandler(hc);
138         }
139         catch (Exception JavaDoc e) {
140             fail(e.getMessage());
141         }
142         l.setIntLevel(BasicLevel.DEBUG);
143         String JavaDoc str = "configurability mode " + mode + " " + useClassPath;
144         l.log(BasicLevel.DEBUG, str);
145         String JavaDoc[] found = getLastLines("test.log", 1);
146         assertNotNull("TestHelper error", found);
147         assertNotNull("TestHelper error", found[0]);
148         assertTrue("no log in collocated Handler", found[0].endsWith(str));
149     }
150 }
151
Popular Tags