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

Last change on this file since 9816 was 9669, checked in by bastiK, 8 years ago

add missing svn:eol-style=native (see #12410)

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