source: josm/trunk/test/unit/org/openstreetmap/josm/tools/LoggingTest.java@ 11324

Last change on this file since 11324 was 11324, checked in by Don-vip, 7 years ago

findbugs

  • Property svn:eol-style set to native
File size: 10.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.Assert.assertArrayEquals;
5import static org.junit.Assert.assertEquals;
6import static org.junit.Assert.assertFalse;
7import static org.junit.Assert.assertNotNull;
8import static org.junit.Assert.assertNull;
9import static org.junit.Assert.assertTrue;
10
11import java.io.IOException;
12import java.util.function.Consumer;
13import java.util.logging.Handler;
14import java.util.logging.Level;
15import java.util.logging.LogRecord;
16
17import org.junit.After;
18import org.junit.Before;
19import org.junit.Test;
20
21import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22
23/**
24 * @author michael
25 *
26 */
27public class LoggingTest {
28
29 private LogRecord captured;
30 private final Handler handler = new Handler() {
31
32 @Override
33 public void publish(LogRecord record) {
34 captured = record;
35 }
36
37 @Override
38 public void flush() {
39 }
40
41 @Override
42 public void close() throws SecurityException {
43 }
44 };
45
46 /**
47 * @throws java.lang.Exception
48 */
49 @Before
50 public void setUp() throws Exception {
51 captured = null;
52 Logging.getLogger().addHandler(handler);
53 }
54
55 /**
56 * @throws java.lang.Exception
57 */
58 @After
59 public void tearDown() throws Exception {
60 Logging.getLogger().removeHandler(handler);
61 }
62
63 /**
64 * Test method for {@link org.openstreetmap.josm.tools.Logging#setLogLevel(java.util.logging.Level)}.
65 */
66 @Test
67 public void testSetLogLevel() {
68 Logging.setLogLevel(Logging.LEVEL_DEBUG);
69 assertEquals(Logging.LEVEL_DEBUG, Logging.getLogger().getLevel());
70 Logging.setLogLevel(Logging.LEVEL_WARN);
71 assertEquals(Logging.LEVEL_WARN, Logging.getLogger().getLevel());
72 }
73
74 private void testLogCaptured(Level level, String expected, Runnable printMessage) {
75 testLogCaptured(level, result -> assertEquals(expected, result), printMessage);
76 }
77
78 @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION")
79 private void testLogCaptured(Level level, Consumer<String> expectedTester, Runnable printMessage) {
80 Logging.setLogLevel(level);
81 captured = null;
82 printMessage.run();
83
84 assertNotNull(captured);
85 expectedTester.accept(captured.getMessage());
86 assertEquals(level, captured.getLevel());
87
88 captured = null;
89 Logging.setLogLevel(Level.OFF);
90 printMessage.run();
91 assertNull(captured);
92 }
93
94 /**
95 * Test method for {@link org.openstreetmap.josm.tools.Logging#error(java.lang.String)}.
96 */
97 @Test
98 public void testErrorString() {
99 testLogCaptured(Logging.LEVEL_ERROR, "test", () -> Logging.error("test"));
100 }
101
102 /**
103 * Test method for {@link org.openstreetmap.josm.tools.Logging#error(java.lang.String, java.lang.Object[])}.
104 */
105 @Test
106 public void testErrorStringObjectArray() {
107 testLogCaptured(Logging.LEVEL_ERROR, "test x 1", () -> Logging.error("test {0} {1}", "x", 1));
108 }
109
110 /**
111 * Test method for {@link org.openstreetmap.josm.tools.Logging#warn(java.lang.String)}.
112 */
113 @Test
114 public void testWarnString() {
115 testLogCaptured(Logging.LEVEL_WARN, "test", () -> Logging.warn("test"));
116 }
117
118 /**
119 * Test method for {@link org.openstreetmap.josm.tools.Logging#warn(java.lang.String, java.lang.Object[])}.
120 */
121 @Test
122 public void testWarnStringObjectArray() {
123 testLogCaptured(Logging.LEVEL_WARN, "test x 1", () -> Logging.warn("test {0} {1}", "x", 1));
124 }
125
126 /**
127 * Test method for {@link org.openstreetmap.josm.tools.Logging#info(java.lang.String)}.
128 */
129 @Test
130 public void testInfoString() {
131 testLogCaptured(Logging.LEVEL_INFO, "test", () -> Logging.info("test"));
132 }
133
134 /**
135 * Test method for {@link org.openstreetmap.josm.tools.Logging#info(java.lang.String, java.lang.Object[])}.
136 */
137 @Test
138 public void testInfoStringObjectArray() {
139 testLogCaptured(Logging.LEVEL_INFO, "test x 1", () -> Logging.info("test {0} {1}", "x", 1));
140 }
141
142 /**
143 * Test method for {@link org.openstreetmap.josm.tools.Logging#debug(java.lang.String)}.
144 */
145 @Test
146 public void testDebugString() {
147 testLogCaptured(Logging.LEVEL_DEBUG, "test", () -> Logging.debug("test"));
148 }
149
150 /**
151 * Test method for {@link org.openstreetmap.josm.tools.Logging#debug(java.lang.String, java.lang.Object[])}.
152 */
153 @Test
154 public void testDebugStringObjectArray() {
155 testLogCaptured(Logging.LEVEL_DEBUG, "test x 1", () -> Logging.debug("test {0} {1}", "x", 1));
156 }
157
158 /**
159 * Test method for {@link org.openstreetmap.josm.tools.Logging#trace(java.lang.String)}.
160 */
161 @Test
162 public void testTraceString() {
163 testLogCaptured(Logging.LEVEL_TRACE, "test", () -> Logging.trace("test"));
164 }
165
166 /**
167 * Test method for {@link org.openstreetmap.josm.tools.Logging#trace(java.lang.String, java.lang.Object[])}.
168 */
169 @Test
170 public void testTraceStringObjectArray() {
171 testLogCaptured(Logging.LEVEL_TRACE, "test x 1", () -> Logging.trace("test {0} {1}", "x", 1));
172 }
173
174 /**
175 * Test method for {@link org.openstreetmap.josm.tools.Logging#log(java.util.logging.Level, java.lang.Throwable)}.
176 */
177 @Test
178 public void testLogLevelThrowable() {
179 testLogCaptured(Logging.LEVEL_ERROR, "java.io.IOException: x", () -> Logging.log(Logging.LEVEL_ERROR, new IOException("x")));
180
181 testLogCaptured(Logging.LEVEL_TRACE, "java.io.IOException: x", () -> Logging.log(Logging.LEVEL_TRACE, new IOException("x")));
182 }
183
184 /**
185 * Test method for {@link org.openstreetmap.josm.tools.Logging#log(java.util.logging.Level, java.lang.String, java.lang.Throwable)}.
186 */
187 @Test
188 public void testLogLevelStringThrowable() {
189 testLogCaptured(Logging.LEVEL_ERROR, "y: java.io.IOException: x", () -> Logging.log(Logging.LEVEL_ERROR, "y", new IOException("x")));
190
191 testLogCaptured(Logging.LEVEL_TRACE, "y: java.io.IOException: x", () -> Logging.log(Logging.LEVEL_TRACE, "y", new IOException("x")));
192 }
193
194 /**
195 * Test method for {@link org.openstreetmap.josm.tools.Logging#logWithStackTrace(java.util.logging.Level, java.lang.Throwable)}.
196 */
197 @Test
198 public void testLogWithStackTraceLevelThrowable() {
199 Consumer<String> test = string -> {
200 assertTrue(string.startsWith("java.io.IOException: x"));
201 assertTrue(string.indexOf("testLogWithStackTraceLevelThrowable") >= 0);
202 };
203 testLogCaptured(Logging.LEVEL_ERROR, test, () -> Logging.logWithStackTrace(Logging.LEVEL_ERROR, new IOException("x")));
204 testLogCaptured(Logging.LEVEL_TRACE, test, () -> Logging.logWithStackTrace(Logging.LEVEL_TRACE, new IOException("x")));
205
206 testLogCaptured(Logging.LEVEL_TRACE, string -> assertTrue(string.startsWith("java.io.IOException\n")),
207 () -> Logging.logWithStackTrace(Logging.LEVEL_TRACE, new IOException()));
208
209 testLogCaptured(Logging.LEVEL_TRACE, string -> assertTrue(string.indexOf("Cause:") >= 0),
210 () -> Logging.logWithStackTrace(Logging.LEVEL_TRACE, new IOException(new IOException())));
211
212 }
213
214 /**
215 * Test method for {@link org.openstreetmap.josm.tools.Logging#logWithStackTrace(java.util.logging.Level, java.lang.String, java.lang.Throwable)}.
216 */
217 @Test
218 public void testLogWithStackTraceLevelStringThrowable() {
219 Consumer<String> test = string -> {
220 assertTrue(string.startsWith("y: java.io.IOException: x"));
221 assertTrue(string.indexOf("testLogWithStackTraceLevelStringThrowable") > 0);
222 };
223 testLogCaptured(Logging.LEVEL_ERROR, test, () -> Logging.logWithStackTrace(Logging.LEVEL_ERROR, "y", new IOException("x")));
224 testLogCaptured(Logging.LEVEL_TRACE, test, () -> Logging.logWithStackTrace(Logging.LEVEL_TRACE, "y", new IOException("x")));
225 }
226
227 /**
228 * Test method for {@link org.openstreetmap.josm.tools.Logging#isLoggingEnabled(java.util.logging.Level)}.
229 */
230 @Test
231 public void testIsLoggingEnabled() {
232 Logging.setLogLevel(Logging.LEVEL_ERROR);
233 assertTrue(Logging.isLoggingEnabled(Logging.LEVEL_ERROR));
234 assertFalse(Logging.isLoggingEnabled(Logging.LEVEL_INFO));
235 assertFalse(Logging.isLoggingEnabled(Logging.LEVEL_TRACE));
236 Logging.setLogLevel(Logging.LEVEL_INFO);
237 assertTrue(Logging.isLoggingEnabled(Logging.LEVEL_ERROR));
238 assertTrue(Logging.isLoggingEnabled(Logging.LEVEL_INFO));
239 assertFalse(Logging.isLoggingEnabled(Logging.LEVEL_TRACE));
240 Logging.setLogLevel(Logging.LEVEL_TRACE);
241 assertTrue(Logging.isLoggingEnabled(Logging.LEVEL_ERROR));
242 assertTrue(Logging.isLoggingEnabled(Logging.LEVEL_INFO));
243 assertTrue(Logging.isLoggingEnabled(Logging.LEVEL_TRACE));
244 }
245
246 /**
247 * Test method for {@link org.openstreetmap.josm.tools.Logging#clearLastErrorAndWarnings()}.
248 */
249 @Test
250 public void testClearLastErrorAndWarnings() {
251 Logging.setLogLevel(Logging.LEVEL_WARN);
252 Logging.clearLastErrorAndWarnings();
253 Logging.error("x");
254 assertFalse(Logging.getLastErrorAndWarnings().isEmpty());
255 assertFalse(Logging.getLastErrorAndWarnings().isEmpty());
256 Logging.clearLastErrorAndWarnings();
257 assertTrue(Logging.getLastErrorAndWarnings().isEmpty());
258 }
259
260 /**
261 * Test method for {@link org.openstreetmap.josm.tools.Logging#getLastErrorAndWarnings()}.
262 */
263 @Test
264 public void testGetLastErrorAndWarnings() {
265 Logging.setLogLevel(Logging.LEVEL_WARN);
266 Logging.clearLastErrorAndWarnings();
267 Logging.warn("x");
268
269 assertEquals(1, Logging.getLastErrorAndWarnings().size());
270 assertEquals("W: x", Logging.getLastErrorAndWarnings().get(0));
271
272 Logging.setLogLevel(Logging.LEVEL_ERROR);
273 Logging.warn("x");
274
275 assertEquals(1, Logging.getLastErrorAndWarnings().size());
276
277 Logging.error("y\nz");
278
279 assertEquals(2, Logging.getLastErrorAndWarnings().size());
280 assertArrayEquals(new Object[] {"W: x", "E: y"}, Logging.getLastErrorAndWarnings().toArray());
281
282 // limit somewhere reasonable
283 for (int i = 3; i < 6; i++) {
284 Logging.error("x");
285 assertEquals(i, Logging.getLastErrorAndWarnings().size());
286 }
287 for (int i = 2; i < 100; i++) {
288 Logging.error("x");
289 }
290 assertTrue(Logging.getLastErrorAndWarnings().size() < 101);
291 }
292}
Note: See TracBrowser for help on using the repository browser.