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

Last change on this file was 18870, checked in by taylor.smock, 8 months ago

See #16567: Update to JUnit 5

This converts most tests to use @Annotations. There are also some performance
improvements as it relates to tests.

  • Property svn:eol-style set to native
File size: 24.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5
6import java.io.IOException;
7import java.net.HttpURLConnection;
8import java.net.SocketException;
9import java.net.URL;
10import java.net.UnknownHostException;
11import java.util.TimeZone;
12
13import org.junit.jupiter.api.BeforeEach;
14import org.junit.jupiter.api.Test;
15import org.openstreetmap.josm.io.ChangesetClosedException;
16import org.openstreetmap.josm.io.IllegalDataException;
17import org.openstreetmap.josm.io.MissingOAuthAccessTokenException;
18import org.openstreetmap.josm.io.OfflineAccessException;
19import org.openstreetmap.josm.io.OsmApi;
20import org.openstreetmap.josm.io.OsmApiException;
21import org.openstreetmap.josm.io.OsmApiInitializationException;
22import org.openstreetmap.josm.io.auth.CredentialsManager;
23import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
24import org.openstreetmap.josm.tools.date.DateUtils;
25import org.openstreetmap.josm.tools.date.DateUtilsTest;
26
27/**
28 * Unit tests of {@link ExceptionUtil} class.
29 */
30@BasicPreferences
31@org.openstreetmap.josm.testutils.annotations.OsmApi(org.openstreetmap.josm.testutils.annotations.OsmApi.APIType.FAKE)
32class ExceptionUtilTest {
33 private static String baseUrl;
34 private static String serverUrl;
35 private static String host;
36 private static String user;
37
38 /**
39 * Setup test.
40 * @throws Exception in case of error
41 */
42 @BeforeEach
43 public void setUp() throws Exception {
44 OsmApi api = OsmApi.getOsmApi();
45 baseUrl = api.getBaseUrl();
46 serverUrl = api.getServerUrl();
47 host = new URL(serverUrl).getHost();
48 user = CredentialsManager.getInstance().getUsername();
49 DateUtils.PROP_ISO_DATES.put(Boolean.TRUE);
50 }
51
52 /**
53 * Test of {@link ExceptionUtil#explainBadRequest} method.
54 */
55 @Test
56 void testExplainBadRequest() {
57 assertEquals("<html>The OSM server '"+baseUrl+"' reported a bad request.<br></html>",
58 ExceptionUtil.explainBadRequest(new OsmApiException("")));
59
60 assertEquals("<html>The OSM server '"+baseUrl+"' reported a bad request.<br><br>"+
61 "Error message(untranslated): header</html>",
62 ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "header", "")));
63
64 assertEquals("<html>The OSM server '"+baseUrl+"' reported a bad request.<br><br>"+
65 "Error message(untranslated): header</html>",
66 ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "header", "", "invalid_url")));
67
68 assertEquals("<html>The OSM server '"+host+"' reported a bad request.<br><br>"+
69 "Error message(untranslated): header</html>",
70 ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "header", "", baseUrl)));
71
72 assertEquals("<html>The OSM server '"+baseUrl+"' reported a bad request.<br><br>"+
73 "The area you tried to download is too big or your request was too large.<br>"+
74 "Either request a smaller area or use an export file provided by the OSM community.</html>",
75 ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "The maximum bbox", "")));
76
77 assertEquals("<html>The OSM server '"+baseUrl+"' reported a bad request.<br><br>"+
78 "The area you tried to download is too big or your request was too large.<br>"+
79 "Either request a smaller area or use an export file provided by the OSM community.</html>",
80 ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "You requested too many nodes", "")));
81 }
82
83 /**
84 * Test of {@link ExceptionUtil#explainBandwidthLimitExceeded} method.
85 */
86 @Test
87 void testExplainBandwidthLimitExceeded() {
88 assertEquals("<html>Communication with the OSM server '"+baseUrl+"'failed. "+
89 "The server replied<br>the following error code and the following error message:<br>"+
90 "<strong>Error code:<strong> 0<br><strong>Error message (untranslated)</strong>: no error message available</html>",
91 ExceptionUtil.explainBandwidthLimitExceeded(new OsmApiException("")));
92 }
93
94 /**
95 * Test of {@link ExceptionUtil#explainChangesetClosedException} method.
96 */
97 @Test
98 void testExplainChangesetClosedException() {
99 assertEquals("<html>Failed to upload to changeset <strong>0</strong><br>because it has already been closed on ?.",
100 ExceptionUtil.explainChangesetClosedException(new ChangesetClosedException("")));
101
102 assertEquals("<html>Failed to upload to changeset <strong>1</strong><br>because it has already been closed on 2016-01-01 00:00:00.",
103 ExceptionUtil.explainChangesetClosedException(new ChangesetClosedException(1, DateUtils.parseInstant("2016-01-01"), null)));
104 }
105
106 /**
107 * Test of {@link ExceptionUtil#explainClientTimeout} method.
108 */
109 @Test
110 void testExplainClientTimeout() {
111 assertEquals("<html>Communication with the OSM server '"+baseUrl+"' timed out. Please retry later.</html>",
112 ExceptionUtil.explainClientTimeout(new OsmApiException("")));
113 }
114
115 /**
116 * Test of {@link ExceptionUtil#explainConflict} method.
117 */
118 @Test
119 void testExplainConflict() {
120 int code = HttpURLConnection.HTTP_CONFLICT;
121 assertEquals("<html>The server reported that it has detected a conflict.</html>",
122 ExceptionUtil.explainConflict(new OsmApiException("")));
123 assertEquals("<html>The server reported that it has detected a conflict.<br>Error message (untranslated):<br>header</html>",
124 ExceptionUtil.explainConflict(new OsmApiException(code, "header", "")));
125 assertEquals("<html>Closing of changeset <strong>1</strong> failed <br>because it has already been closed.",
126 ExceptionUtil.explainConflict(new OsmApiException(code, "The changeset 1 was closed at xxx", "")));
127 assertEquals("<html>Closing of changeset <strong>1</strong> failed<br> because it has already been closed on 2016-01-01 12:34:56.",
128 ExceptionUtil.explainConflict(new OsmApiException(code, "The changeset 1 was closed at 2016-01-01 12:34:56 UTC", "")));
129 DateUtilsTest.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
130 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
131 assertEquals("<html>Closing of changeset <strong>1</strong> failed<br> because it has already been closed on 2016-01-01 13:34:56.",
132 ExceptionUtil.explainConflict(new OsmApiException(code, "The changeset 1 was closed at 2016-01-01 12:34:56 UTC", "")));
133 }
134
135 /**
136 * Test of {@link ExceptionUtil#explainException} method.
137 */
138 @Test
139 void testExplainException() {
140 assertEquals("ResponseCode=0",
141 ExceptionUtil.explainException(new OsmApiException("")));
142 assertEquals("java.lang.Exception: ",
143 ExceptionUtil.explainException(new Exception("")));
144 assertEquals("java.lang.Exception",
145 ExceptionUtil.explainException(new Exception(null, null)));
146 assertEquals("test",
147 ExceptionUtil.explainException(new Exception("test")));
148 }
149
150 /**
151 * Test of {@link ExceptionUtil#explainFailedAuthorisation} method.
152 */
153 @Test
154 void testExplainFailedAuthorisation() {
155 assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>"+
156 "'The server replied an error with code 0.'</html>",
157 ExceptionUtil.explainFailedAuthorisation(new OsmApiException("")));
158 assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>"+
159 "'header (Code=403)'</html>",
160 ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "header", null)));
161 assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:"+
162 "<br>'header. body (Code=403)'</html>",
163 ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "header", "body")));
164 assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:"+
165 "<br>'header_body (Code=403)'</html>",
166 ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "header_body", "header_body")));
167 assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>"+
168 "'body (Code=403)'</html>",
169 ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, null, "body")));
170 }
171
172 /**
173 * Test of {@link ExceptionUtil#explainFailedOAuthAuthorisation} method.
174 */
175 @Test
176 void testExplainFailedOAuthAuthorisation() {
177 assertEquals("<html>Authorisation at the OSM server with the OAuth token 'null' failed.<br>"+
178 "The token is not authorised to access the protected resource<br>'unknown'.<br>"+
179 "Please launch the preferences dialog and retrieve another OAuth token.</html>",
180 ExceptionUtil.explainFailedOAuthAuthorisation(new OsmApiException("")));
181 assertEquals("<html>Authorisation at the OSM server with the OAuth token 'null' failed.<br>"+
182 "The token is not authorised to access the protected resource<br>'"+baseUrl+"'.<br>"+
183 "Please launch the preferences dialog and retrieve another OAuth token.</html>",
184 ExceptionUtil.explainFailedOAuthAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "", "", baseUrl)));
185 }
186
187 /**
188 * Test of {@link ExceptionUtil#explainFailedBasicAuthentication} method.
189 */
190 @Test
191 void testExplainFailedBasicAuthentication() {
192 assertEquals("<html>Authentication at the OSM server with the username '"+user+"' failed.<br>"+
193 "Please check the username and the password in the JOSM preferences.</html>",
194 ExceptionUtil.explainFailedBasicAuthentication(new OsmApiException("")));
195 }
196
197 /**
198 * Test of {@link ExceptionUtil#explainFailedOAuthAuthentication} method.
199 */
200 @Test
201 void testExplainFailedOAuthAuthentication() {
202 assertEquals("<html>Authentication at the OSM server with the OAuth token 'null' failed.<br>"+
203 "Please launch the preferences dialog and retrieve another OAuth token.</html>",
204 ExceptionUtil.explainFailedOAuthAuthentication(new OsmApiException("")));
205 }
206
207 /**
208 * Test of {@link ExceptionUtil#explainGenericOsmApiException} method.
209 */
210 @Test
211 void testExplainGenericOsmApiException() {
212 assertEquals("<html>Communication with the OSM server '"+baseUrl+"'failed. The server replied<br>"+
213 "the following error code and the following error message:<br><strong>Error code:<strong> 0<br>"+
214 "<strong>Error message (untranslated)</strong>: no error message available</html>",
215 ExceptionUtil.explainGenericOsmApiException(new OsmApiException("")));
216
217 assertEquals("<html>Communication with the OSM server '"+baseUrl+"'failed. The server replied<br>"+
218 "the following error code and the following error message:<br><strong>Error code:<strong> 500<br>"+
219 "<strong>Error message (untranslated)</strong>: header</html>",
220 ExceptionUtil.explainGenericOsmApiException(new OsmApiException(HttpURLConnection.HTTP_INTERNAL_ERROR, "header", null)));
221
222 assertEquals("<html>Communication with the OSM server '"+baseUrl+"'failed. The server replied<br>"+
223 "the following error code and the following error message:<br><strong>Error code:<strong> 500<br>"+
224 "<strong>Error message (untranslated)</strong>: body</html>",
225 ExceptionUtil.explainGenericOsmApiException(new OsmApiException(HttpURLConnection.HTTP_INTERNAL_ERROR, null, "body")));
226 }
227
228 /**
229 * Test of {@link ExceptionUtil#explainGoneForUnknownPrimitive} method.
230 */
231 @Test
232 void testExplainGoneForUnknownPrimitive() {
233 assertEquals("<html>The server reports that an object is deleted.<br>"+
234 "<strong>Uploading failed</strong> if you tried to update or delete this object.<br> "+
235 "<strong>Downloading failed</strong> if you tried to download this object.<br><br>"+
236 "The error message is:<br>ResponseCode=0</html>",
237 ExceptionUtil.explainGoneForUnknownPrimitive(new OsmApiException("")));
238 }
239
240 /**
241 * Test of {@link ExceptionUtil#explainInternalServerError} method.
242 */
243 @Test
244 void testExplainInternalServerError() {
245 assertEquals("<html>The OSM server<br>'"+baseUrl+"'<br>reported an internal server error.<br>"+
246 "This is most likely a temporary problem. Please try again later.</html>",
247 ExceptionUtil.explainInternalServerError(new OsmApiException("")));
248 }
249
250 /**
251 * Test of {@link ExceptionUtil#explainMissingOAuthAccessTokenException} method.
252 */
253 @Test
254 void testExplainMissingOAuthAccessTokenException() {
255 assertEquals("<html>Failed to authenticate at the OSM server 'http://fake.xxx/api'.<br>"+
256 "You are using OAuth to authenticate but currently there is no<br>OAuth Access Token configured.<br>"+
257 "Please open the Preferences Dialog and generate or enter an Access Token.</html>",
258 ExceptionUtil.explainMissingOAuthAccessTokenException(new MissingOAuthAccessTokenException()));
259 }
260
261 /**
262 * Test of {@link ExceptionUtil#explainNestedIllegalDataException} method.
263 */
264 @Test
265 void testExplainNestedIllegalDataException() {
266 assertEquals("<html>Failed to download data. Its format is either unsupported, ill-formed, and/or inconsistent.<br><br>"+
267 "Details (untranslated): null</html>",
268 ExceptionUtil.explainNestedIllegalDataException(new OsmApiException("")));
269
270 assertEquals("<html>Failed to download data. Its format is either unsupported, ill-formed, and/or inconsistent.<br><br>"+
271 "Details (untranslated): test</html>",
272 ExceptionUtil.explainNestedIllegalDataException(new OsmApiException(new IllegalDataException("test"))));
273 }
274
275 /**
276 * Test of {@link ExceptionUtil#explainNestedIOException} method.
277 */
278 @Test
279 void testExplainNestedIOException() {
280 assertEquals("<html>Failed to upload data to or download data from<br>'"+baseUrl+"'<br>"+
281 "due to a problem with transferring data.<br>Details (untranslated): null</html>",
282 ExceptionUtil.explainNestedIOException(new OsmApiException("")));
283
284 assertEquals("<html>Failed to upload data to or download data from<br>'"+baseUrl+"'<br>"+
285 "due to a problem with transferring data.<br>Details (untranslated): test</html>",
286 ExceptionUtil.explainNestedIOException(new OsmApiException(new IOException("test"))));
287 }
288
289 /**
290 * Test of {@link ExceptionUtil#explainNestedSocketException} method.
291 */
292 @Test
293 void testExplainNestedSocketException() {
294 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'.<br>"+
295 "Please check your internet connection.</html>",
296 ExceptionUtil.explainNestedSocketException(new OsmApiException("")));
297 }
298
299 /**
300 * Test of {@link ExceptionUtil#explainNestedUnknownHostException} method.
301 */
302 @Test
303 void testExplainNestedUnknownHostException() {
304 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'.<br>"+
305 "Host name '"+host+"' could not be resolved. <br>"+
306 "Please check the API URL in your preferences and your internet connection.</html>",
307 ExceptionUtil.explainNestedUnknownHostException(new OsmApiException("")));
308 }
309
310 /**
311 * Test of {@link ExceptionUtil#explainNotFound} method.
312 */
313 @Test
314 void testExplainNotFound() {
315 assertEquals("<html>The OSM server '"+baseUrl+"' does not know about an object<br>"+
316 "you tried to read, update, or delete. Either the respective object<br>"+
317 "does not exist on the server or you are using an invalid URL to access<br>"+
318 "it. Please carefully check the server's address '"+baseUrl+"' for typos.</html>",
319 ExceptionUtil.explainNotFound(new OsmApiException("")));
320 }
321
322 /**
323 * Test of {@link ExceptionUtil#explainOfflineAccessException} method.
324 */
325 @Test
326 void testExplainOfflineAccessException() {
327 assertEquals("<html>Failed to download data.<br><br>Details: null</html>",
328 ExceptionUtil.explainOfflineAccessException(new OsmApiException("")));
329 assertEquals("<html>Failed to download data.<br><br>Details: test</html>",
330 ExceptionUtil.explainOfflineAccessException(new OsmApiException(new OfflineAccessException("test"))));
331 }
332
333 /**
334 * Test of {@link ExceptionUtil#explainOsmApiInitializationException} method.
335 */
336 @Test
337 void testExplainOsmApiInitializationException() {
338 assertEquals("<html>Failed to initialize communication with the OSM server "+serverUrl+".<br>"+
339 "Check the server URL in your preferences and your internet connection.</html>",
340 ExceptionUtil.explainOsmApiInitializationException(new OsmApiInitializationException("")));
341 }
342
343 /**
344 * Test of {@link ExceptionUtil#explainOsmTransferException} method.
345 */
346 @Test
347 void testExplainOsmTransferException() {
348 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'<br>"+
349 "for security reasons. This is most likely because you are running<br>"+
350 "in an applet and because you did not load your applet from '"+host+"'.</html>",
351 ExceptionUtil.explainOsmTransferException(new OsmApiException(new SecurityException("test"))));
352
353 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'.<br>"+
354 "Please check your internet connection.</html>",
355 ExceptionUtil.explainOsmTransferException(new OsmApiException(new SocketException("test"))));
356
357 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'.<br>"+
358 "Host name '"+host+"' could not be resolved. <br>"+
359 "Please check the API URL in your preferences and your internet connection.</html>",
360 ExceptionUtil.explainOsmTransferException(new OsmApiException(new UnknownHostException("test"))));
361
362 assertEquals("<html>Failed to upload data to or download data from<br>'"+baseUrl+"'<br>"+
363 "due to a problem with transferring data.<br>Details (untranslated): test</html>",
364 ExceptionUtil.explainOsmTransferException(new OsmApiException(new IOException("test"))));
365
366 assertEquals("<html>Failed to initialize communication with the OSM server "+serverUrl+".<br>"+
367 "Check the server URL in your preferences and your internet connection.</html>",
368 ExceptionUtil.explainOsmTransferException(new OsmApiInitializationException("")));
369
370 assertEquals("<html>Failed to upload to changeset <strong>0</strong><br>because it has already been closed on ?.",
371 ExceptionUtil.explainOsmTransferException(new ChangesetClosedException("")));
372
373 assertEquals("<html>Uploading to the server <strong>failed</strong> because your current<br>"+
374 "dataset violates a precondition.<br>The error message is:<br>ResponseCode=412</html>",
375 ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_PRECON_FAILED, "", "")));
376
377 assertEquals("<html>The server reports that an object is deleted.<br>"+
378 "<strong>Uploading failed</strong> if you tried to update or delete this object.<br> "+
379 "<strong>Downloading failed</strong> if you tried to download this object.<br><br>"+
380 "The error message is:<br>ResponseCode=410</html>",
381 ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_GONE, "", "")));
382
383 assertEquals("<html>The OSM server<br>'"+baseUrl+"'<br>reported an internal server error.<br>"+
384 "This is most likely a temporary problem. Please try again later.</html>",
385 ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_INTERNAL_ERROR, "", "")));
386
387 assertEquals("<html>The OSM server '"+baseUrl+"' reported a bad request.<br><br>Error message(untranslated): </html>",
388 ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "", "")));
389
390 assertEquals("<html>Communication with the OSM server '"+baseUrl+"'failed. The server replied<br>"+
391 "the following error code and the following error message:<br><strong>Error code:<strong> 509<br>"+
392 "<strong>Error message (untranslated)</strong>: </html>",
393 ExceptionUtil.explainOsmTransferException(new OsmApiException(509, "", "")));
394
395 assertEquals("ResponseCode=0",
396 ExceptionUtil.explainOsmTransferException(new OsmApiException("")));
397 }
398
399 /**
400 * Test of {@link ExceptionUtil#explainPreconditionFailed} method.
401 */
402 @Test
403 void testExplainPreconditionFailed() {
404 int code = HttpURLConnection.HTTP_PRECON_FAILED;
405 assertEquals("<html>Uploading to the server <strong>failed</strong> because your current<br>dataset violates a precondition.<br>"+
406 "The error message is:<br>ResponseCode=0</html>",
407 ExceptionUtil.explainPreconditionFailed(new OsmApiException("")));
408
409 assertEquals("<html>Uploading to the server <strong>failed</strong> because your current<br>dataset violates a precondition.<br>"+
410 "The error message is:<br>ResponseCode=412, Error Header=&lt;test&gt;</html>",
411 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "test", "")));
412
413 assertEquals("<html><strong>Failed</strong> to delete <strong>node 1</strong>. It is still referred to by relation 1.<br>"+
414 "Please load the relation, remove the reference to the node, and upload again.</html>",
415 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Node 1 is still used by relation 1", "")));
416
417 assertEquals("<html><strong>Failed</strong> to delete <strong>node 1</strong>. It is still referred to by way 1.<br>"+
418 "Please load the way, remove the reference to the node, and upload again.</html>",
419 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Node 1 is still used by way 1", "")));
420
421 assertEquals("<html><strong>Failed</strong> to delete <strong>relation 1</strong>. It is still referred to by relation 2.<br>"+
422 "Please load the relation, remove the reference to the relation, and upload again.</html>",
423 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "The relation 1 is used in relation 2", "")));
424
425 assertEquals("<html><strong>Failed</strong> to delete <strong>way 1</strong>. It is still referred to by relation 1.<br>"+
426 "Please load the relation, remove the reference to the way, and upload again.</html>",
427 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Way 1 is still used by relation 1", "")));
428
429 assertEquals("<html><strong>Failed</strong> to upload <strong>way 1</strong>. It refers to deleted nodes [1, 2].<br>"+
430 "Please load the nodes, remove the reference in the way, and upload again.</html>",
431 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Way 1 requires the nodes with id in 1,2", "")));
432 }
433}
Note: See TracBrowser for help on using the repository browser.