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.TopicalLogger;
008: import org.objectweb.util.monolog.api.MonologFactory;
009:
010: import java.util.Properties;
011:
012: /**
013: * This class test the configurability of the monolog wrappers via the
014: * Configurable interface.
015: *
016: * @author Sebastien Chassande-Barrioz
017: */
018: public class TestConfigurability extends TestHelper {
019:
020: /**
021: * For running the TestConfigurability suite standalone.
022: * arguments:
023: * <logger factory class name> <mode> [<config file name> [ true | false ]]
024: */
025: public static void main(String args[]) {
026: if (args.length < 2) {
027: System.out.println("Syntax error !");
028: System.out
029: .println("java TestConfigurability <logger factory class name> <mode> "
030: + "[<config file name> [ true | false ]]");
031: System.exit(1);
032: }
033: int size = args.length;
034: String[] params = new String[size];
035: String[] methods = new String[size];
036: params[0] = args[1];
037: methods[0] = "setConfigMode";
038: if (args.length > 2) {
039: params[1] = args[2];
040: methods[1] = "setConfigFile";
041: }
042: if (args.length > 3) {
043: params[2] = args[3];
044: methods[2] = "setUseClassPath";
045: }
046: params[params.length - 1] = "";
047: methods[methods.length - 1] = "init";
048: TestHelper.run(TestConfigurability.class, methods, params,
049: args[0]);
050: }
051:
052: /**
053: * Return the test suite associated to this class.
054: */
055: public static TestSuite getTestSuite(String lfcn, String mode,
056: String configFile, String useCP) {
057: int size = 1 + (mode != null ? 1 : 0)
058: + (configFile != null ? 1 : 0)
059: + (useCP != null ? 1 : 0);
060:
061: String[] params = new String[size];
062: String[] methods = new String[size];
063: params[0] = mode;
064: methods[0] = "setConfigMode";
065: if (configFile != null) {
066: params[1] = configFile;
067: methods[1] = "setConfigFile";
068: }
069: if (useCP != null) {
070: params[2] = useCP;
071: methods[2] = "setUseClassPath";
072: }
073: params[params.length - 1] = "";
074: methods[methods.length - 1] = "init";
075:
076: return TestHelper.getTestSuite(TestLogger.class, methods,
077: params, lfcn);
078: }
079:
080: String mode = null;
081: String configFileName = null;
082: String useClassPath = null;
083:
084: public void setConfigMode(String configMode) {
085: mode = configMode;
086: }
087:
088: public void setConfigFile(String configFile) {
089: configFileName = configFile;
090: }
091:
092: public void setUseClassPath(String useclasspath) {
093: useClassPath = useclasspath;
094: }
095:
096: /**
097: * Call the configure method on the wrapper
098: */
099: public void init(String unused) {
100:
101: Properties prop = null;
102:
103: if (mode != null && !mode.equalsIgnoreCase("null")) {
104: prop = new Properties();
105: prop.put(MonologFactory.LOG_CONFIGURATION_TYPE, mode);
106: if (configFileName != null) {
107: prop.put(MonologFactory.LOG_CONFIGURATION_FILE,
108: configFileName);
109: }
110:
111: if (useClassPath != null) {
112: prop
113: .put(
114: MonologFactory.LOG_CONFIGURATION_FILE_USE_CLASSPATH,
115: useClassPath);
116: }
117: debug("Test the configurability with in "
118: + mode
119: + " mode"
120: + (useClassPath != null ? ", use of the classpath: "
121: + useClassPath
122: : ""));
123: }
124: try {
125: ((MonologFactory) lf).configure(prop);
126: } catch (Exception e) {
127: fail("Impossible to configure in " + mode + " mode: "
128: + e.getMessage());
129: }
130: }
131:
132: public void testSimple() {
133: quietRootLogger();
134: TopicalLogger l = (TopicalLogger) lf
135: .getLogger("test.configurability.simple");
136: Handler hc = hf.createHandler("myhandler_configurability",
137: "file");
138: hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, "test.log");
139: hc.setAttribute(Handler.PATTERN_ATTRIBUTE, "%m%n");
140: hc.setAttribute("activation", lf);
141: try {
142: l.addHandler(hc);
143: } catch (Exception e) {
144: fail(e.getMessage());
145: }
146: l.setIntLevel(BasicLevel.DEBUG);
147: String str = "configurability mode " + mode + " "
148: + useClassPath;
149: l.log(BasicLevel.DEBUG, str);
150: String[] found = getLastLines("test.log", 1);
151: assertNotNull("TestHelper error", found);
152: assertNotNull("TestHelper error", found[0]);
153: assertTrue("no log in collocated Handler", found[0]
154: .endsWith(str));
155: }
156: }
|