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