Skip to content

Commit

Permalink
Updating to throw IllegalArgument exception for null value coordinate…
Browse files Browse the repository at this point in the history
…s. Tests included.
  • Loading branch information
nknize committed Nov 14, 2014
1 parent 4993565 commit 0067a0c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
Expand Up @@ -216,7 +216,7 @@ private static CoordinateNode parseCoordinates(XContentParser parser) throws IOE
token = parser.nextToken();
return new CoordinateNode(new Coordinate(lon, lat));
} else if (token == XContentParser.Token.VALUE_NULL) {
return null;
throw new ElasticsearchIllegalArgumentException("coordinates cannot contain NULL values)");
}

List<CoordinateNode> nodes = new ArrayList<>();
Expand Down
Expand Up @@ -26,6 +26,8 @@
import com.spatial4j.core.shape.jts.JtsGeometry;
import com.spatial4j.core.shape.jts.JtsPoint;
import com.vividsolutions.jts.geom.*;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -172,7 +174,7 @@ public void testParse_invalidPolygon() throws IOException {
.endObject().string();
XContentParser parser = JsonXContent.jsonXContent.createParser(invalidPoly1);
parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser);
ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class);

// test case 2: create an invalid polygon with only 1 point
String invalidPoly2 = XContentFactory.jsonBuilder().startObject().field("type", "polygon")
Expand All @@ -185,7 +187,7 @@ public void testParse_invalidPolygon() throws IOException {

parser = JsonXContent.jsonXContent.createParser(invalidPoly2);
parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser);
ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class);

// test case 3: create an invalid polygon with 0 points
String invalidPoly3 = XContentFactory.jsonBuilder().startObject().field("type", "polygon")
Expand All @@ -198,7 +200,7 @@ public void testParse_invalidPolygon() throws IOException {

parser = JsonXContent.jsonXContent.createParser(invalidPoly3);
parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser);
ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class);

// test case 4: create an invalid polygon with null value points
String invalidPoly4 = XContentFactory.jsonBuilder().startObject().field("type", "polygon")
Expand All @@ -211,7 +213,7 @@ public void testParse_invalidPolygon() throws IOException {

parser = JsonXContent.jsonXContent.createParser(invalidPoly4);
parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser);
ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchIllegalArgumentException.class);

// test case 5: create an invalid polygon with 1 invalid LinearRing
String invalidPoly5 = XContentFactory.jsonBuilder().startObject().field("type", "polygon")
Expand All @@ -222,7 +224,7 @@ public void testParse_invalidPolygon() throws IOException {

parser = JsonXContent.jsonXContent.createParser(invalidPoly5);
parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser);
ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchIllegalArgumentException.class);
}

@Test
Expand Down
Expand Up @@ -249,12 +249,13 @@ private static double distance(double lat1, double lon1, double lat2, double lon
return GeoDistance.ARC.calculate(lat1, lon1, lat2, lon2, DistanceUnit.DEFAULT);
}

public static void assertValidParseException(XContentParser parser) {
public static void assertValidException(XContentParser parser, Class expectedException) {
try {
ShapeBuilder.parse(parser);
Assert.fail("process completed successfully when parse exception expected");
Assert.fail("process completed successfully when " + expectedException.getName() + " expected");
} catch (Exception e) {
assert(e instanceof ElasticsearchParseException): "expected ElasticsearchParse exception but found " + e.getClass().getName();
assert(e.getClass().equals(expectedException)):
"expected " + expectedException.getName() + " but found " + e.getClass().getName();
}
}
}

0 comments on commit 0067a0c

Please sign in to comment.