001: /**
002: * Copyright (C) 2002
003: */package org.objectweb.util.monolog;
004:
005: import org.objectweb.util.monolog.api.BasicLevel;
006: import org.objectweb.util.monolog.api.Handler;
007: import org.objectweb.util.monolog.api.Level;
008: import org.objectweb.util.monolog.api.Logger;
009: import org.objectweb.util.monolog.api.TopicalLogger;
010: import org.objectweb.util.monolog.wrapper.log4j.LevelImpl;
011:
012: /**
013: * It verifies that a monolog wrapper support the intermediate level feature.
014: *
015: * @author Sebastien Chassande-Barrioz
016: */
017: public class TestIntermediateLevel extends TestHelper {
018:
019: public static final String LOG_FILE_NAME = "test.log";
020: public static final String LOG_PATTERN = "%m%n";
021:
022: TopicalLogger l = null;
023:
024: /**
025: * For running the TestLogger suite standalone.
026: */
027: public static void main(String args[]) {
028: if (args.length < 1) {
029: System.out.println("Syntax error !");
030: System.out
031: .println("java TestIntermediateLevel <logger factory class name>");
032: System.exit(1);
033: }
034: TestHelper.run(TestIntermediateLevel.class, new String[0],
035: new String[0], args[0]);
036: }
037:
038: /**
039: * Return the test suite associated to this class.
040: */
041: public static TestSuite getTestSuite(String lfcn) {
042: return TestHelper.getTestSuite(TestIntermediateLevel.class,
043: new String[0], new String[0], lfcn);
044: }
045:
046: public void testIntermediateLevel() {
047: debug("testIntermediateLevel : LOG_FILE_NAME =" + LOG_FILE_NAME);
048: debug("BasicLevel.DEBUG=" + BasicLevel.DEBUG);
049: int debug1 = BasicLevel.DEBUG + 1;
050: debug("debug1=" + debug1);
051: int debug2 = BasicLevel.DEBUG + 2;
052: debug("debug2=" + debug2);
053: Level debug3 = lef.defineLevel("DEBUG3", BasicLevel.DEBUG + 3);
054: debug("debug3="
055: + (debug3 != null ? "Level: " + debug3.getIntValue()
056: : "null"));
057: debug("BasicLevel.INFO=" + BasicLevel.INFO);
058: debug("BasicLevel.WARN=" + BasicLevel.WARN);
059: debug("BasicLevel.ERROR=" + BasicLevel.ERROR);
060: debug("BasicLevel.FATAL=" + BasicLevel.FATAL);
061:
062: quietRootLogger();
063: l = (TopicalLogger) lf.getLogger("test.intermediate_level.foo");
064:
065: Handler hc = hf.createHandler("intermediate_level_handler",
066: "file");
067: hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
068: hc.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
069: hc.setAttribute("activation", hf);
070: try {
071: l.addHandler(hc);
072: } catch (Exception e) {
073: fail(e.getMessage());
074: }
075:
076: debug("Current Level: " + l.getCurrentIntLevel());
077: l.setIntLevel(debug2);
078: debug("Change the level to debug2: " + l.getCurrentIntLevel());
079:
080: logNCheckFail(l, BasicLevel.DEBUG, "intermediate level test 1",
081: LOG_FILE_NAME, "intermediate level test FAIL 1");
082: logNCheckFail(l, debug1, "intermediate level test 2",
083: LOG_FILE_NAME, "intermediate level test FAIL 2");
084: logNCheckFail(l, new LevelImpl("DEBUG1", debug1),
085: "intermediate level test 3", LOG_FILE_NAME,
086: "intermediate level test FAIL 3");
087:
088: logNCheckTrue(l, debug2, "intermediate level test 4",
089: LOG_FILE_NAME, "intermediate level test PASSED 4");
090: logNCheckTrue(l, new LevelImpl("DEBUG2", debug2),
091: "intermediate level test 5", LOG_FILE_NAME,
092: "intermediate level test PASSED 5");
093: logNCheckTrue(l, debug3, "intermediate level test 6",
094: LOG_FILE_NAME, "intermediate level test PASSED 6");
095: logNCheckTrue(l, BasicLevel.INFO, "intermediate level test 7",
096: LOG_FILE_NAME, "intermediate level test PASSED 7");
097: logNCheckTrue(l, BasicLevel.WARN, "intermediate level test 8",
098: LOG_FILE_NAME, "intermediate level test PASSED 8");
099: logNCheckTrue(l, BasicLevel.ERROR, "intermediate level test 9",
100: LOG_FILE_NAME, "intermediate level test PASSED 9");
101: logNCheckTrue(l, BasicLevel.FATAL,
102: "intermediate level test 10", LOG_FILE_NAME,
103: "intermediate level test PASSED 10");
104:
105: l = (TopicalLogger) lf.getLogger("test.intermediate_level.bar");
106: try {
107: l.addHandler(hc);
108: } catch (Exception e) {
109: fail(e.getMessage());
110: }
111: debug("Current Level: " + l.getCurrentIntLevel());
112: l.setIntLevel(debug2);
113: debug("Change the level to debug2: " + l.getCurrentIntLevel());
114: logNCheckTrue(l, BasicLevel.INFO, "intermediate level test 11",
115: LOG_FILE_NAME, "intermediate level test PASSED 11");
116: logNCheckTrue(l, BasicLevel.WARN, "intermediate level test 12",
117: LOG_FILE_NAME, "intermediate level test PASSED 12");
118: logNCheckTrue(l, BasicLevel.ERROR,
119: "intermediate level test 13", LOG_FILE_NAME,
120: "intermediate level test PASSED 13");
121: logNCheckTrue(l, BasicLevel.FATAL,
122: "intermediate level test 14", LOG_FILE_NAME,
123: "intermediate level test PASSED 14");
124: logNCheckTrue(l, debug3, "intermediate level test 15",
125: LOG_FILE_NAME, "intermediate level test PASSED 15");
126: logNCheckTrue(l, debug2, "intermediate level test 16",
127: LOG_FILE_NAME, "intermediate level test PASSED 16");
128: logNCheckFail(l, debug1, "intermediate level test 17",
129: LOG_FILE_NAME, "intermediate level test FAIL 17");
130: logNCheckFail(l, BasicLevel.DEBUG,
131: "intermediate level test 18", LOG_FILE_NAME,
132: "intermediate level test FAIL 18");
133: }
134:
135: public void logNCheckTrue(Logger l, int level, String errormessage,
136: String filename, String required) {
137: l.log(level, required);
138: String[] found = getLastLines(filename, 1);
139: assertNotNull("TestHelper error", found);
140: assertNotNull("TestHelper error", found[0]);
141: assertTrue(errormessage + ": Wrong size f:" + found[0].length()
142: + " /r:" + required.length(),
143: found[0].length() >= required.length());
144: String f = found[0].substring(found[0].length()
145: - required.length(), found[0].length());
146: assertEquals(errormessage, required, f);
147: }
148:
149: public void logNCheckFail(Logger l, int level, String errormessage,
150: String filename, String required) {
151: l.log(level, required);
152: String[] found = getLastLines(filename, 1);
153: if (found != null && found.length > 1
154: && found[0].length() >= required.length()
155: && found[0].endsWith(required)) {
156: fail(errormessage);
157: }
158: }
159:
160: public void logNCheckTrue(Logger l, Level level,
161: String errormessage, String filename, String required) {
162: l.log(level, required);
163: String[] found = getLastLines(filename, 1);
164: assertNotNull("TestHelper error", found);
165: assertNotNull("TestHelper error", found[0]);
166: assertTrue(errormessage + ": Wrong size f:" + found[0].length()
167: + " /r:" + required.length(),
168: found[0].length() >= required.length());
169: String f = found[0].substring(found[0].length()
170: - required.length(), found[0].length());
171: assertEquals(errormessage, required, f);
172: }
173:
174: public void logNCheckFail(Logger l, Level level,
175: String errormessage, String filename, String required) {
176: l.log(level, required);
177: String[] found = getLastLines(filename, 1);
178: if (found != null && found.length > 1
179: && found[0].length() >= required.length()
180: && found[0].endsWith(required)) {
181: fail(errormessage);
182: }
183: }
184: }
|