|
| 1 | +package io.r2dbc.postgresql.codec; |
| 2 | + |
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.r2dbc.postgresql.client.EncodedParameter; |
| 5 | +import io.r2dbc.postgresql.client.ParameterAssert; |
| 6 | +import io.r2dbc.postgresql.util.ByteBufUtils; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.locationtech.jts.geom.Coordinate; |
| 9 | +import org.locationtech.jts.geom.Geometry; |
| 10 | +import org.locationtech.jts.geom.GeometryCollection; |
| 11 | +import org.locationtech.jts.geom.GeometryFactory; |
| 12 | +import org.locationtech.jts.geom.LineString; |
| 13 | +import org.locationtech.jts.geom.LinearRing; |
| 14 | +import org.locationtech.jts.geom.MultiLineString; |
| 15 | +import org.locationtech.jts.geom.MultiPoint; |
| 16 | +import org.locationtech.jts.geom.MultiPolygon; |
| 17 | +import org.locationtech.jts.geom.Point; |
| 18 | +import org.locationtech.jts.geom.Polygon; |
| 19 | +import org.locationtech.jts.geom.PrecisionModel; |
| 20 | +import org.locationtech.jts.io.WKBWriter; |
| 21 | + |
| 22 | +import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE; |
| 23 | +import static io.r2dbc.postgresql.codec.PostgresqlObjectId.JSON; |
| 24 | +import static io.r2dbc.postgresql.codec.PostgresqlObjectId.JSONB; |
| 25 | +import static io.r2dbc.postgresql.codec.PostgresqlObjectId.VARCHAR; |
| 26 | +import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; |
| 27 | +import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; |
| 28 | +import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST; |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 31 | + |
| 32 | +/** |
| 33 | + * Unit tests for {@link PostgisGeometryCodec }. |
| 34 | + */ |
| 35 | +final class PostgisGeometryCodecUnitTests { |
| 36 | + |
| 37 | + private static final int WGS84_SRID = 4326; |
| 38 | + |
| 39 | + private static final int dataType = 23456; |
| 40 | + |
| 41 | + private final PostgisGeometryCodec codec = new PostgisGeometryCodec(TEST, dataType); |
| 42 | + |
| 43 | + private final WKBWriter wkbWriter = new WKBWriter(); |
| 44 | + |
| 45 | + private final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), WGS84_SRID); |
| 46 | + |
| 47 | + private final Point point = geometryFactory.createPoint(new Coordinate(1.0, 1.0)); |
| 48 | + |
| 49 | + @Test |
| 50 | + void constructorNoByteBufAllocator() { |
| 51 | + assertThatIllegalArgumentException().isThrownBy(() -> new PostgisGeometryCodec(null, dataType)) |
| 52 | + .withMessage("byteBufAllocator must not be null"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void canDecodeNoFormat() { |
| 57 | + assertThatIllegalArgumentException().isThrownBy(() -> codec.canDecode(dataType, null, Geometry.class)) |
| 58 | + .withMessage("format must not be null"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void canDecodeNoClass() { |
| 63 | + assertThatIllegalArgumentException().isThrownBy(() -> codec.canDecode(dataType, FORMAT_TEXT, null)) |
| 64 | + .withMessage("type must not be null"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void canDecode() { |
| 69 | + assertThat(codec.canDecode(dataType, FORMAT_TEXT, Geometry.class)).isTrue(); |
| 70 | + assertThat(codec.canDecode(dataType, FORMAT_BINARY, Geometry.class)).isTrue(); |
| 71 | + |
| 72 | + assertThat(codec.canDecode(dataType, FORMAT_TEXT, Point.class)).isTrue(); |
| 73 | + assertThat(codec.canDecode(dataType, FORMAT_TEXT, MultiPoint.class)).isTrue(); |
| 74 | + assertThat(codec.canDecode(dataType, FORMAT_TEXT, LineString.class)).isTrue(); |
| 75 | + assertThat(codec.canDecode(dataType, FORMAT_TEXT, LinearRing.class)).isTrue(); |
| 76 | + assertThat(codec.canDecode(dataType, FORMAT_TEXT, MultiLineString.class)).isTrue(); |
| 77 | + assertThat(codec.canDecode(dataType, FORMAT_TEXT, Polygon.class)).isTrue(); |
| 78 | + assertThat(codec.canDecode(dataType, FORMAT_TEXT, MultiPolygon.class)).isTrue(); |
| 79 | + assertThat(codec.canDecode(dataType, FORMAT_TEXT, GeometryCollection.class)).isTrue(); |
| 80 | + |
| 81 | + assertThat(codec.canDecode(VARCHAR.getObjectId(), FORMAT_BINARY, Geometry.class)).isFalse(); |
| 82 | + assertThat(codec.canDecode(JSON.getObjectId(), FORMAT_TEXT, Geometry.class)).isFalse(); |
| 83 | + assertThat(codec.canDecode(JSONB.getObjectId(), FORMAT_BINARY, Geometry.class)).isFalse(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void canEncodeNoValue() { |
| 88 | + assertThatIllegalArgumentException().isThrownBy(() -> codec.canEncode(null)) |
| 89 | + .withMessage("value must not be null"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void canEncode() { |
| 94 | + assertThat(codec.canEncode(geometryFactory.createPoint())).isTrue(); |
| 95 | + assertThat(codec.canEncode(geometryFactory.createMultiPoint())).isTrue(); |
| 96 | + assertThat(codec.canEncode(geometryFactory.createLineString())).isTrue(); |
| 97 | + assertThat(codec.canEncode(geometryFactory.createLinearRing())).isTrue(); |
| 98 | + assertThat(codec.canEncode(geometryFactory.createMultiLineString())).isTrue(); |
| 99 | + assertThat(codec.canEncode(geometryFactory.createPolygon())).isTrue(); |
| 100 | + assertThat(codec.canEncode(geometryFactory.createMultiPolygon())).isTrue(); |
| 101 | + assertThat(codec.canEncode(geometryFactory.createGeometryCollection())).isTrue(); |
| 102 | + |
| 103 | + assertThat(codec.canEncode("Geometry")).isFalse(); |
| 104 | + assertThat(codec.canEncode(1)).isFalse(); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @SuppressWarnings("unchecked") |
| 109 | + void decode() { |
| 110 | + byte[] pointBytes = wkbWriter.write(point); |
| 111 | + ByteBuf pointByteBuf = ByteBufUtils.encode(TEST, WKBWriter.toHex(pointBytes)); |
| 112 | + |
| 113 | + assertThat(codec.decode(pointByteBuf, dataType, FORMAT_TEXT, Geometry.class)).isEqualTo(point); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + @SuppressWarnings("unchecked") |
| 118 | + void decodeNoByteBuf() { |
| 119 | + assertThat(codec.decode(null, dataType, FORMAT_TEXT, Geometry.class)).isNull(); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void encode() { |
| 124 | + ByteBuf encoded = ByteBufUtils.encode(TEST, point.toText()); |
| 125 | + |
| 126 | + ParameterAssert.assertThat(codec.encode(point)) |
| 127 | + .hasFormat(FORMAT_TEXT) |
| 128 | + .hasType(dataType) |
| 129 | + .hasValue(encoded); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void encodeNoValue() { |
| 134 | + assertThatIllegalArgumentException().isThrownBy(() -> codec.encode(null)) |
| 135 | + .withMessage("value must not be null"); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void encodeNull() { |
| 140 | + assertThat(new PostgisGeometryCodec(TEST, dataType).encodeNull()) |
| 141 | + .isEqualTo(new EncodedParameter(FORMAT_BINARY, dataType, NULL_VALUE)); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments