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