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.jdk14;
018:
019: import java.io.InputStream;
020: import java.util.Iterator;
021: import java.util.logging.Handler;
022: import java.util.logging.Level;
023: import java.util.logging.LogManager;
024: import java.util.logging.LogRecord;
025: import java.util.logging.Logger;
026:
027: import junit.framework.Test;
028: import junit.framework.TestSuite;
029:
030: /**
031: * <p>TestCase for JDK 1.4 logging when running on a JDK 1.4 system with
032: * custom configuration, so that JDK 1.4 should be selected and an appropriate
033: * logger configured per the configuration properties.</p>
034: *
035: * @author Craig R. McClanahan
036: * @version $Revision: 370012 $ $Date: 2006-01-18 02:34:05 +0000 (Wed, 18 Jan 2006) $
037: */
038:
039: public class CustomConfigTestCase extends DefaultConfigTestCase {
040:
041: // ----------------------------------------------------------- Constructors
042:
043: /**
044: * <p>Construct a new instance of this test case.</p>
045: *
046: * @param name Name of the test case
047: */
048: public CustomConfigTestCase(String name) {
049: super (name);
050: }
051:
052: // ----------------------------------------------------- Instance Variables
053:
054: /**
055: * <p>The customized <code>Handler</code> we will be using.</p>
056: */
057: protected TestHandler handler = null;
058:
059: /**
060: * <p>The underlying <code>Handler</code>s we will be using.</p>
061: */
062: protected Handler handlers[] = null;
063:
064: /**
065: * <p>The underlying <code>Logger</code> we will be using.</p>
066: */
067: protected Logger logger = null;
068:
069: /**
070: * <p>The underlying <code>LogManager</code> we will be using.</p>
071: */
072: protected LogManager manager = null;
073:
074: /**
075: * <p>The message levels that should have been logged.</p>
076: */
077: protected Level testLevels[] = { Level.FINE, Level.INFO,
078: Level.WARNING, Level.SEVERE, Level.SEVERE };
079:
080: /**
081: * <p>The message strings that should have been logged.</p>
082: */
083: protected String testMessages[] = { "debug", "info", "warn",
084: "error", "fatal" };
085:
086: // ------------------------------------------- JUnit Infrastructure Methods
087:
088: /**
089: * Set up instance variables required by this test case.
090: */
091: public void setUp() throws Exception {
092: setUpManager("org/apache/commons/logging/jdk14/CustomConfig.properties");
093: setUpLogger("TestLogger");
094: setUpHandlers();
095: setUpFactory();
096: setUpLog("TestLogger");
097: }
098:
099: /**
100: * Return the tests included in this test suite.
101: */
102: public static Test suite() throws Exception {
103: /*
104: PathableClassLoader loader = new PathableClassLoader(null);
105: loader.useSystemLoader("junit.");
106:
107: PathableClassLoader child = new PathableClassLoader(parent);
108: child.addLogicalLib("testclasses");
109: child.addLogicalLib("commons-logging");
110:
111: Class testClass = child.loadClass(CustomConfigTestCase.class.getName());
112: return new PathableTestSuite(testClass, child);
113: */
114: return new TestSuite(CustomConfigTestCase.class);
115: }
116:
117: /**
118: * Tear down instance variables required by this test case.
119: */
120: public void tearDown() {
121: super .tearDown();
122: handlers = null;
123: logger = null;
124: manager = null;
125: }
126:
127: // ----------------------------------------------------------- Test Methods
128:
129: // Test logging message strings with exceptions
130: public void testExceptionMessages() throws Exception {
131:
132: logExceptionMessages();
133: checkLogRecords(true);
134:
135: }
136:
137: // Test logging plain message strings
138: public void testPlainMessages() throws Exception {
139:
140: logPlainMessages();
141: checkLogRecords(false);
142:
143: }
144:
145: // Test pristine Handlers instances
146: public void testPristineHandlers() {
147:
148: assertNotNull(handlers);
149: assertEquals(1, handlers.length);
150: assertTrue(handlers[0] instanceof TestHandler);
151: assertNotNull(handler);
152:
153: }
154:
155: // Test pristine Logger instance
156: public void testPristineLogger() {
157:
158: assertNotNull("Logger exists", logger);
159: assertEquals("Logger name", "TestLogger", logger.getName());
160:
161: // Assert which logging levels have been enabled
162: assertTrue(logger.isLoggable(Level.SEVERE));
163: assertTrue(logger.isLoggable(Level.WARNING));
164: assertTrue(logger.isLoggable(Level.INFO));
165: assertTrue(logger.isLoggable(Level.CONFIG));
166: assertTrue(logger.isLoggable(Level.FINE));
167: assertTrue(!logger.isLoggable(Level.FINER));
168: assertTrue(!logger.isLoggable(Level.FINEST));
169:
170: }
171:
172: // Test Serializability of Log instance
173: public void testSerializable() throws Exception {
174:
175: super .testSerializable();
176: testExceptionMessages();
177:
178: }
179:
180: // -------------------------------------------------------- Support Methods
181:
182: // Check the log instance
183: protected void checkLog() {
184:
185: assertNotNull("Log exists", log);
186: assertEquals("Log class",
187: "org.apache.commons.logging.impl.Jdk14Logger", log
188: .getClass().getName());
189:
190: // Assert which logging levels have been enabled
191: assertTrue(log.isFatalEnabled());
192: assertTrue(log.isErrorEnabled());
193: assertTrue(log.isWarnEnabled());
194: assertTrue(log.isInfoEnabled());
195: assertTrue(log.isDebugEnabled());
196: assertTrue(!log.isTraceEnabled());
197:
198: }
199:
200: // Check the recorded messages
201: protected void checkLogRecords(boolean thrown) {
202: Iterator records = handler.records();
203: for (int i = 0; i < testMessages.length; i++) {
204: assertTrue(records.hasNext());
205: LogRecord record = (LogRecord) records.next();
206: assertEquals("LogRecord level", testLevels[i], record
207: .getLevel());
208: assertEquals("LogRecord message", testMessages[i], record
209: .getMessage());
210: assertTrue(
211: "LogRecord class",
212: record
213: .getSourceClassName()
214: .startsWith(
215: "org.apache.commons.logging.jdk14.CustomConfig"));
216: if (thrown) {
217: assertEquals("LogRecord method",
218: "logExceptionMessages", record
219: .getSourceMethodName());
220: } else {
221: assertEquals("LogRecord method", "logPlainMessages",
222: record.getSourceMethodName());
223: }
224: if (thrown) {
225: assertNotNull("LogRecord thrown", record.getThrown());
226: assertTrue(
227: "LogRecord thrown type",
228: record.getThrown() instanceof IndexOutOfBoundsException);
229: } else {
230: assertNull("LogRecord thrown", record.getThrown());
231: }
232: }
233: assertTrue(!records.hasNext());
234: handler.flush();
235: }
236:
237: // Log the messages with exceptions
238: protected void logExceptionMessages() {
239: Throwable t = new IndexOutOfBoundsException();
240: log.trace("trace", t); // Should not actually get logged
241: log.debug("debug", t);
242: log.info("info", t);
243: log.warn("warn", t);
244: log.error("error", t);
245: log.fatal("fatal", t);
246: }
247:
248: // Log the plain messages
249: protected void logPlainMessages() {
250: log.trace("trace"); // Should not actually get logged
251: log.debug("debug");
252: log.info("info");
253: log.warn("warn");
254: log.error("error");
255: log.fatal("fatal");
256: }
257:
258: // Set up handlers instance
259: protected void setUpHandlers() throws Exception {
260: Logger parent = logger;
261: while (parent.getParent() != null) {
262: parent = parent.getParent();
263: }
264: handlers = parent.getHandlers();
265:
266: // The CustomConfig.properties file explicitly defines one handler class
267: // to be attached to the root logger, so if it isn't there then
268: // something is badly wrong...
269: //
270: // Yes this testing is also done in testPristineHandlers but
271: // unfortunately:
272: // * we need to set up the handlers variable here,
273: // * we don't want that to be set up incorrectly, as that can
274: // produce weird error messages in other tests, and
275: // * we can't rely on testPristineHandlers being the first
276: // test to run.
277: // so we need to test things here too.
278: assertNotNull("No Handlers defined for JDK14 logging", handlers);
279: assertEquals("Unexpected number of handlers for JDK14 logging",
280: 1, handlers.length);
281: assertNotNull("Handler is null", handlers[0]);
282: assertTrue("Handler not of expected type",
283: handlers[0] instanceof TestHandler);
284: handler = (TestHandler) handlers[0];
285: }
286:
287: // Set up logger instance
288: protected void setUpLogger(String name) throws Exception {
289: logger = Logger.getLogger(name);
290: }
291:
292: // Set up LogManager instance
293: protected void setUpManager(String config) throws Exception {
294: manager = LogManager.getLogManager();
295: InputStream is = this.getClass().getClassLoader()
296: .getResourceAsStream(config);
297: manager.readConfiguration(is);
298: is.close();
299: }
300:
301: }
|