KickJava   Java API By Example, From Geeks To Geeks.

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


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.TopicalLogger;
9 import org.objectweb.util.monolog.api.Handler;
10 import org.objectweb.util.monolog.wrapper.printwriter.PrintWriterImpl;
11
12 import java.io.PrintWriter JavaDoc;
13 import java.io.File JavaDoc;
14
15 /**
16  *
17  * @author Sebastien Chassande-Barrioz
18  */

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

29     public static void main(String JavaDoc args[]) {
30         if (args.length < 1) {
31             System.out.println("Syntax error !");
32             System.out.println("java TestPw2Logger <logger factory class name>");
33             System.exit(1);
34         }
35         TestHelper.run(TestPw2Logger.class, new String JavaDoc[0],
36             new String JavaDoc[0], args[0]);
37     }
38
39     public static TestSuite getTestSuite(String JavaDoc lfcn) {
40         return TestHelper.getTestSuite(TestPw2Logger.class, new String JavaDoc[0],
41             new String JavaDoc[0], lfcn);
42     }
43
44     public void testSimple() {
45         quietRootLogger();
46         new File JavaDoc(LOG_FILE_NAME).delete();
47         l = (TopicalLogger) lf.getLogger("test.pw2logger.foo");
48         l.setIntLevel(BasicLevel.DEBUG);
49         Handler h = hf.createHandler("pw2logger", "file");
50         h.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
51         h.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
52         h.setAttribute("activation", hf);
53         try {
54             l.addHandler(h);
55         } catch (Exception JavaDoc e) {
56             fail(e.getMessage());
57         }
58
59         PrintWriter JavaDoc pw = new PrintWriterImpl(l);
60         pw.print(true);
61         pw.print('a');
62         pw.print(123);
63         pw.print(1.3);
64         pw.println();
65         pw.println("toto");
66         String JavaDoc[] found = getFirstLines(LOG_FILE_NAME, 2);
67         assertNotNull("TestHelper error: return null", found);
68         assertEquals("TestHelper error: wrong size", 2, found.length);
69         assertNotNull("TestHelper error: element 0 is null", found[0]);
70         assertNotNull("TestHelper error: element 1 is null", found[1]);
71         assertTrue("end with 'truea1231.3' expected, but found " + found[0],
72                 found[0].endsWith("truea1231.3"));
73         assertTrue("", found[1].endsWith("toto"));
74         new File JavaDoc(LOG_FILE_NAME).delete();
75     }
76 }
77
Popular Tags