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

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