1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.logging.simple;
18
19
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.ObjectInputStream;
23 import java.io.ObjectOutputStream;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.commons.logging.PathableClassLoader;
31 import org.apache.commons.logging.PathableTestSuite;
32 import org.apache.commons.logging.impl.SimpleLog;
33
34
35 /***
36 * <p>TestCase for simple logging when running with zero configuration
37 * other than selecting the SimpleLog implementation.</p>
38 *
39 * @author Craig R. McClanahan
40 * @version $Revision: 370012 $ $Date: 2006-01-18 02:34:05 +0000 (Wed, 18 Jan 2006) $
41 */
42
43 public class DefaultConfigTestCase extends TestCase {
44
45
46
47
48
49 /***
50 * <p>The {@link LogFactory} implementation we have selected.</p>
51 */
52 protected LogFactory factory = null;
53
54
55 /***
56 * <p>The {@link Log} implementation we have selected.</p>
57 */
58 protected Log log = null;
59
60
61
62
63
64 /***
65 * Return the tests included in this test suite.
66 * <p>
67 * We need to use a PathableClassLoader here because the SimpleLog class
68 * is a pile of junk and chock-full of static variables. Any other test
69 * (like simple.CustomConfigTestCase) that has used the SimpleLog class
70 * will already have caused it to do once-only initialisation that we
71 * can't reset, even by calling LogFactory.releaseAll, because of those
72 * ugly statics. The only clean solution is to load a clean copy of
73 * commons-logging including SimpleLog via a nice clean classloader.
74 * Or we could fix SimpleLog to be sane...
75 */
76 public static Test suite() throws Exception {
77 Class thisClass = DefaultConfigTestCase.class;
78
79 PathableClassLoader loader = new PathableClassLoader(null);
80 loader.useSystemLoader("junit.");
81 loader.addLogicalLib("testclasses");
82 loader.addLogicalLib("commons-logging");
83
84 Class testClass = loader.loadClass(thisClass.getName());
85 return new PathableTestSuite(testClass, loader);
86 }
87
88 /***
89 * Set system properties that will control the LogFactory/Log objects
90 * when they are created. Subclasses can override this method to
91 * define properties that suit them.
92 */
93 public void setProperties() {
94 System.setProperty(
95 "org.apache.commons.logging.Log",
96 "org.apache.commons.logging.impl.SimpleLog");
97 }
98
99 /***
100 * Set up instance variables required by this test case.
101 */
102 public void setUp() throws Exception {
103 LogFactory.releaseAll();
104 setProperties();
105 setUpFactory();
106 setUpLog("TestLogger");
107 }
108
109 /***
110 * Tear down instance variables required by this test case.
111 */
112 public void tearDown() {
113 log = null;
114 factory = null;
115 LogFactory.releaseAll();
116 }
117
118
119
120
121
122
123 public void testPristineDecorated() {
124
125 setUpDecorated("DecoratedLogger");
126 checkDecorated();
127
128 }
129
130
131
132 public void testPristineLog() {
133
134 checkStandard();
135
136 }
137
138
139
140 public void testPristineFactory() {
141
142 assertNotNull("LogFactory exists", factory);
143 assertEquals("LogFactory class",
144 "org.apache.commons.logging.impl.LogFactoryImpl",
145 factory.getClass().getName());
146
147 String names[] = factory.getAttributeNames();
148 assertNotNull("Names exists", names);
149 assertEquals("Names empty", 0, names.length);
150
151 }
152
153
154
155 public void testSerializable() throws Exception {
156
157
158 ByteArrayOutputStream baos = new ByteArrayOutputStream();
159 ObjectOutputStream oos = new ObjectOutputStream(baos);
160 oos.writeObject(log);
161 oos.close();
162 ByteArrayInputStream bais =
163 new ByteArrayInputStream(baos.toByteArray());
164 ObjectInputStream ois = new ObjectInputStream(bais);
165 log = (Log) ois.readObject();
166 ois.close();
167
168
169 checkStandard();
170
171 }
172
173
174
175
176
177
178
179 protected void checkDecorated() {
180
181 assertNotNull("Log exists", log);
182 assertEquals("Log class",
183 "org.apache.commons.logging.simple.DecoratedSimpleLog",
184 log.getClass().getName());
185
186
187 assertTrue(!log.isDebugEnabled());
188 assertTrue(log.isErrorEnabled());
189 assertTrue(log.isFatalEnabled());
190 assertTrue(log.isInfoEnabled());
191 assertTrue(!log.isTraceEnabled());
192 assertTrue(log.isWarnEnabled());
193
194
195 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
196
197
198 assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
199 ((DecoratedSimpleLog) log).getDateTimeFormat());
200 assertEquals("DecoratedLogger",
201 ((DecoratedSimpleLog) log).getLogName());
202 assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
203 assertTrue(((DecoratedSimpleLog) log).getShowShortName());
204
205 }
206
207
208
209 protected void checkStandard() {
210
211 assertNotNull("Log exists", log);
212 assertEquals("Log class",
213 "org.apache.commons.logging.impl.SimpleLog",
214 log.getClass().getName());
215
216
217 assertTrue(!log.isDebugEnabled());
218 assertTrue(log.isErrorEnabled());
219 assertTrue(log.isFatalEnabled());
220 assertTrue(log.isInfoEnabled());
221 assertTrue(!log.isTraceEnabled());
222 assertTrue(log.isWarnEnabled());
223
224
225 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
226
227 }
228
229
230
231 protected void setUpDecorated(String name) {
232 log = new DecoratedSimpleLog(name);
233 }
234
235
236
237 protected void setUpFactory() throws Exception {
238 factory = LogFactory.getFactory();
239 }
240
241
242
243 protected void setUpLog(String name) throws Exception {
244 log = LogFactory.getLog(name);
245 }
246
247
248 }