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.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.commons.logging.PathableClassLoader;
030: import org.apache.commons.logging.PathableTestSuite;
031: import org.apache.commons.logging.impl.SimpleLog;
032:
033: /**
034: * <p>TestCase for simple logging when running with zero configuration
035: * other than selecting the SimpleLog implementation.</p>
036: *
037: * @author Craig R. McClanahan
038: * @version $Revision: 370012 $ $Date: 2006-01-18 02:34:05 +0000 (Wed, 18 Jan 2006) $
039: */
040:
041: public class DefaultConfigTestCase extends TestCase {
042:
043: // ----------------------------------------------------- Instance Variables
044:
045: /**
046: * <p>The {@link LogFactory} implementation we have selected.</p>
047: */
048: protected LogFactory factory = null;
049:
050: /**
051: * <p>The {@link Log} implementation we have selected.</p>
052: */
053: protected Log log = null;
054:
055: // ------------------------------------------- JUnit Infrastructure Methods
056:
057: /**
058: * Return the tests included in this test suite.
059: * <p>
060: * We need to use a PathableClassLoader here because the SimpleLog class
061: * is a pile of junk and chock-full of static variables. Any other test
062: * (like simple.CustomConfigTestCase) that has used the SimpleLog class
063: * will already have caused it to do once-only initialisation that we
064: * can't reset, even by calling LogFactory.releaseAll, because of those
065: * ugly statics. The only clean solution is to load a clean copy of
066: * commons-logging including SimpleLog via a nice clean classloader.
067: * Or we could fix SimpleLog to be sane...
068: */
069: public static Test suite() throws Exception {
070: Class this Class = DefaultConfigTestCase.class;
071:
072: PathableClassLoader loader = new PathableClassLoader(null);
073: loader.useSystemLoader("junit.");
074: loader.addLogicalLib("testclasses");
075: loader.addLogicalLib("commons-logging");
076:
077: Class testClass = loader.loadClass(this Class.getName());
078: return new PathableTestSuite(testClass, loader);
079: }
080:
081: /**
082: * Set system properties that will control the LogFactory/Log objects
083: * when they are created. Subclasses can override this method to
084: * define properties that suit them.
085: */
086: public void setProperties() {
087: System.setProperty("org.apache.commons.logging.Log",
088: "org.apache.commons.logging.impl.SimpleLog");
089: }
090:
091: /**
092: * Set up instance variables required by this test case.
093: */
094: public void setUp() throws Exception {
095: LogFactory.releaseAll();
096: setProperties();
097: setUpFactory();
098: setUpLog("TestLogger");
099: }
100:
101: /**
102: * Tear down instance variables required by this test case.
103: */
104: public void tearDown() {
105: log = null;
106: factory = null;
107: LogFactory.releaseAll();
108: }
109:
110: // ----------------------------------------------------------- Test Methods
111:
112: // Test pristine DecoratedSimpleLog instance
113: public void testPristineDecorated() {
114:
115: setUpDecorated("DecoratedLogger");
116: checkDecorated();
117:
118: }
119:
120: // Test pristine Log instance
121: public void testPristineLog() {
122:
123: checkStandard();
124:
125: }
126:
127: // Test pristine LogFactory instance
128: public void testPristineFactory() {
129:
130: assertNotNull("LogFactory exists", factory);
131: assertEquals("LogFactory class",
132: "org.apache.commons.logging.impl.LogFactoryImpl",
133: factory.getClass().getName());
134:
135: String names[] = factory.getAttributeNames();
136: assertNotNull("Names exists", names);
137: assertEquals("Names empty", 0, names.length);
138:
139: }
140:
141: // Test Serializability of standard instance
142: public void testSerializable() throws Exception {
143:
144: // Serialize and deserialize the instance
145: ByteArrayOutputStream baos = new ByteArrayOutputStream();
146: ObjectOutputStream oos = new ObjectOutputStream(baos);
147: oos.writeObject(log);
148: oos.close();
149: ByteArrayInputStream bais = new ByteArrayInputStream(baos
150: .toByteArray());
151: ObjectInputStream ois = new ObjectInputStream(bais);
152: log = (Log) ois.readObject();
153: ois.close();
154:
155: // Check the characteristics of the resulting object
156: checkStandard();
157:
158: }
159:
160: // -------------------------------------------------------- Support Methods
161:
162: // Check the decorated log instance
163: protected void checkDecorated() {
164:
165: assertNotNull("Log exists", log);
166: assertEquals("Log class",
167: "org.apache.commons.logging.simple.DecoratedSimpleLog",
168: log.getClass().getName());
169:
170: // Can we call level checkers with no exceptions?
171: assertTrue(!log.isDebugEnabled());
172: assertTrue(log.isErrorEnabled());
173: assertTrue(log.isFatalEnabled());
174: assertTrue(log.isInfoEnabled());
175: assertTrue(!log.isTraceEnabled());
176: assertTrue(log.isWarnEnabled());
177:
178: // Can we retrieve the current log level?
179: assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log)
180: .getLevel());
181:
182: // Can we validate the extra exposed properties?
183: assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
184: ((DecoratedSimpleLog) log).getDateTimeFormat());
185: assertEquals("DecoratedLogger", ((DecoratedSimpleLog) log)
186: .getLogName());
187: assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
188: assertTrue(((DecoratedSimpleLog) log).getShowShortName());
189:
190: }
191:
192: // Check the standard log instance
193: protected void checkStandard() {
194:
195: assertNotNull("Log exists", log);
196: assertEquals("Log class",
197: "org.apache.commons.logging.impl.SimpleLog", log
198: .getClass().getName());
199:
200: // Can we call level checkers with no exceptions?
201: assertTrue(!log.isDebugEnabled());
202: assertTrue(log.isErrorEnabled());
203: assertTrue(log.isFatalEnabled());
204: assertTrue(log.isInfoEnabled());
205: assertTrue(!log.isTraceEnabled());
206: assertTrue(log.isWarnEnabled());
207:
208: // Can we retrieve the current log level?
209: assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log)
210: .getLevel());
211:
212: }
213:
214: // Set up decorated log instance
215: protected void setUpDecorated(String name) {
216: log = new DecoratedSimpleLog(name);
217: }
218:
219: // Set up factory instance
220: protected void setUpFactory() throws Exception {
221: factory = LogFactory.getFactory();
222: }
223:
224: // Set up log instance
225: protected void setUpLog(String name) throws Exception {
226: log = LogFactory.getLog(name);
227: }
228:
229: }
|