001: /**
002: * Copyright (C) 2002
003: */package org.objectweb.util.monolog;
004:
005: import org.objectweb.util.monolog.api.TopicalLogger;
006: import org.objectweb.util.monolog.api.Handler;
007: import org.objectweb.util.monolog.api.BasicLevel;
008:
009: /**
010: * This test check the additivity flag of the logger.
011: *
012: * @author Sebastien Chassande-Barrioz
013: */
014: public class TestAdditivity extends TestHelper {
015:
016: public static final String LOG_FILE_NAME = "test.log";
017: public static final String LOG_PATTERN = "%m%n";
018:
019: TopicalLogger l = null;
020:
021: /**
022: * For running the TestLogger suite standalone.
023: */
024: public static void main(String args[]) {
025: if (args.length < 1) {
026: System.out.println("Syntax error !");
027: System.out
028: .println("java TestAdditivity <logger factory class name>");
029: System.exit(1);
030: }
031: TestHelper.run(TestAdditivity.class, new String[0],
032: new String[0], args[0]);
033: }
034:
035: public static TestSuite getTestSuite(String lfcn) {
036: return TestHelper.getTestSuite(TestAdditivity.class,
037: new String[0], new String[0], lfcn);
038: }
039:
040: // ------ TEST METHODS ------ //
041: //----------------------------//
042:
043: public void testA() {
044: quietRootLogger();
045: TopicalLogger l1 = (TopicalLogger) lf
046: .getLogger("test.additivity");
047: TopicalLogger l2 = (TopicalLogger) lf
048: .getLogger("test.additivity.foo");
049: Handler hc1 = hf.createHandler("myhandler", "file");
050: hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '1');
051: hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
052: hc1.setAttribute("activation", lf);
053: Handler hc2 = hf.createHandler("myhandler2", "file");
054: hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '2');
055: hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
056: hc2.setAttribute("activation", lf);
057: try {
058: l1.addHandler(hc1);
059: l2.addHandler(hc2);
060: } catch (Exception e) {
061: fail(e.getMessage());
062: }
063: l2.setAdditivity(false);
064: l2.setIntLevel(BasicLevel.DEBUG);
065: l2.log(BasicLevel.DEBUG, "simple additivity");
066: // The log message must be found in the file 2
067: String[] found2 = getLastLines(LOG_FILE_NAME + '2', 1);
068: assertNotNull("TestHelper error", found2);
069: assertNotNull("TestHelper error", found2[0]);
070: assertTrue("A.1", found2[0].endsWith("simple additivity"));
071:
072: // The log message must NOT be found in the file 1
073: String[] found1 = getLastLines(LOG_FILE_NAME + '1', 1);
074: if (found1 != null && found1.length > 0 && found1[0] != null
075: && found1[0].endsWith("simple additivity")) {
076: fail("A.2");
077: }
078:
079: l2 = (TopicalLogger) lf.getLogger("test.additivity.foo.bar");
080: l2.log(BasicLevel.DEBUG, "simple additivity2");
081: // The log message must be found in the file 2
082: found2 = getLastLines(LOG_FILE_NAME + '2', 1);
083: assertNotNull("TestHelper error", found2);
084: assertNotNull("TestHelper error", found2[0]);
085: assertTrue("A.3", found2[0].endsWith("simple additivity2"));
086:
087: // The log message must NOT be found in the file 1
088: found1 = getLastLines(LOG_FILE_NAME + '1', 1);
089: if (found1 != null && found1.length > 0 && found1[0] != null
090: && found1[0].endsWith("simple additivity2")) {
091: fail("A.4");
092: }
093: }
094:
095: public void testB() {
096: quietRootLogger();
097: TopicalLogger l1 = (TopicalLogger) lf
098: .getLogger("test.additivity");
099: TopicalLogger l2 = (TopicalLogger) lf
100: .getLogger("test.additivity.foo");
101: Handler hc1 = hf.createHandler("myhandler", "file");
102: hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '1');
103: hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
104: hc1.setAttribute("activation", lf);
105: Handler hc2 = hf.createHandler("myhandler2", "file");
106: hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '2');
107: hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
108: hc2.setAttribute("activation", lf);
109: try {
110: l1.addHandler(hc1);
111: l2.addHandler(hc2);
112: } catch (Exception e) {
113: fail(e.getMessage());
114: }
115: l2.setAdditivity(false);
116: l2.log(BasicLevel.DEBUG, "simple additivity B");
117: // The log message must be found in the file 2
118: String[] found2 = getLastLines(LOG_FILE_NAME + '2', 1);
119: assertNotNull("TestHelper error", found2);
120: assertNotNull("TestHelper error", found2[0]);
121: assertTrue("B.1", found2[0].endsWith("simple additivity B"));
122:
123: // The log message must NOT be found in the file 1
124: String[] found1 = getLastLines(LOG_FILE_NAME + '1', 1);
125: if (found1 != null && found1.length > 0 && found1[0] != null
126: && found1[0].endsWith("simple additivity B")) {
127: fail("B.2");
128: }
129:
130: l2 = (TopicalLogger) lf.getLogger("test.additivity.foo.bar");
131: l2.setIntLevel(BasicLevel.DEBUG);
132: l2.log(BasicLevel.DEBUG, "simple additivity B 2");
133: // The log message must be found in the file 2
134: found2 = getLastLines(LOG_FILE_NAME + '2', 1);
135: assertNotNull("TestHelper error", found2);
136: assertNotNull("TestHelper error", found2[0]);
137: assertTrue("B.3", found2[0].endsWith("simple additivity B 2"));
138:
139: // The log message must NOT be found in the file 1
140: found1 = getFirstLines(LOG_FILE_NAME + '1', 1);
141: if (found1 != null && found1.length > 0 && found1[0] != null
142: && found1[0].endsWith("simple additivity B 2")) {
143: fail("B.4");
144: }
145: }
146: }
|