diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/GpxImporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/GpxImporter.java index 571859cc9..e1d58b354 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/GpxImporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/GpxImporter.java @@ -342,8 +342,14 @@ public class GpxImporter extends DefaultHandler { // create new location and set attributes Location loc = new Location(LocationManager.GPS_PROVIDER); - loc.setLatitude(Double.parseDouble(latitude)); - loc.setLongitude(Double.parseDouble(longitude)); + try { + loc.setLatitude(Double.parseDouble(latitude)); + loc.setLongitude(Double.parseDouble(longitude)); + } catch (NumberFormatException e) { + String msg = createErrorMessage( + "Unable to parse lat/long: " + latitude + "/" + longitude); + throw new SAXException(msg, e); + } return loc; } @@ -472,9 +478,14 @@ public class GpxImporter extends DefaultHandler { } } - private void onAltitudeElementEnd() { + private void onAltitudeElementEnd() throws SAXException { if (location != null) { - location.setAltitude(Double.parseDouble(content)); + try { + location.setAltitude(Double.parseDouble(content)); + } catch (NumberFormatException e) { + String msg = createErrorMessage("Unable to parse altitude: " + content); + throw new SAXException(msg, e); + } } } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxImporterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxImporterTest.java index 0c293b7d0..b819f5c2a 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxImporterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxImporterTest.java @@ -19,9 +19,9 @@ import static com.google.android.testing.mocking.AndroidMock.eq; import static com.google.android.testing.mocking.AndroidMock.expect; import com.google.android.apps.mytracks.content.MyTracksProviderUtils; -import com.google.android.apps.mytracks.content.MyTracksProviderUtils.Factory; import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.TracksColumns; +import com.google.android.apps.mytracks.content.MyTracksProviderUtils.Factory; import com.google.android.apps.mytracks.testing.TestingProviderUtilsFactory; import com.google.android.testing.mocking.AndroidMock; import com.google.android.testing.mocking.UsesMocks; @@ -77,7 +77,7 @@ public class GpxImporterTest extends AndroidTestCase { DATE_FORMAT2.setTimeZone(utc); } - // TODO use real files from different sources with more track points + // TODO: use real files from different sources with more track points. private static final String VALID_TEST_GPX = "" + " trackParam = new Capture(); @@ -171,7 +177,7 @@ public class GpxImporterTest extends AndroidTestCase { } /** - * Test with invalid location - track should be deleted + * Test with invalid location - track should be deleted. */ public void testImportLocationFailure() throws ParserConfigurationException, SAXException, IOException { @@ -179,7 +185,7 @@ public class GpxImporterTest extends AndroidTestCase { } /** - * Test with invalid time - track should be deleted + * Test with invalid time - track should be deleted. */ public void testImportTimeFailure() throws ParserConfigurationException, SAXException, IOException { @@ -187,13 +193,37 @@ public class GpxImporterTest extends AndroidTestCase { } /** - * Test with invalid xml - track should be deleted + * Test with invalid xml - track should be deleted. */ public void testImportXMLFailure() throws ParserConfigurationException, SAXException, IOException { testInvalidXML(INVALID_XML_TEST_GPX); } + + /** + * Test with invalid altitude - track should be deleted. + */ + public void testImportInvalidAltitude() throws ParserConfigurationException, + SAXException, IOException { + testInvalidXML(INVALID_ALTITUDE_TEST_GPX); + } + /** + * Test with invalid latitude - track should be deleted. + */ + public void testImportInvalidLatitude() throws ParserConfigurationException, + SAXException, IOException { + testInvalidXML(INVALID_LATITUDE_TEST_GPX); + } + + /** + * Test with invalid longitude - track should be deleted. + */ + public void testImportInvalidLongitude() throws ParserConfigurationException, + SAXException, IOException { + testInvalidXML(INVALID_LONGITUDE_TEST_GPX); + } + private void testInvalidXML(String xml) throws ParserConfigurationException, IOException { expect(providerUtils.insertTrack((Track) AndroidMock.anyObject()))