|
| 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.BOX; |
| 12 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.LSEG; |
| 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 LsegCodec}. |
| 21 | + */ |
| 22 | +public class LsegCodecUnitTest { |
| 23 | + |
| 24 | + private static final int dataType = LSEG.getObjectId(); |
| 25 | + |
| 26 | + @Test |
| 27 | + void constructorNoByteBufAllocator() { |
| 28 | + assertThatIllegalArgumentException().isThrownBy(() -> new LsegCodec(null)) |
| 29 | + .withMessage("byteBufAllocator must not be null"); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void decode() { |
| 34 | + Lseg lseg = Lseg.of(Point.of(1.11, 2.22), Point.of(3.33, 4.44)); |
| 35 | + |
| 36 | + ByteBuf boxTextFormat = encode(TEST, "[(1.11,2.22),(3.33,4.44)]"); |
| 37 | + assertThat(new LsegCodec(TEST).decode(boxTextFormat, dataType, FORMAT_TEXT, Lseg.class)) |
| 38 | + .isEqualTo(lseg); |
| 39 | + |
| 40 | + ByteBuf boxByteFormat = TEST.buffer(32) |
| 41 | + .writeDouble(1.11).writeDouble(2.22) |
| 42 | + .writeDouble(3.33).writeDouble(4.44); |
| 43 | + assertThat(new LsegCodec(TEST).decode(boxByteFormat, dataType, FORMAT_BINARY, Lseg.class)) |
| 44 | + .isEqualTo(lseg); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void decodeNoByteBuf() { |
| 49 | + assertThat(new LsegCodec(TEST).decode(null, dataType, FORMAT_TEXT, Lseg.class)).isNull(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void doCanDecode() { |
| 54 | + LsegCodec codec = new LsegCodec(TEST); |
| 55 | + |
| 56 | + assertThat(codec.doCanDecode(LSEG, FORMAT_BINARY)).isTrue(); |
| 57 | + assertThat(codec.doCanDecode(BOX, FORMAT_TEXT)).isFalse(); |
| 58 | + assertThat(codec.doCanDecode(POINT, FORMAT_TEXT)).isFalse(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void doCanDecodeNoFormat() { |
| 63 | + assertThatIllegalArgumentException().isThrownBy(() -> new LsegCodec(TEST).doCanDecode(LSEG, null)) |
| 64 | + .withMessage("format must not be null"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void doCanDecodeNoType() { |
| 69 | + assertThatIllegalArgumentException().isThrownBy(() -> new LsegCodec(TEST).doCanDecode(null, FORMAT_TEXT)) |
| 70 | + .withMessage("type must not be null"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void doEncode() { |
| 75 | + Lseg lseg = Lseg.of(Point.of(1.11, 2.22), Point.of(3.33, 4.44)); |
| 76 | + |
| 77 | + ParameterAssert.assertThat(new LsegCodec(TEST).doEncode(lseg)) |
| 78 | + .hasFormat(FORMAT_BINARY) |
| 79 | + .hasType(dataType) |
| 80 | + .hasValue(TEST.buffer(32) |
| 81 | + .writeDouble(1.11).writeDouble(2.22) |
| 82 | + .writeDouble(3.33).writeDouble(4.44) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void doEncodeNoValue() { |
| 88 | + assertThatIllegalArgumentException().isThrownBy(() -> new LsegCodec(TEST).doEncode(null)) |
| 89 | + .withMessage("value must not be null"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void encodeNull() { |
| 94 | + ParameterAssert.assertThat(new LsegCodec(TEST).encodeNull()) |
| 95 | + .isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE)); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments