1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.logging.simple;
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 import org.apache.commons.logging.impl.SimpleLog;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33
34
35
36
37
38
39 public class DefaultConfigTestCase extends TestCase {
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public static Test suite() throws Exception {
54 final Class thisClass = DefaultConfigTestCase.class;
55
56 final PathableClassLoader loader = new PathableClassLoader(null);
57 loader.useExplicitLoader("junit.", Test.class.getClassLoader());
58 loader.addLogicalLib("testclasses");
59 loader.addLogicalLib("commons-logging");
60
61 final Class testClass = loader.loadClass(thisClass.getName());
62 return new PathableTestSuite(testClass, loader);
63 }
64
65
66
67
68 protected LogFactory factory;
69
70
71
72
73 protected Log log;
74
75
76 protected void checkDecorated() {
77 assertNotNull("Log exists", log);
78 assertEquals("Log class",
79 "org.apache.commons.logging.simple.DecoratedSimpleLog",
80 log.getClass().getName());
81
82
83 assertFalse(log.isDebugEnabled());
84 assertTrue(log.isErrorEnabled());
85 assertTrue(log.isFatalEnabled());
86 assertTrue(log.isInfoEnabled());
87 assertFalse(log.isTraceEnabled());
88 assertTrue(log.isWarnEnabled());
89
90
91 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
92
93
94 assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
95 ((DecoratedSimpleLog) log).getDateTimeFormat());
96 assertEquals("DecoratedLogger",
97 ((DecoratedSimpleLog) log).getLogName());
98 assertFalse(((DecoratedSimpleLog) log).getShowDateTime());
99 assertTrue(((DecoratedSimpleLog) log).getShowShortName());
100 }
101
102
103 protected void checkStandard() {
104 assertNotNull("Log exists", log);
105 assertEquals("Log class",
106 "org.apache.commons.logging.impl.SimpleLog",
107 log.getClass().getName());
108
109
110 assertFalse(log.isDebugEnabled());
111 assertTrue(log.isErrorEnabled());
112 assertTrue(log.isFatalEnabled());
113 assertTrue(log.isInfoEnabled());
114 assertFalse(log.isTraceEnabled());
115 assertTrue(log.isWarnEnabled());
116
117
118 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
119 }
120
121
122
123
124
125
126 public void setProperties() {
127 System.setProperty(
128 "org.apache.commons.logging.Log",
129 "org.apache.commons.logging.impl.SimpleLog");
130 }
131
132
133
134
135 @Override
136 public void setUp() throws Exception {
137 LogFactory.releaseAll();
138 setProperties();
139 setUpFactory();
140 setUpLog("TestLogger");
141 }
142
143
144 protected void setUpDecorated(final String name) {
145 log = new DecoratedSimpleLog(name);
146 }
147
148
149 protected void setUpFactory() throws Exception {
150 factory = LogFactory.getFactory();
151 }
152
153
154 protected void setUpLog(final String name) throws Exception {
155 log = LogFactory.getLog(name);
156 }
157
158
159
160
161 @Override
162 public void tearDown() {
163 log = null;
164 factory = null;
165 LogFactory.releaseAll();
166 }
167
168
169 public void testPristineDecorated() {
170 setUpDecorated("DecoratedLogger");
171 checkDecorated();
172 }
173
174
175 public void testPristineFactory() {
176 assertNotNull("LogFactory exists", factory);
177 assertEquals("LogFactory class",
178 "org.apache.commons.logging.impl.LogFactoryImpl",
179 factory.getClass().getName());
180
181 final String[] names = factory.getAttributeNames();
182 assertNotNull("Names exists", names);
183 assertEquals("Names empty", 0, names.length);
184 }
185
186
187 public void testPristineLog() {
188 checkStandard();
189 }
190
191
192 public void testSerializable() throws Exception {
193
194
195 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
196 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
197 oos.writeObject(log);
198 }
199 final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
200 try (ObjectInputStream ois = new ObjectInputStream(bais)) {
201 log = (Log) ois.readObject();
202 }
203
204
205 checkStandard();
206
207 }
208
209 }