From 77a328a91fdb85cd9688672570f2bac5a83fccb9 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 8 Oct 2015 12:02:19 +0200 Subject: [PATCH] Add unittest for GeoPoint seriazliation and corresponding writeGeoPoint method --- .../common/io/stream/StreamOutput.java | 19 +++++++++--------- .../common/io/stream/BytesStreamsTests.java | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 563f4a1fd0f..8120a83533f 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -411,8 +411,7 @@ public abstract class StreamOutput extends OutputStream { writeBytesRef((BytesRef) value); } else if (type == GeoPoint.class) { writeByte((byte) 22); - writeDouble(((GeoPoint) value).lat()); - writeDouble(((GeoPoint) value).lon()); + writeGeoPoint((GeoPoint) value); } else { throw new IOException("Can't write type [" + type + "]"); } @@ -458,14 +457,6 @@ public abstract class StreamOutput extends OutputStream { } } - private static int parseIntSafe(String val, int defaultVal) { - try { - return Integer.parseInt(val); - } catch (NumberFormatException ex) { - return defaultVal; - } - } - public void writeThrowable(Throwable throwable) throws IOException { if (throwable == null) { writeBoolean(false); @@ -573,4 +564,12 @@ public abstract class StreamOutput extends OutputStream { writeString(namedWriteable.getWriteableName()); namedWriteable.writeTo(this); } + + /** + * Writes the given {@link GeoPoint} to the stream + */ + public void writeGeoPoint(GeoPoint geoPoint) throws IOException { + writeDouble(geoPoint.lat()); + writeDouble(geoPoint.lon()); + } } diff --git a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java index d313dd71d81..daf292e8ca2 100644 --- a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java +++ b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.common.io.stream; import org.apache.lucene.util.Constants; +import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.test.ESTestCase; @@ -477,4 +478,23 @@ public class BytesStreamsTests extends ESTestCase { getRandom().nextBytes(data); return data; } + + public void testReadWriteGeoPoint() throws IOException { + { + BytesStreamOutput out = new BytesStreamOutput(); + GeoPoint geoPoint = new GeoPoint(randomDouble(), randomDouble()); + out.writeGenericValue(geoPoint); + StreamInput wrap = StreamInput.wrap(out.bytes()); + GeoPoint point = (GeoPoint) wrap.readGenericValue(); + assertEquals(point, geoPoint); + } + { + BytesStreamOutput out = new BytesStreamOutput(); + GeoPoint geoPoint = new GeoPoint(randomDouble(), randomDouble()); + out.writeGeoPoint(geoPoint); + StreamInput wrap = StreamInput.wrap(out.bytes()); + GeoPoint point = wrap.readGeoPoint(); + assertEquals(point, geoPoint); + } + } }