001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.logging.simple;
019
020
021 import java.io.ByteArrayInputStream;
022 import java.io.ByteArrayOutputStream;
023 import java.io.ObjectInputStream;
024 import java.io.ObjectOutputStream;
025
026 import junit.framework.Test;
027 import junit.framework.TestCase;
028
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031 import org.apache.commons.logging.PathableClassLoader;
032 import org.apache.commons.logging.PathableTestSuite;
033 import org.apache.commons.logging.impl.SimpleLog;
034
035
036 /**
037 * <p>TestCase for simple logging when running with zero configuration
038 * other than selecting the SimpleLog implementation.</p>
039 *
040 * @author Craig R. McClanahan
041 * @version $Revision: 425249 $ $Date: 2006-07-25 03:30:16 +0200 (ti, 25 jul 2006) $
042 */
043
044 public class DefaultConfigTestCase extends TestCase {
045
046
047 // ----------------------------------------------------- Instance Variables
048
049
050 /**
051 * <p>The {@link LogFactory} implementation we have selected.</p>
052 */
053 protected LogFactory factory = null;
054
055
056 /**
057 * <p>The {@link Log} implementation we have selected.</p>
058 */
059 protected Log log = null;
060
061
062 // ------------------------------------------- JUnit Infrastructure Methods
063
064
065 /**
066 * Return the tests included in this test suite.
067 * <p>
068 * We need to use a PathableClassLoader here because the SimpleLog class
069 * is a pile of junk and chock-full of static variables. Any other test
070 * (like simple.CustomConfigTestCase) that has used the SimpleLog class
071 * will already have caused it to do once-only initialisation that we
072 * can't reset, even by calling LogFactory.releaseAll, because of those
073 * ugly statics. The only clean solution is to load a clean copy of
074 * commons-logging including SimpleLog via a nice clean classloader.
075 * Or we could fix SimpleLog to be sane...
076 */
077 public static Test suite() throws Exception {
078 Class thisClass = DefaultConfigTestCase.class;
079
080 PathableClassLoader loader = new PathableClassLoader(null);
081 loader.useExplicitLoader("junit.", Test.class.getClassLoader());
082 loader.addLogicalLib("testclasses");
083 loader.addLogicalLib("commons-logging");
084
085 Class testClass = loader.loadClass(thisClass.getName());
086 return new PathableTestSuite(testClass, loader);
087 }
088
089 /**
090 * Set system properties that will control the LogFactory/Log objects
091 * when they are created. Subclasses can override this method to
092 * define properties that suit them.
093 */
094 public void setProperties() {
095 System.setProperty(
096 "org.apache.commons.logging.Log",
097 "org.apache.commons.logging.impl.SimpleLog");
098 }
099
100 /**
101 * Set up instance variables required by this test case.
102 */
103 public void setUp() throws Exception {
104 LogFactory.releaseAll();
105 setProperties();
106 setUpFactory();
107 setUpLog("TestLogger");
108 }
109
110 /**
111 * Tear down instance variables required by this test case.
112 */
113 public void tearDown() {
114 log = null;
115 factory = null;
116 LogFactory.releaseAll();
117 }
118
119
120 // ----------------------------------------------------------- Test Methods
121
122
123 // Test pristine DecoratedSimpleLog instance
124 public void testPristineDecorated() {
125
126 setUpDecorated("DecoratedLogger");
127 checkDecorated();
128
129 }
130
131
132 // Test pristine Log instance
133 public void testPristineLog() {
134
135 checkStandard();
136
137 }
138
139
140 // Test pristine LogFactory instance
141 public void testPristineFactory() {
142
143 assertNotNull("LogFactory exists", factory);
144 assertEquals("LogFactory class",
145 "org.apache.commons.logging.impl.LogFactoryImpl",
146 factory.getClass().getName());
147
148 String names[] = factory.getAttributeNames();
149 assertNotNull("Names exists", names);
150 assertEquals("Names empty", 0, names.length);
151
152 }
153
154
155 // Test Serializability of standard instance
156 public void testSerializable() throws Exception {
157
158 // Serialize and deserialize the instance
159 ByteArrayOutputStream baos = new ByteArrayOutputStream();
160 ObjectOutputStream oos = new ObjectOutputStream(baos);
161 oos.writeObject(log);
162 oos.close();
163 ByteArrayInputStream bais =
164 new ByteArrayInputStream(baos.toByteArray());
165 ObjectInputStream ois = new ObjectInputStream(bais);
166 log = (Log) ois.readObject();
167 ois.close();
168
169 // Check the characteristics of the resulting object
170 checkStandard();
171
172 }
173
174
175 // -------------------------------------------------------- Support Methods
176
177
178
179 // Check the decorated log instance
180 protected void checkDecorated() {
181
182 assertNotNull("Log exists", log);
183 assertEquals("Log class",
184 "org.apache.commons.logging.simple.DecoratedSimpleLog",
185 log.getClass().getName());
186
187 // Can we call level checkers with no exceptions?
188 assertTrue(!log.isDebugEnabled());
189 assertTrue(log.isErrorEnabled());
190 assertTrue(log.isFatalEnabled());
191 assertTrue(log.isInfoEnabled());
192 assertTrue(!log.isTraceEnabled());
193 assertTrue(log.isWarnEnabled());
194
195 // Can we retrieve the current log level?
196 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
197
198 // Can we validate the extra exposed properties?
199 assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
200 ((DecoratedSimpleLog) log).getDateTimeFormat());
201 assertEquals("DecoratedLogger",
202 ((DecoratedSimpleLog) log).getLogName());
203 assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
204 assertTrue(((DecoratedSimpleLog) log).getShowShortName());
205
206 }
207
208
209 // Check the standard log instance
210 protected void checkStandard() {
211
212 assertNotNull("Log exists", log);
213 assertEquals("Log class",
214 "org.apache.commons.logging.impl.SimpleLog",
215 log.getClass().getName());
216
217 // Can we call level checkers with no exceptions?
218 assertTrue(!log.isDebugEnabled());
219 assertTrue(log.isErrorEnabled());
220 assertTrue(log.isFatalEnabled());
221 assertTrue(log.isInfoEnabled());
222 assertTrue(!log.isTraceEnabled());
223 assertTrue(log.isWarnEnabled());
224
225 // Can we retrieve the current log level?
226 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
227
228 }
229
230
231 // Set up decorated log instance
232 protected void setUpDecorated(String name) {
233 log = new DecoratedSimpleLog(name);
234 }
235
236
237 // Set up factory instance
238 protected void setUpFactory() throws Exception {
239 factory = LogFactory.getFactory();
240 }
241
242
243 // Set up log instance
244 protected void setUpLog(String name) throws Exception {
245 log = LogFactory.getLog(name);
246 }
247
248
249 }