|
| 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 org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE; |
| 10 | +import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; |
| 11 | +import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; |
| 12 | +import static io.r2dbc.postgresql.util.ByteBufUtils.encode; |
| 13 | +import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST; |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Abstract class for unit testing {@link ArrayCodec}s. It provides |
| 19 | + * generic tests for array codecs. |
| 20 | + */ |
| 21 | +abstract class AbstractArrayCodecUnitTests<T> { |
| 22 | + |
| 23 | + ArrayCodec<T> codec; |
| 24 | + |
| 25 | + abstract ArrayCodec<T> createInstance(); |
| 26 | + |
| 27 | + @BeforeEach |
| 28 | + void setUp() { |
| 29 | + codec = createInstance(); |
| 30 | + } |
| 31 | + |
| 32 | + abstract PostgresqlObjectId getPostgresqlObjectId(); |
| 33 | + |
| 34 | + abstract PostgresqlObjectId getArrayPostgresqlObjectId(); |
| 35 | + |
| 36 | + abstract ByteBuf getSingleDimensionBinaryArray(); |
| 37 | + |
| 38 | + abstract ByteBuf getTwoDimensionBinaryArray(); |
| 39 | + |
| 40 | + abstract Class<? extends T[]> getSingleDimensionArrayType(); |
| 41 | + |
| 42 | + abstract Class<? extends T[][]> getTwoDimensionArrayType(); |
| 43 | + |
| 44 | + abstract T[] getExpectedSingleDimensionArray(); |
| 45 | + |
| 46 | + abstract T[][] getExpectedTwoDimensionArray(); |
| 47 | + |
| 48 | + abstract String getSingleDimensionStringInput(); |
| 49 | + |
| 50 | + abstract String getTwoDimensionStringInput(); |
| 51 | + |
| 52 | + @Test |
| 53 | + void decodeItem() { |
| 54 | + T[] expected = getExpectedSingleDimensionArray(); |
| 55 | + assertThat(codec.decode(getSingleDimensionBinaryArray(), getArrayPostgresqlObjectId().getObjectId(), FORMAT_BINARY, getSingleDimensionArrayType())) |
| 56 | + .isEqualTo(expected); |
| 57 | + assertThat(codec.decode(encode(TEST, getSingleDimensionStringInput()), getArrayPostgresqlObjectId().getObjectId(), FORMAT_TEXT, getSingleDimensionArrayType())) |
| 58 | + .isEqualTo(expected); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void decodeItem_textArray() { |
| 63 | + assertThat(codec.decode(encode(TEST, getSingleDimensionStringInput()), getArrayPostgresqlObjectId().getObjectId(), FORMAT_TEXT, getSingleDimensionArrayType())) |
| 64 | + .isEqualTo(getExpectedSingleDimensionArray()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @SuppressWarnings("unchecked") |
| 69 | + void decodeItem_emptyArray() { |
| 70 | + T[][] empty = (T[][]) new Object[][]{}; |
| 71 | + assertThat(codec.decode(encode(TEST, "{}"), getArrayPostgresqlObjectId().getObjectId(), FORMAT_TEXT, getTwoDimensionArrayType())) |
| 72 | + .isEqualTo(empty); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @SuppressWarnings("unchecked") |
| 77 | + void decodeItem_emptyBinaryArray() { |
| 78 | + ByteBuf buf = TEST |
| 79 | + .buffer() |
| 80 | + .writeInt(0) |
| 81 | + .writeInt(0) |
| 82 | + .writeInt(getPostgresqlObjectId().getObjectId()); |
| 83 | + |
| 84 | + T[][] empty = (T[][]) new Object[][]{}; |
| 85 | + assertThat(codec.decode(buf, getArrayPostgresqlObjectId().getObjectId(), FORMAT_BINARY, getTwoDimensionArrayType())) |
| 86 | + .isEqualTo(empty); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void decodeItem_expectedLessDimensionsInArray() { |
| 91 | + assertThatIllegalArgumentException() |
| 92 | + .isThrownBy(() -> codec.decode(encode(TEST, "{" + getSingleDimensionStringInput() + "}"), getArrayPostgresqlObjectId().getObjectId(), FORMAT_TEXT, getSingleDimensionArrayType())) |
| 93 | + .withMessage("Dimensions mismatch: 1 expected, but 2 returned from DB"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void decodeItem_expectedLessDimensionsInBinaryArray() { |
| 98 | + assertThatIllegalArgumentException() |
| 99 | + .isThrownBy(() -> codec.decode(getTwoDimensionBinaryArray(), getArrayPostgresqlObjectId().getObjectId(), FORMAT_BINARY, getSingleDimensionArrayType())) |
| 100 | + .withMessage("Dimensions mismatch: 1 expected, but 2 returned from DB"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void decodeItem_expectedMoreDimensionsInArray() { |
| 105 | + assertThatIllegalArgumentException() |
| 106 | + .isThrownBy(() -> codec.decode(encode(TEST, getSingleDimensionStringInput()), getArrayPostgresqlObjectId().getObjectId(), FORMAT_TEXT, getTwoDimensionArrayType())) |
| 107 | + .withMessage("Dimensions mismatch: 2 expected, but 1 returned from DB"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void decodeItem_expectedMoreDimensionsInBinaryArray() { |
| 112 | + assertThatIllegalArgumentException() |
| 113 | + .isThrownBy(() -> codec.decode(getSingleDimensionBinaryArray(), getArrayPostgresqlObjectId().getObjectId(), FORMAT_BINARY, getTwoDimensionArrayType())) |
| 114 | + .withMessage("Dimensions mismatch: 2 expected, but 1 returned from DB"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void decodeItem_twoDimensionalArrayWithNull() { |
| 119 | + assertThat(codec.decode(encode(TEST, getTwoDimensionStringInput()), getArrayPostgresqlObjectId().getObjectId(), FORMAT_TEXT, getTwoDimensionArrayType())) |
| 120 | + .isEqualTo(getExpectedTwoDimensionArray()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void decodeItem_twoDimensionalBinaryArrayWithNull() { |
| 125 | + assertThat(codec.decode(getTwoDimensionBinaryArray(), getArrayPostgresqlObjectId().getObjectId(), FORMAT_BINARY, getTwoDimensionArrayType())) |
| 126 | + .isEqualTo(getExpectedTwoDimensionArray()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 131 | + void decodeObject() { |
| 132 | + Codec genericCodec = codec; |
| 133 | + assertThat(genericCodec.canDecode(getArrayPostgresqlObjectId().getObjectId(), FORMAT_TEXT, Object.class)).isTrue(); |
| 134 | + T[] expected = getExpectedSingleDimensionArray(); |
| 135 | + |
| 136 | + assertThat(genericCodec.decode(getSingleDimensionBinaryArray(), getArrayPostgresqlObjectId().getObjectId(), FORMAT_BINARY, Object.class)) |
| 137 | + .isEqualTo(expected); |
| 138 | + assertThat(genericCodec.decode(encode(TEST, getSingleDimensionStringInput()), getArrayPostgresqlObjectId().getObjectId(), FORMAT_TEXT, Object.class)) |
| 139 | + .isEqualTo(expected); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void doCanDecode() { |
| 144 | + assertThat(codec.doCanDecode(getPostgresqlObjectId(), FORMAT_TEXT)).isFalse(); |
| 145 | + assertThat(codec.doCanDecode(getArrayPostgresqlObjectId(), FORMAT_TEXT)).isTrue(); |
| 146 | + assertThat(codec.doCanDecode(getArrayPostgresqlObjectId(), FORMAT_BINARY)).isTrue(); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void doCanDecodeNoType() { |
| 151 | + assertThatIllegalArgumentException().isThrownBy(() -> codec.doCanDecode(null, null)) |
| 152 | + .withMessage("type must not be null"); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void encodeArray() { |
| 157 | + ParameterAssert.assertThat(codec.encodeArray(() -> encode(TEST, getSingleDimensionStringInput()), getArrayPostgresqlObjectId())) |
| 158 | + .hasFormat(FORMAT_TEXT) |
| 159 | + .hasType(getArrayPostgresqlObjectId().getObjectId()) |
| 160 | + .hasValue(encode(TEST, getSingleDimensionStringInput())); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void encodeNull() { |
| 165 | + ParameterAssert.assertThat(codec.encodeNull()) |
| 166 | + .isEqualTo(new EncodedParameter(FORMAT_BINARY, getArrayPostgresqlObjectId().getObjectId(), NULL_VALUE)); |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments