001: /*
002: * Copyright 2001-2004 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.simple;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import junit.framework.Test;
024:
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.commons.logging.PathableClassLoader;
027: import org.apache.commons.logging.PathableTestSuite;
028: import org.apache.commons.logging.impl.SimpleLog;
029:
030: /**
031: * <p>TestCase for simple logging when running with custom configuration
032: * properties.</p>
033: *
034: * @author Craig R. McClanahan
035: * @version $Revision: 370012 $ $Date: 2006-01-18 02:34:05 +0000 (Wed, 18 Jan 2006) $
036: */
037: public class CustomConfigTestCase extends DefaultConfigTestCase {
038:
039: // ----------------------------------------------------- Instance Variables
040:
041: /**
042: * <p>The expected log records.</p>
043: */
044: protected List expected;
045:
046: /**
047: * <p>The message levels that should have been logged.</p>
048: */
049: /*
050: protected Level testLevels[] =
051: { Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
052: */
053:
054: /**
055: * <p>The message strings that should have been logged.</p>
056: */
057: protected String testMessages[] = { "debug", "info", "warn",
058: "error", "fatal" };
059:
060: // ------------------------------------------- JUnit Infrastructure Methods
061:
062: /**
063: * Set system properties that will control the LogFactory/Log objects
064: * when they are created. Subclasses can override this method to
065: * define properties that suit them.
066: */
067: public void setProperties() {
068: System.setProperty("org.apache.commons.logging.Log",
069: "org.apache.commons.logging.simple.DecoratedSimpleLog");
070: System.setProperty(
071: "org.apache.commons.logging.simplelog.defaultlog",
072: "debug");
073: }
074:
075: /**
076: * Set up instance variables required by this test case.
077: */
078: public void setUp() throws Exception {
079: LogFactory.releaseAll();
080: setProperties();
081: expected = new ArrayList();
082: setUpFactory();
083: setUpLog("DecoratedLogger");
084: }
085:
086: /**
087: * Return the tests included in this test suite.
088: * <p>
089: * We need to use a PathableClassLoader here because the SimpleLog class
090: * is a pile of junk and chock-full of static variables. Any other test
091: * (like simple.CustomConfigTestCase) that has used the SimpleLog class
092: * will already have caused it to do once-only initialisation that we
093: * can't reset, even by calling LogFactory.releaseAll, because of those
094: * ugly statics. The only clean solution is to load a clean copy of
095: * commons-logging including SimpleLog via a nice clean classloader.
096: * Or we could fix SimpleLog to be sane...
097: */
098: public static Test suite() throws Exception {
099: Class this Class = CustomConfigTestCase.class;
100:
101: PathableClassLoader loader = new PathableClassLoader(null);
102: loader.useSystemLoader("junit.");
103: loader.addLogicalLib("testclasses");
104: loader.addLogicalLib("commons-logging");
105:
106: Class testClass = loader.loadClass(this Class.getName());
107: return new PathableTestSuite(testClass, loader);
108: }
109:
110: /**
111: * Tear down instance variables required by this test case.
112: */
113: public void tearDown() {
114: super .tearDown();
115: expected = null;
116: }
117:
118: // ----------------------------------------------------------- Test Methods
119:
120: // Test logging message strings with exceptions
121: public void testExceptionMessages() throws Exception {
122:
123: ((DecoratedSimpleLog) log).clearCache();
124: logExceptionMessages();
125: checkExpected();
126:
127: }
128:
129: // Test logging plain message strings
130: public void testPlainMessages() throws Exception {
131:
132: ((DecoratedSimpleLog) log).clearCache();
133: logPlainMessages();
134: checkExpected();
135:
136: }
137:
138: // Test Serializability of standard instance
139: public void testSerializable() throws Exception {
140:
141: ((DecoratedSimpleLog) log).clearCache();
142: logPlainMessages();
143: super .testSerializable();
144: logExceptionMessages();
145: checkExpected();
146:
147: }
148:
149: // -------------------------------------------------------- Support Methods
150:
151: // Check the decorated log instance
152: protected void checkDecorated() {
153:
154: assertNotNull("Log exists", log);
155: assertEquals("Log class",
156: "org.apache.commons.logging.simple.DecoratedSimpleLog",
157: log.getClass().getName());
158:
159: // Can we call level checkers with no exceptions?
160: assertTrue(log.isDebugEnabled());
161: assertTrue(log.isErrorEnabled());
162: assertTrue(log.isFatalEnabled());
163: assertTrue(log.isInfoEnabled());
164: assertTrue(!log.isTraceEnabled());
165: assertTrue(log.isWarnEnabled());
166:
167: // Can we retrieve the current log level?
168: assertEquals(SimpleLog.LOG_LEVEL_DEBUG, ((SimpleLog) log)
169: .getLevel());
170:
171: // Can we validate the extra exposed properties?
172: checkDecoratedDateTime();
173: assertEquals("DecoratedLogger", ((DecoratedSimpleLog) log)
174: .getLogName());
175: checkShowDateTime();
176: assertTrue(((DecoratedSimpleLog) log).getShowShortName());
177:
178: }
179:
180: /** Hook for subclassses */
181: protected void checkShowDateTime() {
182: assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
183: }
184:
185: /** Hook for subclasses */
186: protected void checkDecoratedDateTime() {
187: assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
188: ((DecoratedSimpleLog) log).getDateTimeFormat());
189: }
190:
191: // Check the actual log records against the expected ones
192: protected void checkExpected() {
193:
194: List acts = ((DecoratedSimpleLog) log).getCache();
195: Iterator exps = expected.iterator();
196: int n = 0;
197: while (exps.hasNext()) {
198: LogRecord exp = (LogRecord) exps.next();
199: LogRecord act = (LogRecord) acts.get(n++);
200: assertEquals("Row " + n + " type", exp.type, act.type);
201: assertEquals("Row " + n + " message", exp.message,
202: act.message);
203: assertEquals("Row " + n + " throwable", exp.t, act.t);
204: }
205:
206: }
207:
208: // Check the standard log instance
209: protected void checkStandard() {
210:
211: checkDecorated();
212:
213: }
214:
215: // Log the messages with exceptions
216: protected void logExceptionMessages() {
217:
218: // Generate log records
219: Throwable t = new IndexOutOfBoundsException();
220: log.trace("trace", t); // Should not actually get logged
221: log.debug("debug", t);
222: log.info("info", t);
223: log.warn("warn", t);
224: log.error("error", t);
225: log.fatal("fatal", t);
226:
227: // Record the log records we expect
228: expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug",
229: t));
230: expected
231: .add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", t));
232: expected
233: .add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", t));
234: expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error",
235: t));
236: expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal",
237: t));
238:
239: }
240:
241: // Log the plain messages
242: protected void logPlainMessages() {
243:
244: // Generate log records
245: log.trace("trace"); // Should not actually get logged
246: log.debug("debug");
247: log.info("info");
248: log.warn("warn");
249: log.error("error");
250: log.fatal("fatal");
251:
252: // Record the log records we expect
253: expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug",
254: null));
255: expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info",
256: null));
257: expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn",
258: null));
259: expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error",
260: null));
261: expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal",
262: null));
263:
264: }
265:
266: }
|