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

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

fix #13048 - Make ExceptionUtilTest respect time zone (patch by michael2402) - gsoc-core

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