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 .Period ;
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 PeriodCodecTest {
25
+
26
+ @ Test
27
+ void constructorNoByteBufAllocator () {
28
+ assertThatIllegalArgumentException ().isThrownBy (() -> new PeriodCodec (null ))
29
+ .withMessage ("byteBufAllocator must not be null" );
30
+ }
31
+
32
+ @ Test
33
+ void decodeDays () {
34
+ final int days = 4 ;
35
+
36
+ final Period period = Period .ofDays (days );
37
+ assertDecodeOfPeriod (period );
38
+ }
39
+
40
+ @ Test
41
+ void decodeJunkString () {
42
+ final String junkString = "hello world" ;
43
+ final ByteBuf buffer = TEST .buffer ();
44
+
45
+ final int charsWritten = buffer .writeCharSequence (junkString , Charset .defaultCharset ());
46
+ assertThat (charsWritten ).isEqualTo (junkString .length ());
47
+
48
+ assertThatExceptionOfType (DateTimeParseException .class )
49
+ .isThrownBy (() -> new PeriodCodec (TEST ).decode (buffer , VARCHAR , FORMAT_TEXT , Period .class ));
50
+ }
51
+
52
+ @ Test
53
+ void decodeMonths () {
54
+ final int months = 3 ;
55
+
56
+ final Period period = Period .ofMonths (months );
57
+ assertDecodeOfPeriod (period );
58
+ }
59
+
60
+ @ Test
61
+ void decodeNoByteBuf () {
62
+ assertThat (new PeriodCodec (TEST ).decode (null , VARCHAR .getObjectId (), FORMAT_TEXT , Period .class )).isNull ();
63
+ }
64
+
65
+ @ Test
66
+ void decodeWeeks () {
67
+ final int weeks = 7 ;
68
+
69
+ final Period period = Period .ofWeeks (weeks );
70
+ assertDecodeOfPeriod (period );
71
+ }
72
+
73
+ @ Test
74
+ void decodeYearsMonthsDays () {
75
+ final int years = 5 ;
76
+ final int months = 4 ;
77
+ final int days = 7 ;
78
+
79
+ final Period period = Period .of (years , months , days );
80
+ assertDecodeOfPeriod (period );
81
+ }
82
+
83
+ @ Test
84
+ void doCanDecode () {
85
+ final PeriodCodec codec = new PeriodCodec (TEST );
86
+
87
+ assertThat (codec .doCanDecode (VARCHAR , FORMAT_TEXT )).isTrue ();
88
+ assertThat (codec .doCanDecode (CHAR , FORMAT_TEXT )).isTrue ();
89
+ assertThat (codec .doCanDecode (BPCHAR , FORMAT_TEXT )).isTrue ();
90
+ assertThat (codec .doCanDecode (NAME , FORMAT_TEXT )).isTrue ();
91
+ assertThat (codec .doCanDecode (TEXT , FORMAT_TEXT )).isTrue ();
92
+ }
93
+
94
+ @ Test
95
+ void doCanDecodeNoType () {
96
+ assertThatIllegalArgumentException ().isThrownBy (() -> new PeriodCodec (TEST ).doCanDecode (null , FORMAT_TEXT ))
97
+ .withMessage ("type must not be null" );
98
+ }
99
+
100
+ @ Test
101
+ void doEncodeNoValue () {
102
+ assertThatIllegalArgumentException ().isThrownBy (() -> new PeriodCodec (TEST ).doEncode (null ))
103
+ .withMessage ("value must not be null" );
104
+ }
105
+
106
+ @ Test
107
+ void encodeItemNoValue () {
108
+ assertThatIllegalArgumentException ().isThrownBy (() -> new PeriodCodec (TEST ).encode (null ))
109
+ .withMessage ("value must not be null" );
110
+ }
111
+
112
+ @ Test
113
+ void encodeNull () {
114
+ ParameterAssert .assertThat (new PeriodCodec (TEST ).encodeNull ())
115
+ .isEqualTo (new EncodedParameter (FORMAT_TEXT , VARCHAR .getObjectId (), NULL_VALUE ));
116
+ }
117
+
118
+ private static void assertDecodeOfPeriod (Period period ) {
119
+ final ByteBuf buffer = TEST .buffer ();
120
+
121
+ final int charsWritten = buffer .writeCharSequence (period .toString (), Charset .defaultCharset ());
122
+ assertThat (charsWritten ).isEqualTo (period .toString ().length ());
123
+
124
+ assertThat (new PeriodCodec (TEST )
125
+ .decode (buffer , VARCHAR , FORMAT_TEXT , Period .class ))
126
+ .isEqualTo (period );
127
+ }
128
+
129
+ }
0 commit comments