001: /*
002: * LoggerTest.java
003: * JUnit based test
004: *
005: * Created on 19. Januar 2003, 18:09
006: */
007:
008: package org.jzonic.jlo;
009:
010: import junit.framework.Test;
011: import junit.framework.TestCase;
012: import junit.framework.TestSuite;
013: import org.jzonic.jlo.handler.MockHandler;
014:
015: import java.util.Vector;
016:
017: /**
018: *
019: * @author Administrator
020: */
021: public class CompleteLoggerTest extends TestCase {
022:
023: private static final LogManager manager = LogManager.getInstance();
024:
025: public CompleteLoggerTest(java.lang.String testName) {
026: super (testName);
027: }
028:
029: public void setUp() throws Exception {
030: }
031:
032: public static void main(java.lang.String[] args) {
033: junit.textui.TestRunner.run(suite());
034: }
035:
036: public static Test suite() {
037: TestSuite suite = new TestSuite(CompleteLoggerTest.class);
038: return suite;
039: }
040:
041: public void testSimpleLogging() {
042: MockHandler mockHandler = getFreshHandler("TestGenerator");
043: Logger logger = LogManager.getLogger("org.jzonic.jlo");
044: logger.debug("debug message");
045: Vector entries = mockHandler.getMessages();
046: assertEquals(2, entries.size());
047: }
048:
049: public void testSimpleLoggingTargetOff() {
050: MockHandler mockHandler = getFreshHandler("TestGenerator");
051: Logger logger = LogManager.getLogger("org.jzonic.jlo");
052: int targets = logger.getTargets();
053: logger.setTargets(0);
054: logger.debug("debug message");
055: Vector entries = mockHandler.getMessages();
056: assertEquals(0, entries.size());
057: logger.setTargets(targets);
058: }
059:
060: public void testChannel() {
061: MockHandler mockHandler = getFreshHandler("TestGenerator");
062: Channel channel = LogManager.getChannel("debug");
063: channel.log("debug message");
064: Vector entries = mockHandler.getMessages();
065: assertEquals(1, entries.size());
066: }
067:
068: public void testLogger2() {
069: MockHandler mockHandler = getFreshHandler("TestGenerator");
070: Logger testLogger = LogManager.getLogger("org.jzonic.jlo");
071: testLogger.trace("Hello world");
072: assertEquals(1, mockHandler.getMessages().size());
073: testLogger.info("Hello world");
074: assertEquals(2, mockHandler.getMessages().size());
075: testLogger.debug("Hello world");
076: assertEquals(4, mockHandler.getMessages().size());
077: testLogger.warn("Hello world");
078: assertEquals(5, mockHandler.getMessages().size());
079: testLogger.error("Hello world");
080: assertEquals(6, mockHandler.getMessages().size());
081: testLogger.fatal("Hello world");
082: assertEquals(7, mockHandler.getMessages().size());
083: }
084:
085: public void testLogger2Loggable() {
086: Logger testLogger = LogManager.getLogger("org.jzonic.jlo");
087: boolean test = testLogger.isTraceEnabled();
088: assertEquals(true, test);
089: test = testLogger.isInfoEnabled();
090: assertEquals(true, test);
091: test = testLogger.isDebugEnabled();
092: assertEquals(true, test);
093: test = testLogger.isWarnEnabled();
094: assertEquals(true, test);
095: test = testLogger.isErrorEnabled();
096: assertEquals(true, test);
097: test = testLogger.isFatalEnabled();
098: assertEquals(true, test);
099: }
100:
101: public void testLoggerWithFilter() {
102: MockHandler mockHandler = getFreshHandler("TestGenerator2");
103: Logger logger = LogManager.getLogger("org.jzonic");
104: logger.debug("hello world");
105: assertEquals(0, mockHandler.getMessages().size());
106: logger.debug("Exception");
107: assertEquals(1, mockHandler.getMessages().size());
108: }
109:
110: public void testLoggerWithRegExFilter() {
111: MockHandler mockHandler = getFreshHandler("TestGenerator");
112: Logger logger = LogManager.getLogger("org.jzonic.filter");
113: logger.debug("hello world");
114: assertEquals(0, mockHandler.getMessages().size());
115: logger.debug("This is 1.2 text");
116: assertEquals(2, mockHandler.getMessages().size());
117: }
118:
119: private MockHandler getFreshHandler(String handlerName) {
120: LogConfiguration config = manager.getLogConfiguration();
121: assertNotNull(config);
122: LogGenerator lg = config.getLogGenerator(handlerName);
123: assertNotNull(lg);
124: MockHandler mockHandler = (MockHandler) lg.getHandler();
125: mockHandler.clear();
126: return mockHandler;
127: }
128:
129: }
|