1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.logging.logkit;
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.LogKitLogger;
33 import org.apache.commons.logging.impl.NoOpLog;
34
35 import org.apache.commons.logging.AbstractLogTest;
36
37 /***
38 * Basic tests for Avalon LogKit logger adapter.
39 */
40
41 public class StandardTestCase extends AbstractLogTest {
42
43
44
45
46
47 /***
48 * <p>The {@link LogFactory} implementation we have selected.</p>
49 */
50 protected LogFactory factory = null;
51
52
53 /***
54 * <p>The {@link Log} implementation we have selected.</p>
55 */
56 protected Log log = null;
57
58
59
60
61
62 /***
63 * Return the tests included in this test suite.
64 */
65 public static Test suite() throws Exception {
66 Class thisClass = StandardTestCase.class;
67
68 PathableClassLoader loader = new PathableClassLoader(null);
69 loader.useSystemLoader("junit.");
70 loader.addLogicalLib("testclasses");
71 loader.addLogicalLib("commons-logging");
72 loader.addLogicalLib("logkit");
73
74 Class testClass = loader.loadClass(thisClass.getName());
75 return new PathableTestSuite(testClass, loader);
76 }
77
78 /***
79 * Set up instance variables required by this test case.
80 */
81 public void setUp() throws Exception {
82 LogFactory.releaseAll();
83
84 System.setProperty(
85 "org.apache.commons.logging.Log",
86 "org.apache.commons.logging.impl.LogKitLogger");
87
88 factory = LogFactory.getFactory();
89 log = LogFactory.getLog("TestLogger");
90 }
91
92 /***
93 * Tear down instance variables required by this test case.
94 */
95 public void tearDown() {
96 log = null;
97 factory = null;
98 LogFactory.releaseAll();
99 }
100
101
102
103 /***
104 * Override the abstract method from the parent class so that the
105 * inherited tests can access the right Log object type.
106 */
107 public Log getLogObject()
108 {
109 return new LogKitLogger(this.getClass().getName());
110 }
111
112
113 public void testPristineFactory() {
114
115 assertNotNull("LogFactory exists", factory);
116 assertEquals("LogFactory class",
117 "org.apache.commons.logging.impl.LogFactoryImpl",
118 factory.getClass().getName());
119
120 String names[] = factory.getAttributeNames();
121 assertNotNull("Names exists", names);
122 assertEquals("Names empty", 0, names.length);
123 }
124
125
126 public void testPristineLog() {
127 checkStandard();
128 }
129
130
131 public void testSerializable() throws Exception {
132 checkStandard();
133
134
135 ByteArrayOutputStream baos = new ByteArrayOutputStream();
136 ObjectOutputStream oos = new ObjectOutputStream(baos);
137 oos.writeObject(log);
138 oos.close();
139 ByteArrayInputStream bais =
140 new ByteArrayInputStream(baos.toByteArray());
141 ObjectInputStream ois = new ObjectInputStream(bais);
142 log = (Log) ois.readObject();
143 ois.close();
144
145 checkStandard();
146 }
147
148
149
150
151
152 protected void checkStandard() {
153
154 assertNotNull("Log exists", log);
155 assertEquals("Log class",
156 "org.apache.commons.logging.impl.LogKitLogger",
157 log.getClass().getName());
158
159
160
161 assertTrue(log.isTraceEnabled());
162 assertTrue(log.isDebugEnabled());
163 assertTrue(log.isInfoEnabled());
164 assertTrue(log.isWarnEnabled());
165 assertTrue(log.isErrorEnabled());
166 assertTrue(log.isFatalEnabled());
167 }
168 }