01: /**
02: * Copyright (C) 2002
03: */package org.objectweb.util.monolog;
04:
05: import org.objectweb.util.monolog.api.BasicLevel;
06: import org.objectweb.util.monolog.api.TopicalLogger;
07: import org.objectweb.util.monolog.api.Handler;
08: import org.objectweb.util.monolog.wrapper.printwriter.PrintWriterImpl;
09:
10: import java.io.PrintWriter;
11: import java.io.File;
12:
13: /**
14: *
15: * @author Sebastien Chassande-Barrioz
16: */
17: public class TestPw2Logger extends TestHelper {
18:
19: public static final String LOG_FILE_NAME = "test.log";
20: public static final String LOG_PATTERN = "%m%n";
21:
22: TopicalLogger l = null;
23:
24: /**
25: * For running the TestLogger suite standalone.
26: */
27: public static void main(String args[]) {
28: if (args.length < 1) {
29: System.out.println("Syntax error !");
30: System.out
31: .println("java TestPw2Logger <logger factory class name>");
32: System.exit(1);
33: }
34: TestHelper.run(TestPw2Logger.class, new String[0],
35: new String[0], args[0]);
36: }
37:
38: public static TestSuite getTestSuite(String lfcn) {
39: return TestHelper.getTestSuite(TestPw2Logger.class,
40: new String[0], new String[0], lfcn);
41: }
42:
43: public void testSimple() {
44: quietRootLogger();
45: new File(LOG_FILE_NAME).delete();
46: l = (TopicalLogger) lf.getLogger("test.pw2logger.foo");
47: l.setIntLevel(BasicLevel.DEBUG);
48: Handler h = hf.createHandler("pw2logger", "file");
49: h.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
50: h.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
51: h.setAttribute("activation", hf);
52: try {
53: l.addHandler(h);
54: } catch (Exception e) {
55: fail(e.getMessage());
56: }
57:
58: PrintWriter pw = new PrintWriterImpl(l);
59: pw.print(true);
60: pw.print('a');
61: pw.print(123);
62: pw.print(1.3);
63: pw.println();
64: pw.println("toto");
65: String[] found = getFirstLines(LOG_FILE_NAME, 2);
66: assertNotNull("TestHelper error: return null", found);
67: assertEquals("TestHelper error: wrong size", 2, found.length);
68: assertNotNull("TestHelper error: element 0 is null", found[0]);
69: assertNotNull("TestHelper error: element 1 is null", found[1]);
70: assertTrue("end with 'truea1231.3' expected, but found "
71: + found[0], found[0].endsWith("truea1231.3"));
72: assertTrue("", found[1].endsWith("toto"));
73: new File(LOG_FILE_NAME).delete();
74: }
75: }
|