diff --git a/src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java b/src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java index 261fc223..4136935d 100644 --- a/src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java +++ b/src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java @@ -253,14 +253,15 @@ private static List> getDefaultCodecs(ByteBufAllocator byteBufAllocator new LocalDateTimeCodec(byteBufAllocator, configuration::getZoneId), new LocalTimeCodec(byteBufAllocator), new LongCodec(byteBufAllocator), + new MonthCodec(byteBufAllocator), new OffsetDateTimeCodec(byteBufAllocator), new OffsetTimeCodec(byteBufAllocator), new ShortCodec(byteBufAllocator), new UriCodec(byteBufAllocator), new UrlCodec(byteBufAllocator), new UuidCodec(byteBufAllocator), - new ZoneIdCodec(byteBufAllocator), new YearCodec(byteBufAllocator), + new ZoneIdCodec(byteBufAllocator), // JSON new JsonCodec(byteBufAllocator, preferAttachedBuffers), diff --git a/src/main/java/io/r2dbc/postgresql/codec/MonthCodec.java b/src/main/java/io/r2dbc/postgresql/codec/MonthCodec.java new file mode 100644 index 00000000..239bacc1 --- /dev/null +++ b/src/main/java/io/r2dbc/postgresql/codec/MonthCodec.java @@ -0,0 +1,28 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.r2dbc.postgresql.codec; + +import io.netty.buffer.ByteBufAllocator; + +import java.time.Month; + +final class MonthCodec extends IntegerCodecDelegate { + + MonthCodec(ByteBufAllocator byteBufAllocator) { + super(Month.class, byteBufAllocator, Month::getValue, Month::of); + } +} diff --git a/src/test/java/io/r2dbc/postgresql/codec/MonthCodecTest.java b/src/test/java/io/r2dbc/postgresql/codec/MonthCodecTest.java new file mode 100644 index 00000000..6ff47167 --- /dev/null +++ b/src/test/java/io/r2dbc/postgresql/codec/MonthCodecTest.java @@ -0,0 +1,109 @@ +package io.r2dbc.postgresql.codec; + +import io.r2dbc.postgresql.client.EncodedParameter; +import io.r2dbc.postgresql.client.ParameterAssert; +import org.junit.jupiter.api.Test; + +import java.time.Month; +import java.util.Arrays; +import java.util.function.Consumer; + +import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE; +import static io.r2dbc.postgresql.codec.PostgresqlObjectId.*; +import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; +import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; +import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +class MonthCodecTest { + + @Test + void constructorNoByteBufAllocator() { + assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(null)) + .withMessage("byteBufAllocator must not be null"); + } + + + @Test + void decode() { + forEveryMonth(m -> + assertThat(new MonthCodec(TEST).decode(TEST.buffer().writeInt(m.getValue()), INT4, FORMAT_BINARY, Month.class)).isEqualTo(m)); + } + + @Test + void decodeNoByteBuf() { + assertThat(new MonthCodec(TEST).decode(null, INT4.getObjectId(), FORMAT_BINARY, Month.class)).isNull(); + } + + @Test + void doCanDecode() { + MonthCodec codec = new MonthCodec(TEST); + + assertThat(codec.doCanDecode(INT4, FORMAT_BINARY)).isTrue(); + assertThat(codec.doCanDecode(INT2, FORMAT_BINARY)).isTrue(); + assertThat(codec.doCanDecode(INT8, FORMAT_BINARY)).isTrue(); + assertThat(codec.doCanDecode(NUMERIC, FORMAT_TEXT)).isTrue(); + assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isFalse(); + } + + @Test + void doCanDecodeNoType() { + assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(TEST).doCanDecode(null, FORMAT_BINARY)) + .withMessage("type must not be null"); + } + + @Test + void doEncodeInt() { + + forEveryMonth(m -> { + ParameterAssert.assertThat(new MonthCodec(TEST).doEncode(m)) + .hasFormat(FORMAT_BINARY) + .hasType(INT4.getObjectId()) + .hasValue(TEST.buffer().writeInt(m.getValue())); + }); + } + + @Test + void doEncodeShort() { + forEveryMonth(m -> { + ParameterAssert.assertThat(new MonthCodec(TEST).doEncode(m, INT2)) + .hasFormat(FORMAT_BINARY) + .hasType(INT2.getObjectId()) + .hasValue(TEST.buffer().writeShort(m.getValue())); + }); + } + + @Test + void doEncodeLong() { + + forEveryMonth(m -> { + ParameterAssert.assertThat(new MonthCodec(TEST).doEncode(m, INT8)) + .hasFormat(FORMAT_BINARY) + .hasType(INT8.getObjectId()) + .hasValue(TEST.buffer().writeLong(m.getValue())); + }); + } + + @Test + void doEncodeNoValue() { + assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(TEST).doEncode(null)) + .withMessage("value must not be null"); + } + + @Test + void encodeItemNoValue() { + assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(TEST).encode(null)) + .withMessage("value must not be null"); + } + + @Test + void encodeNull() { + ParameterAssert.assertThat(new MonthCodec(TEST).encodeNull()) + .isEqualTo(new EncodedParameter(FORMAT_BINARY, INT4.getObjectId(), NULL_VALUE)); + } + + private void forEveryMonth(Consumer assertion) { + Arrays.stream(Month.values()).forEach(assertion); + } +} \ No newline at end of file