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

Last change on this file was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

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