Skip to content

Commit 986ca22

Browse files
committed
adding support for Month data type - pgjdbcgh-591
1 parent 626efb9 commit 986ca22

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed

src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,15 @@ private static List<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator
253253
new LocalDateTimeCodec(byteBufAllocator, configuration::getZoneId),
254254
new LocalTimeCodec(byteBufAllocator),
255255
new LongCodec(byteBufAllocator),
256+
new MonthCodec(byteBufAllocator),
256257
new OffsetDateTimeCodec(byteBufAllocator),
257258
new OffsetTimeCodec(byteBufAllocator),
258259
new ShortCodec(byteBufAllocator),
259260
new UriCodec(byteBufAllocator),
260261
new UrlCodec(byteBufAllocator),
261262
new UuidCodec(byteBufAllocator),
262-
new ZoneIdCodec(byteBufAllocator),
263263
new YearCodec(byteBufAllocator),
264+
new ZoneIdCodec(byteBufAllocator),
264265

265266
// JSON
266267
new JsonCodec(byteBufAllocator, preferAttachedBuffers),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.r2dbc.postgresql.codec;
18+
19+
import io.netty.buffer.ByteBufAllocator;
20+
21+
import java.time.Month;
22+
23+
final class MonthCodec extends IntegerCodecDelegate<Month> {
24+
25+
MonthCodec(ByteBufAllocator byteBufAllocator) {
26+
super(Month.class, byteBufAllocator, Month::getValue, Month::of);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package io.r2dbc.postgresql.codec;
2+
3+
import io.r2dbc.postgresql.client.EncodedParameter;
4+
import io.r2dbc.postgresql.client.ParameterAssert;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.time.Month;
8+
import java.util.Arrays;
9+
import java.util.function.Consumer;
10+
11+
import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE;
12+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.*;
13+
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
14+
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
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+
class MonthCodecTest {
20+
21+
@Test
22+
void constructorNoByteBufAllocator() {
23+
assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(null))
24+
.withMessage("byteBufAllocator must not be null");
25+
}
26+
27+
28+
@Test
29+
void decode() {
30+
forEveryMonth(m ->
31+
assertThat(new MonthCodec(TEST).decode(TEST.buffer().writeInt(m.getValue()), INT4, FORMAT_BINARY, Month.class)).isEqualTo(m));
32+
}
33+
34+
@Test
35+
void decodeNoByteBuf() {
36+
assertThat(new MonthCodec(TEST).decode(null, INT4.getObjectId(), FORMAT_BINARY, Month.class)).isNull();
37+
}
38+
39+
@Test
40+
void doCanDecode() {
41+
MonthCodec codec = new MonthCodec(TEST);
42+
43+
assertThat(codec.doCanDecode(INT4, FORMAT_BINARY)).isTrue();
44+
assertThat(codec.doCanDecode(INT2, FORMAT_BINARY)).isTrue();
45+
assertThat(codec.doCanDecode(INT8, FORMAT_BINARY)).isTrue();
46+
assertThat(codec.doCanDecode(NUMERIC, FORMAT_TEXT)).isTrue();
47+
assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isFalse();
48+
}
49+
50+
@Test
51+
void doCanDecodeNoType() {
52+
assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(TEST).doCanDecode(null, FORMAT_BINARY))
53+
.withMessage("type must not be null");
54+
}
55+
56+
@Test
57+
void doEncodeInt() {
58+
59+
forEveryMonth(m -> {
60+
ParameterAssert.assertThat(new MonthCodec(TEST).doEncode(m))
61+
.hasFormat(FORMAT_BINARY)
62+
.hasType(INT4.getObjectId())
63+
.hasValue(TEST.buffer().writeInt(m.getValue()));
64+
});
65+
}
66+
67+
@Test
68+
void doEncodeShort() {
69+
forEveryMonth(m -> {
70+
ParameterAssert.assertThat(new MonthCodec(TEST).doEncode(m, INT2))
71+
.hasFormat(FORMAT_BINARY)
72+
.hasType(INT2.getObjectId())
73+
.hasValue(TEST.buffer().writeShort(m.getValue()));
74+
});
75+
}
76+
77+
@Test
78+
void doEncodeLong() {
79+
80+
forEveryMonth(m -> {
81+
ParameterAssert.assertThat(new MonthCodec(TEST).doEncode(m, INT8))
82+
.hasFormat(FORMAT_BINARY)
83+
.hasType(INT8.getObjectId())
84+
.hasValue(TEST.buffer().writeLong(m.getValue()));
85+
});
86+
}
87+
88+
@Test
89+
void doEncodeNoValue() {
90+
assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(TEST).doEncode(null))
91+
.withMessage("value must not be null");
92+
}
93+
94+
@Test
95+
void encodeItemNoValue() {
96+
assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(TEST).encode(null))
97+
.withMessage("value must not be null");
98+
}
99+
100+
@Test
101+
void encodeNull() {
102+
ParameterAssert.assertThat(new MonthCodec(TEST).encodeNull())
103+
.isEqualTo(new EncodedParameter(FORMAT_BINARY, INT4.getObjectId(), NULL_VALUE));
104+
}
105+
106+
private void forEveryMonth(Consumer<Month> assertion) {
107+
Arrays.stream(Month.values()).forEach(assertion);
108+
}
109+
}

0 commit comments

Comments
 (0)