Updating to throw IllegalArgument exception for null value coordinates. Tests included.

This commit is contained in:
Nicholas Knize 2014-11-14 10:28:30 -06:00
parent 49935659e4
commit 0067a0cb7e
3 changed files with 12 additions and 9 deletions

View File

@ -216,7 +216,7 @@ public abstract class ShapeBuilder implements ToXContent {
token = parser.nextToken(); token = parser.nextToken();
return new CoordinateNode(new Coordinate(lon, lat)); return new CoordinateNode(new Coordinate(lon, lat));
} else if (token == XContentParser.Token.VALUE_NULL) { } else if (token == XContentParser.Token.VALUE_NULL) {
return null; throw new ElasticsearchIllegalArgumentException("coordinates cannot contain NULL values)");
} }
List<CoordinateNode> nodes = new ArrayList<>(); List<CoordinateNode> nodes = new ArrayList<>();

View File

@ -26,6 +26,8 @@ import com.spatial4j.core.shape.ShapeCollection;
import com.spatial4j.core.shape.jts.JtsGeometry; import com.spatial4j.core.shape.jts.JtsGeometry;
import com.spatial4j.core.shape.jts.JtsPoint; import com.spatial4j.core.shape.jts.JtsPoint;
import com.vividsolutions.jts.geom.*; import com.vividsolutions.jts.geom.*;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.geo.builders.ShapeBuilder; import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
@ -172,7 +174,7 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase {
.endObject().string(); .endObject().string();
XContentParser parser = JsonXContent.jsonXContent.createParser(invalidPoly1); XContentParser parser = JsonXContent.jsonXContent.createParser(invalidPoly1);
parser.nextToken(); parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class);
// test case 2: create an invalid polygon with only 1 point // test case 2: create an invalid polygon with only 1 point
String invalidPoly2 = XContentFactory.jsonBuilder().startObject().field("type", "polygon") String invalidPoly2 = XContentFactory.jsonBuilder().startObject().field("type", "polygon")
@ -185,7 +187,7 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase {
parser = JsonXContent.jsonXContent.createParser(invalidPoly2); parser = JsonXContent.jsonXContent.createParser(invalidPoly2);
parser.nextToken(); parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class);
// test case 3: create an invalid polygon with 0 points // test case 3: create an invalid polygon with 0 points
String invalidPoly3 = XContentFactory.jsonBuilder().startObject().field("type", "polygon") String invalidPoly3 = XContentFactory.jsonBuilder().startObject().field("type", "polygon")
@ -198,7 +200,7 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase {
parser = JsonXContent.jsonXContent.createParser(invalidPoly3); parser = JsonXContent.jsonXContent.createParser(invalidPoly3);
parser.nextToken(); parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class);
// test case 4: create an invalid polygon with null value points // test case 4: create an invalid polygon with null value points
String invalidPoly4 = XContentFactory.jsonBuilder().startObject().field("type", "polygon") String invalidPoly4 = XContentFactory.jsonBuilder().startObject().field("type", "polygon")
@ -211,7 +213,7 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase {
parser = JsonXContent.jsonXContent.createParser(invalidPoly4); parser = JsonXContent.jsonXContent.createParser(invalidPoly4);
parser.nextToken(); parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchIllegalArgumentException.class);
// test case 5: create an invalid polygon with 1 invalid LinearRing // test case 5: create an invalid polygon with 1 invalid LinearRing
String invalidPoly5 = XContentFactory.jsonBuilder().startObject().field("type", "polygon") String invalidPoly5 = XContentFactory.jsonBuilder().startObject().field("type", "polygon")
@ -222,7 +224,7 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase {
parser = JsonXContent.jsonXContent.createParser(invalidPoly5); parser = JsonXContent.jsonXContent.createParser(invalidPoly5);
parser.nextToken(); parser.nextToken();
ElasticsearchGeoAssertions.assertValidParseException(parser); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchIllegalArgumentException.class);
} }
@Test @Test

View File

@ -249,12 +249,13 @@ public class ElasticsearchGeoAssertions {
return GeoDistance.ARC.calculate(lat1, lon1, lat2, lon2, DistanceUnit.DEFAULT); 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 { try {
ShapeBuilder.parse(parser); ShapeBuilder.parse(parser);
Assert.fail("process completed successfully when parse exception expected"); Assert.fail("process completed successfully when " + expectedException.getName() + " expected");
} catch (Exception e) { } 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();
} }
} }
} }