KickJava   Java API By Example, From Geeks To Geeks.

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


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.LevelFactory;
9 import org.objectweb.util.monolog.api.Level;
10 import junit.framework.TestCase;
11 import junit.textui.TestRunner;
12
13 /**
14  * It verifies a Level implementation and a LevelFactory implementation.
15  *
16  * @author Sebastien Chassande-Barrioz
17  */

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

24     public static final String JavaDoc[] SETTER_METHODS = {"setLevelFactoryClassName"};
25
26     protected LevelFactory lf = null;
27
28     private Level l = null;
29
30     public TestLevel() {
31         super("");
32     }
33
34     /**
35      * It assigns the LevelFactory class name. This method tries to get an
36      * instance with this class name.
37      */

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

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

91     /**
92      * This method tests if it is possible to define a LevelConf with a
93      * predefined level and the + operator: DEBUG + 1
94      */

95     public void testInterLevelDef() {
96         assertEquals("Predefined level unreconized",
97             BasicLevel.DEBUG,
98             lf.getLevel("DEBUG").getIntValue());
99         l = lf.defineLevel("MY_LEVEL", "DEBUG + 1");
100         assertNotNull("Null level", l);
101         assertEquals("intermediate level definition",
102             BasicLevel.DEBUG + 1,
103             l.getIntValue());
104     }
105
106     /**
107      * This method tests if it is possible to define a LevelConf with an
108      * other level and the + operator:
109      * <ul>
110      * <li>MY_LEVEL = DEBUG + 1</li>
111      * <li>MY_LEVEL2 = MY_LEVEL + 1</li>
112      * </ul>
113      */

114     public void testInterLevelDefWithOther() {
115         lf.defineLevel("MY_LEVEL", "DEBUG + 1");
116         l = lf.defineLevel("MY_LEVEL2", "MY_LEVEL + 1");
117         assertEquals(
118             "intermediate level definition based on another intermediate level",
119             BasicLevel.DEBUG + 2,
120             l.getIntValue());
121     }
122
123     /**
124      * This method tests if it is possible to define a LevelConf with a
125      * predefined level and the minus operator: DEBUG + 1
126      */

127     public void testInterLevelDefByMinus() {
128         l = lf.defineLevel("TOTO", "DEBUG - 1");
129         assertEquals(
130             "intermediate level definition minus operator",
131             BasicLevel.DEBUG - 1,
132             l.getIntValue());
133     }
134
135     /**
136      * This method tests if it is possible to define a LevelConf with a
137      * predefined level, the + operator and a number with several figures:
138      * DEBUG + 156
139      */

140     public void testInterLevelDefWithFigures() {
141         l = lf.defineLevel("TOTO2", "INFO +156 ");
142         assertEquals(
143             "intermediate level definition, number with several figures",
144             BasicLevel.INFO + 156,
145             l.getIntValue());
146     }
147
148     /**
149      * This method tests if it is possible to define a LevelConf with a
150      * predefined level witten with a combinaison of lower case and upper case
151      * and the + operator: InFO + 1
152      */

153     public void testInterLevelWithLowerCase() {
154         l = lf.defineLevel("TOTO3", " InFO + 1 ");
155         assertEquals(
156             "intermediate level definition, level name with lower case",
157             BasicLevel.INFO + 1,
158             l.getIntValue());
159     }
160
161     /**
162      * This method tests if it is possible to define a LevelConf with just a
163      * number.
164      */

165     public void testInterLevelJustNb() {
166         l = lf.defineLevel("TOTO4", "23455");
167         assertEquals(
168             "intermediate level definition, level value is just a number",
169             23455,
170             l.getIntValue());
171         l = lf.defineLevel("TOTO7", 23455);
172         assertEquals(
173             "intermediate level definition, level value is just a number",
174             23455,
175             l.getIntValue());
176
177     }
178
179     /**
180      * This method tests if it is possible to define a LevelConf with just an
181      * other level.
182      */

183     public void testInterLevelJustOther() {
184         lf.defineLevel("TOTO3", " InFO + 1 ");
185         l = lf.defineLevel("TOTO5", "TOTO3");
186         assertEquals(
187             "intermediate level definition, level value is just another intermediate level",
188             BasicLevel.INFO + 1,
189             l.getIntValue());
190     }
191
192 }
193
Popular Tags