/**
* 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.TopicalLogger;
import org.objectweb.util.monolog.api.MonologFactory;
import java.util.Properties;
/**
* This class test the configurability of the monolog wrappers via the
* Configurable interface.
*
* @author Sebastien Chassande-Barrioz
*/
public class TestConfigurability extends TestHelper {
/**
* For running the TestConfigurability suite standalone.
* arguments:
* <logger factory class name> <mode> [<config file name> [ true | false ]]
*/
public static void main(String args[]) {
if (args.length < 2) {
System.out.println("Syntax error !");
System.out.println(
"java TestConfigurability <logger factory class name> <mode> "
+ "[<config file name> [ true | false ]]");
System.exit(1);
}
int size = args.length;
String[] params = new String[size];
String[] methods = new String[size];
params[0] = args[1];
methods[0] = "setConfigMode";
if (args.length > 2) {
params[1] = args[2];
methods[1] = "setConfigFile";
}
if (args.length > 3) {
params[2] = args[3];
methods[2] = "setUseClassPath";
}
params[params.length - 1] = "";
methods[methods.length - 1] = "init";
TestHelper.run(TestConfigurability.class, methods, params, args[0]);
}
/**
* Return the test suite associated to this class.
*/
public static TestSuite getTestSuite(String lfcn, String mode,
String configFile, String useCP) {
int size = 1 + (mode != null ? 1 : 0) + (configFile != null ? 1 : 0)
+ (useCP != null ? 1 : 0);
String[] params = new String[size];
String[] methods = new String[size];
params[0] = mode;
methods[0] = "setConfigMode";
if (configFile != null) {
params[1] = configFile;
methods[1] = "setConfigFile";
}
if (useCP != null) {
params[2] = useCP;
methods[2] = "setUseClassPath";
}
params[params.length - 1] = "";
methods[methods.length - 1] = "init";
return TestHelper.getTestSuite(TestLogger.class, methods, params, lfcn);
}
String mode = null;
String configFileName = null;
String useClassPath = null;
public void setConfigMode(String configMode) {
mode = configMode;
}
public void setConfigFile(String configFile) {
configFileName = configFile;
}
public void setUseClassPath(String useclasspath) {
useClassPath = useclasspath;
}
/**
* Call the configure method on the wrapper
*/
public void init(String unused) {
Properties prop = null;
if (mode != null && !mode.equalsIgnoreCase("null")) {
prop = new Properties();
prop.put(MonologFactory.LOG_CONFIGURATION_TYPE, mode);
if (configFileName != null) {
prop.put(MonologFactory.LOG_CONFIGURATION_FILE, configFileName);
}
if (useClassPath != null) {
prop.put(MonologFactory.LOG_CONFIGURATION_FILE_USE_CLASSPATH,
useClassPath);
}
debug("Test the configurability with in " + mode + " mode"
+ (useClassPath != null ?
", use of the classpath: " + useClassPath
: ""));
}
try {
((MonologFactory) lf).configure(prop);
}
catch (Exception e) {
fail("Impossible to configure in " + mode + " mode: "
+ e.getMessage());
}
}
public void testSimple() {
quietRootLogger();
TopicalLogger l = (TopicalLogger)
lf.getLogger("test.configurability.simple");
Handler hc =
hf.createHandler("myhandler_configurability", "file");
hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, "test.log");
hc.setAttribute(Handler.PATTERN_ATTRIBUTE, "%m%n");
hc.setAttribute("activation", lf);
try {
l.addHandler(hc);
}
catch (Exception e) {
fail(e.getMessage());
}
l.setIntLevel(BasicLevel.DEBUG);
String str = "configurability mode " + mode + " " + useClassPath;
l.log(BasicLevel.DEBUG, str);
String[] found = getLastLines("test.log", 1);
assertNotNull("TestHelper error", found);
assertNotNull("TestHelper error", found[0]);
assertTrue("no log in collocated Handler", found[0].endsWith(str));
}
}