1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.spi;
19
20 import junit.framework.TestCase;
21
22 import org.apache.log4j.Level;
23 import org.apache.log4j.Logger;
24 import org.apache.log4j.MDC;
25 import org.apache.log4j.NDC;
26 import org.apache.log4j.spi.LocationInfo;
27 import org.apache.log4j.util.SerializationTestHelper;
28 import org.apache.log4j.Priority;
29 import org.apache.log4j.Category;
30
31
32 /***
33 * Tests LoggingEvent.
34 *
35 * @author Curt Arnold
36 */
37 public class LoggingEventTest extends TestCase {
38 /***
39 * Create LoggingEventTest.
40 *
41 * @param name test name.
42 */
43 public LoggingEventTest(final String name) {
44 super(name);
45 }
46
47 /***
48 * Serialize a simple logging event and check it against
49 * a witness.
50 * @throws Exception if exception during test.
51 */
52 public void testSerializationSimple() throws Exception {
53 Logger root = Logger.getRootLogger();
54 LoggingEvent event =
55 new LoggingEvent(
56 root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
57
58
59 int[] skip = new int[] { 352, 353, 354, 355, 356 };
60 SerializationTestHelper.assertSerializationEquals(
61 "witness/serialization/simple.bin", event, skip, 237);
62 }
63
64 /***
65 * Serialize a logging event with an exception and check it against
66 * a witness.
67 * @throws Exception if exception during test.
68 *
69 */
70 public void testSerializationWithException() throws Exception {
71 Logger root = Logger.getRootLogger();
72 Exception ex = new Exception("Don't panic");
73 LoggingEvent event =
74 new LoggingEvent(
75 root.getClass().getName(), root, Level.INFO, "Hello, world.", ex);
76
77
78 int[] skip = new int[] { 352, 353, 354, 355, 356 };
79 SerializationTestHelper.assertSerializationEquals(
80 "witness/serialization/exception.bin", event, skip, 237);
81 }
82
83 /***
84 * Serialize a logging event with an exception and check it against
85 * a witness.
86 * @throws Exception if exception during test.
87 *
88 */
89 public void testSerializationWithLocation() throws Exception {
90 Logger root = Logger.getRootLogger();
91 LoggingEvent event =
92 new LoggingEvent(
93 root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
94 LocationInfo info = event.getLocationInformation();
95
96
97 int[] skip = new int[] { 352, 353, 354, 355, 356 };
98 SerializationTestHelper.assertSerializationEquals(
99 "witness/serialization/location.bin", event, skip, 237);
100 }
101
102 /***
103 * Serialize a logging event with ndc.
104 * @throws Exception if exception during test.
105 *
106 */
107 public void testSerializationNDC() throws Exception {
108 Logger root = Logger.getRootLogger();
109 NDC.push("ndc test");
110
111 LoggingEvent event =
112 new LoggingEvent(
113 root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
114
115
116 int[] skip = new int[] { 352, 353, 354, 355, 356 };
117 SerializationTestHelper.assertSerializationEquals(
118 "witness/serialization/ndc.bin", event, skip, 237);
119 }
120
121 /***
122 * Serialize a logging event with mdc.
123 * @throws Exception if exception during test.
124 *
125 */
126 public void testSerializationMDC() throws Exception {
127 Logger root = Logger.getRootLogger();
128 MDC.put("mdckey", "mdcvalue");
129
130 LoggingEvent event =
131 new LoggingEvent(
132 root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
133
134
135 int[] skip = new int[] { 352, 353, 354, 355, 356 };
136 SerializationTestHelper.assertSerializationEquals(
137 "witness/serialization/mdc.bin", event, skip, 237);
138 }
139
140 /***
141 * Deserialize a simple logging event.
142 * @throws Exception if exception during test.
143 *
144 */
145 public void testDeserializationSimple() throws Exception {
146 Object obj =
147 SerializationTestHelper.deserializeStream(
148 "witness/serialization/simple.bin");
149 assertTrue(obj instanceof LoggingEvent);
150
151 LoggingEvent event = (LoggingEvent) obj;
152 assertEquals("Hello, world.", event.getMessage());
153 assertEquals(Level.INFO, event.getLevel());
154 }
155
156 /***
157 * Deserialize a logging event with an exception.
158 * @throws Exception if exception during test.
159 *
160 */
161 public void testDeserializationWithException() throws Exception {
162 Object obj =
163 SerializationTestHelper.deserializeStream(
164 "witness/serialization/exception.bin");
165 assertTrue(obj instanceof LoggingEvent);
166
167 LoggingEvent event = (LoggingEvent) obj;
168 assertEquals("Hello, world.", event.getMessage());
169 assertEquals(Level.INFO, event.getLevel());
170 }
171
172 /***
173 * Deserialize a logging event with an exception.
174 * @throws Exception if exception during test.
175 *
176 */
177 public void testDeserializationWithLocation() throws Exception {
178 Object obj =
179 SerializationTestHelper.deserializeStream(
180 "witness/serialization/location.bin");
181 assertTrue(obj instanceof LoggingEvent);
182
183 LoggingEvent event = (LoggingEvent) obj;
184 assertEquals("Hello, world.", event.getMessage());
185 assertEquals(Level.INFO, event.getLevel());
186 }
187
188 /***
189 * Tests LoggingEvent.fqnOfCategoryClass.
190 */
191 public void testFQNOfCategoryClass() {
192 Category root = Logger.getRootLogger();
193 Priority info = Level.INFO;
194 String catName = Logger.class.toString();
195 LoggingEvent event =
196 new LoggingEvent(
197 catName, root, info, "Hello, world.", null);
198 assertEquals(catName, event.fqnOfCategoryClass);
199 }
200
201 /***
202 * Tests LoggingEvent.level.
203 * @deprecated
204 */
205 public void testLevel() {
206 Category root = Logger.getRootLogger();
207 Priority info = Level.INFO;
208 String catName = Logger.class.toString();
209 LoggingEvent event =
210 new LoggingEvent(
211 catName, root, 0L, info, "Hello, world.", null);
212 Priority error = Level.ERROR;
213 event.level = error;
214 assertEquals(Level.ERROR, event.level);
215 }
216
217 /***
218 * Tests LoggingEvent.getLocationInfo() when no FQCN is specified.
219 * See bug 41186.
220 */
221 public void testLocationInfoNoFQCN() {
222 Category root = Logger.getRootLogger();
223 Priority level = Level.INFO;
224 LoggingEvent event =
225 new LoggingEvent(
226 null, root, 0L, level, "Hello, world.", null);
227 LocationInfo info = event.getLocationInformation();
228
229
230
231
232 assertNotNull(info);
233 if (info != null) {
234 assertEquals("?", info.getLineNumber());
235 assertEquals("?", info.getClassName());
236 assertEquals("?", info.getFileName());
237 assertEquals("?", info.getMethodName());
238 }
239 }
240
241
242 }