// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.tools;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openstreetmap.josm.JOSMFixture;
import org.openstreetmap.josm.io.ChangesetClosedException;
import org.openstreetmap.josm.io.IllegalDataException;
import org.openstreetmap.josm.io.MissingOAuthAccessTokenException;
import org.openstreetmap.josm.io.OfflineAccessException;
import org.openstreetmap.josm.io.OsmApi;
import org.openstreetmap.josm.io.OsmApiException;
import org.openstreetmap.josm.io.OsmApiInitializationException;
import org.openstreetmap.josm.io.auth.CredentialsManager;
import org.openstreetmap.josm.tools.date.DateUtils;
/**
* Unit tests of {@link ExceptionUtil} class.
*/
public class ExceptionUtilTest {
private static String baseUrl;
private static String serverUrl;
private static String host;
private static String user;
/**
* Setup test.
* @throws Exception in case of error
*/
@BeforeClass
public static void setUp() throws Exception {
JOSMFixture.createUnitTestFixture().init();
OsmApi api = OsmApi.getOsmApi();
api.initialize(null);
baseUrl = api.getBaseUrl();
serverUrl = api.getServerUrl();
host = new URL(serverUrl).getHost();
user = CredentialsManager.getInstance().getUsername();
DateUtils.PROP_ISO_DATES.put(Boolean.TRUE);
}
/**
* Test of {@link ExceptionUtil#explainBadRequest} method.
*/
@Test
public void testExplainBadRequest() {
assertEquals("The OSM server '"+baseUrl+"' reported a bad request.
",
ExceptionUtil.explainBadRequest(new OsmApiException("")));
assertEquals("The OSM server '"+baseUrl+"' reported a bad request.
"+
"Error message(untranslated): header",
ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "header", "")));
assertEquals("The OSM server '"+baseUrl+"' reported a bad request.
"+
"Error message(untranslated): header",
ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "header", "", "invalid_url")));
assertEquals("The OSM server '"+host+"' reported a bad request.
"+
"Error message(untranslated): header",
ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "header", "", baseUrl)));
assertEquals("The OSM server '"+baseUrl+"' reported a bad request.
"+
"The area you tried to download is too big or your request was too large.
"+
"Either request a smaller area or use an export file provided by the OSM community.",
ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "The maximum bbox", "")));
assertEquals("The OSM server '"+baseUrl+"' reported a bad request.
"+
"The area you tried to download is too big or your request was too large.
"+
"Either request a smaller area or use an export file provided by the OSM community.",
ExceptionUtil.explainBadRequest(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "You requested too many nodes", "")));
}
/**
* Test of {@link ExceptionUtil#explainBandwidthLimitExceeded} method.
*/
@Test
public void testExplainBandwidthLimitExceeded() {
assertEquals("Communication with the OSM server '"+baseUrl+"'failed. "+
"The server replied
the following error code and the following error message:
"+
"Error code: 0
Error message (untranslated): no error message available",
ExceptionUtil.explainBandwidthLimitExceeded(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainChangesetClosedException} method.
*/
@Test
public void testExplainChangesetClosedException() {
assertEquals("Failed to upload to changeset 0
because it has already been closed on ?.",
ExceptionUtil.explainChangesetClosedException(new ChangesetClosedException("")));
assertEquals("Failed to upload to changeset 1
because it has already been closed on 2016-01-01 00:00:00.",
ExceptionUtil.explainChangesetClosedException(new ChangesetClosedException(1, DateUtils.fromString("2016-01-01"), null)));
}
/**
* Test of {@link ExceptionUtil#explainClientTimeout} method.
*/
@Test
public void testExplainClientTimeout() {
assertEquals("Communication with the OSM server '"+baseUrl+"' timed out. Please retry later.",
ExceptionUtil.explainClientTimeout(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainConflict} method.
*/
@Test
public void testExplainConflict() {
int code = HttpURLConnection.HTTP_CONFLICT;
assertEquals("The server reported that it has detected a conflict.",
ExceptionUtil.explainConflict(new OsmApiException("")));
assertEquals("The server reported that it has detected a conflict.
Error message (untranslated):
header",
ExceptionUtil.explainConflict(new OsmApiException(code, "header", "")));
assertEquals("Closing of changeset 1 failed
because it has already been closed.",
ExceptionUtil.explainConflict(new OsmApiException(code, "The changeset 1 was closed at xxx", "")));
assertEquals("Closing of changeset 1 failed
because it has already been closed on 2016-01-01 13:34:56.",
ExceptionUtil.explainConflict(new OsmApiException(code, "The changeset 1 was closed at 2016-01-01 12:34:56 UTC", "")));
}
/**
* Test of {@link ExceptionUtil#explainException} method.
*/
@Test
public void testExplainException() {
assertEquals("ResponseCode=0",
ExceptionUtil.explainException(new OsmApiException("")));
assertEquals("java.lang.Exception: ",
ExceptionUtil.explainException(new Exception("")));
assertEquals("java.lang.Exception",
ExceptionUtil.explainException(new Exception(null, null)));
assertEquals("test",
ExceptionUtil.explainException(new Exception("test")));
}
/**
* Test of {@link ExceptionUtil#explainFailedAuthorisation} method.
*/
@Test
public void testExplainFailedAuthorisation() {
assertEquals("Authorisation at the OSM server failed.
",
ExceptionUtil.explainFailedAuthorisation(new OsmApiException("")));
assertEquals("Authorisation at the OSM server failed.
The server reported the following error:
'header'",
ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "header", null)));
assertEquals("Authorisation at the OSM server failed.
The server reported the following error:
'header (body)'",
ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "header", "body")));
assertEquals("Authorisation at the OSM server failed.
The server reported the following error:
'body'",
ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, null, "body")));
}
/**
* Test of {@link ExceptionUtil#explainFailedOAuthAuthorisation} method.
*/
@Test
public void testExplainFailedOAuthAuthorisation() {
assertEquals("Authorisation at the OSM server with the OAuth token 'null' failed.
"+
"The token is not authorised to access the protected resource
'unknown'.
"+
"Please launch the preferences dialog and retrieve another OAuth token.",
ExceptionUtil.explainFailedOAuthAuthorisation(new OsmApiException("")));
assertEquals("Authorisation at the OSM server with the OAuth token 'null' failed.
"+
"The token is not authorised to access the protected resource
'"+baseUrl+"'.
"+
"Please launch the preferences dialog and retrieve another OAuth token.",
ExceptionUtil.explainFailedOAuthAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "", "", baseUrl)));
}
/**
* Test of {@link ExceptionUtil#explainFailedBasicAuthentication} method.
*/
@Test
public void testExplainFailedBasicAuthentication() {
assertEquals("Authentication at the OSM server with the username '"+user+"' failed.
"+
"Please check the username and the password in the JOSM preferences.",
ExceptionUtil.explainFailedBasicAuthentication(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainFailedOAuthAuthentication} method.
*/
@Test
public void testExplainFailedOAuthAuthentication() {
assertEquals("Authentication at the OSM server with the OAuth token 'null' failed.
"+
"Please launch the preferences dialog and retrieve another OAuth token.",
ExceptionUtil.explainFailedOAuthAuthentication(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainGenericOsmApiException} method.
*/
@Test
public void testExplainGenericOsmApiException() {
assertEquals("Communication with the OSM server '"+baseUrl+"'failed. The server replied
"+
"the following error code and the following error message:
Error code: 0
"+
"Error message (untranslated): no error message available",
ExceptionUtil.explainGenericOsmApiException(new OsmApiException("")));
assertEquals("Communication with the OSM server '"+baseUrl+"'failed. The server replied
"+
"the following error code and the following error message:
Error code: 500
"+
"Error message (untranslated): header",
ExceptionUtil.explainGenericOsmApiException(new OsmApiException(HttpURLConnection.HTTP_INTERNAL_ERROR, "header", null)));
assertEquals("Communication with the OSM server '"+baseUrl+"'failed. The server replied
"+
"the following error code and the following error message:
Error code: 500
"+
"Error message (untranslated): body",
ExceptionUtil.explainGenericOsmApiException(new OsmApiException(HttpURLConnection.HTTP_INTERNAL_ERROR, null, "body")));
}
/**
* Test of {@link ExceptionUtil#explainGoneForUnknownPrimitive} method.
*/
@Test
public void testExplainGoneForUnknownPrimitive() {
assertEquals("The server reports that an object is deleted.
"+
"Uploading failed if you tried to update or delete this object.
"+
"Downloading failed if you tried to download this object.
"+
"The error message is:
ResponseCode=0",
ExceptionUtil.explainGoneForUnknownPrimitive(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainInternalServerError} method.
*/
@Test
public void testExplainInternalServerError() {
assertEquals("The OSM server
'"+baseUrl+"'
reported an internal server error.
"+
"This is most likely a temporary problem. Please try again later.",
ExceptionUtil.explainInternalServerError(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainMissingOAuthAccessTokenException} method.
*/
@Test
public void testExplainMissingOAuthAccessTokenException() {
assertEquals("Failed to authenticate at the OSM server 'http://api06.dev.openstreetmap.org/api'.
"+
"You are using OAuth to authenticate but currently there is no
OAuth Access Token configured.
"+
"Please open the Preferences Dialog and generate or enter an Access Token.",
ExceptionUtil.explainMissingOAuthAccessTokenException(new MissingOAuthAccessTokenException()));
}
/**
* Test of {@link ExceptionUtil#explainNestedIllegalDataException} method.
*/
@Test
public void testExplainNestedIllegalDataException() {
assertEquals("Failed to download data. Its format is either unsupported, ill-formed, and/or inconsistent.
"+
"Details (untranslated): null",
ExceptionUtil.explainNestedIllegalDataException(new OsmApiException("")));
assertEquals("Failed to download data. Its format is either unsupported, ill-formed, and/or inconsistent.
"+
"Details (untranslated): test",
ExceptionUtil.explainNestedIllegalDataException(new OsmApiException(new IllegalDataException("test"))));
}
/**
* Test of {@link ExceptionUtil#explainNestedIOException} method.
*/
@Test
public void testExplainNestedIOException() {
assertEquals("Failed to upload data to or download data from
'"+baseUrl+"'
"+
"due to a problem with transferring data.
Details (untranslated): null",
ExceptionUtil.explainNestedIOException(new OsmApiException("")));
assertEquals("Failed to upload data to or download data from
'"+baseUrl+"'
"+
"due to a problem with transferring data.
Details (untranslated): test",
ExceptionUtil.explainNestedIOException(new OsmApiException(new IOException("test"))));
}
/**
* Test of {@link ExceptionUtil#explainNestedSocketException} method.
*/
@Test
public void testExplainNestedSocketException() {
assertEquals("Failed to open a connection to the remote server
'"+baseUrl+"'.
"+
"Please check your internet connection.",
ExceptionUtil.explainNestedSocketException(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainNestedUnknownHostException} method.
*/
@Test
public void testExplainNestedUnknownHostException() {
assertEquals("Failed to open a connection to the remote server
'"+baseUrl+"'.
"+
"Host name '"+host+"' could not be resolved.
"+
"Please check the API URL in your preferences and your internet connection.",
ExceptionUtil.explainNestedUnknownHostException(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainNotFound} method.
*/
@Test
public void testExplainNotFound() {
assertEquals("The OSM server '"+baseUrl+"' does not know about an object
"+
"you tried to read, update, or delete. Either the respective object
"+
"does not exist on the server or you are using an invalid URL to access
"+
"it. Please carefully check the server's address '"+baseUrl+"' for typos.",
ExceptionUtil.explainNotFound(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainOfflineAccessException} method.
*/
@Test
public void testExplainOfflineAccessException() {
assertEquals("Failed to download data.
Details: null",
ExceptionUtil.explainOfflineAccessException(new OsmApiException("")));
assertEquals("Failed to download data.
Details: test",
ExceptionUtil.explainOfflineAccessException(new OsmApiException(new OfflineAccessException("test"))));
}
/**
* Test of {@link ExceptionUtil#explainOsmApiInitializationException} method.
*/
@Test
public void testExplainOsmApiInitializationException() {
assertEquals("Failed to initialize communication with the OSM server "+serverUrl+".
"+
"Check the server URL in your preferences and your internet connection.",
ExceptionUtil.explainOsmApiInitializationException(new OsmApiInitializationException("")));
}
/**
* Test of {@link ExceptionUtil#explainOsmTransferException} method.
*/
@Test
public void testExplainOsmTransferException() {
assertEquals("Failed to open a connection to the remote server
'"+baseUrl+"'
"+
"for security reasons. This is most likely because you are running
"+
"in an applet and because you did not load your applet from '"+host+"'.",
ExceptionUtil.explainOsmTransferException(new OsmApiException(new SecurityException("test"))));
assertEquals("Failed to open a connection to the remote server
'"+baseUrl+"'.
"+
"Please check your internet connection.",
ExceptionUtil.explainOsmTransferException(new OsmApiException(new SocketException("test"))));
assertEquals("Failed to open a connection to the remote server
'"+baseUrl+"'.
"+
"Host name '"+host+"' could not be resolved.
"+
"Please check the API URL in your preferences and your internet connection.",
ExceptionUtil.explainOsmTransferException(new OsmApiException(new UnknownHostException("test"))));
assertEquals("Failed to upload data to or download data from
'"+baseUrl+"'
"+
"due to a problem with transferring data.
Details (untranslated): test",
ExceptionUtil.explainOsmTransferException(new OsmApiException(new IOException("test"))));
assertEquals("Failed to initialize communication with the OSM server "+serverUrl+".
"+
"Check the server URL in your preferences and your internet connection.",
ExceptionUtil.explainOsmTransferException(new OsmApiInitializationException("")));
assertEquals("Failed to upload to changeset 0
because it has already been closed on ?.",
ExceptionUtil.explainOsmTransferException(new ChangesetClosedException("")));
assertEquals("Uploading to the server failed because your current
"+
"dataset violates a precondition.
The error message is:
ResponseCode=412",
ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_PRECON_FAILED, "", "")));
assertEquals("The server reports that an object is deleted.
"+
"Uploading failed if you tried to update or delete this object.
"+
"Downloading failed if you tried to download this object.
"+
"The error message is:
ResponseCode=410",
ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_GONE, "", "")));
assertEquals("The OSM server
'"+baseUrl+"'
reported an internal server error.
"+
"This is most likely a temporary problem. Please try again later.",
ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_INTERNAL_ERROR, "", "")));
assertEquals("The OSM server '"+baseUrl+"' reported a bad request.
Error message(untranslated): ",
ExceptionUtil.explainOsmTransferException(new OsmApiException(HttpURLConnection.HTTP_BAD_REQUEST, "", "")));
assertEquals("Communication with the OSM server '"+baseUrl+"'failed. The server replied
"+
"the following error code and the following error message:
Error code: 509
"+
"Error message (untranslated): ",
ExceptionUtil.explainOsmTransferException(new OsmApiException(509, "", "")));
assertEquals("ResponseCode=0",
ExceptionUtil.explainOsmTransferException(new OsmApiException("")));
}
/**
* Test of {@link ExceptionUtil#explainPreconditionFailed} method.
*/
@Test
public void testExplainPreconditionFailed() {
int code = HttpURLConnection.HTTP_PRECON_FAILED;
assertEquals("Uploading to the server failed because your current
dataset violates a precondition.
"+
"The error message is:
ResponseCode=0",
ExceptionUtil.explainPreconditionFailed(new OsmApiException("")));
assertEquals("Uploading to the server failed because your current
dataset violates a precondition.
"+
"The error message is:
ResponseCode=412, Error Header=<test>",
ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "test", "")));
assertEquals("Failed to delete node 1. It is still referred to by relation 1.
"+
"Please load the relation, remove the reference to the node, and upload again.",
ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Node 1 is still used by relation 1", "")));
assertEquals("Failed to delete node 1. It is still referred to by way 1.
"+
"Please load the way, remove the reference to the node, and upload again.",
ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Node 1 is still used by way 1", "")));
assertEquals("Failed to delete relation 1. It is still referred to by relation 2.
"+
"Please load the relation, remove the reference to the relation, and upload again.",
ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "The relation 1 is used in relation 2", "")));
assertEquals("Failed to delete way 1. It is still referred to by relation 1.
"+
"Please load the relation, remove the reference to the way, and upload again.",
ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Way 1 is still used by relation 1", "")));
assertEquals("Failed to delete way 1. It is still referred to by nodes [1, 2].
"+
"Please load the nodes, remove the reference to the way, and upload again.",
ExceptionUtil.explainPreconditionFailed(new OsmApiException(code, "Way 1 requires the nodes with id in 1,2", "")));
}
}