01: package org.objectweb.celtix.common.logging;
02:
03: import java.util.logging.Handler;
04: import java.util.logging.Level;
05: import java.util.logging.LogRecord;
06: import java.util.logging.Logger;
07:
08: import junit.framework.TestCase;
09:
10: import org.easymock.IArgumentMatcher;
11: import org.easymock.classextension.EasyMock;
12:
13: import org.objectweb.celtix.common.i18n.BundleUtils;
14:
15: public class LogUtilsTest extends TestCase {
16: private static final Logger LOG = LogUtils
17: .getL7dLogger(LogUtilsTest.class);
18:
19: public void testGetL7dLog() throws Exception {
20: assertNotNull("expected non-null logger", LOG);
21: assertEquals("unexpected resource bundle name", BundleUtils
22: .getBundleName(LogUtilsTest.class), LOG
23: .getResourceBundleName());
24: }
25:
26: public void testHandleL7dMessage() throws Exception {
27: Handler handler = EasyMock.createNiceMock(Handler.class);
28: LOG.addHandler(handler);
29: // handler called *before* localization of message
30: LogRecord record = new LogRecord(Level.WARNING, "FOOBAR_MSG");
31: EasyMock.reportMatcher(new LogRecordMatcher(record));
32: handler.publish(record);
33: EasyMock.replay(handler);
34: LOG.log(Level.WARNING, "FOOBAR_MSG");
35: EasyMock.verify(handler);
36: }
37:
38: public void testLogParamSubstitutionWithThrowable()
39: throws Exception {
40: Handler handler = EasyMock.createNiceMock(Handler.class);
41: LOG.addHandler(handler);
42: // handler called *after* localization of message
43: Exception ex = new Exception();
44: LogRecord record = new LogRecord(Level.SEVERE,
45: "subbed in 1 only");
46: record.setThrown(ex);
47: EasyMock.reportMatcher(new LogRecordMatcher(record));
48: handler.publish(record);
49: EasyMock.replay(handler);
50: LogUtils.log(LOG, Level.SEVERE, "SUB1_MSG", ex, 1);
51: EasyMock.verify(handler);
52: }
53:
54: public void testLogParamsSubstitutionWithThrowable()
55: throws Exception {
56: Handler handler = EasyMock.createNiceMock(Handler.class);
57: LOG.addHandler(handler);
58: // handler called *after* localization of message
59: Exception ex = new Exception();
60: LogRecord record = new LogRecord(Level.SEVERE,
61: "subbed in 4 & 3");
62: record.setThrown(ex);
63: EasyMock.reportMatcher(new LogRecordMatcher(record));
64: handler.publish(record);
65: EasyMock.replay(handler);
66: LogUtils.log(LOG, Level.SEVERE, "SUB2_MSG", ex, new Object[] {
67: 3, 4 });
68: EasyMock.verify(handler);
69: }
70:
71: private static final class LogRecordMatcher implements
72: IArgumentMatcher {
73: private final LogRecord record;
74:
75: private LogRecordMatcher(LogRecord r) {
76: this .record = r;
77: }
78:
79: public boolean matches(Object obj) {
80: if (obj instanceof LogRecord) {
81: LogRecord other = (LogRecord) obj;
82: return record.getMessage().equals(other.getMessage())
83: && record.getLevel().equals(other.getLevel())
84: && record.getThrown() == other.getThrown();
85: }
86: return false;
87: }
88:
89: public void appendTo(StringBuffer buffer) {
90: buffer.append("log records did not match");
91: }
92: }
93: }
|