|
| 1 | +package io.r2dbc.postgresql.codec; |
| 2 | + |
| 3 | +import io.r2dbc.postgresql.client.Parameter; |
| 4 | +import io.r2dbc.postgresql.client.ParameterAssert; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE; |
| 8 | +import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; |
| 9 | +import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; |
| 10 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.LINE; |
| 11 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.PATH; |
| 12 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.POLYGON; |
| 13 | +import static io.r2dbc.postgresql.util.ByteBufUtils.encode; |
| 14 | +import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST; |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 17 | + |
| 18 | +/** |
| 19 | + * Unit tests for {@link PolygonCodec}. |
| 20 | + */ |
| 21 | +public class PolygonCodecUnitTest { |
| 22 | + |
| 23 | + private static final int dataType = POLYGON.getObjectId(); |
| 24 | + |
| 25 | + @Test |
| 26 | + void constructorNoByteBufAllocator() { |
| 27 | + assertThatIllegalArgumentException().isThrownBy(() -> new PolygonCodec(null)) |
| 28 | + .withMessage("byteBufAllocator must not be null"); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void decode() { |
| 33 | + Polygon polygon = Polygon.of(Point.of(-10.42, 3.14), Point.of(10.42, -3.14)); |
| 34 | + |
| 35 | + assertThat(new PolygonCodec(TEST).decode(encode(TEST, "((-10.42,3.14),(10.42,-3.14))"), dataType, FORMAT_TEXT, Polygon.class)).isEqualTo(polygon); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void decodeNoByteBuf() { |
| 40 | + assertThat(new PolygonCodec(TEST).decode(null, dataType, FORMAT_TEXT, Polygon.class)).isNull(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void doCanDecode() { |
| 45 | + PolygonCodec codec = new PolygonCodec(TEST); |
| 46 | + assertThat(codec.doCanDecode(POLYGON, FORMAT_BINARY)).isTrue(); |
| 47 | + assertThat(codec.doCanDecode(LINE, FORMAT_TEXT)).isFalse(); |
| 48 | + assertThat(codec.doCanDecode(PATH, FORMAT_TEXT)).isFalse(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void doCanDecodeNoFormat() { |
| 53 | + assertThatIllegalArgumentException().isThrownBy(() -> new PolygonCodec(TEST).doCanDecode(PATH, null)) |
| 54 | + .withMessage("format must not be null"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void doCanDecodeNoType() { |
| 59 | + assertThatIllegalArgumentException().isThrownBy(() -> new PolygonCodec(TEST).doCanDecode(null, FORMAT_TEXT)) |
| 60 | + .withMessage("type must not be null"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void doEncode() { |
| 65 | + Polygon polygon = Polygon.of(Point.of(-10.42, 3.14), Point.of(10.42, -3.14)); |
| 66 | + ParameterAssert.assertThat(new PolygonCodec(TEST).doEncode(polygon)) |
| 67 | + .hasFormat(FORMAT_BINARY) |
| 68 | + .hasType(dataType) |
| 69 | + .hasValue(TEST.buffer(37) |
| 70 | + .writeInt(2) |
| 71 | + .writeDouble(-10.42) |
| 72 | + .writeDouble(3.14) |
| 73 | + .writeDouble(10.42) |
| 74 | + .writeDouble(-3.14) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void doEncodeNoValue() { |
| 80 | + assertThatIllegalArgumentException().isThrownBy(() -> new PolygonCodec(TEST).doEncode(null)) |
| 81 | + .withMessage("value must not be null"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void encodeNull() { |
| 86 | + ParameterAssert.assertThat(new PolygonCodec(TEST).encodeNull()) |
| 87 | + .isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE)); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments