From 9c882bea3dcf7d983619dc5acdd0b3219bc6eee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=84=9C=EA=B2=BD=EC=9B=90=28SseO=29?= <66459882+SseO-KR@users.noreply.github.com> Date: Wed, 20 Dec 2023 19:45:09 +0900 Subject: [PATCH] fix: IllegalArgumentException is clearer than ArrayIndexOutOfBounds (#1241) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --------- Co-authored-by: skw4223 <66459882+skw4223@users.noreply.github.com> Co-authored-by: Enrique López Mañas --- .../android/data/kml/KmlFeatureParser.java | 11 ++++-- .../data/kml/KmlFeatureParserTest.java | 36 +++++++++++++++---- .../amu_wrong_not_exist_coordinates.kml | 10 ++++++ ...u_wrong_not_exist_latitude_coordinates.kml | 10 ++++++ 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 library/src/test/resources/amu_wrong_not_exist_coordinates.kml create mode 100644 library/src/test/resources/amu_wrong_not_exist_latitude_coordinates.kml diff --git a/library/src/main/java/com/google/maps/android/data/kml/KmlFeatureParser.java b/library/src/main/java/com/google/maps/android/data/kml/KmlFeatureParser.java index 1a644fbb6..967456b4e 100644 --- a/library/src/main/java/com/google/maps/android/data/kml/KmlFeatureParser.java +++ b/library/src/main/java/com/google/maps/android/data/kml/KmlFeatureParser.java @@ -1,12 +1,12 @@ /* * Copyright 2020 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -432,6 +432,11 @@ private static LatLngAlt convertToLatLngAlt(String coordinateString) { */ private static LatLngAlt convertToLatLngAlt(String coordinateString, String separator) { String[] coordinate = coordinateString.split(separator); + + if (coordinate.length < 2) { + throw new IllegalArgumentException("Wrong coordinate, latitude and longitude must be set"); + } + double lat = Double.parseDouble(coordinate[LATITUDE_INDEX]); double lon = Double.parseDouble(coordinate[LONGITUDE_INDEX]); Double alt = (coordinate.length > 2) ? Double.parseDouble(coordinate[ALTITUDE_INDEX]) : null; diff --git a/library/src/test/java/com/google/maps/android/data/kml/KmlFeatureParserTest.java b/library/src/test/java/com/google/maps/android/data/kml/KmlFeatureParserTest.java index 87487c459..820c91bdc 100644 --- a/library/src/test/java/com/google/maps/android/data/kml/KmlFeatureParserTest.java +++ b/library/src/test/java/com/google/maps/android/data/kml/KmlFeatureParserTest.java @@ -15,6 +15,13 @@ */ package com.google.maps.android.data.kml; +import static com.google.maps.android.data.kml.KmlTestUtil.createParser; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import com.google.android.gms.maps.model.LatLng; import com.google.maps.android.data.Geometry; import org.junit.Test; @@ -25,12 +32,6 @@ import java.util.ArrayList; import java.util.List; -import static com.google.maps.android.data.kml.KmlTestUtil.createParser; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - @RunWith(RobolectricTestRunner.class) public class KmlFeatureParserTest { @@ -101,4 +102,27 @@ public void testMultiGeometries() throws Exception { assertEquals(subObjects.get(0).getGeometryType(), "Point"); assertEquals(subObjects.get(1).getGeometryType(), "LineString"); } + + @Test(expected = IllegalArgumentException.class) + public void testWrongNotExistCoordinates() throws Exception { + XmlPullParser xmlPullParser = createParser("amu_wrong_not_exist_coordinates.kml"); + KmlFeatureParser.createPlacemark(xmlPullParser); + } + + @Test(expected = IllegalArgumentException.class) + public void testWrongNotExistLatitude() throws Exception { + XmlPullParser xmlPullParser = createParser("amu_wrong_not_exist_latitude_coordinates.kml"); + KmlFeatureParser.createPlacemark(xmlPullParser); + } + + @Test + public void testSuitableCoordinates() throws Exception { + XmlPullParser xmlPullParser = createParser("amu_basic_folder.kml"); + KmlPlacemark feature = KmlFeatureParser.createPlacemark(xmlPullParser); + assertEquals(feature.getProperty("name"), "Pin on a mountaintop"); + assertEquals(feature.getGeometry().getGeometryType(), "Point"); + LatLng latLng = (LatLng) feature.getGeometry().getGeometryObject(); + assertEquals(latLng.latitude, -43.60505741890396, 0.001); + assertEquals(latLng.longitude, 170.1435558771009, 0.001); + } } diff --git a/library/src/test/resources/amu_wrong_not_exist_coordinates.kml b/library/src/test/resources/amu_wrong_not_exist_coordinates.kml new file mode 100644 index 000000000..a62e94b1a --- /dev/null +++ b/library/src/test/resources/amu_wrong_not_exist_coordinates.kml @@ -0,0 +1,10 @@ + + Basic Folder + + Pin on a mountaintop + #pushpin + + + + + diff --git a/library/src/test/resources/amu_wrong_not_exist_latitude_coordinates.kml b/library/src/test/resources/amu_wrong_not_exist_latitude_coordinates.kml new file mode 100644 index 000000000..c0fba377c --- /dev/null +++ b/library/src/test/resources/amu_wrong_not_exist_latitude_coordinates.kml @@ -0,0 +1,10 @@ + + Basic Folder + + Pin on a mountaintop + #pushpin + + 170.1435558771009 + + +