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