/**
* Copyright (C) 2002
*/
package org.objectweb.util.monolog;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Handler;
import org.objectweb.util.monolog.api.Level;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.TopicalLogger;
import org.objectweb.util.monolog.wrapper.log4j.LevelImpl;
/**
* It verifies that a monolog wrapper support the intermediate level feature.
*
* @author Sebastien Chassande-Barrioz
*/
public class TestIntermediateLevel extends TestHelper {
public static final String LOG_FILE_NAME = "test.log";
public static final String LOG_PATTERN = "%m%n";
TopicalLogger l = null;
/**
* For running the TestLogger suite standalone.
*/
public static void main(String args[]) {
if (args.length < 1) {
System.out.println("Syntax error !");
System.out.println("java TestIntermediateLevel <logger factory class name>");
System.exit(1);
}
TestHelper.run(TestIntermediateLevel.class, new String[0], new String[0], args[0]);
}
/**
* Return the test suite associated to this class.
*/
public static TestSuite getTestSuite(String lfcn) {
return TestHelper.getTestSuite(TestIntermediateLevel.class, new String[0],
new String[0], lfcn);
}
public void testIntermediateLevel() {
debug("testIntermediateLevel : LOG_FILE_NAME =" + LOG_FILE_NAME);
debug("BasicLevel.DEBUG=" + BasicLevel.DEBUG);
int debug1 = BasicLevel.DEBUG + 1;
debug("debug1=" + debug1);
int debug2 = BasicLevel.DEBUG + 2;
debug("debug2=" + debug2);
Level debug3 = lef.defineLevel("DEBUG3", BasicLevel.DEBUG + 3);
debug("debug3=" +
(debug3 != null ? "Level: " + debug3.getIntValue() : "null"));
debug("BasicLevel.INFO=" + BasicLevel.INFO);
debug("BasicLevel.WARN=" + BasicLevel.WARN);
debug("BasicLevel.ERROR=" + BasicLevel.ERROR);
debug("BasicLevel.FATAL=" + BasicLevel.FATAL);
quietRootLogger();
l = (TopicalLogger)
lf.getLogger("test.intermediate_level.foo");
Handler hc = hf.createHandler(
"intermediate_level_handler", "file");
hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
hc.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
hc.setAttribute("activation", hf);
try {
l.addHandler(hc);
}
catch (Exception e) {
fail(e.getMessage());
}
debug("Current Level: " + l.getCurrentIntLevel());
l.setIntLevel(debug2);
debug("Change the level to debug2: " + l.getCurrentIntLevel());
logNCheckFail(l, BasicLevel.DEBUG, "intermediate level test 1", LOG_FILE_NAME, "intermediate level test FAIL 1");
logNCheckFail(l, debug1, "intermediate level test 2", LOG_FILE_NAME, "intermediate level test FAIL 2");
logNCheckFail(l, new LevelImpl("DEBUG1", debug1), "intermediate level test 3", LOG_FILE_NAME, "intermediate level test FAIL 3");
logNCheckTrue(l, debug2, "intermediate level test 4", LOG_FILE_NAME, "intermediate level test PASSED 4");
logNCheckTrue(l, new LevelImpl("DEBUG2", debug2), "intermediate level test 5", LOG_FILE_NAME, "intermediate level test PASSED 5");
logNCheckTrue(l, debug3, "intermediate level test 6", LOG_FILE_NAME, "intermediate level test PASSED 6");
logNCheckTrue(l, BasicLevel.INFO, "intermediate level test 7", LOG_FILE_NAME, "intermediate level test PASSED 7");
logNCheckTrue(l, BasicLevel.WARN, "intermediate level test 8", LOG_FILE_NAME, "intermediate level test PASSED 8");
logNCheckTrue(l, BasicLevel.ERROR, "intermediate level test 9", LOG_FILE_NAME, "intermediate level test PASSED 9");
logNCheckTrue(l, BasicLevel.FATAL, "intermediate level test 10", LOG_FILE_NAME, "intermediate level test PASSED 10");
l = (TopicalLogger) lf.getLogger("test.intermediate_level.bar");
try {
l.addHandler(hc);
}
catch (Exception e) {
fail(e.getMessage());
}
debug("Current Level: " + l.getCurrentIntLevel());
l.setIntLevel(debug2);
debug("Change the level to debug2: " + l.getCurrentIntLevel());
logNCheckTrue(l, BasicLevel.INFO, "intermediate level test 11", LOG_FILE_NAME, "intermediate level test PASSED 11");
logNCheckTrue(l, BasicLevel.WARN, "intermediate level test 12", LOG_FILE_NAME, "intermediate level test PASSED 12");
logNCheckTrue(l, BasicLevel.ERROR, "intermediate level test 13", LOG_FILE_NAME, "intermediate level test PASSED 13");
logNCheckTrue(l, BasicLevel.FATAL, "intermediate level test 14", LOG_FILE_NAME, "intermediate level test PASSED 14");
logNCheckTrue(l, debug3, "intermediate level test 15", LOG_FILE_NAME, "intermediate level test PASSED 15");
logNCheckTrue(l, debug2, "intermediate level test 16", LOG_FILE_NAME, "intermediate level test PASSED 16");
logNCheckFail(l, debug1, "intermediate level test 17", LOG_FILE_NAME, "intermediate level test FAIL 17");
logNCheckFail(l, BasicLevel.DEBUG, "intermediate level test 18", LOG_FILE_NAME, "intermediate level test FAIL 18");
}
public void logNCheckTrue(Logger l, int level, String errormessage,
String filename, String required) {
l.log(level, required);
String[] found = getLastLines(filename, 1);
assertNotNull("TestHelper error", found);
assertNotNull("TestHelper error", found[0]);
assertTrue(errormessage + ": Wrong size f:"
+ found[0].length() + " /r:" + required.length(),
found[0].length() >= required.length());
String f = found[0]
.substring(found[0].length() - required.length(), found[0].length());
assertEquals(errormessage, required, f);
}
public void logNCheckFail(Logger l, int level, String errormessage,
String filename, String required) {
l.log(level, required);
String[] found = getLastLines(filename, 1);
if ( found!=null
&& found.length > 1
&& found[0].length() >= required.length()
&& found[0].endsWith(required)) {
fail(errormessage);
}
}
public void logNCheckTrue(Logger l, Level level, String errormessage,
String filename, String required) {
l.log(level, required);
String[] found = getLastLines(filename, 1);
assertNotNull("TestHelper error", found);
assertNotNull("TestHelper error", found[0]);
assertTrue(errormessage + ": Wrong size f:"
+ found[0].length() + " /r:" + required.length(),
found[0].length() >= required.length());
String f = found[0]
.substring(found[0].length() - required.length(), found[0].length());
assertEquals(errormessage, required, f);
}
public void logNCheckFail(Logger l, Level level, String errormessage,
String filename, String required) {
l.log(level, required);
String[] found = getLastLines(filename, 1);
if ( found!=null
&& found.length > 1
&& found[0].length() >= required.length()
&& found[0].endsWith(required)) {
fail(errormessage);
}
}
}