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 package org.slf4j.dummyExt;
26
27 import java.util.Date;
28 import java.util.Locale;
29 import java.util.TimeZone;
30
31 import junit.framework.TestCase;
32
33 import org.apache.log4j.spi.LocationInfo;
34 import org.apache.log4j.spi.LoggingEvent;
35 import org.slf4j.MDC;
36 import org.slf4j.ext.EventData;
37 import org.slf4j.ext.EventLogger;
38
39 public class EventLoggerTest extends TestCase {
40
41 ListAppender listAppender;
42 org.apache.log4j.Logger log4;
43
44 final static String EXPECTED_FILE_NAME = "EventLoggerTest.java";
45
46 public EventLoggerTest(String name) {
47 super(name);
48 }
49
50 public void setUp() throws Exception {
51 super.setUp();
52
53
54
55 listAppender = new ListAppender();
56 listAppender.extractLocationInfo = true;
57 org.apache.log4j.Logger eventLogger =
58 org.apache.log4j.Logger.getLogger("EventLogger");
59 eventLogger.addAppender(listAppender);
60 eventLogger.setLevel(org.apache.log4j.Level.TRACE);
61 eventLogger.setAdditivity(false);
62
63 MDC.put("ipAddress", "192.168.1.110");
64 MDC.put("login", "TestUSer");
65 MDC.put("hostname", "localhost");
66 MDC.put("productName", "SLF4J");
67 MDC.put("locale", Locale.getDefault().getDisplayName());
68 MDC.put("timezone", TimeZone.getDefault().getDisplayName());
69
70 }
71
72 public void tearDown() throws Exception {
73 super.tearDown();
74 MDC.clear();
75 }
76
77 void verify(LoggingEvent le, String expectedMsg) {
78 assertEquals(expectedMsg, le.getMessage());
79 assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName());
80 }
81
82
83 public void testEventLogger() {
84 EventData data[] = new EventData[2];
85 data[0] = new EventData();
86 data[0].setEventType("Login");
87 data[0].setEventId("1");
88 data[0].setEventDateTime(new Date());
89 data[0].put("Userid", "TestUser");
90 EventLogger.logEvent(data[0]);
91
92 data[1] = new EventData();
93 data[1].setEventType("Update");
94 data[1].setEventId("2");
95 data[1].setEventDateTime(new Date());
96 data[1].put("FileName", "/etc/hosts");
97 EventLogger.logEvent(data[1]);
98
99 assertEquals(2, listAppender.list.size());
100 for (int i=0; i < 2; ++i) {
101 LoggingEvent event = listAppender.list.get(i);
102 verify(event, data[i].toXML());
103 LocationInfo li = event.getLocationInformation();
104 assertEquals(this.getClass().getName(), li.getClassName());
105 assertEquals(event.getMDC("hostname"), "localhost");
106 }
107 }
108 }