/**
* Copyright (C) 2002
*/
package org.objectweb.util.monolog;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.TopicalLogger;
import org.objectweb.util.monolog.api.Handler;
import org.objectweb.util.monolog.wrapper.printwriter.PrintWriterImpl;
import java.io.PrintWriter;
import java.io.File;
/**
*
* @author Sebastien Chassande-Barrioz
*/
public class TestPw2Logger 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 TestPw2Logger <logger factory class name>");
System.exit(1);
}
TestHelper.run(TestPw2Logger.class, new String[0],
new String[0], args[0]);
}
public static TestSuite getTestSuite(String lfcn) {
return TestHelper.getTestSuite(TestPw2Logger.class, new String[0],
new String[0], lfcn);
}
public void testSimple() {
quietRootLogger();
new File(LOG_FILE_NAME).delete();
l = (TopicalLogger) lf.getLogger("test.pw2logger.foo");
l.setIntLevel(BasicLevel.DEBUG);
Handler h = hf.createHandler("pw2logger", "file");
h.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
h.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
h.setAttribute("activation", hf);
try {
l.addHandler(h);
} catch (Exception e) {
fail(e.getMessage());
}
PrintWriter pw = new PrintWriterImpl(l);
pw.print(true);
pw.print('a');
pw.print(123);
pw.print(1.3);
pw.println();
pw.println("toto");
String[] found = getFirstLines(LOG_FILE_NAME, 2);
assertNotNull("TestHelper error: return null", found);
assertEquals("TestHelper error: wrong size", 2, found.length);
assertNotNull("TestHelper error: element 0 is null", found[0]);
assertNotNull("TestHelper error: element 1 is null", found[1]);
assertTrue("end with 'truea1231.3' expected, but found " + found[0],
found[0].endsWith("truea1231.3"));
assertTrue("", found[1].endsWith("toto"));
new File(LOG_FILE_NAME).delete();
}
}