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 TestConfigLoggerTest extends TestCase {
022:
023: private static final LogManager manager = LogManager.getInstance();
024:
025: public TestConfigLoggerTest(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(TestConfigLoggerTest.class);
038: return suite;
039: }
040:
041: public void testSimpleLogging() {
042: MockHandler mockHandler = getFreshHandler("TestGenerator");
043: Logger logger = LogManager.getLogger("de.xcom", "test");
044: logger.debug("debug message");
045: Vector entries = mockHandler.getMessages();
046: assertEquals(1, entries.size());
047: }
048:
049: public void testSimpleLoggingTargetOff() {
050: MockHandler mockHandler = getFreshHandler("TestGenerator");
051: Logger logger = LogManager.getLogger("de.xcom", "test");
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("timing", "test");
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("de.xcom", "test");
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(3, mockHandler.getMessages().size());
077: testLogger.warn("Hello world");
078: assertEquals(4, mockHandler.getMessages().size());
079: testLogger.error("Hello world");
080: assertEquals(5, mockHandler.getMessages().size());
081: testLogger.fatal("Hello world");
082: assertEquals(6, mockHandler.getMessages().size());
083: }
084:
085: public void testLogger2Loggable() {
086: MockHandler mockHandler = getFreshHandler("TestGenerator");
087: Logger testLogger = LogManager
088: .getLogger("de.xcom.test", "test");
089: boolean test = testLogger.isTraceEnabled();
090: assertEquals(true, test);
091: test = testLogger.isInfoEnabled();
092: assertEquals(false, test);
093: test = testLogger.isDebugEnabled();
094: assertEquals(false, test);
095: test = testLogger.isWarnEnabled();
096: assertEquals(true, test);
097: test = testLogger.isErrorEnabled();
098: assertEquals(true, test);
099: test = testLogger.isFatalEnabled();
100: assertEquals(true, test);
101: }
102:
103: private MockHandler getFreshHandler(String handlerName) {
104: LogConfiguration config = manager.getLogConfiguration("test");
105: assertNotNull(config);
106: LogGenerator lg = config.getLogGenerator(handlerName);
107: assertNotNull(lg);
108: MockHandler mockHandler = (MockHandler) lg.getHandler();
109: assertNotNull(mockHandler);
110: mockHandler.clear();
111: return mockHandler;
112: }
113: }
|