1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.logging.logkit;
19
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25
26 import junit.framework.Test;
27
28 import org.apache.commons.logging.AbstractLogTest;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.commons.logging.PathableClassLoader;
32 import org.apache.commons.logging.PathableTestSuite;
33 import org.apache.commons.logging.impl.LogKitLogger;
34
35
36
37
38
39 public class StandardTestCase extends AbstractLogTest {
40
41
42
43
44
45
46
47
48 protected LogFactory factory = null;
49
50
51
52
53
54 protected Log log = null;
55
56
57
58
59
60
61
62
63 public static Test suite() throws Exception {
64 Class thisClass = StandardTestCase.class;
65
66 PathableClassLoader loader = new PathableClassLoader(null);
67 loader.useExplicitLoader("junit.", Test.class.getClassLoader());
68 loader.addLogicalLib("testclasses");
69 loader.addLogicalLib("commons-logging");
70 loader.addLogicalLib("logkit");
71
72 Class testClass = loader.loadClass(thisClass.getName());
73 return new PathableTestSuite(testClass, loader);
74 }
75
76
77
78
79 public void setUp() throws Exception {
80 LogFactory.releaseAll();
81
82 System.setProperty(
83 "org.apache.commons.logging.Log",
84 "org.apache.commons.logging.impl.LogKitLogger");
85
86 factory = LogFactory.getFactory();
87 log = LogFactory.getLog("TestLogger");
88 }
89
90
91
92
93 public void tearDown() {
94 log = null;
95 factory = null;
96 LogFactory.releaseAll();
97 }
98
99
100
101
102
103
104
105 public Log getLogObject()
106 {
107 return new LogKitLogger(this.getClass().getName());
108 }
109
110
111 public void testPristineFactory() {
112
113 assertNotNull("LogFactory exists", factory);
114 assertEquals("LogFactory class",
115 "org.apache.commons.logging.impl.LogFactoryImpl",
116 factory.getClass().getName());
117
118 String names[] = factory.getAttributeNames();
119 assertNotNull("Names exists", names);
120 assertEquals("Names empty", 0, names.length);
121 }
122
123
124 public void testPristineLog() {
125 checkStandard();
126 }
127
128
129 public void testSerializable() throws Exception {
130 checkStandard();
131
132
133 ByteArrayOutputStream baos = new ByteArrayOutputStream();
134 ObjectOutputStream oos = new ObjectOutputStream(baos);
135 oos.writeObject(log);
136 oos.close();
137 ByteArrayInputStream bais =
138 new ByteArrayInputStream(baos.toByteArray());
139 ObjectInputStream ois = new ObjectInputStream(bais);
140 log = (Log) ois.readObject();
141 ois.close();
142
143 checkStandard();
144 }
145
146
147
148
149
150 protected void checkStandard() {
151
152 assertNotNull("Log exists", log);
153 assertEquals("Log class",
154 "org.apache.commons.logging.impl.LogKitLogger",
155 log.getClass().getName());
156
157
158
159 assertTrue(log.isTraceEnabled());
160 assertTrue(log.isDebugEnabled());
161 assertTrue(log.isInfoEnabled());
162 assertTrue(log.isWarnEnabled());
163 assertTrue(log.isErrorEnabled());
164 assertTrue(log.isFatalEnabled());
165 }
166 }