KickJava   Java API By Example, From Geeks To Geeks.

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


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.Level;
10 import org.objectweb.util.monolog.api.Logger;
11 import org.objectweb.util.monolog.api.TopicalLogger;
12 import org.objectweb.util.monolog.wrapper.log4j.LevelImpl;
13
14 /**
15  * It verifies that a monolog wrapper support the intermediate level feature.
16  *
17  * @author Sebastien Chassande-Barrioz
18  */

19 public class TestIntermediateLevel 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 TestIntermediateLevel <logger factory class name>");
33             System.exit(1);
34         }
35         TestHelper.run(TestIntermediateLevel.class, new String JavaDoc[0], new String JavaDoc[0], args[0]);
36     }
37
38     /**
39      * Return the test suite associated to this class.
40      */

41     public static TestSuite getTestSuite(String JavaDoc lfcn) {
42         return TestHelper.getTestSuite(TestIntermediateLevel.class, new String JavaDoc[0],
43             new String JavaDoc[0], lfcn);
44     }
45
46     public void testIntermediateLevel() {
47         debug("testIntermediateLevel : LOG_FILE_NAME =" + LOG_FILE_NAME);
48         debug("BasicLevel.DEBUG=" + BasicLevel.DEBUG);
49         int debug1 = BasicLevel.DEBUG + 1;
50         debug("debug1=" + debug1);
51         int debug2 = BasicLevel.DEBUG + 2;
52         debug("debug2=" + debug2);
53         Level debug3 = lef.defineLevel("DEBUG3", BasicLevel.DEBUG + 3);
54         debug("debug3=" +
55             (debug3 != null ? "Level: " + debug3.getIntValue() : "null"));
56         debug("BasicLevel.INFO=" + BasicLevel.INFO);
57         debug("BasicLevel.WARN=" + BasicLevel.WARN);
58         debug("BasicLevel.ERROR=" + BasicLevel.ERROR);
59         debug("BasicLevel.FATAL=" + BasicLevel.FATAL);
60
61         quietRootLogger();
62         l = (TopicalLogger)
63             lf.getLogger("test.intermediate_level.foo");
64
65         Handler hc = hf.createHandler(
66             "intermediate_level_handler", "file");
67         hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
68         hc.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
69         hc.setAttribute("activation", hf);
70         try {
71             l.addHandler(hc);
72         }
73         catch (Exception JavaDoc e) {
74             fail(e.getMessage());
75         }
76
77         debug("Current Level: " + l.getCurrentIntLevel());
78         l.setIntLevel(debug2);
79         debug("Change the level to debug2: " + l.getCurrentIntLevel());
80
81         logNCheckFail(l, BasicLevel.DEBUG, "intermediate level test 1", LOG_FILE_NAME, "intermediate level test FAIL 1");
82         logNCheckFail(l, debug1, "intermediate level test 2", LOG_FILE_NAME, "intermediate level test FAIL 2");
83         logNCheckFail(l, new LevelImpl("DEBUG1", debug1), "intermediate level test 3", LOG_FILE_NAME, "intermediate level test FAIL 3");
84
85         logNCheckTrue(l, debug2, "intermediate level test 4", LOG_FILE_NAME, "intermediate level test PASSED 4");
86         logNCheckTrue(l, new LevelImpl("DEBUG2", debug2), "intermediate level test 5", LOG_FILE_NAME, "intermediate level test PASSED 5");
87         logNCheckTrue(l, debug3, "intermediate level test 6", LOG_FILE_NAME, "intermediate level test PASSED 6");
88         logNCheckTrue(l, BasicLevel.INFO, "intermediate level test 7", LOG_FILE_NAME, "intermediate level test PASSED 7");
89         logNCheckTrue(l, BasicLevel.WARN, "intermediate level test 8", LOG_FILE_NAME, "intermediate level test PASSED 8");
90         logNCheckTrue(l, BasicLevel.ERROR, "intermediate level test 9", LOG_FILE_NAME, "intermediate level test PASSED 9");
91         logNCheckTrue(l, BasicLevel.FATAL, "intermediate level test 10", LOG_FILE_NAME, "intermediate level test PASSED 10");
92
93         l = (TopicalLogger) lf.getLogger("test.intermediate_level.bar");
94         try {
95             l.addHandler(hc);
96         }
97         catch (Exception JavaDoc e) {
98             fail(e.getMessage());
99         }
100         debug("Current Level: " + l.getCurrentIntLevel());
101         l.setIntLevel(debug2);
102         debug("Change the level to debug2: " + l.getCurrentIntLevel());
103         logNCheckTrue(l, BasicLevel.INFO, "intermediate level test 11", LOG_FILE_NAME, "intermediate level test PASSED 11");
104         logNCheckTrue(l, BasicLevel.WARN, "intermediate level test 12", LOG_FILE_NAME, "intermediate level test PASSED 12");
105         logNCheckTrue(l, BasicLevel.ERROR, "intermediate level test 13", LOG_FILE_NAME, "intermediate level test PASSED 13");
106         logNCheckTrue(l, BasicLevel.FATAL, "intermediate level test 14", LOG_FILE_NAME, "intermediate level test PASSED 14");
107         logNCheckTrue(l, debug3, "intermediate level test 15", LOG_FILE_NAME, "intermediate level test PASSED 15");
108         logNCheckTrue(l, debug2, "intermediate level test 16", LOG_FILE_NAME, "intermediate level test PASSED 16");
109         logNCheckFail(l, debug1, "intermediate level test 17", LOG_FILE_NAME, "intermediate level test FAIL 17");
110         logNCheckFail(l, BasicLevel.DEBUG, "intermediate level test 18", LOG_FILE_NAME, "intermediate level test FAIL 18");
111     }
112
113     public void logNCheckTrue(Logger l, int level, String JavaDoc errormessage,
114                               String JavaDoc filename, String JavaDoc required) {
115         l.log(level, required);
116         String JavaDoc[] found = getLastLines(filename, 1);
117         assertNotNull("TestHelper error", found);
118         assertNotNull("TestHelper error", found[0]);
119         assertTrue(errormessage + ": Wrong size f:"
120             + found[0].length() + " /r:" + required.length(),
121             found[0].length() >= required.length());
122         String JavaDoc f = found[0]
123             .substring(found[0].length() - required.length(), found[0].length());
124         assertEquals(errormessage, required, f);
125     }
126
127     public void logNCheckFail(Logger l, int level, String JavaDoc errormessage,
128                               String JavaDoc filename, String JavaDoc required) {
129         l.log(level, required);
130         String JavaDoc[] found = getLastLines(filename, 1);
131         if ( found!=null
132                 && found.length > 1
133                 && found[0].length() >= required.length()
134                 && found[0].endsWith(required)) {
135             fail(errormessage);
136         }
137     }
138
139     public void logNCheckTrue(Logger l, Level level, String JavaDoc errormessage,
140                               String JavaDoc filename, String JavaDoc required) {
141         l.log(level, required);
142         String JavaDoc[] found = getLastLines(filename, 1);
143         assertNotNull("TestHelper error", found);
144         assertNotNull("TestHelper error", found[0]);
145         assertTrue(errormessage + ": Wrong size f:"
146             + found[0].length() + " /r:" + required.length(),
147             found[0].length() >= required.length());
148         String JavaDoc f = found[0]
149             .substring(found[0].length() - required.length(), found[0].length());
150         assertEquals(errormessage, required, f);
151     }
152
153     public void logNCheckFail(Logger l, Level level, String JavaDoc errormessage,
154                               String JavaDoc filename, String JavaDoc required) {
155         l.log(level, required);
156         String JavaDoc[] found = getLastLines(filename, 1);
157         if ( found!=null
158                 && found.length > 1
159                 && found[0].length() >= required.length()
160                 && found[0].endsWith(required)) {
161             fail(errormessage);
162         }
163     }
164 }
165
Popular Tags