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

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

fix unit test

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