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 .BeforeEach ;
7
+ import org .junit .jupiter .api .Test ;
8
+
9
+ import static io .r2dbc .postgresql .client .EncodedParameter .NULL_VALUE ;
10
+ import static io .r2dbc .postgresql .codec .PostgresqlObjectId .CIRCLE ;
11
+ import static io .r2dbc .postgresql .codec .PostgresqlObjectId .CIRCLE_ARRAY ;
12
+ import static io .r2dbc .postgresql .message .Format .FORMAT_BINARY ;
13
+ import static io .r2dbc .postgresql .message .Format .FORMAT_TEXT ;
14
+ import static io .r2dbc .postgresql .util .ByteBufUtils .encode ;
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
+ /**
20
+ * Unit tests for {@link ArrayCodec<Circle>}.
21
+ */
22
+ final class CircleArrayCodecUnitTests {
23
+
24
+ private static final int dataType = CIRCLE_ARRAY .getObjectId ();
25
+
26
+ private ArrayCodec <Circle > codec ;
27
+
28
+ private final ByteBuf SINGLE_DIM_BINARY_ARRAY = TEST
29
+ .buffer ()
30
+ .writeInt (1 ) // num of dimensions
31
+ .writeInt (0 ) // flag: has nulls
32
+ .writeInt (718 ) // oid
33
+ .writeInt (2 ) // num of elements
34
+ .writeInt (1 ) // ignore Lower Bound
35
+ .writeInt (24 ) // length of element
36
+ .writeDouble (1.2 ) // center point x
37
+ .writeDouble (123.1 ) // center point y
38
+ .writeDouble (10.0 ) // radius
39
+ .writeInt (24 ) // length of element
40
+ .writeDouble (-2.4 ) // center point x
41
+ .writeDouble (-456.2 ) // center point y
42
+ .writeDouble (20.0 ); // radius
43
+
44
+ private final ByteBuf TWO_DIM_BINARY_ARRAY = TEST
45
+ .buffer ()
46
+ .writeInt (2 ) // num of dims
47
+ .writeInt (1 ) // flag: has nulls
48
+ .writeInt (718 ) // oid
49
+ .writeInt (2 ) // dim 1 length
50
+ .writeInt (1 ) // dim 1 lower bound
51
+ .writeInt (1 ) // dim 2 length
52
+ .writeInt (1 ) // dim 2 lower bound
53
+ .writeInt (24 ) // length of element
54
+ .writeDouble (1.2 ) // center point x
55
+ .writeDouble (123.1 ) // center point y
56
+ .writeDouble (10.0 ) // radius
57
+ .writeInt (-1 ); // length of null element
58
+
59
+ @ BeforeEach
60
+ void setup () {
61
+ codec = new ArrayCodec <>(TEST , CIRCLE_ARRAY , new CircleCodec (TEST ), Circle .class );
62
+ }
63
+
64
+ @ Test
65
+ void decodeItem () {
66
+ assertThat (codec .decode (SINGLE_DIM_BINARY_ARRAY , dataType , FORMAT_BINARY , Circle [].class ))
67
+ .isEqualTo (new Circle []{Circle .of (Point .of (1.2 , 123.1 ), 10 ), Circle .of (Point .of (-2.4 , -456.2 ), 20 )});
68
+ }
69
+
70
+ @ Test
71
+ void decodeItem_textArray () {
72
+ Circle [] expected = {Circle .of (Point .of (1.2 , 123.1 ), 10 ), Circle .of (Point .of (-2.4 , -456.2 ), 20 )};
73
+ assertThat (codec .decode (encode (TEST , "{\" <(1.2, 123.1), 10>\" ,\" <(-2.4, -456.2), 20>\" }" ), dataType , FORMAT_TEXT , Circle [].class ))
74
+ .isEqualTo (expected );
75
+ assertThat (codec .decode (encode (TEST , "{\" ((1.2, 123.1), 10)\" ,\" ((-2.4, -456.2), 20)\" }" ), dataType , FORMAT_TEXT , Circle [].class ))
76
+ .isEqualTo (expected );
77
+ assertThat (codec .decode (encode (TEST , "{\" (1.2, 123.1), 10\" ,\" (-2.4, -456.2), 20\" }" ), dataType , FORMAT_TEXT , Circle [].class ))
78
+ .isEqualTo (expected );
79
+ assertThat (codec .decode (encode (TEST , "{\" 1.2, 123.1, 10\" ,\" -2.4, -456.2, 20\" }" ), dataType , FORMAT_TEXT , Circle [].class ))
80
+ .isEqualTo (expected );
81
+ }
82
+
83
+ @ Test
84
+ void decodeItem_emptyArray () {
85
+ assertThat (codec .decode (encode (TEST , "{}" ), dataType , FORMAT_TEXT , Circle [][].class ))
86
+ .isEqualTo (new Circle [][]{});
87
+ }
88
+
89
+ @ Test
90
+ void decodeItem_emptyBinaryArray () {
91
+ ByteBuf buf = TEST
92
+ .buffer ()
93
+ .writeInt (0 )
94
+ .writeInt (0 )
95
+ .writeInt (718 );
96
+
97
+ assertThat (codec .decode (buf , dataType , FORMAT_BINARY , Circle [][].class ))
98
+ .isEqualTo (new Circle [][]{});
99
+ }
100
+
101
+ @ Test
102
+ void decodeItem_expectedLessDimensionsInArray () {
103
+ assertThatIllegalArgumentException ()
104
+ .isThrownBy (() -> codec .decode (encode (TEST , "{{\" ((1.2, 123.1), 10)\" }}" ), dataType , FORMAT_TEXT , Circle [].class ))
105
+ .withMessage ("Dimensions mismatch: 1 expected, but 2 returned from DB" );
106
+ }
107
+
108
+ @ Test
109
+ void decodeItem_expectedLessDimensionsInBinaryArray () {
110
+ assertThatIllegalArgumentException ()
111
+ .isThrownBy (() -> codec .decode (TWO_DIM_BINARY_ARRAY , dataType , FORMAT_BINARY , Circle [].class ))
112
+ .withMessage ("Dimensions mismatch: 1 expected, but 2 returned from DB" );
113
+ }
114
+
115
+ @ Test
116
+ void decodeItem_expectedMoreDimensionsInArray () {
117
+ assertThatIllegalArgumentException ()
118
+ .isThrownBy (() -> codec .decode (encode (TEST , "{\" 1.2, 123.1, 10\" ,\" -2.4, -456.2, 20\" }" ), dataType , FORMAT_TEXT , Circle [][].class ))
119
+ .withMessage ("Dimensions mismatch: 2 expected, but 1 returned from DB" );
120
+ }
121
+
122
+ @ Test
123
+ void decodeItem_expectedMoreDimensionsInBinaryArray () {
124
+ assertThatIllegalArgumentException ()
125
+ .isThrownBy (() -> codec .decode (SINGLE_DIM_BINARY_ARRAY , dataType , FORMAT_BINARY , Circle [][].class ))
126
+ .withMessage ("Dimensions mismatch: 2 expected, but 1 returned from DB" );
127
+ }
128
+
129
+ @ Test
130
+ void decodeItem_twoDimensionalArrayWithNull () {
131
+ assertThat (codec .decode (encode (TEST , "{{\" ((1.2, 123.1), 10)\" },{NULL}}" ), dataType , FORMAT_TEXT , Circle [][].class ))
132
+ .isEqualTo (new Circle [][]{{Circle .of (Point .of (1.2 , 123.1 ), 10 )}, {null }});
133
+ }
134
+
135
+ @ Test
136
+ void decodeItem_twoDimensionalBinaryArrayWithNull () {
137
+ assertThat (codec .decode (TWO_DIM_BINARY_ARRAY , dataType , FORMAT_BINARY , Circle [][].class ))
138
+ .isEqualTo (new Circle [][]{{Circle .of (Point .of (1.2 , 123.1 ), 10 )}, {null }});
139
+ }
140
+
141
+ @ Test
142
+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
143
+ void decodeObject () {
144
+ Codec genericCodec = codec ;
145
+ assertThat (genericCodec .canDecode (CIRCLE_ARRAY .getObjectId (), FORMAT_TEXT , Object .class )).isTrue ();
146
+ Circle [] expected = {Circle .of (Point .of (1.2 , 123.1 ), 10 ), Circle .of (Point .of (-2.4 , -456.2 ), 20 )};
147
+
148
+ assertThat (genericCodec .decode (SINGLE_DIM_BINARY_ARRAY , dataType , FORMAT_BINARY , Object .class ))
149
+ .isEqualTo (expected );
150
+ assertThat (genericCodec .decode (encode (TEST , "{\" <(1.2, 123.1), 10>\" ,\" <(-2.4, -456.2), 20>\" }" ), dataType , FORMAT_TEXT , Object .class ))
151
+ .isEqualTo (expected );
152
+ }
153
+
154
+ @ Test
155
+ void doCanDecode () {
156
+ assertThat (codec .doCanDecode (CIRCLE , FORMAT_TEXT )).isFalse ();
157
+ assertThat (codec .doCanDecode (CIRCLE_ARRAY , FORMAT_TEXT )).isTrue ();
158
+ assertThat (codec .doCanDecode (CIRCLE_ARRAY , FORMAT_BINARY )).isTrue ();
159
+ }
160
+
161
+ @ Test
162
+ void doCanDecodeNoType () {
163
+ assertThatIllegalArgumentException ().isThrownBy (() -> codec .doCanDecode (null , null ))
164
+ .withMessage ("type must not be null" );
165
+ }
166
+
167
+ @ Test
168
+ void encodeArray () {
169
+ ParameterAssert .assertThat (codec .encodeArray (() -> encode (TEST , "{\" <(1.2, 123.1), 10>\" ,\" <(-2.4, -456.2), 20>\" }" ), CIRCLE_ARRAY ))
170
+ .hasFormat (FORMAT_TEXT )
171
+ .hasType (CIRCLE_ARRAY .getObjectId ())
172
+ .hasValue (encode (TEST , "{\" <(1.2, 123.1), 10>\" ,\" <(-2.4, -456.2), 20>\" }" ));
173
+ }
174
+
175
+ @ Test
176
+ void encodeNull () {
177
+ ParameterAssert .assertThat (codec .encodeNull ())
178
+ .isEqualTo (new EncodedParameter (FORMAT_BINARY , CIRCLE_ARRAY .getObjectId (), NULL_VALUE ));
179
+ }
180
+
181
+ }
0 commit comments