-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
CategoryTest.java
361 lines (324 loc) · 12.5 KB
/
CategoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.log4j;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.message.MapMessage;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.ObjectMessage;
import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.apache.logging.log4j.util.Constants;
import org.apache.logging.log4j.util.Strings;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Tests of Category.
*/
public class CategoryTest {
static ConfigurationFactory cf = new BasicConfigurationFactory();
private static ListAppender appender = new ListAppender("List");
@BeforeClass
public static void setupClass() {
appender.start();
ConfigurationFactory.setConfigurationFactory(cf);
LoggerContext.getContext().reconfigure();
}
@AfterClass
public static void cleanupClass() {
ConfigurationFactory.removeConfigurationFactory(cf);
appender.stop();
}
@Before
public void before() {
appender.clear();
}
@Test
public void testExist() {
assertNull(Category.exists("Does not exist for sure"));
}
/**
* Tests Category.forcedLog.
*/
@Test
@SuppressWarnings("deprecation")
public void testForcedLog() {
final MockCategory category = new MockCategory("org.example.foo");
category.setAdditivity(false);
((org.apache.logging.log4j.core.Logger) category.getLogger()).addAppender(appender);
// Logging a String
category.info("Hello, World");
List<LogEvent> list = appender.getEvents();
int events = list.size();
assertTrue("Number of events should be 1, was " + events, events == 1);
LogEvent event = list.get(0);
Message msg = event.getMessage();
assertNotNull("No message", msg);
// LOG4J2-3080: use message type consistently
assertTrue("Incorrect Message type", msg instanceof SimpleMessage);
assertEquals("Hello, World", msg.getFormat());
appender.clear();
// Logging a String map
category.info(Collections.singletonMap("hello", "world"));
list = appender.getEvents();
events = list.size();
assertTrue("Number of events should be 1, was " + events, events == 1);
event = list.get(0);
msg = event.getMessage();
assertNotNull("No message", msg);
assertTrue("Incorrect Message type", msg instanceof MapMessage);
Object[] objects = msg.getParameters();
assertEquals("world", objects[0]);
appender.clear();
// Logging a generic map
category.info(Collections.singletonMap(1234L, "world"));
list = appender.getEvents();
events = list.size();
assertTrue("Number of events should be 1, was " + events, events == 1);
event = list.get(0);
msg = event.getMessage();
assertNotNull("No message", msg);
assertTrue("Incorrect Message type", msg instanceof MapMessage);
objects = msg.getParameters();
assertEquals("world", objects[0]);
appender.clear();
// Logging an object
final Object obj = new Object();
category.info(obj);
list = appender.getEvents();
events = list.size();
assertTrue("Number of events should be 1, was " + events, events == 1);
event = list.get(0);
msg = event.getMessage();
assertNotNull("No message", msg);
assertTrue("Incorrect Message type", msg instanceof ObjectMessage);
objects = msg.getParameters();
assertEquals(obj, objects[0]);
appender.clear();
category.log(Priority.INFO, "Hello, World");
list = appender.getEvents();
events = list.size();
assertTrue("Number of events should be 1, was " + events, events == 1);
event = list.get(0);
msg = event.getMessage();
assertNotNull("No message", msg);
assertTrue("Incorrect Message type", msg instanceof SimpleMessage);
assertEquals("Hello, World", msg.getFormat());
appender.clear();
}
/**
* Tests that the return type of getChainedPriority is Priority.
*
* @throws Exception thrown if Category.getChainedPriority can not be found.
*/
@Test
public void testGetChainedPriorityReturnType() throws Exception {
final Method method = Category.class.getMethod("getChainedPriority", (Class[]) null);
assertTrue(method.getReturnType() == Priority.class);
}
/**
* Tests l7dlog(Priority, String, Throwable).
*/
@Test
public void testL7dlog() {
final Logger logger = Logger.getLogger("org.example.foo");
logger.setLevel(Level.ERROR);
final Priority debug = Level.DEBUG;
logger.l7dlog(debug, "Hello, World", null);
assertTrue(appender.getEvents().isEmpty());
}
/**
* Tests l7dlog(Priority, String, Object[], Throwable).
*/
@Test
public void testL7dlog4Param() {
final Logger logger = Logger.getLogger("org.example.foo");
logger.setLevel(Level.ERROR);
final Priority debug = Level.DEBUG;
logger.l7dlog(debug, "Hello, World", Constants.EMPTY_OBJECT_ARRAY, null);
assertTrue(appender.getEvents().isEmpty());
}
/**
* Test using a pre-existing Log4j 2 logger
*/
@Test
public void testExistingLog4j2Logger() {
// create the logger using LogManager
org.apache.logging.log4j.LogManager.getLogger("existingLogger");
// Logger will be the one created above
final Logger logger = Logger.getLogger("existingLogger");
final Logger l2 = LogManager.getLogger("existingLogger");
assertEquals(logger, l2);
logger.setLevel(Level.ERROR);
final Priority debug = Level.DEBUG;
// the next line will throw an exception if the LogManager loggers
// aren't supported by 1.2 Logger/Category
logger.l7dlog(debug, "Hello, World", Constants.EMPTY_OBJECT_ARRAY, null);
assertTrue(appender.getEvents().isEmpty());
}
/**
* Tests setPriority(Priority).
*
* @deprecated
*/
@Deprecated
@Test
public void testSetPriority() {
final Logger logger = Logger.getLogger("org.example.foo");
final Priority debug = Level.DEBUG;
logger.setPriority(debug);
}
/**
* Tests setPriority(Priority).
*
* @deprecated
*/
@Deprecated
@Test
public void testSetPriorityNull() {
Logger.getLogger("org.example.foo").setPriority(null);
}
@Test
public void testClassName() {
final Category category = Category.getInstance("TestCategory");
final Layout<String> layout = PatternLayout.newBuilder().withPattern("%d %p %C{1.} [%t] %m%n").build();
final ListAppender appender = new ListAppender("List2", null, layout, false, false);
appender.start();
category.setAdditivity(false);
((org.apache.logging.log4j.core.Logger) category.getLogger()).addAppender(appender);
category.error("Test Message");
final List<String> msgs = appender.getMessages();
assertTrue("Incorrect number of messages. Expected 1 got " + msgs.size(), msgs.size() == 1);
final String msg = msgs.get(0);
appender.clear();
final String threadName = Thread.currentThread().getName();
final String expected = "ERROR o.a.l.CategoryTest [" + threadName + "] Test Message" + Strings.LINE_SEPARATOR;
assertTrue("Incorrect message " + Strings.dquote(msg) + " expected " + Strings.dquote(expected), msg.endsWith(expected));
}
@Test
public void testStringLog() {
final String payload = "payload";
testMessageImplementation(
payload,
SimpleMessage.class,
message -> assertEquals(message.getFormattedMessage(), payload));
}
@Test
public void testCharSequenceLog() {
final CharSequence payload = new CharSequence() {
@Override
public int length() {
return 3;
}
@Override
public char charAt(final int index) {
return "abc".charAt(index);
}
@Override
public CharSequence subSequence(final int start, final int end) {
return "abc".subSequence(start, end);
}
@Override
public String toString() {
return "abc";
}
};
testMessageImplementation(
payload,
SimpleMessage.class,
message -> assertEquals(message.getFormattedMessage(), payload.toString()));
}
@Test
public void testMapLog() {
final String key = "key";
final Object value = 0xDEADBEEF;
final Map<String, Object> payload = Collections.singletonMap(key, value);
testMessageImplementation(
payload,
MapMessage.class,
message -> assertEquals(message.getData(), payload));
}
@Test
public void testObjectLog() {
final Object payload = new Object();
testMessageImplementation(
payload,
ObjectMessage.class,
message -> assertEquals(message.getParameter(), payload));
}
private <M extends Message> void testMessageImplementation(
final Object messagePayload,
final Class<M> expectedMessageClass,
final Consumer<M> messageTester) {
// Setup the logger and the appender.
final Category category = Category.getInstance("TestCategory");
final org.apache.logging.log4j.core.Logger logger =
(org.apache.logging.log4j.core.Logger) category.getLogger();
logger.addAppender(appender);
// Log the message payload.
category.info(messagePayload);
// Verify collected log events.
final List<LogEvent> events = appender.getEvents();
assertEquals("was expecting a single event", 1, events.size());
final LogEvent logEvent = events.get(0);
// Verify the collected message.
final Message message = logEvent.getMessage();
final Class<? extends Message> actualMessageClass = message.getClass();
assertTrue(
"was expecting message to be instance of " + expectedMessageClass + ", found: " + actualMessageClass,
expectedMessageClass.isAssignableFrom(actualMessageClass));
@SuppressWarnings("unchecked")
final M typedMessage = (M) message;
messageTester.accept(typedMessage);
}
/**
* Derived category to check method signature of forcedLog.
*/
private static class MockCategory extends Logger {
/**
* Create new instance of MockCategory.
*
* @param name category name
*/
public MockCategory(final String name) {
super(name);
}
/**
* Request an info level message.
*
* @param msg message
*/
public void info(final String msg) {
final Priority info = Level.INFO;
forcedLog(MockCategory.class.toString(), info, msg, null);
}
}
}