Add unittest for GeoPoint seriazliation and corresponding writeGeoPoint method

This commit is contained in:
Simon Willnauer 2015-10-08 12:02:19 +02:00
parent cc0f8c2eb1
commit 77a328a91f
2 changed files with 29 additions and 10 deletions

View File

@ -411,8 +411,7 @@ public abstract class StreamOutput extends OutputStream {
writeBytesRef((BytesRef) value); writeBytesRef((BytesRef) value);
} else if (type == GeoPoint.class) { } else if (type == GeoPoint.class) {
writeByte((byte) 22); writeByte((byte) 22);
writeDouble(((GeoPoint) value).lat()); writeGeoPoint((GeoPoint) value);
writeDouble(((GeoPoint) value).lon());
} else { } else {
throw new IOException("Can't write type [" + type + "]"); 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 { public void writeThrowable(Throwable throwable) throws IOException {
if (throwable == null) { if (throwable == null) {
writeBoolean(false); writeBoolean(false);
@ -573,4 +564,12 @@ public abstract class StreamOutput extends OutputStream {
writeString(namedWriteable.getWriteableName()); writeString(namedWriteable.getWriteableName());
namedWriteable.writeTo(this); namedWriteable.writeTo(this);
} }
/**
* Writes the given {@link GeoPoint} to the stream
*/
public void writeGeoPoint(GeoPoint geoPoint) throws IOException {
writeDouble(geoPoint.lat());
writeDouble(geoPoint.lon());
}
} }

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.io.stream; package org.elasticsearch.common.io.stream;
import org.apache.lucene.util.Constants; import org.apache.lucene.util.Constants;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
@ -477,4 +478,23 @@ public class BytesStreamsTests extends ESTestCase {
getRandom().nextBytes(data); getRandom().nextBytes(data);
return 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);
}
}
} }