From c614dafbb3f74ceb61d934e67e3d56054dc96ed5 Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Thu, 5 Feb 2015 23:06:24 +0100 Subject: [PATCH] increase GeoShapeType coverage --- .../java/io/crate/types/GeoShapeType.java | 1 - .../java/io/crate/types/GeoShapeTypeTest.java | 41 ++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/io/crate/types/GeoShapeType.java b/core/src/main/java/io/crate/types/GeoShapeType.java index 18591d97f422..390b565ff7e6 100644 --- a/core/src/main/java/io/crate/types/GeoShapeType.java +++ b/core/src/main/java/io/crate/types/GeoShapeType.java @@ -63,7 +63,6 @@ public Shape value(Object value) throws IllegalArgumentException, ClassCastExcep if (value == null) { return null; } - if (value instanceof BytesRef) { return shapeFromString(BytesRefs.toString(value)); } diff --git a/core/src/test/java/io/crate/types/GeoShapeTypeTest.java b/core/src/test/java/io/crate/types/GeoShapeTypeTest.java index e566762682df..eea38f1a775a 100644 --- a/core/src/test/java/io/crate/types/GeoShapeTypeTest.java +++ b/core/src/test/java/io/crate/types/GeoShapeTypeTest.java @@ -21,25 +21,56 @@ package io.crate.types; +import com.carrotsearch.randomizedtesting.RandomizedTest; import com.spatial4j.core.shape.Shape; import org.elasticsearch.common.io.stream.BytesStreamInput; import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.hamcrest.Matchers.is; -public class GeoShapeTypeTest { +public class GeoShapeTypeTest extends RandomizedTest { + + private GeoShapeType type = GeoShapeType.INSTANCE; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); @Test public void testStreamer() throws Exception { - Shape value = GeoShapeType.INSTANCE.value("POINT (10 10)"); + Shape value = type.value("POINT (10 10)"); BytesStreamOutput out = new BytesStreamOutput(); - GeoShapeType.INSTANCE.writeValueTo(out, value); + type.writeValueTo(out, value); BytesStreamInput in = new BytesStreamInput(out.bytes()); - Shape streamedShape = GeoShapeType.INSTANCE.readValueFrom(in); + Shape streamedShape = type.readValueFrom(in); assertThat(streamedShape, equalTo(value)); } + + @Test + public void testCompareValueTo() throws Exception { + Shape val1 = type.value("POLYGON ( (0 0, 20 0, 20 20, 0 20, 0 0 ))"); + Shape val2 = type.value("POINT (10 10)"); + + assertThat(type.compareValueTo(val1, val2), is(1)); + assertThat(type.compareValueTo(val2, val1), is(-1)); + assertThat(type.compareValueTo(val2, val2), is(0)); + } + + @Test + public void testInvalidStringValueCausesIllegalArgumentException() throws Exception { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Cannot convert \"foobar\" to geo_shape"); + type.value("foobar"); + } + @Test + public void testInvalidTypeCausesIllegalArgumentException() throws Exception { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Cannot convert \"200\" to geo_shape"); + type.value(200); + } } \ No newline at end of file