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 .Test ;
7
+
8
+ import java .nio .charset .Charset ;
9
+ import java .time .YearMonth ;
10
+ import java .time .format .DateTimeParseException ;
11
+
12
+ import static io .r2dbc .postgresql .client .EncodedParameter .NULL_VALUE ;
13
+ import static io .r2dbc .postgresql .codec .PostgresqlObjectId .BPCHAR ;
14
+ import static io .r2dbc .postgresql .codec .PostgresqlObjectId .CHAR ;
15
+ import static io .r2dbc .postgresql .codec .PostgresqlObjectId .NAME ;
16
+ import static io .r2dbc .postgresql .codec .PostgresqlObjectId .TEXT ;
17
+ import static io .r2dbc .postgresql .codec .PostgresqlObjectId .VARCHAR ;
18
+ import static io .r2dbc .postgresql .message .Format .FORMAT_TEXT ;
19
+ import static io .r2dbc .postgresql .util .TestByteBufAllocator .TEST ;
20
+ import static org .assertj .core .api .Assertions .assertThat ;
21
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
22
+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
23
+
24
+ class YearMonthCodecTest {
25
+
26
+ @ Test
27
+ void constructorNoByteBufAllocator () {
28
+ assertThatIllegalArgumentException ().isThrownBy (() -> new YearMonthCodec (null ))
29
+ .withMessage ("byteBufAllocator must not be null" );
30
+ }
31
+
32
+ @ Test
33
+ void decode () {
34
+ final YearMonth yearMonth = YearMonth .now ();
35
+
36
+ final ByteBuf buffer = TEST .buffer ();
37
+
38
+ final int charsWritten = buffer .writeCharSequence (yearMonth .toString (), Charset .defaultCharset ());
39
+ assertThat (charsWritten ).isEqualTo (yearMonth .toString ().length ());
40
+
41
+ assertThat (new YearMonthCodec (TEST )
42
+ .decode (buffer , VARCHAR , FORMAT_TEXT , YearMonth .class ))
43
+ .isEqualTo (yearMonth );
44
+ }
45
+
46
+ @ Test
47
+ void decodeJunkString () {
48
+ final String junkString = "hello world" ;
49
+ final ByteBuf buffer = TEST .buffer ();
50
+
51
+ final int charsWritten = buffer .writeCharSequence (junkString , Charset .defaultCharset ());
52
+ assertThat (charsWritten ).isEqualTo (junkString .length ());
53
+
54
+ assertThatExceptionOfType (DateTimeParseException .class )
55
+ .isThrownBy (() -> new YearMonthCodec (TEST ).decode (buffer , VARCHAR , FORMAT_TEXT , YearMonth .class ));
56
+ }
57
+
58
+ @ Test
59
+ void decodeNoByteBuf () {
60
+ assertThat (new YearMonthCodec (TEST ).decode (null , VARCHAR .getObjectId (), FORMAT_TEXT , YearMonth .class )).isNull ();
61
+ }
62
+
63
+ @ Test
64
+ void doCanDecode () {
65
+ YearMonthCodec codec = new YearMonthCodec (TEST );
66
+
67
+ assertThat (codec .doCanDecode (VARCHAR , FORMAT_TEXT )).isTrue ();
68
+ assertThat (codec .doCanDecode (CHAR , FORMAT_TEXT )).isTrue ();
69
+ assertThat (codec .doCanDecode (BPCHAR , FORMAT_TEXT )).isTrue ();
70
+ assertThat (codec .doCanDecode (NAME , FORMAT_TEXT )).isTrue ();
71
+ assertThat (codec .doCanDecode (TEXT , FORMAT_TEXT )).isTrue ();
72
+ }
73
+
74
+ @ Test
75
+ void doCanDecodeNoType () {
76
+ assertThatIllegalArgumentException ().isThrownBy (() -> new YearMonthCodec (TEST ).doCanDecode (null , FORMAT_TEXT ))
77
+ .withMessage ("type must not be null" );
78
+ }
79
+
80
+ @ Test
81
+ void doEncodeNoValue () {
82
+ assertThatIllegalArgumentException ().isThrownBy (() -> new YearMonthCodec (TEST ).doEncode (null ))
83
+ .withMessage ("value must not be null" );
84
+ }
85
+
86
+ @ Test
87
+ void encodeItemNoValue () {
88
+ assertThatIllegalArgumentException ().isThrownBy (() -> new YearMonthCodec (TEST ).encode (null ))
89
+ .withMessage ("value must not be null" );
90
+ }
91
+
92
+ @ Test
93
+ void encodeNull () {
94
+ ParameterAssert .assertThat (new YearMonthCodec (TEST ).encodeNull ())
95
+ .isEqualTo (new EncodedParameter (FORMAT_TEXT , VARCHAR .getObjectId (), NULL_VALUE ));
96
+ }
97
+
98
+ }
0 commit comments