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

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

ExceptionUtil: add unit test, fix javadoc, various NPEs, HTML formatting problems (missing end tags)

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