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.util.SerializationTestHelper;
27 import org.apache.log4j.Priority;
28 import org.apache.log4j.Category;
29
30
31
32
33
34
35
36 public class LoggingEventTest extends TestCase {
37
38
39
40
41
42 public LoggingEventTest(final String name) {
43 super(name);
44 }
45
46
47
48
49
50
51 public void testSerializationSimple() throws Exception {
52 Logger root = Logger.getRootLogger();
53 LoggingEvent event =
54 new LoggingEvent(
55 root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
56
57
58 int[] skip = new int[] { 352, 353, 354, 355, 356 };
59 SerializationTestHelper.assertSerializationEquals(
60 "witness/serialization/simple.bin", event, skip, 237);
61 }
62
63
64
65
66
67
68
69 public void testSerializationWithException() throws Exception {
70 Logger root = Logger.getRootLogger();
71 Exception ex = new Exception("Don't panic");
72 LoggingEvent event =
73 new LoggingEvent(
74 root.getClass().getName(), root, Level.INFO, "Hello, world.", ex);
75
76
77 int[] skip = new int[] { 352, 353, 354, 355, 356 };
78 SerializationTestHelper.assertSerializationEquals(
79 "witness/serialization/exception.bin", event, skip, 237);
80 }
81
82
83
84
85
86
87
88 public void testSerializationWithLocation() throws Exception {
89 Logger root = Logger.getRootLogger();
90 LoggingEvent event =
91 new LoggingEvent(
92 root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
93 event.getLocationInformation();
94
95
96 int[] skip = new int[] { 352, 353, 354, 355, 356 };
97 SerializationTestHelper.assertSerializationEquals(
98 "witness/serialization/location.bin", event, skip, 237);
99 }
100
101
102
103
104
105
106 public void testSerializationNDC() throws Exception {
107 Logger root = Logger.getRootLogger();
108 NDC.push("ndc test");
109
110 LoggingEvent event =
111 new LoggingEvent(
112 root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
113
114
115 int[] skip = new int[] { 352, 353, 354, 355, 356 };
116 SerializationTestHelper.assertSerializationEquals(
117 "witness/serialization/ndc.bin", event, skip, 237);
118 }
119
120
121
122
123
124
125 public void testSerializationMDC() throws Exception {
126 Logger root = Logger.getRootLogger();
127 MDC.put("mdckey", "mdcvalue");
128
129 LoggingEvent event =
130 new LoggingEvent(
131 root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
132
133
134 int[] skip = new int[] { 352, 353, 354, 355, 356 };
135 SerializationTestHelper.assertSerializationEquals(
136 "witness/serialization/mdc.bin", event, skip, 237);
137 }
138
139
140
141
142
143
144 public void testDeserializationSimple() throws Exception {
145 Object obj =
146 SerializationTestHelper.deserializeStream(
147 "witness/serialization/simple.bin");
148 assertTrue(obj instanceof LoggingEvent);
149
150 LoggingEvent event = (LoggingEvent) obj;
151 assertEquals("Hello, world.", event.getMessage());
152 assertEquals(Level.INFO, event.getLevel());
153 }
154
155
156
157
158
159
160 public void testDeserializationWithException() throws Exception {
161 Object obj =
162 SerializationTestHelper.deserializeStream(
163 "witness/serialization/exception.bin");
164 assertTrue(obj instanceof LoggingEvent);
165
166 LoggingEvent event = (LoggingEvent) obj;
167 assertEquals("Hello, world.", event.getMessage());
168 assertEquals(Level.INFO, event.getLevel());
169 }
170
171
172
173
174
175
176 public void testDeserializationWithLocation() throws Exception {
177 Object obj =
178 SerializationTestHelper.deserializeStream(
179 "witness/serialization/location.bin");
180 assertTrue(obj instanceof LoggingEvent);
181
182 LoggingEvent event = (LoggingEvent) obj;
183 assertEquals("Hello, world.", event.getMessage());
184 assertEquals(Level.INFO, event.getLevel());
185 }
186
187
188
189
190 public void testFQNOfCategoryClass() {
191 Category root = Logger.getRootLogger();
192 Priority info = Level.INFO;
193 String catName = Logger.class.toString();
194 LoggingEvent event =
195 new LoggingEvent(
196 catName, root, info, "Hello, world.", null);
197 assertEquals(catName, event.fqnOfCategoryClass);
198 }
199
200
201
202
203
204 public void testLevel() {
205 Category root = Logger.getRootLogger();
206 Priority info = Level.INFO;
207 String catName = Logger.class.toString();
208 LoggingEvent event =
209 new LoggingEvent(
210 catName, root, 0L, info, "Hello, world.", null);
211 Priority error = Level.ERROR;
212 event.level = error;
213 assertEquals(Level.ERROR, event.level);
214 }
215
216
217
218
219
220 public void testLocationInfoNoFQCN() {
221 Category root = Logger.getRootLogger();
222 Priority level = Level.INFO;
223 LoggingEvent event =
224 new LoggingEvent(
225 null, root, 0L, level, "Hello, world.", null);
226 LocationInfo info = event.getLocationInformation();
227
228
229
230
231 assertNotNull(info);
232 if (info != null) {
233 assertEquals("?", info.getLineNumber());
234 assertEquals("?", info.getClassName());
235 assertEquals("?", info.getFileName());
236 assertEquals("?", info.getMethodName());
237 }
238 }
239
240
241
242
243
244 private static class BadMessage {
245 public BadMessage() {
246 }
247
248 public String toString() {
249 throw new RuntimeException();
250 }
251 }
252
253
254
255
256
257
258 public void testBadMessage() {
259 Category root = Logger.getRootLogger();
260 Priority info = Level.INFO;
261 String catName = Logger.class.toString();
262 BadMessage msg = new BadMessage();
263 LoggingEvent event =
264 new LoggingEvent(
265 catName, root, 0L, info, msg, null);
266
267 event.getRenderedMessage();
268 }
269
270
271 }