|
17 | 17 | package io.r2dbc.postgresql.codec;
|
18 | 18 |
|
19 | 19 | import io.netty.buffer.ByteBuf;
|
| 20 | +import io.netty.buffer.Unpooled; |
20 | 21 | import io.r2dbc.postgresql.client.EncodedParameter;
|
21 | 22 | import io.r2dbc.postgresql.client.ParameterAssert;
|
22 | 23 | import io.r2dbc.postgresql.util.ByteBufUtils;
|
@@ -54,106 +55,100 @@ final class PostgisGeometryCodecUnitTests {
|
54 | 55 |
|
55 | 56 | private static final int dataType = 23456;
|
56 | 57 |
|
57 |
| - private final PostgisGeometryCodec codec = new PostgisGeometryCodec(TEST, dataType); |
| 58 | + private final PostgisGeometryCodec codec = new PostgisGeometryCodec(dataType); |
58 | 59 |
|
59 | 60 | private final WKBWriter wkbWriter = new WKBWriter();
|
60 | 61 |
|
61 | 62 | private final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), WGS84_SRID);
|
62 | 63 |
|
63 |
| - private final Point point = geometryFactory.createPoint(new Coordinate(1.0, 1.0)); |
64 |
| - |
65 |
| - @Test |
66 |
| - void constructorNoByteBufAllocator() { |
67 |
| - assertThatIllegalArgumentException().isThrownBy(() -> new PostgisGeometryCodec(null, dataType)) |
68 |
| - .withMessage("byteBufAllocator must not be null"); |
69 |
| - } |
| 64 | + private final Point point = this.geometryFactory.createPoint(new Coordinate(1.0, 1.0)); |
70 | 65 |
|
71 | 66 | @Test
|
72 | 67 | void canDecodeNoFormat() {
|
73 |
| - assertThatIllegalArgumentException().isThrownBy(() -> codec.canDecode(dataType, null, Geometry.class)) |
| 68 | + assertThatIllegalArgumentException().isThrownBy(() -> this.codec.canDecode(dataType, null, Geometry.class)) |
74 | 69 | .withMessage("format must not be null");
|
75 | 70 | }
|
76 | 71 |
|
77 | 72 | @Test
|
78 | 73 | void canDecodeNoClass() {
|
79 |
| - assertThatIllegalArgumentException().isThrownBy(() -> codec.canDecode(dataType, FORMAT_TEXT, null)) |
| 74 | + assertThatIllegalArgumentException().isThrownBy(() -> this.codec.canDecode(dataType, FORMAT_TEXT, null)) |
80 | 75 | .withMessage("type must not be null");
|
81 | 76 | }
|
82 | 77 |
|
83 | 78 | @Test
|
84 | 79 | void canDecode() {
|
85 |
| - assertThat(codec.canDecode(dataType, FORMAT_TEXT, Geometry.class)).isTrue(); |
86 |
| - assertThat(codec.canDecode(dataType, FORMAT_BINARY, Geometry.class)).isTrue(); |
87 |
| - |
88 |
| - assertThat(codec.canDecode(dataType, FORMAT_TEXT, Point.class)).isTrue(); |
89 |
| - assertThat(codec.canDecode(dataType, FORMAT_TEXT, MultiPoint.class)).isTrue(); |
90 |
| - assertThat(codec.canDecode(dataType, FORMAT_TEXT, LineString.class)).isTrue(); |
91 |
| - assertThat(codec.canDecode(dataType, FORMAT_TEXT, LinearRing.class)).isTrue(); |
92 |
| - assertThat(codec.canDecode(dataType, FORMAT_TEXT, MultiLineString.class)).isTrue(); |
93 |
| - assertThat(codec.canDecode(dataType, FORMAT_TEXT, Polygon.class)).isTrue(); |
94 |
| - assertThat(codec.canDecode(dataType, FORMAT_TEXT, MultiPolygon.class)).isTrue(); |
95 |
| - assertThat(codec.canDecode(dataType, FORMAT_TEXT, GeometryCollection.class)).isTrue(); |
96 |
| - |
97 |
| - assertThat(codec.canDecode(VARCHAR.getObjectId(), FORMAT_BINARY, Geometry.class)).isFalse(); |
98 |
| - assertThat(codec.canDecode(JSON.getObjectId(), FORMAT_TEXT, Geometry.class)).isFalse(); |
99 |
| - assertThat(codec.canDecode(JSONB.getObjectId(), FORMAT_BINARY, Geometry.class)).isFalse(); |
| 80 | + assertThat(this.codec.canDecode(dataType, FORMAT_TEXT, Geometry.class)).isTrue(); |
| 81 | + assertThat(this.codec.canDecode(dataType, FORMAT_BINARY, Geometry.class)).isTrue(); |
| 82 | + |
| 83 | + assertThat(this.codec.canDecode(dataType, FORMAT_TEXT, Point.class)).isTrue(); |
| 84 | + assertThat(this.codec.canDecode(dataType, FORMAT_TEXT, MultiPoint.class)).isTrue(); |
| 85 | + assertThat(this.codec.canDecode(dataType, FORMAT_TEXT, LineString.class)).isTrue(); |
| 86 | + assertThat(this.codec.canDecode(dataType, FORMAT_TEXT, LinearRing.class)).isTrue(); |
| 87 | + assertThat(this.codec.canDecode(dataType, FORMAT_TEXT, MultiLineString.class)).isTrue(); |
| 88 | + assertThat(this.codec.canDecode(dataType, FORMAT_TEXT, Polygon.class)).isTrue(); |
| 89 | + assertThat(this.codec.canDecode(dataType, FORMAT_TEXT, MultiPolygon.class)).isTrue(); |
| 90 | + assertThat(this.codec.canDecode(dataType, FORMAT_TEXT, GeometryCollection.class)).isTrue(); |
| 91 | + |
| 92 | + assertThat(this.codec.canDecode(VARCHAR.getObjectId(), FORMAT_BINARY, Geometry.class)).isFalse(); |
| 93 | + assertThat(this.codec.canDecode(JSON.getObjectId(), FORMAT_TEXT, Geometry.class)).isFalse(); |
| 94 | + assertThat(this.codec.canDecode(JSONB.getObjectId(), FORMAT_BINARY, Geometry.class)).isFalse(); |
100 | 95 | }
|
101 | 96 |
|
102 | 97 | @Test
|
103 | 98 | void canEncodeNoValue() {
|
104 |
| - assertThatIllegalArgumentException().isThrownBy(() -> codec.canEncode(null)) |
| 99 | + assertThatIllegalArgumentException().isThrownBy(() -> this.codec.canEncode(null)) |
105 | 100 | .withMessage("value must not be null");
|
106 | 101 | }
|
107 | 102 |
|
108 | 103 | @Test
|
109 | 104 | void canEncode() {
|
110 |
| - assertThat(codec.canEncode(geometryFactory.createPoint())).isTrue(); |
111 |
| - assertThat(codec.canEncode(geometryFactory.createMultiPoint())).isTrue(); |
112 |
| - assertThat(codec.canEncode(geometryFactory.createLineString())).isTrue(); |
113 |
| - assertThat(codec.canEncode(geometryFactory.createLinearRing())).isTrue(); |
114 |
| - assertThat(codec.canEncode(geometryFactory.createMultiLineString())).isTrue(); |
115 |
| - assertThat(codec.canEncode(geometryFactory.createPolygon())).isTrue(); |
116 |
| - assertThat(codec.canEncode(geometryFactory.createMultiPolygon())).isTrue(); |
117 |
| - assertThat(codec.canEncode(geometryFactory.createGeometryCollection())).isTrue(); |
118 |
| - |
119 |
| - assertThat(codec.canEncode("Geometry")).isFalse(); |
120 |
| - assertThat(codec.canEncode(1)).isFalse(); |
| 105 | + assertThat(this.codec.canEncode(this.geometryFactory.createPoint())).isTrue(); |
| 106 | + assertThat(this.codec.canEncode(this.geometryFactory.createMultiPoint())).isTrue(); |
| 107 | + assertThat(this.codec.canEncode(this.geometryFactory.createLineString())).isTrue(); |
| 108 | + assertThat(this.codec.canEncode(this.geometryFactory.createLinearRing())).isTrue(); |
| 109 | + assertThat(this.codec.canEncode(this.geometryFactory.createMultiLineString())).isTrue(); |
| 110 | + assertThat(this.codec.canEncode(this.geometryFactory.createPolygon())).isTrue(); |
| 111 | + assertThat(this.codec.canEncode(this.geometryFactory.createMultiPolygon())).isTrue(); |
| 112 | + assertThat(this.codec.canEncode(this.geometryFactory.createGeometryCollection())).isTrue(); |
| 113 | + |
| 114 | + assertThat(this.codec.canEncode("Geometry")).isFalse(); |
| 115 | + assertThat(this.codec.canEncode(1)).isFalse(); |
121 | 116 | }
|
122 | 117 |
|
123 | 118 | @Test
|
124 | 119 | @SuppressWarnings("unchecked")
|
125 | 120 | void decode() {
|
126 |
| - byte[] pointBytes = wkbWriter.write(point); |
| 121 | + byte[] pointBytes = this.wkbWriter.write(this.point); |
127 | 122 | ByteBuf pointByteBuf = ByteBufUtils.encode(TEST, WKBWriter.toHex(pointBytes));
|
128 | 123 |
|
129 |
| - assertThat(codec.decode(pointByteBuf, dataType, FORMAT_TEXT, Geometry.class)).isEqualTo(point); |
| 124 | + assertThat(this.codec.decode(pointByteBuf, dataType, FORMAT_TEXT, Geometry.class)).isEqualTo(this.point); |
130 | 125 | }
|
131 | 126 |
|
132 | 127 | @Test
|
133 | 128 | @SuppressWarnings("unchecked")
|
134 | 129 | void decodeNoByteBuf() {
|
135 |
| - assertThat(codec.decode(null, dataType, FORMAT_TEXT, Geometry.class)).isNull(); |
| 130 | + assertThat(this.codec.decode(null, dataType, FORMAT_TEXT, Geometry.class)).isNull(); |
136 | 131 | }
|
137 | 132 |
|
138 | 133 | @Test
|
139 | 134 | void encode() {
|
140 |
| - ByteBuf encoded = ByteBufUtils.encode(TEST, point.toText()); |
| 135 | + ByteBuf encoded = Unpooled.wrappedBuffer(new WKBWriter(2, true).write(this.point)); |
141 | 136 |
|
142 |
| - ParameterAssert.assertThat(codec.encode(point)) |
143 |
| - .hasFormat(FORMAT_TEXT) |
| 137 | + ParameterAssert.assertThat(this.codec.encode(this.point)) |
| 138 | + .hasFormat(FORMAT_BINARY) |
144 | 139 | .hasType(dataType)
|
145 | 140 | .hasValue(encoded);
|
146 | 141 | }
|
147 | 142 |
|
148 | 143 | @Test
|
149 | 144 | void encodeNoValue() {
|
150 |
| - assertThatIllegalArgumentException().isThrownBy(() -> codec.encode(null)) |
| 145 | + assertThatIllegalArgumentException().isThrownBy(() -> this.codec.encode(null)) |
151 | 146 | .withMessage("value must not be null");
|
152 | 147 | }
|
153 | 148 |
|
154 | 149 | @Test
|
155 | 150 | void encodeNull() {
|
156 |
| - assertThat(new PostgisGeometryCodec(TEST, dataType).encodeNull()) |
| 151 | + assertThat(new PostgisGeometryCodec(dataType).encodeNull()) |
157 | 152 | .isEqualTo(new EncodedParameter(FORMAT_BINARY, dataType, NULL_VALUE));
|
158 | 153 | }
|
159 | 154 |
|
|
0 commit comments