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 junit.framework.TestCase;
28
29 import org.apache.log4j.spi.LocationInfo;
30 import org.apache.log4j.spi.LoggingEvent;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 public class XLoggerTest extends TestCase {
35
36 ListAppender listAppender;
37 org.apache.log4j.Logger log4jRoot;
38
39 final static String EXPECTED_FILE_NAME = "XLoggerTest.java";
40
41 public XLoggerTest(String name) {
42 super(name);
43 }
44
45 public void setUp() throws Exception {
46 super.setUp();
47
48
49
50 listAppender = new ListAppender();
51 listAppender.extractLocationInfo = true;
52 log4jRoot = org.apache.log4j.Logger.getRootLogger();
53 log4jRoot.addAppender(listAppender);
54 log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
55 }
56
57 public void tearDown() throws Exception {
58 super.tearDown();
59 }
60
61 void verify(LoggingEvent le, String expectedMsg) {
62 assertEquals(expectedMsg, le.getMessage());
63 assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName());
64 }
65
66 void verifyWithException(LoggingEvent le, String expectedMsg, Throwable t) {
67 verify(le, expectedMsg);
68 assertEquals(t.toString(), le.getThrowableStrRep()[0]);
69 }
70
71 void verifyWithLevelAndException(LoggingEvent le, XLogger.Level level, String expectedMsg, Throwable t) {
72 verify(le, expectedMsg);
73 assertEquals(t.toString(), le.getThrowableStrRep()[0]);
74 assertEquals(le.getLevel().toString(), level.toString());
75 }
76
77 public void testEntering() {
78 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
79 logger.entry();
80 logger.entry(1);
81 logger.entry("test");
82 logger.entry("a", "b", "c", "d");
83 logger.entry("a", "b", "c", "d", "e");
84 logger.entry("a", "b", "c", "d", "e", "f");
85
86 assertEquals(6, listAppender.list.size());
87 verify((LoggingEvent) listAppender.list.get(0), "entry");
88 verify((LoggingEvent) listAppender.list.get(1), "entry with (1)");
89 verify((LoggingEvent) listAppender.list.get(2), "entry with (test)");
90 }
91
92 public void testExiting() {
93 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
94 logger.exit();
95 assertEquals(Integer.valueOf(0), logger.exit(0));
96 assertEquals(Boolean.FALSE, logger.exit(false));
97
98 assertEquals(3, listAppender.list.size());
99 verify((LoggingEvent) listAppender.list.get(0), "exit");
100 verify((LoggingEvent) listAppender.list.get(1), "exit with (0)");
101 verify((LoggingEvent) listAppender.list.get(2), "exit with (false)");
102 }
103
104 public void testThrowing() {
105 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
106 Throwable t = new UnsupportedOperationException("Test");
107 assertEquals(t, logger.throwing(t));
108 assertEquals(t, logger.throwing(XLogger.Level.DEBUG,t));
109 assertEquals(2, listAppender.list.size());
110 verifyWithException((LoggingEvent) listAppender.list.get(0), "throwing", t);
111 LoggingEvent event = (LoggingEvent)listAppender.list.get(1);
112 verifyWithLevelAndException(event, XLogger.Level.DEBUG,
113 "throwing", t);
114 }
115
116 public void testCaught() {
117 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
118 long x = 5;
119 Throwable t = null;
120 try {
121 @SuppressWarnings("unused")
122 long y = x / 0;
123 } catch (Exception ex) {
124 t = ex;
125 logger.catching(ex);
126 logger.catching(XLogger.Level.DEBUG, ex);
127 }
128 verifyWithException((LoggingEvent) listAppender.list.get(0), "catching", t);
129 verifyWithLevelAndException((LoggingEvent) listAppender.list.get(1), XLogger.Level.DEBUG,
130 "catching", t);
131 }
132
133
134 public void testLocationExtraction_Bug114() {
135 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
136 int line = 137;
137 logger.exit();
138 logger.debug("hello");
139
140 assertEquals(2, listAppender.list.size());
141
142 {
143 LoggingEvent e = listAppender.list.get(0);
144 LocationInfo li = e.getLocationInformation();
145 assertEquals(this.getClass().getName(), li.getClassName());
146 assertEquals(""+line, li.getLineNumber());
147 }
148
149 {
150 LoggingEvent e = listAppender.list.get(1);
151 LocationInfo li = e.getLocationInformation();
152 assertEquals(this.getClass().getName(), li.getClassName());
153 assertEquals(""+(line+1), li.getLineNumber());
154 }
155
156 }
157 }