001: /**
002: * Copyright (C) 2002
003: */package org.objectweb.util.monolog;
004:
005: import junit.framework.Assert;
006: import junit.framework.TestCase;
007: import junit.textui.TestRunner;
008:
009: import org.objectweb.util.monolog.api.Handler;
010: import org.objectweb.util.monolog.api.HandlerFactory;
011:
012: /**
013: * This test verifies an implementation of the Handler interface and an
014: * implementation of the HandlerFactory interface.
015: *
016: * @author Sebastien Chassande-Barrioz
017: */
018: public class TestHandler extends TestCase {
019:
020: /**
021: * This constant field contains the list of the setter name needed to
022: * initialize this class.
023: */
024: public static final String[] SETTER_METHODS = { "setHandlerFactoryClassName" };
025:
026: protected HandlerFactory hf = null;
027:
028: private Handler hc = null;
029:
030: public TestHandler() {
031: super ("");
032: }
033:
034: /**
035: * It assigns the HandlerFactory class name. This method tries to get an
036: * instance with this class name.
037: */
038: public void setHandlerFactoryClassName(String lfcn) {
039: try {
040: hf = (HandlerFactory) Class.forName(lfcn).newInstance();
041: } catch (ClassCastException e) {
042: throw new UnsupportedOperationException(
043: "The specified class is not a Handler factory: "
044: + lfcn);
045: } catch (Exception e) {
046: throw new UnsupportedOperationException(
047: "Handler factory class is not availlable: " + lfcn);
048: }
049: }
050:
051: /**
052: * For running the TestHandler suite standalone. A particular TestSuite is
053: * used to initialized the class instance by setter methods.
054: * One parameter is required:
055: * <ul>
056: * <li>a HandlerFactory class name</li>
057: * </ul>
058: */
059: public static void main(String args[]) {
060: if (args.length < 1) {
061: System.out.println("Syntax error !");
062: System.out
063: .println("java TestHandler <handler factory class name>");
064: System.exit(12);
065: }
066: Object[] params = { args[0] };
067: try {
068: TestSuite suite = new TestSuite(TestHandler.class,
069: SETTER_METHODS, params);
070: TestRunner.run(suite);
071: } catch (Exception e) {
072: e.printStackTrace();
073: }
074: }
075:
076: public static TestSuite getTestSuite(String lfcn) {
077: Object[] params = { lfcn };
078: try {
079: return new TestSuite(TestHandler.class, SETTER_METHODS,
080: params);
081: } catch (Exception e) {
082: e.printStackTrace();
083: return null;
084: }
085: }
086:
087: // ------ TEST METHODS ------ //
088: //----------------------------//
089:
090: /**
091: * It tests to define and configure a console handler.
092: * see testHandlerDef(byte) method
093: */
094: public void testConsoleHandlerDef() {
095: testHandlerDef("console");
096: }
097:
098: /**
099: * It tests to define and configure a file handler.
100: * see testHandlerDef(byte) method
101: */
102: public void testFileHandlerDef() {
103: testHandlerDef("file");
104: }
105:
106: /**
107: * It tests to define and configure a rolling file handler.
108: * see testHandlerDef(byte) method
109: */
110: public void testRollingFileHandlerDef() {
111: testHandlerDef("rollingfile");
112: }
113:
114: /**
115: * It creates one handler. Then it fetches it by its name with the
116: * getHandler(String) method. It checks the name, properties, the
117: * number of handler in the HandlerFactory.
118: */
119: protected void testHandlerDef(String handlerType) {
120: hc = hf.createHandler("MyHandler", handlerType);
121: hc.setAttribute("toto_key", "toto_value");
122: hc.setAttribute("titi.key", "titi.value");
123: // do not activate the handler because it is not properly configured
124:
125: assertEquals("assigns property", "toto_value", hc
126: .getAttribute("toto_key"));
127: assertEquals("assigns property with dot", "titi.value", hc
128: .getAttribute("titi.key"));
129: assertEquals("assigns a handler name", "MyHandler", hc
130: .getName());
131:
132: assertEquals(
133: "Handler not correctly registered into MonologConf",
134: hc, hf.getHandler("MyHandler"));
135: assertEquals(
136: "Handler not correctly registered into MonologConf (time 2)",
137: hc, hf.getHandler("MyHandler"));
138: hc = hf.getHandler("MyHandler");
139:
140: assertEquals("assigns property after getHandlerConf(String)",
141: "toto_value", hc.getAttribute("toto_key"));
142: assertEquals(
143: "assigns property with dot after getHandlerConf(String)",
144: "titi.value", hc.getAttribute("titi.key"));
145: assertEquals(
146: "assigns a handler name after getHandlerConf(String)",
147: "MyHandler", hc.getName());
148:
149: assertEquals("getHandler() with one HandlerConf", 1, hf
150: .getHandlers().length);
151:
152: hf.removeHandler("MyHandler");
153:
154: assertEquals("removeHandler with one HandlerConf", 0, hf
155: .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 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 name = "MyHandler" + i;
196: hc = hf.getHandler(name);
197: assertNotNull("fetch " + name, hc);
198: assertEquals("name value by fetch " + name, name, hc
199: .getName());
200:
201: assertEquals("assigns property by fetch " + name,
202: "toto_value" + i, hc.getAttribute("toto_key" + i));
203: assertEquals("assigns property with dot by fetch " + name,
204: "titi.value" + i, hc.getAttribute("titi.key" + i));
205: }
206: }
207:
208: /**
209: * It tests to fetch several console handlers by the getHandler() method.
210: * see testGetAllHandler(byte) method
211: */
212: public void testGetAllHandlerConsole() {
213: testGetAllHandler("console");
214: }
215:
216: /**
217: * It tests to fetch several file handlers by the getHandler() method.
218: * see testGetAllHandler(byte) method
219: */
220: public void testGetAllHandlerFile() {
221: testGetAllHandler("file");
222: }
223:
224: /**
225: * It tests to fetch several rolling file handlers by the getHandler()
226: * method.
227: * see testGetAllHandler(byte) method
228: */
229: public void testGetAllHandlerRollingFile() {
230: testGetAllHandler("rollingfile");
231: }
232:
233: /**
234: * It creates 20 handlers. Then it fetches them by the
235: * getHandler() method.
236: */
237: protected void testGetAllHandler(String handlerType) {
238: int iternumber = 20;
239: for (int i = 0; i < iternumber; i++) {
240: hc = hf.createHandler("MyHandler" + i, handlerType);
241: hc.setAttribute("toto_key" + i, "toto_value" + i);
242: hc.setAttribute("titi.key" + i, "titi.value" + i);
243: // do not activate the handler because it is not properly configured
244: }
245: Handler[] hcs = hf.getHandlers();
246: assertEquals("getHandler with " + iternumber + " HandlerConf",
247: iternumber, hcs.length);
248:
249: Handler[] hcs2 = new Handler[hcs.length];
250: for (int i = 0; i < hcs.length; i++) {
251: hc = hcs[i];
252: int j = Integer.parseInt(hc.getName().substring(9,
253: hc.getName().length()));
254: assertNull("Duplicate HandlerConf", hcs2[j]);
255: hcs2[j] = hc;
256: String name = "MyHandler" + j;
257: assertNotNull("fetch " + name, hc);
258: assertEquals("name value by fetch " + name, name, hc
259: .getName());
260:
261: assertEquals("assigns property by fetch " + name,
262: "toto_value" + j, hc.getAttribute("toto_key" + j));
263: assertEquals("assigns property with dot by fetch " + name,
264: "titi.value" + j, hc.getAttribute("titi.key" + j));
265: }
266: }
267:
268: /**
269: * It tests to remove several console handlers.
270: * see testRemoveHandler(byte) method
271: */
272: public void testRemoveConsoleHandler() {
273: testRemoveHandler("console");
274: }
275:
276: /**
277: * It tests to remove several file handlers.
278: * see testRemoveHandler(byte) method
279: */
280: public void testRemoveFileHandler() {
281: testRemoveHandler("file");
282: }
283:
284: /**
285: * It tests to remove several rolling file handlers.
286: * see testRemoveHandler(byte) method
287: */
288: public void testRemoveRollingFileHandler() {
289: testRemoveHandler("rollingfile");
290: }
291:
292: /**
293: * It creates 20 handlers. Then it removes them.
294: */
295: public void testRemoveHandler(String handlerType) {
296: int iternumber = 20;
297: for (int i = 0; i < iternumber; i++) {
298: hc = hf.createHandler("MyHandler" + i, handlerType);
299: hc.setAttribute("toto_key" + i, "toto_value" + i);
300: hc.setAttribute("titi.key" + i, "titi.value" + i);
301: // do not activate the handler because it is not properly configured
302: }
303:
304: Handler[] hcs = hf.getHandlers();
305: for (int i = 0; i < hcs.length; i++) {
306: String name = "MyHandler" + i;
307: hf.removeHandler(name);
308: Assert.assertNull("Remove " + name, hf.getHandler(name));
309: }
310: }
311:
312: /**
313: * It creates 20 handlers. Then it fetches them by their name with the
314: * getHandler(String) method.
315: */
316: protected void testgetHandlerByName() {
317: int iternumber = 20;
318: for (int i = 0; i < iternumber; i++) {
319: if (i < (iternumber / 3)) {
320: hc = hf.createHandler("MyHandler" + i, "console");
321: } else if (i < ((2 * iternumber) / 3)) {
322: hc = hf.createHandler("MyHandler" + i, "file");
323: } else {
324: hc = hf.createHandler("MyHandler" + i, "rollingfile");
325: }
326: hc.setAttribute("toto_key" + i, "toto_value" + i);
327: hc.setAttribute("titi.key" + i, "titi.value" + i);
328: // do not activate the handler because it is not properly configured
329: }
330: for (int i = 0; i < iternumber; i++) {
331: String name = "MyHandler" + i;
332: hc = hf.getHandler(name);
333: assertNotNull("fetch " + name, hc);
334: assertEquals("name value by fetch " + name, name, hc
335: .getName());
336:
337: assertEquals("assigns property by fetch " + name,
338: "toto_value" + i, hc.getAttribute("toto_key" + i));
339: assertEquals("assigns property with dot by fetch " + name,
340: "titi.value" + i, hc.getAttribute("titi.key" + i));
341: }
342: }
343: }
|