Add unittest for GeoPoint seriazliation and corresponding writeGeoPoint method
This commit is contained in:
parent
cc0f8c2eb1
commit
77a328a91f
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue