1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.logging.jdk14;
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.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.commons.logging.PathableClassLoader;
28 import org.apache.commons.logging.PathableTestSuite;
29
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32
33
34
35
36
37
38 public class DefaultConfigTestCase extends TestCase {
39
40
41
42
43 public static Test suite() throws Exception {
44 final PathableClassLoader loader = new PathableClassLoader(null);
45 loader.useExplicitLoader("junit.", Test.class.getClassLoader());
46 loader.addLogicalLib("testclasses");
47 loader.addLogicalLib("commons-logging");
48
49 final Class testClass = loader.loadClass(DefaultConfigTestCase.class.getName());
50 return new PathableTestSuite(testClass, loader);
51 }
52
53
54
55
56 protected LogFactory factory;
57
58
59
60
61 protected Log log;
62
63
64
65
66
67
68 public DefaultConfigTestCase(final String name) {
69 super(name);
70 }
71
72
73 protected void checkLog() {
74
75 assertNotNull("Log exists", log);
76 assertEquals("Log class",
77 "org.apache.commons.logging.impl.Jdk14Logger",
78 log.getClass().getName());
79
80
81 log.isDebugEnabled();
82 log.isErrorEnabled();
83 log.isFatalEnabled();
84 log.isInfoEnabled();
85 log.isTraceEnabled();
86 log.isWarnEnabled();
87
88 }
89
90
91
92
93 @Override
94 public void setUp() throws Exception {
95 setUpFactory();
96 setUpLog("TestLogger");
97 }
98
99
100 protected void setUpFactory() throws Exception {
101 factory = LogFactory.getFactory();
102 }
103
104
105 protected void setUpLog(final String name) throws Exception {
106 log = LogFactory.getLog(name);
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 assertNotNull("LogFactory exists", factory);
122 assertEquals("LogFactory class",
123 "org.apache.commons.logging.impl.LogFactoryImpl",
124 factory.getClass().getName());
125
126 final String[] names = factory.getAttributeNames();
127 assertNotNull("Names exists", names);
128 assertEquals("Names empty", 0, names.length);
129 }
130
131
132 public void testPristineLog() {
133 checkLog();
134 }
135
136
137 public void testSerializable() throws Exception {
138
139
140 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
141 final ObjectOutputStream oos = new ObjectOutputStream(baos);
142 oos.writeObject(log);
143 oos.close();
144 final ByteArrayInputStream bais =
145 new ByteArrayInputStream(baos.toByteArray());
146 final ObjectInputStream ois = new ObjectInputStream(bais);
147 log = (Log) ois.readObject();
148 ois.close();
149
150
151 checkLog();
152
153 }
154
155 }