001: /**
002: * Copyright (C) 2002
003: */package org.objectweb.util.monolog;
004:
005: import junit.framework.Assert;
006: import org.objectweb.util.monolog.api.BasicLevel;
007: import org.objectweb.util.monolog.api.Handler;
008: import org.objectweb.util.monolog.api.TopicalLogger;
009:
010: /**
011: * It verifies a TopicalLogger implementation and a TopicalFactory
012: * implementation.
013: *
014: * @author Sebastien Chassande-Barrioz
015: */
016: public class TestLogger extends TestHelper {
017:
018: public static final String LOG_FILE_NAME = "test.log";
019: public static final String LOG_PATTERN = "%m%n";
020:
021: TopicalLogger l = null;
022:
023: public TestLogger() {
024: }
025:
026: public TestLogger(String s) {
027: super (s);
028: }
029:
030: /**
031: * For running the TestLogger suite standalone.
032: */
033: public static void main(String args[]) {
034: if (args.length < 1) {
035: System.out.println("Syntax error !");
036: System.out
037: .println("java TestLogger <logger factory class name>");
038: System.exit(1);
039: }
040: TestHelper.run(TestLogger.class, new String[0], new String[0],
041: args[0]);
042: }
043:
044: public static TestSuite _getTestSuite(String lfcn) {
045: return TestHelper.getTestSuite(TestLogger.class, new String[0],
046: new String[0], lfcn);
047: }
048:
049: // ------ TEST METHODS ------ //
050: //----------------------------//
051: public void testNbLogger() {
052: assertEquals("nb initial loggers", 1, lf.getLoggers().length);
053: }
054:
055: public void testRootLoggerConf() {
056: l = (TopicalLogger) lf.getLogger("");
057: assertNotNull("Root logger undefined", l);
058: String[] ts = l.getTopic();
059: assertNotNull("Topic list is null", ts);
060: assertEquals("several topic", 1, ts.length);
061: assertNotNull("Topic list is null", ts[0]);
062: assertEquals("name 'root' not equals to ''", l, lf
063: .getLogger("root"));
064: assertTrue("several topic", ts[0].length() == 0
065: || "root".equals(ts[0]));
066:
067: }
068:
069: public void testGetOneLoggerByName() {
070: l = (TopicalLogger) lf.getLogger("foo");
071:
072: assertEquals("same name but 2 instances", l, lf
073: .getLogger("foo"));
074: String[] topics = ((TopicalLogger) lf.getLogger("foo"))
075: .getTopic();
076: assertNotNull("Topic list is null", topics);
077: assertEquals("several topic", 1, topics.length);
078: assertNotNull("Topic elem is null", topics[0]);
079: assertTrue("bad name", topics[0].equals("foo"));
080: }
081:
082: public void testGetAllLoggerConf() {
083: int iternumber = 20;
084: for (int i = 0; i < iternumber; i++) {
085: l = (TopicalLogger) lf.getLogger("foo" + i);
086: }
087: TopicalLogger[] locs = (TopicalLogger[]) lf.getLoggers();
088: assertNotNull("LoggerConf list is null", locs);
089: TopicalLogger[] locs2 = new TopicalLogger[iternumber];
090: for (int i = 0; i < locs.length; i++) {
091: assertNotNull("LoggerConf list element is null", locs[i]);
092: String[] ts = locs[i].getTopic();
093: assertNotNull("topic list is null", ts);
094: assertEquals("bad Topic list size", 1, ts.length);
095: assertNotNull("Null topic", ts[0]);
096: int j = 0;
097: if ((ts[0].length() > 3) && ts[0].startsWith("foo", 0)) {
098: try {
099: j = Integer.parseInt(ts[0].substring(3, ts[0]
100: .length()));
101: } catch (NumberFormatException e) {
102: fail("Bad topic name: " + ts[0]);
103: }
104: assertNull("duplicate LoggerConf", locs2[j]);
105: locs2[j] = locs[i];
106: assertEquals("bad topic", "foo" + j, ts[0]);
107: }
108: }
109: boolean allset = true;
110:
111: for (int i = 0; i < iternumber && allset; i++) {
112: allset = locs2[i] != null;
113: }
114: if (!allset) {
115: for (int i = 0; i < iternumber; i++) {
116: if (locs2[i] == null)
117: System.out.println("losc2[" + i + "]=null value");
118: else
119: System.out.println("losc2[" + i + "]="
120: + locs2[i].getTopic()[0]);
121: }
122: assertTrue("Some logger has not been found", allset);
123: }
124: }
125:
126: public void testMultipleTopic() {
127: l = (TopicalLogger) lf.getLogger("foo");
128:
129: assertEquals("same name but 2 instances", l, lf
130: .getLogger("foo"));
131: try {
132: l.addTopic("bar");
133: l.addTopic("azerty");
134: l.addTopic("querty");
135: } catch (Exception e) {
136: fail("does not support multiple topic");
137: }
138: /*assertEquals("additionnal (1) name but 2 instances",
139: l, lf.getLogger("bar"));
140: assertEquals("additionnal (2) name but 2 instances",
141: l, lf.getLogger("azerty"));
142: assertEquals("additionnal (3) name but 2 instances",
143: l, lf.getLogger("querty")); */
144:
145: String[] tcs = l.getTopic();
146: assertNotNull("Topic list is null", tcs);
147: assertEquals("Wrong topic number", 4, tcs.length);
148: String[] tcs2 = new String[4];
149: for (int i = 0; i < tcs.length; i++) {
150: assertNotNull("Null topic", tcs[i]);
151: if (tcs[i].equals("foo")) {
152: assertNull("duplicate first name", tcs2[0]);
153: tcs2[0] = tcs[i];
154: } else if (tcs[i].equals("bar")) {
155: assertNull("duplicate name", tcs2[1]);
156: tcs2[1] = tcs[i];
157: } else if (tcs[i].equals("azerty")) {
158: assertNull("duplicate name", tcs2[2]);
159: tcs2[2] = tcs[i];
160: } else if (tcs[i].equals("querty")) {
161: assertNull("duplicate name", tcs2[3]);
162: tcs2[3] = tcs[i];
163: }
164: }
165: }
166:
167: public void testSimpleInheritanceLevel() {
168: l = (TopicalLogger) lf
169: .getLogger("test.simple.inheritance.level.toto");
170: l.setIntLevel(BasicLevel.WARN);
171: l = (TopicalLogger) lf
172: .getLogger("test.simple.inheritance.level.toto.titi");
173: assertTrue("wrong isLoggable return 1", l
174: .isLoggable(BasicLevel.WARN));
175: assertTrue("wrong isLoggable return 2", !l
176: .isLoggable(BasicLevel.DEBUG));
177:
178: l.setIntLevel(BasicLevel.DEBUG);
179: assertTrue("wrong isLoggable return 3", l
180: .isLoggable(BasicLevel.WARN));
181: assertTrue("wrong isLoggable return 4", l
182: .isLoggable(BasicLevel.DEBUG));
183: }
184:
185: public void testTopicsInheritanceLevel() {
186: l = (TopicalLogger) lf
187: .getLogger("test.topic.inheritance.level.toto");
188: l.setIntLevel(BasicLevel.WARN);
189: l = (TopicalLogger) lf
190: .getLogger("test.topic.inheritance.level.toto.titi");
191: assertTrue("wrong isLoggable return 1", l
192: .isLoggable(BasicLevel.WARN));
193: assertTrue("wrong isLoggable return 2", !l
194: .isLoggable(BasicLevel.DEBUG));
195:
196: try {
197: l.addTopic("test.topic.inheritance.level.tutu.titi");
198: } catch (Exception e) {
199: Assert.fail("Multiple topic error: " + e.getMessage());
200: }
201: l = (TopicalLogger) lf
202: .getLogger("test.topic.inheritance.level.tutu");
203: l.setIntLevel(BasicLevel.DEBUG);
204: assertTrue("wrong isLoggable return 3", l
205: .isLoggable(BasicLevel.WARN));
206: assertTrue("wrong isLoggable return 4", l
207: .isLoggable(BasicLevel.DEBUG));
208: }
209:
210: public void testLogInCollocatedHandler() {
211: quietRootLogger();
212: l = (TopicalLogger) lf.getLogger("test.simple.log");
213: Handler hc = hf.createHandler("myhandler", "file");
214: hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
215: hc.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
216: hc.setAttribute("activation", lf);
217: try {
218: l.addHandler(hc);
219: } catch (Exception e) {
220: fail(e.getMessage());
221: }
222: l.setIntLevel(BasicLevel.DEBUG);
223: l.log(BasicLevel.DEBUG, "collocated Handler bar");
224: String[] found = getFirstLines(LOG_FILE_NAME, 1);
225: assertNotNull("TestHelper error", found);
226: assertNotNull("TestHelper error", found[0]);
227: assertTrue("no log in collocated Handler", found[0]
228: .endsWith("collocated Handler bar"));
229: }
230:
231: public void testLogInSeveralCollocatedHandler() {
232: quietRootLogger();
233: l = (TopicalLogger) lf.getLogger("test.simple.log");
234: Handler hc1 = hf.createHandler("myhandler", "file");
235: hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + "1");
236: hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
237: hc1.setAttribute("activation", hf);
238:
239: Handler hc2 = hf.createHandler("myhandler2", "file");
240: hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + "2");
241: hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
242: hc2.setAttribute("activation", hf);
243:
244: try {
245: l.addHandler(hc1);
246: l.addHandler(hc2);
247: } catch (Exception e) {
248: fail(e.getMessage());
249: }
250:
251: l.setIntLevel(BasicLevel.DEBUG);
252: l.log(BasicLevel.DEBUG, "several collocated Handler bar");
253:
254: String[] found = getFirstLines(LOG_FILE_NAME + "1", 1);
255: assertNotNull("TestHelper error", found);
256: assertNotNull("TestHelper error", found[0]);
257: assertTrue("no log in collocated Handler", found[0]
258: .endsWith("several collocated Handler bar"));
259:
260: found = getFirstLines(LOG_FILE_NAME + "2", 1);
261: assertNotNull("TestHelper error", found);
262: assertNotNull("TestHelper error", found[0]);
263: assertTrue("no log in collocated Handler", found[0]
264: .endsWith("several collocated Handler bar"));
265: }
266:
267: public void testLogInInheritedHandler() {
268: quietRootLogger();
269: l = (TopicalLogger) lf.getLogger("test.simple.log");
270: Handler hc = hf.createHandler("myhandler", "file");
271: hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
272: hc.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
273: hc.setAttribute("activation", hf);
274: try {
275: l.addHandler(hc);
276: } catch (Exception e) {
277: fail(e.getMessage());
278: }
279: l.setIntLevel(BasicLevel.DEBUG);
280: l = (TopicalLogger) lf.getLogger("test.simple.log.foo");
281: l.log(BasicLevel.DEBUG, "inherited Handler bar");
282: String[] found = getLastLines(LOG_FILE_NAME, 1);
283: assertNotNull("TestHelper error", found);
284: assertNotNull("TestHelper error", found[0]);
285: assertTrue("no log in inherited Handler", found[0]
286: .endsWith("inherited Handler bar"));
287: }
288:
289: public void testLogInSeveralInheritedHandler() {
290: quietRootLogger();
291: l = (TopicalLogger) lf.getLogger("test.simple.log");
292: Handler hc1 = hf.createHandler("myhandler", "file");
293: hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + "1");
294: hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
295: hc1.setAttribute("activation", hf);
296:
297: Handler hc2 = hf.createHandler("myhandler2", "file");
298: hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + "2");
299: hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
300: hc2.setAttribute("activation", hf);
301:
302: try {
303: l.addHandler(hc1);
304: l = (TopicalLogger) lf.getLogger("test.simple.log.foo");
305: l.addHandler(hc2);
306: } catch (Exception e) {
307: fail(e.getMessage());
308: }
309:
310: l = (TopicalLogger) lf.getLogger("test.simple.log.foo.bar");
311: l.setIntLevel(BasicLevel.DEBUG);
312: l.log(BasicLevel.DEBUG, "several collocated Handler bar");
313:
314: String[] found = getFirstLines(LOG_FILE_NAME + "1", 1);
315: assertNotNull("TestHelper error", found);
316: assertNotNull("TestHelper error", found[0]);
317: assertTrue("no log in collocated Handler", found[0]
318: .endsWith("several collocated Handler bar"));
319:
320: found = getFirstLines(LOG_FILE_NAME + "2", 1);
321: assertNotNull("TestHelper error", found);
322: assertNotNull("TestHelper error", found[0]);
323: assertTrue("no log in collocated Handler", found[0]
324: .endsWith("several collocated Handler bar"));
325: }
326: }
|