|
16 | 16 |
|
17 | 17 | package io.r2dbc.postgresql.codec;
|
18 | 18 |
|
| 19 | +import io.netty.buffer.ByteBuf; |
19 | 20 | import io.netty.buffer.Unpooled;
|
20 | 21 | import io.r2dbc.postgresql.client.Parameter;
|
21 | 22 | import io.r2dbc.postgresql.util.ByteBufUtils;
|
22 | 23 | import org.junit.jupiter.api.Test;
|
23 | 24 |
|
24 | 25 | import java.io.ByteArrayInputStream;
|
25 | 26 | import java.nio.ByteBuffer;
|
| 27 | +import java.nio.charset.StandardCharsets; |
26 | 28 |
|
27 | 29 | import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE;
|
28 | 30 | import static io.r2dbc.postgresql.client.ParameterAssert.assertThat;
|
|
34 | 36 | import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
|
35 | 37 | import static org.assertj.core.api.Assertions.assertThat;
|
36 | 38 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 39 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
37 | 40 |
|
38 | 41 | /**
|
39 | 42 | * Unit tests for {@link JsonCodec}.
|
@@ -110,6 +113,32 @@ void doEncode() {
|
110 | 113 | .hasValue(Unpooled.wrappedBuffer(Unpooled.wrappedBuffer(new byte[]{1}), ByteBufUtils.encode(TEST, json)));
|
111 | 114 | }
|
112 | 115 |
|
| 116 | + @Test |
| 117 | + void doEncodeReleasedByteBuf() { |
| 118 | + String json = "{\"name\":\"John Doe\"}"; |
| 119 | + JsonCodec jsonCodec = new JsonCodec(TEST); |
| 120 | + |
| 121 | + ByteBuf buffer = TEST.buffer(); |
| 122 | + buffer.writeCharSequence(json, StandardCharsets.UTF_8); |
| 123 | + |
| 124 | + assertThat(jsonCodec.doEncode(Json.of(buffer))) |
| 125 | + .hasValue(Unpooled.wrappedBuffer(Unpooled.wrappedBuffer(new byte[]{1}), ByteBufUtils.encode(TEST, json))); |
| 126 | + |
| 127 | + assertThat(buffer.refCnt()).isZero(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void doEncodeReleasedJsonOutput() { |
| 132 | + String json = "{\"name\":\"John Doe\"}"; |
| 133 | + JsonCodec jsonCodec = new JsonCodec(TEST); |
| 134 | + Json decodedBytes = jsonCodec.decode(ByteBufUtils.encode(TEST, json), JSON.getObjectId(), FORMAT_TEXT, Json.class); |
| 135 | + |
| 136 | + assertThat(jsonCodec.doEncode(decodedBytes)) |
| 137 | + .hasValue(Unpooled.wrappedBuffer(Unpooled.wrappedBuffer(new byte[]{1}), ByteBufUtils.encode(TEST, json))); |
| 138 | + |
| 139 | + assertThatIllegalStateException().isThrownBy(decodedBytes::asString).withMessage("JSON is already released"); |
| 140 | + } |
| 141 | + |
113 | 142 | @Test
|
114 | 143 | void doEncodeNoValue() {
|
115 | 144 | assertThatIllegalArgumentException().isThrownBy(() -> new JsonCodec(TEST).doEncode(null))
|
|
0 commit comments