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 public void testEntering() {
72 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
73 logger.entry();
74 logger.entry(1);
75 logger.entry("test");
76
77 assertEquals(3, listAppender.list.size());
78 verify((LoggingEvent) listAppender.list.get(0), "entry");
79 verify((LoggingEvent) listAppender.list.get(1), "entry with (1)");
80 verify((LoggingEvent) listAppender.list.get(2), "entry with (test)");
81 }
82
83 public void testExiting() {
84 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
85 logger.exit();
86 logger.exit(0);
87 logger.exit(false);
88
89 assertEquals(3, listAppender.list.size());
90 verify((LoggingEvent) listAppender.list.get(0), "exit");
91 verify((LoggingEvent) listAppender.list.get(1), "exit with (0)");
92 verify((LoggingEvent) listAppender.list.get(2), "exit with (false)");
93 }
94
95 public void testThrowing() {
96 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
97 Throwable t = new UnsupportedOperationException("Test");
98 logger.throwing(t);
99 assertEquals(1, listAppender.list.size());
100 verifyWithException((LoggingEvent) listAppender.list.get(0), "throwing", t);
101 }
102
103 public void testCaught() {
104 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
105 long x = 5;
106 Throwable t = null;
107 try {
108 @SuppressWarnings("unused")
109 long y = x / 0;
110 } catch (Exception ex) {
111 t = ex;
112 logger.catching(ex);
113 }
114 verifyWithException((LoggingEvent) listAppender.list.get(0), "catching", t);
115 }
116
117
118 public void testLocationExtraction_Bug114() {
119 XLogger logger = XLoggerFactory.getXLogger("UnitTest");
120 int line = 121;
121 logger.exit();
122 logger.debug("hello");
123
124 assertEquals(2, listAppender.list.size());
125
126 {
127 LoggingEvent e = listAppender.list.get(0);
128 LocationInfo li = e.getLocationInformation();
129 assertEquals(this.getClass().getName(), li.getClassName());
130 assertEquals(""+line, li.getLineNumber());
131 }
132
133 {
134 LoggingEvent e = listAppender.list.get(1);
135 LocationInfo li = e.getLocationInformation();
136 assertEquals(this.getClass().getName(), li.getClassName());
137 assertEquals(""+(line+1), li.getLineNumber());
138 }
139
140 }
141 }