diff options
| -rw-r--r-- | main/src/cgeo/geocaching/Waypoint.java | 1 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/geopoint/Geopoint.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/geopoint/GeopointParser.java | 8 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/WaypointTest.java | 34 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/geopoint/GeoPointParserTest.java | 2 |
5 files changed, 44 insertions, 5 deletions
diff --git a/main/src/cgeo/geocaching/Waypoint.java b/main/src/cgeo/geocaching/Waypoint.java index 6626c4d..efedff5 100644 --- a/main/src/cgeo/geocaching/Waypoint.java +++ b/main/src/cgeo/geocaching/Waypoint.java @@ -3,7 +3,6 @@ package cgeo.geocaching; import cgeo.geocaching.enumerations.LoadFlags; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.Geopoint; -import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.MatcherWrapper; import org.apache.commons.lang3.StringUtils; diff --git a/main/src/cgeo/geocaching/geopoint/Geopoint.java b/main/src/cgeo/geocaching/geopoint/Geopoint.java index f21df01..3d69bd4 100644 --- a/main/src/cgeo/geocaching/geopoint/Geopoint.java +++ b/main/src/cgeo/geocaching/geopoint/Geopoint.java @@ -51,8 +51,8 @@ public final class Geopoint implements ICoordinates, Parcelable { */ public Geopoint(final String text) { final Geopoint parsed = GeopointParser.parse(text); - this.latitude = parsed.latitude; - this.longitude = parsed.longitude; + latitude = parsed.latitude; + longitude = parsed.longitude; } /** diff --git a/main/src/cgeo/geocaching/geopoint/GeopointParser.java b/main/src/cgeo/geocaching/geopoint/GeopointParser.java index c043d6f..e59cd23 100644 --- a/main/src/cgeo/geocaching/geopoint/GeopointParser.java +++ b/main/src/cgeo/geocaching/geopoint/GeopointParser.java @@ -57,7 +57,6 @@ class GeopointParser { */ public static Geopoint parse(final String text) { final ResultWrapper latitudeWrapper = parseHelper(text, LatLon.LAT); - final double lat = latitudeWrapper.result; // cut away the latitude part when parsing the longitude final ResultWrapper longitudeWrapper = parseHelper(text.substring(latitudeWrapper.matcherPos + latitudeWrapper.matcherLength), LatLon.LON); @@ -65,7 +64,14 @@ class GeopointParser { throw new Geopoint.ParseException("Distance between latitude and longitude text is to large.", LatLon.LON); } + final double lat = latitudeWrapper.result; final double lon = longitudeWrapper.result; + if (lat > 90 || lat < -90) { + throw new Geopoint.ParseException(text, LatLon.LAT); + } + if (lon > 180 || lon < -180) { + throw new Geopoint.ParseException(text, LatLon.LON); + } return new Geopoint(lat, lon); } diff --git a/tests/src/cgeo/geocaching/WaypointTest.java b/tests/src/cgeo/geocaching/WaypointTest.java index 3ddc32c..9868f81 100644 --- a/tests/src/cgeo/geocaching/WaypointTest.java +++ b/tests/src/cgeo/geocaching/WaypointTest.java @@ -1,9 +1,12 @@ package cgeo.geocaching; import cgeo.geocaching.enumerations.WaypointType; +import cgeo.geocaching.geopoint.Geopoint; import android.test.AndroidTestCase; +import java.util.Collection; + public class WaypointTest extends AndroidTestCase { public static void testOrder() { @@ -42,4 +45,35 @@ public class WaypointTest extends AndroidTestCase { waypoint.setGeocode("p1"); assertEquals("P1", waypoint.getGeocode()); } + + public static void testParseNoWaypointFromNote() { + final String note = "1 T 126\n" + + "2 B 12\n" + + "3 S 630\n" + + "4c P 51\n" + + "L 1\n" + + "E 14\n" + + "J 11\n" + + "U 12\n" + + "D 1\n" + + "M 7\n" + + "N 5\n" + + "5 IFG 257"; + assertTrue(Waypoint.parseWaypointsFromNote(note).isEmpty()); + } + + public static void testParseWaypointFromNote() { + final String note = "Dummy note\nn 45° 3.5 e 27° 7.5\nNothing else"; + final Collection<Waypoint> waypoints = Waypoint.parseWaypointsFromNote(note); + assertEquals(1, waypoints.size()); + final Geopoint coords = waypoints.iterator().next().getCoords(); + assertEquals(45, coords.getLatDeg()); + assertEquals(3.5, coords.getLatMinRaw()); + assertEquals(27, coords.getLonDeg()); + assertEquals(7.5, coords.getLonMinRaw()); + final String note2 = "Waypoint on two lines\nN 45°3.5\nE 27°7.5\nNothing else"; + final Collection<Waypoint> waypoints2 = Waypoint.parseWaypointsFromNote(note2); + assertEquals(1, waypoints2.size()); + assertEquals(coords, waypoints2.iterator().next().getCoords()); + } } diff --git a/tests/src/cgeo/geocaching/geopoint/GeoPointParserTest.java b/tests/src/cgeo/geocaching/geopoint/GeoPointParserTest.java index 1de2e26..c471dfa 100644 --- a/tests/src/cgeo/geocaching/geopoint/GeoPointParserTest.java +++ b/tests/src/cgeo/geocaching/geopoint/GeoPointParserTest.java @@ -102,7 +102,7 @@ public class GeoPointParserTest extends AndroidTestCase { } public static void testMeridian() { - assertEquals(new Geopoint(123, 0), GeopointParser.parse("N 123° 00.000 00° 00.000")); + assertEquals(new Geopoint(23, 0), GeopointParser.parse("N 23° 00.000 00° 00.000")); } public static void testEquatorMeridian() { |
