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