|
| 1 | +package io.r2dbc.postgresql.codec; |
| 2 | + |
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.r2dbc.postgresql.client.Parameter; |
| 5 | +import io.r2dbc.postgresql.client.ParameterAssert; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE; |
| 9 | +import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; |
| 10 | +import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; |
| 11 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.LINE; |
| 12 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.PATH; |
| 13 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT; |
| 14 | +import static io.r2dbc.postgresql.util.ByteBufUtils.encode; |
| 15 | +import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST; |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 18 | + |
| 19 | +/** |
| 20 | + * Unit tests for {@link PathCodec}. |
| 21 | + */ |
| 22 | +public class PathCodecUnitTest { |
| 23 | + |
| 24 | + private static final int dataType = PATH.getObjectId(); |
| 25 | + |
| 26 | + @Test |
| 27 | + void constructorNoByteBufAllocator() { |
| 28 | + assertThatIllegalArgumentException().isThrownBy(() -> new PathCodec(null)) |
| 29 | + .withMessage("byteBufAllocator must not be null"); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void decode() { |
| 34 | + Path closedPath = Path.of(false, Point.of(-10.42, 3.14), Point.of(10.42, -3.14)); |
| 35 | + Path openPath = Path.of(true, Point.of(-10.42, 3.14), Point.of(10.42, -3.14)); |
| 36 | + |
| 37 | + assertThat(new PathCodec(TEST).decode(encode(TEST, "((-10.42,3.14),(10.42,-3.14))"), dataType, FORMAT_TEXT, Path.class)).isEqualTo(closedPath); |
| 38 | + assertThat(new PathCodec(TEST).decode(encode(TEST, "[(-10.42,3.14),(10.42,-3.14)]"), dataType, FORMAT_TEXT, Path.class)).isEqualTo(openPath); |
| 39 | + |
| 40 | + ByteBuf closedPathByteFormat = TEST.buffer(37) |
| 41 | + .writeBoolean(false) |
| 42 | + .writeInt(2) |
| 43 | + .writeDouble(-10.42) |
| 44 | + .writeDouble(3.14) |
| 45 | + .writeDouble(10.42) |
| 46 | + .writeDouble(-3.14); |
| 47 | + assertThat(new PathCodec(TEST).decode(closedPathByteFormat, dataType, FORMAT_BINARY, Path.class)).isEqualTo(closedPath); |
| 48 | + ByteBuf openPathByteFormat = TEST.buffer(37) |
| 49 | + .writeBoolean(true) |
| 50 | + .writeInt(2) |
| 51 | + .writeDouble(-10.42) |
| 52 | + .writeDouble(3.14) |
| 53 | + .writeDouble(10.42) |
| 54 | + .writeDouble(-3.14); |
| 55 | + assertThat(new PathCodec(TEST).decode(openPathByteFormat, dataType, FORMAT_BINARY, Path.class)).isEqualTo(openPath); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void decodeNoByteBuf() { |
| 60 | + assertThat(new PathCodec(TEST).decode(null, dataType, FORMAT_TEXT, Path.class)).isNull(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void doCanDecode() { |
| 65 | + PathCodec codec = new PathCodec(TEST); |
| 66 | + assertThat(codec.doCanDecode(PATH, FORMAT_BINARY)).isTrue(); |
| 67 | + assertThat(codec.doCanDecode(PATH, FORMAT_TEXT)).isTrue(); |
| 68 | + assertThat(codec.doCanDecode(LINE, FORMAT_TEXT)).isFalse(); |
| 69 | + assertThat(codec.doCanDecode(POINT, FORMAT_TEXT)).isFalse(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void doCanDecodeNoFormat() { |
| 74 | + assertThatIllegalArgumentException().isThrownBy(() -> new PathCodec(TEST).doCanDecode(PATH, null)) |
| 75 | + .withMessage("format must not be null"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void doCanDecodeNoType() { |
| 80 | + assertThatIllegalArgumentException().isThrownBy(() -> new PathCodec(TEST).doCanDecode(null, FORMAT_TEXT)) |
| 81 | + .withMessage("type must not be null"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void doEncode() { |
| 86 | + Path closedPath = Path.of(false, Point.of(-10.42, 3.14), Point.of(10.42, -3.14)); |
| 87 | + ParameterAssert.assertThat(new PathCodec(TEST).doEncode(closedPath)) |
| 88 | + .hasFormat(FORMAT_BINARY) |
| 89 | + .hasType(dataType) |
| 90 | + .hasValue(TEST.buffer(37) |
| 91 | + .writeBoolean(false) |
| 92 | + .writeInt(2) |
| 93 | + .writeDouble(-10.42) |
| 94 | + .writeDouble(3.14) |
| 95 | + .writeDouble(10.42) |
| 96 | + .writeDouble(-3.14) |
| 97 | + ); |
| 98 | + |
| 99 | + Path openPath = Path.of(true, Point.of(-10.42, 3.14), Point.of(10.42, -3.14)); |
| 100 | + ParameterAssert.assertThat(new PathCodec(TEST).doEncode(openPath)) |
| 101 | + .hasFormat(FORMAT_BINARY) |
| 102 | + .hasType(dataType) |
| 103 | + .hasValue(TEST.buffer(37) |
| 104 | + .writeBoolean(true) |
| 105 | + .writeInt(2) |
| 106 | + .writeDouble(-10.42) |
| 107 | + .writeDouble(3.14) |
| 108 | + .writeDouble(10.42) |
| 109 | + .writeDouble(-3.14) |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void doEncodeNoValue() { |
| 115 | + assertThatIllegalArgumentException().isThrownBy(() -> new PathCodec(TEST).doEncode(null)) |
| 116 | + .withMessage("value must not be null"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void encodeNull() { |
| 121 | + ParameterAssert.assertThat(new PathCodec(TEST).encodeNull()) |
| 122 | + .isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE)); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments