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