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

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

see #11924 - add assertions to check if java.locale.providers enables CLDR as default because of JEP 252 in Java 9 (unit tests depending on default Java date formatting are failing)

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