From bb26235b1e9e6c163693682d0d617e14d48aa635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=83=C2=83=C3=82=C2=83=C3=83=C2=82=C3=82=C2=83=C3=83?= =?UTF-8?q?=C2=83=C3=82=C2=82=C3=83=C2=82=C3=82=C2=83=C3=83=C2=83=C3=82?= =?UTF-8?q?=C2=83=C3=83=C2=82=C3=82=C2=82=C3=83=C2=83=C3=82=C2=82=C3=83?= =?UTF-8?q?=C2=82=C3=82=C2=A2=C3=83=C2=83=C3=82=C2=83=C3=83=C2=82=C3=82?= =?UTF-8?q?=C2=83=C3=83=C2=83=C3=82=C2=82=C3=83=C2=82=C3=82=C2=82=C3=83?= =?UTF-8?q?=C2=83=C3=82=C2=83=C3=83=C2=82=C3=82=C2=82=C3=83=C2=83=C3=82?= =?UTF-8?q?=C2=82=C3=83=C2=82=C3=82=C2=80=C3=83=C2=83=C3=82=C2=83=C3=83?= =?UTF-8?q?=C2=82=C3=82=C2=83=C3=83=C2=83=C3=82=C2=82=C3=83=C2=82=C3=82?= =?UTF-8?q?=C2=82=C3=83=C2=83=C3=82=C2=83=C3=83=C2=82=C3=82=C2=82=C3=83?= =?UTF-8?q?=C2=83=C3=82=C2=82=C3=83=C2=82=C3=82=C2=9Cosana=C3=83=C2=83?= =?UTF-8?q?=C3=82=C2=83=C3=83=C2=82=C3=82=C2=83=C3=83=C2=83=C3=82=C2=82?= =?UTF-8?q?=C3=83=C2=82=C3=82=C2=83=C3=83=C2=83=C3=82=C2=83=C3=83=C2=82?= =?UTF-8?q?=C3=82=C2=82=C3=83=C2=83=C3=82=C2=82=C3=83=C2=82=C3=82=C2=A2?= =?UTF-8?q?=C3=83=C2=83=C3=82=C2=83=C3=83=C2=82=C3=82=C2=83=C3=83=C2=83?= =?UTF-8?q?=C3=82=C2=82=C3=83=C2=82=C3=82=C2=82=C3=83=C2=83=C3=82=C2=83?= =?UTF-8?q?=C3=83=C2=82=C3=82=C2=82=C3=83=C2=83=C3=82=C2=82=C3=83=C2=82?= =?UTF-8?q?=C3=82=C2=80=C3=83=C2=83=C3=82=C2=83=C3=83=C2=82=C3=82=C2=83?= =?UTF-8?q?=C3=83=C2=83=C3=82=C2=82=C3=83=C2=82=C3=82=C2=82=C3=83=C2=83?= =?UTF-8?q?=C3=82=C2=83=C3=83=C2=82=C3=82=C2=82=C3=83=C2=83=C3=82=C2=82?= =?UTF-8?q?=C3=83=C2=82=C3=82=C2=9D?= Date: Tue, 19 Dec 2017 10:01:14 -0500 Subject: [android] added parameter validation LatLngBounds.from() --- .../mapbox/mapboxsdk/geometry/LatLngBounds.java | 34 +++++++++- .../mapboxsdk/geometry/LatLngBoundsTest.java | 76 ++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngBounds.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngBounds.java index 6d027a0cef..cf647224ae 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngBounds.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngBounds.java @@ -2,6 +2,7 @@ package com.mapbox.mapboxsdk.geometry; import android.os.Parcel; import android.os.Parcelable; +import android.support.annotation.FloatRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -223,11 +224,42 @@ public class LatLngBounds implements Parcelable { /** * Constructs a LatLngBounds from doubles representing a LatLng pair. + * + * This values of latNorth and latSouth should be in the range of [-90, 90], + * see {@link GeometryConstants#MIN_LATITUDE} and {@link GeometryConstants#MAX_LATITUDE}, + * otherwise IllegalArgumentException will be thrown. *

* This method doesn't recalculate most east or most west boundaries. + * Note that lonEast and lonWest will be wrapped to be in the range of [-180, 180], + * see {@link GeometryConstants#MIN_LONGITUDE} and {@link GeometryConstants#MAX_LONGITUDE} *

*/ - public static LatLngBounds from(double latNorth, double lonEast, double latSouth, double lonWest) { + public static LatLngBounds from( + @FloatRange(from = GeometryConstants.MIN_LATITUDE, to = GeometryConstants.MAX_LATITUDE) double latNorth, + double lonEast, + @FloatRange(from = GeometryConstants.MIN_LATITUDE, to = GeometryConstants.MAX_LATITUDE) double latSouth, + double lonWest) { + + if (Double.isNaN(latNorth) || Double.isNaN(latSouth)) { + throw new IllegalArgumentException("latitude must not be NaN"); + } + + if (Double.isNaN(lonEast) || Double.isNaN(lonWest)) { + throw new IllegalArgumentException("longitude must not be NaN"); + } + + if (Double.isInfinite(lonEast) || Double.isInfinite(lonWest)) { + throw new IllegalArgumentException("longitude must not be infinite"); + } + + if (latNorth > GeometryConstants.MAX_LATITUDE || latNorth < GeometryConstants.MIN_LATITUDE + || latSouth > GeometryConstants.MAX_LATITUDE || latSouth < GeometryConstants.MIN_LATITUDE) { + throw new IllegalArgumentException("latitude must be between -90 and 90"); + } + + lonEast = LatLng.wrap(lonEast, GeometryConstants.MIN_LONGITUDE, GeometryConstants.MAX_LONGITUDE); + lonWest = LatLng.wrap(lonWest, GeometryConstants.MIN_LONGITUDE, GeometryConstants.MAX_LONGITUDE); + return new LatLngBounds(latNorth, lonEast, latSouth, lonWest); } diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/geometry/LatLngBoundsTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/geometry/LatLngBoundsTest.java index c3d13f0c66..e6c1fdd0cf 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/geometry/LatLngBoundsTest.java +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/geometry/LatLngBoundsTest.java @@ -7,7 +7,9 @@ import com.mapbox.mapboxsdk.exceptions.InvalidLatLngBoundsException; import com.mapbox.mapboxsdk.utils.MockParcel; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.util.ArrayList; import java.util.List; @@ -289,4 +291,78 @@ public class LatLngBoundsTest { assertEquals(40.713955826286039, bounds.getLatNorth(), DELTA); } + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Test + public void testConstructorChecksNorthLatitudeNaN() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("latitude must not be NaN"); + LatLngBounds.from(Double.NaN, 0, -20, -20); + } + + @Test + public void testConstructorChecksEastLongitudeNaN() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("longitude must not be NaN"); + LatLngBounds.from(0, Double.NaN, -20, -20); + } + + @Test + public void testConstructorChecksNorthLatitudeGreaterThan90() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("latitude must be between -90 and 90"); + LatLngBounds.from(95, 0, -20, -20); + } + + @Test + public void testConstructorChecksNorthLatitudeLessThanThanNegative90() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("latitude must be between -90 and 90"); + LatLngBounds.from(-95, 0, -20, -20); + } + + @Test + public void testConstructorChecksEastLongitudeInfinity() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("longitude must not be infinite"); + LatLngBounds.from(0, Double.POSITIVE_INFINITY, -20, -20); + } + + + @Test + public void testConstructorChecksSouthLatitudeNaN() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("latitude must not be NaN"); + LatLngBounds.from(20, 20, Double.NaN, 0); + } + + @Test + public void testConstructorChecksWesttLongitudeNaN() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("longitude must not be NaN"); + LatLngBounds.from(20, 20, 0, Double.NaN); + } + + @Test + public void testConstructorChecksSouthLatitudeGreaterThan90() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("latitude must be between -90 and 90"); + LatLngBounds.from(20, 20,95, 0); + } + + @Test + public void testConstructorChecksSouthLatitudeLessThanThanNegative90() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("latitude must be between -90 and 90"); + LatLngBounds.from(20, 20, -95, 0); + } + + @Test + public void testConstructorChecksWestLongitudeInfinity() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("longitude must not be infinite"); + LatLngBounds.from(20, 20, 0, Double.POSITIVE_INFINITY); + } } -- cgit v1.2.1