[go: nahoru, domu]

Skip to content

Commit

Permalink
fix: IllegalArgumentException is clearer than ArrayIndexOutOfBounds (#…
Browse files Browse the repository at this point in the history
…1241)


---------

Co-authored-by: skw4223 <66459882+skw4223@users.noreply.github.com>
Co-authored-by: Enrique López Mañas <eenriquelopez@gmail.com>
  • Loading branch information
3 people committed Dec 20, 2023
1 parent b6aa0b7 commit 9c882be
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions library/src/test/resources/amu_wrong_not_exist_coordinates.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Folder>
<name>Basic Folder</name>
<Placemark id="mountainpin1">
<name>Pin on a mountaintop</name>
<styleUrl>#pushpin</styleUrl>
<LineString>
<coordinates></coordinates>
</LineString>
</Placemark>
</Folder>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Folder>
<name>Basic Folder</name>
<Placemark id="mountainpin1">
<name>Pin on a mountaintop</name>
<styleUrl>#pushpin</styleUrl>
<LineString>
<coordinates>170.1435558771009</coordinates>
</LineString>
</Placemark>
</Folder>

0 comments on commit 9c882be

Please sign in to comment.