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

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

see #17722 - proper notification of user block

  • Property svn:eol-style set to native
File size: 23.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.Assert.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.Before;
14import org.junit.Rule;
15import org.junit.Test;
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 */
33public class ExceptionUtilTest {
34
35 /**
36 * Setup rule.
37 */
38 @Rule
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 @Before
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 public 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 public 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 public 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 public 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 public 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 public 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 public 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>'header (Code=403)'</html>",
168 ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "header", null)));
169 assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>'header. body (Code=403)'</html>",
170 ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "header", "body")));
171 assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>'body (Code=403)'</html>",
172 ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, null, "body")));
173 }
174
175 /**
176 * Test of {@link ExceptionUtil#explainFailedOAuthAuthorisation} method.
177 */
178 @Test
179 public void testExplainFailedOAuthAuthorisation() {
180 assertEquals("<html>Authorisation at the OSM server with the OAuth token 'null' failed.<br>"+
181 "The token is not authorised to access the protected resource<br>'unknown'.<br>"+
182 "Please launch the preferences dialog and retrieve another OAuth token.</html>",
183 ExceptionUtil.explainFailedOAuthAuthorisation(new OsmApiException("")));
184 assertEquals("<html>Authorisation at the OSM server with the OAuth token 'null' failed.<br>"+
185 "The token is not authorised to access the protected resource<br>'"+baseUrl+"'.<br>"+
186 "Please launch the preferences dialog and retrieve another OAuth token.</html>",
187 ExceptionUtil.explainFailedOAuthAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "", "", baseUrl)));
188 }
189
190 /**
191 * Test of {@link ExceptionUtil#explainFailedBasicAuthentication} method.
192 */
193 @Test
194 public void testExplainFailedBasicAuthentication() {
195 assertEquals("<html>Authentication at the OSM server with the username '"+user+"' failed.<br>"+
196 "Please check the username and the password in the JOSM preferences.</html>",
197 ExceptionUtil.explainFailedBasicAuthentication(new OsmApiException("")));
198 }
199
200 /**
201 * Test of {@link ExceptionUtil#explainFailedOAuthAuthentication} method.
202 */
203 @Test
204 public void testExplainFailedOAuthAuthentication() {
205 assertEquals("<html>Authentication at the OSM server with the OAuth token 'null' failed.<br>"+
206 "Please launch the preferences dialog and retrieve another OAuth token.</html>",
207 ExceptionUtil.explainFailedOAuthAuthentication(new OsmApiException("")));
208 }
209
210 /**
211 * Test of {@link ExceptionUtil#explainGenericOsmApiException} method.
212 */
213 @Test
214 public void testExplainGenericOsmApiException() {
215 assertEquals("<html>Communication with the OSM server '"+baseUrl+"'failed. The server replied<br>"+
216 "the following error code and the following error message:<br><strong>Error code:<strong> 0<br>"+
217 "<strong>Error message (untranslated)</strong>: no error message available</html>",
218 ExceptionUtil.explainGenericOsmApiException(new OsmApiException("")));
219
220 assertEquals("<html>Communication with the OSM server '"+baseUrl+"'failed. The server replied<br>"+
221 "the following error code and the following error message:<br><strong>Error code:<strong> 500<br>"+
222 "<strong>Error message (untranslated)</strong>: header</html>",
223 ExceptionUtil.explainGenericOsmApiException(new OsmApiException(HttpURLConnection.HTTP_INTERNAL_ERROR, "header", null)));
224
225 assertEquals("<html>Communication with the OSM server '"+baseUrl+"'failed. The server replied<br>"+
226 "the following error code and the following error message:<br><strong>Error code:<strong> 500<br>"+
227 "<strong>Error message (untranslated)</strong>: body</html>",
228 ExceptionUtil.explainGenericOsmApiException(new OsmApiException(HttpURLConnection.HTTP_INTERNAL_ERROR, null, "body")));
229 }
230
231 /**
232 * Test of {@link ExceptionUtil#explainGoneForUnknownPrimitive} method.
233 */
234 @Test
235 public void testExplainGoneForUnknownPrimitive() {
236 assertEquals("<html>The server reports that an object is deleted.<br>"+
237 "<strong>Uploading failed</strong> if you tried to update or delete this object.<br> "+
238 "<strong>Downloading failed</strong> if you tried to download this object.<br><br>"+
239 "The error message is:<br>ResponseCode=0</html>",
240 ExceptionUtil.explainGoneForUnknownPrimitive(new OsmApiException("")));
241 }
242
243 /**
244 * Test of {@link ExceptionUtil#explainInternalServerError} method.
245 */
246 @Test
247 public void testExplainInternalServerError() {
248 assertEquals("<html>The OSM server<br>'"+baseUrl+"'<br>reported an internal server error.<br>"+
249 "This is most likely a temporary problem. Please try again later.</html>",
250 ExceptionUtil.explainInternalServerError(new OsmApiException("")));
251 }
252
253 /**
254 * Test of {@link ExceptionUtil#explainMissingOAuthAccessTokenException} method.
255 */
256 @Test
257 public void testExplainMissingOAuthAccessTokenException() {
258 assertEquals("<html>Failed to authenticate at the OSM server 'http://fake.xxx/api'.<br>"+
259 "You are using OAuth to authenticate but currently there is no<br>OAuth Access Token configured.<br>"+
260 "Please open the Preferences Dialog and generate or enter an Access Token.</html>",
261 ExceptionUtil.explainMissingOAuthAccessTokenException(new MissingOAuthAccessTokenException()));
262 }
263
264 /**
265 * Test of {@link ExceptionUtil#explainNestedIllegalDataException} method.
266 */
267 @Test
268 public void testExplainNestedIllegalDataException() {
269 assertEquals("<html>Failed to download data. Its format is either unsupported, ill-formed, and/or inconsistent.<br><br>"+
270 "Details (untranslated): null</html>",
271 ExceptionUtil.explainNestedIllegalDataException(new OsmApiException("")));
272
273 assertEquals("<html>Failed to download data. Its format is either unsupported, ill-formed, and/or inconsistent.<br><br>"+
274 "Details (untranslated): test</html>",
275 ExceptionUtil.explainNestedIllegalDataException(new OsmApiException(new IllegalDataException("test"))));
276 }
277
278 /**
279 * Test of {@link ExceptionUtil#explainNestedIOException} method.
280 */
281 @Test
282 public void testExplainNestedIOException() {
283 assertEquals("<html>Failed to upload data to or download data from<br>'"+baseUrl+"'<br>"+
284 "due to a problem with transferring data.<br>Details (untranslated): null</html>",
285 ExceptionUtil.explainNestedIOException(new OsmApiException("")));
286
287 assertEquals("<html>Failed to upload data to or download data from<br>'"+baseUrl+"'<br>"+
288 "due to a problem with transferring data.<br>Details (untranslated): test</html>",
289 ExceptionUtil.explainNestedIOException(new OsmApiException(new IOException("test"))));
290 }
291
292 /**
293 * Test of {@link ExceptionUtil#explainNestedSocketException} method.
294 */
295 @Test
296 public void testExplainNestedSocketException() {
297 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'.<br>"+
298 "Please check your internet connection.</html>",
299 ExceptionUtil.explainNestedSocketException(new OsmApiException("")));
300 }
301
302 /**
303 * Test of {@link ExceptionUtil#explainNestedUnknownHostException} method.
304 */
305 @Test
306 public void testExplainNestedUnknownHostException() {
307 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'.<br>"+
308 "Host name '"+host+"' could not be resolved. <br>"+
309 "Please check the API URL in your preferences and your internet connection.</html>",
310 ExceptionUtil.explainNestedUnknownHostException(new OsmApiException("")));
311 }
312
313 /**
314 * Test of {@link ExceptionUtil#explainNotFound} method.
315 */
316 @Test
317 public void testExplainNotFound() {
318 assertEquals("<html>The OSM server '"+baseUrl+"' does not know about an object<br>"+
319 "you tried to read, update, or delete. Either the respective object<br>"+
320 "does not exist on the server or you are using an invalid URL to access<br>"+
321 "it. Please carefully check the server's address '"+baseUrl+"' for typos.</html>",
322 ExceptionUtil.explainNotFound(new OsmApiException("")));
323 }
324
325 /**
326 * Test of {@link ExceptionUtil#explainOfflineAccessException} method.
327 */
328 @Test
329 public void testExplainOfflineAccessException() {
330 assertEquals("<html>Failed to download data.<br><br>Details: null</html>",
331 ExceptionUtil.explainOfflineAccessException(new OsmApiException("")));
332 assertEquals("<html>Failed to download data.<br><br>Details: test</html>",
333 ExceptionUtil.explainOfflineAccessException(new OsmApiException(new OfflineAccessException("test"))));
334 }
335
336 /**
337 * Test of {@link ExceptionUtil#explainOsmApiInitializationException} method.
338 */
339 @Test
340 public void testExplainOsmApiInitializationException() {
341 assertEquals("<html>Failed to initialize communication with the OSM server "+serverUrl+".<br>"+
342 "Check the server URL in your preferences and your internet connection.</html>",
343 ExceptionUtil.explainOsmApiInitializationException(new OsmApiInitializationException("")));
344 }
345
346 /**
347 * Test of {@link ExceptionUtil#explainOsmTransferException} method.
348 */
349 @Test
350 public void testExplainOsmTransferException() {
351 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'<br>"+
352 "for security reasons. This is most likely because you are running<br>"+
353 "in an applet and because you did not load your applet from '"+host+"'.</html>",
354 ExceptionUtil.explainOsmTransferException(new OsmApiException(new SecurityException("test"))));
355
356 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'.<br>"+
357 "Please check your internet connection.</html>",
358 ExceptionUtil.explainOsmTransferException(new OsmApiException(new SocketException("test"))));
359
360 assertEquals("<html>Failed to open a connection to the remote server<br>'"+baseUrl+"'.<br>"+
361 "Host name '"+host+"' could not be resolved. <br>"+
362 "Please check the API URL in your preferences and your internet connection.</html>",
363 ExceptionUtil.explainOsmTransferException(new OsmApiException(new UnknownHostException("test"))));
364
365 assertEquals("<html>Failed to upload data to or download data from<br>'"+baseUrl+"'<br>"+
366 "due to a problem with transferring data.<br>Details (untranslated): test</html>",
367 ExceptionUtil.explainOsmTransferException(new OsmApiException(new IOException("test"))));
368
369 assertEquals("<html>Failed to initialize communication with the OSM server "+serverUrl+".<br>"+
370 "Check the server URL in your preferences and your internet connection.</html>",
371 ExceptionUtil.explainOsmTransferException(new OsmApiInitializationException("")));
372
373 assertEquals("<html>Failed to upload to changeset <strong>0</strong><br>because it has already been closed on ?.",
374 ExceptionUtil.explainOsmTransferException(new ChangesetClosedException("")));
375
376 assertEquals("<html>Uploading to the server <strong>failed</strong> because your current<br>"+
377 "dataset violates a precondition.<br>The error message is:<br>ResponseCode=412</html>",
378 ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_PRECON_FAILED, "", "")));
379
380 assertEquals("<html>The server reports that an object is deleted.<br>"+
381 "<strong>Uploading failed</strong> if you tried to update or delete this object.<br> "+
382 "<strong>Downloading failed</strong> if you tried to download this object.<br><br>"+
383 "The error message is:<br>ResponseCode=410</html>",
384 ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_GONE, "", "")));
385
386 assertEquals("<html>The OSM server<br>'"+baseUrl+"'<br>reported an internal server error.<br>"+
387 "This is most likely a temporary problem. Please try again later.</html>",
388 ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_INTERNAL_ERROR, "", "")));
389
390 assertEquals("<html>The OSM server '"+baseUrl+"' reported a bad request.<br><br>Error message(untranslated): </html>",
391 ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "", "")));
392
393 assertEquals("<html>Communication with the OSM server '"+baseUrl+"'failed. The server replied<br>"+
394 "the following error code and the following error message:<br><strong>Error code:<strong> 509<br>"+
395 "<strong>Error message (untranslated)</strong>: </html>",
396 ExceptionUtil.explainOsmTransferException(new OsmApiException(509, "", "")));
397
398 assertEquals("ResponseCode=0",
399 ExceptionUtil.explainOsmTransferException(new OsmApiException("")));
400 }
401
402 /**
403 * Test of {@link ExceptionUtil#explainPreconditionFailed} method.
404 */
405 @Test
406 public void testExplainPreconditionFailed() {
407 int code = HttpURLConnection.HTTP_PRECON_FAILED;
408 assertEquals("<html>Uploading to the server <strong>failed</strong> because your current<br>dataset violates a precondition.<br>"+
409 "The error message is:<br>ResponseCode=0</html>",
410 ExceptionUtil.explainPreconditionFailed(new OsmApiException("")));
411
412 assertEquals("<html>Uploading to the server <strong>failed</strong> because your current<br>dataset violates a precondition.<br>"+
413 "The error message is:<br>ResponseCode=412, Error Header=&lt;test&gt;</html>",
414 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "test", "")));
415
416 assertEquals("<html><strong>Failed</strong> to delete <strong>node 1</strong>. It is still referred to by relation 1.<br>"+
417 "Please load the relation, remove the reference to the node, and upload again.</html>",
418 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Node 1 is still used by relation 1", "")));
419
420 assertEquals("<html><strong>Failed</strong> to delete <strong>node 1</strong>. It is still referred to by way 1.<br>"+
421 "Please load the way, remove the reference to the node, and upload again.</html>",
422 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Node 1 is still used by way 1", "")));
423
424 assertEquals("<html><strong>Failed</strong> to delete <strong>relation 1</strong>. It is still referred to by relation 2.<br>"+
425 "Please load the relation, remove the reference to the relation, and upload again.</html>",
426 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "The relation 1 is used in relation 2", "")));
427
428 assertEquals("<html><strong>Failed</strong> to delete <strong>way 1</strong>. It is still referred to by relation 1.<br>"+
429 "Please load the relation, remove the reference to the way, and upload again.</html>",
430 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Way 1 is still used by relation 1", "")));
431
432 assertEquals("<html><strong>Failed</strong> to delete <strong>way 1</strong>. It is still referred to by nodes [1, 2].<br>"+
433 "Please load the nodes, remove the reference to the way, and upload again.</html>",
434 ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Way 1 requires the nodes with id in 1,2", "")));
435 }
436}
Note: See TracBrowser for help on using the repository browser.