001: /*
002: * Copyright 2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.logging.log4j;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import junit.framework.TestCase;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: /**
032: * Abstract set of tests that can be executed with various classpaths set.
033: * <p>
034: * The tests verify that when running on a system with Log4J present,
035: * Log4J is selected and that the logger basically works.
036: */
037:
038: public abstract class StandardTests extends TestCase {
039:
040: /**
041: * Simple structure to store information about messages that actually get
042: * logged by the underlying logging library.
043: */
044: public static class LogEvent {
045: public String msg;
046: public String level;
047: public Throwable throwable;
048: }
049:
050: // -------------------------------------------------------------------
051: // JUnit Infrastructure Methods
052: // -------------------------------------------------------------------
053:
054: /**
055: * Set up instance variables required by this test case.
056: */
057: public void setUp() throws Exception {
058: LogFactory.releaseAll();
059: }
060:
061: /**
062: * Tear down instance variables required by this test case.
063: */
064: public void tearDown() {
065: LogFactory.releaseAll();
066: }
067:
068: // -----------------------------------------------------------
069: // abstract methods
070: // -----------------------------------------------------------
071:
072: /**
073: * Modify log4j's setup so that all messages actually logged get redirected
074: * into the specified list.
075: * <p>
076: * This method also sets the logging level to INFO so that we
077: * can test whether messages are getting properly filtered.
078: */
079: public abstract void setUpTestAppender(List logEvents)
080: throws Exception;
081:
082: // ----------------------------------------------------------- Test Methods
083:
084: /**
085: * Test that a LogFactory gets created as expected.
086: */
087: public void testCreateFactory() {
088: LogFactory factory = LogFactory.getFactory();
089: assertNotNull("LogFactory exists", factory);
090: assertEquals("LogFactory class",
091: "org.apache.commons.logging.impl.LogFactoryImpl",
092: factory.getClass().getName());
093:
094: String names[] = factory.getAttributeNames();
095: assertNotNull("Names exists", names);
096: assertEquals("Names empty", 0, names.length);
097: }
098:
099: /**
100: * Verify that we can log messages without exceptions.
101: */
102: public void testPlainMessages() throws Exception {
103: List logEvents = new ArrayList();
104: setUpTestAppender(logEvents);
105: Log log = LogFactory.getLog("test-category");
106: logPlainMessages(log);
107: checkLoggingEvents(logEvents, false);
108: }
109:
110: /**
111: * Verify that we can log exception messages.
112: */
113: public void testExceptionMessages() throws Exception {
114: List logEvents = new ArrayList();
115: setUpTestAppender(logEvents);
116: Log log = LogFactory.getLog("test-category");
117: logExceptionMessages(log);
118: checkLoggingEvents(logEvents, true);
119: }
120:
121: /**
122: * Test Serializability of Log instance
123: */
124: public void testSerializable() throws Exception {
125: List logEvents = new ArrayList();
126: setUpTestAppender(logEvents);
127: Log log = LogFactory.getLog("test-category");
128:
129: ByteArrayOutputStream baos = new ByteArrayOutputStream();
130: ObjectOutputStream oos = new ObjectOutputStream(baos);
131: oos.writeObject(log);
132: oos.close();
133: ByteArrayInputStream bais = new ByteArrayInputStream(baos
134: .toByteArray());
135: ObjectInputStream ois = new ObjectInputStream(bais);
136: Log newLog = (Log) ois.readObject();
137: ois.close();
138:
139: // Check the characteristics of the resulting object
140: logExceptionMessages(newLog);
141: checkLoggingEvents(logEvents, true);
142: }
143:
144: // -------------------------------------------------------- Support Methods
145:
146: /**
147: * Verify that the TestAppender has received the expected
148: * number of messages. This assumes that:
149: * <ul>
150: * <li>setUpTestAppender has been called
151: * <li>logPlainMessages or logExceptionMessages has been
152: * called to log a known number of messages at known levels.
153: * </ul>
154: *
155: * @param logEvents is the list of log events received.
156: *
157: * @param thrown False if logPlainMessages was called
158: * (ie the TestAppender is expected to have received
159: * logevents with no associated exception info). True if
160: * logExceptionMessages was called.
161: */
162: private void checkLoggingEvents(List logEvents, boolean thrown) {
163: LogEvent ev;
164:
165: assertEquals("Unexpected number of log events", 4, logEvents
166: .size());
167:
168: ev = (LogEvent) logEvents.get(0);
169: assertEquals("Info message expected", "info", ev.msg);
170: assertEquals("Info level expected", "INFO", ev.level);
171: assertEquals("Exception data incorrect",
172: (ev.throwable != null), thrown);
173:
174: ev = (LogEvent) logEvents.get(1);
175: assertEquals("Warn message expected", "warn", ev.msg);
176: assertEquals("Warn level expected", "WARN", ev.level);
177: assertEquals("Exception data incorrect",
178: (ev.throwable != null), thrown);
179:
180: ev = (LogEvent) logEvents.get(2);
181: assertEquals("Error message expected", "error", ev.msg);
182: assertEquals("Error level expected", "ERROR", ev.level);
183: assertEquals("Exception data incorrect",
184: (ev.throwable != null), thrown);
185:
186: ev = (LogEvent) logEvents.get(3);
187: assertEquals("Fatal message expected", "fatal", ev.msg);
188: assertEquals("Fatal level expected", "FATAL", ev.level);
189: assertEquals("Exception data incorrect",
190: (ev.throwable != null), thrown);
191: }
192:
193: /**
194: * Log plain messages.
195: */
196: private void logPlainMessages(Log log) {
197: log.trace("trace"); // Should not actually get logged
198: log.debug("debug"); // Should not actually get logged
199: log.info("info");
200: log.warn("warn");
201: log.error("error");
202: log.fatal("fatal");
203: }
204:
205: /**
206: * Log messages with exceptions
207: */
208: private void logExceptionMessages(Log log) {
209: Throwable t = new IndexOutOfBoundsException();
210: log.trace("trace", t); // Should not actually get logged
211: log.debug("debug", t); // Should not actually get logged
212: log.info("info", t);
213: log.warn("warn", t);
214: log.error("error", t);
215: log.fatal("fatal", t);
216: }
217: }
|