17
17
import java .util .UUID ;
18
18
import java .util .function .Consumer ;
19
19
20
+ import org .hibernate .annotations .Array ;
21
+ import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
22
+ import org .hibernate .cfg .Configuration ;
20
23
import org .hibernate .reactive .BaseReactiveTest ;
21
24
import org .hibernate .reactive .annotations .DisabledFor ;
25
+ import org .hibernate .reactive .testing .SqlStatementTracker ;
22
26
23
27
import org .junit .jupiter .api .Test ;
24
28
29
33
import jakarta .persistence .GeneratedValue ;
30
34
import jakarta .persistence .Id ;
31
35
import jakarta .persistence .Table ;
36
+ import org .assertj .core .api .Condition ;
32
37
33
38
import static java .lang .Boolean .FALSE ;
34
39
import static java .lang .Boolean .TRUE ;
35
40
import static java .util .concurrent .TimeUnit .MINUTES ;
41
+ import static org .assertj .core .api .Assertions .assertThat ;
36
42
import static org .hibernate .reactive .containers .DatabaseConfiguration .DBType .ORACLE ;
43
+ import static org .hibernate .reactive .containers .DatabaseConfiguration .DBType .POSTGRESQL ;
44
+ import static org .hibernate .reactive .containers .DatabaseConfiguration .dbType ;
37
45
import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
38
46
import static org .junit .jupiter .api .Assertions .assertEquals ;
39
47
import static org .junit .jupiter .api .Assertions .assertNotNull ;
40
48
41
49
@ Timeout (value = 10 , timeUnit = MINUTES )
42
- @ DisabledFor ( value = ORACLE , reason = "Vert.x does not support arrays for Oracle" )
50
+ @ DisabledFor (value = ORACLE , reason = "Vert.x does not support arrays for Oracle" )
43
51
public class JavaTypesArrayTest extends BaseReactiveTest {
44
52
53
+ private static SqlStatementTracker sqlTracker ;
54
+
55
+ private final static Condition <String > IS_PG_CREATE_TABLE_QUERY = new Condition <>(
56
+ s -> s .toLowerCase ().startsWith ( "create table" ) && s .contains ( "stringArrayWithArrayAnnotation varchar(255) array[5]," ),
57
+ "generated query for PostgreSQL `create table...`"
58
+ );
59
+
60
+ private final static Condition <String > IS_PG_CREATE_TABLE_NO_ARRAY_ANNOTATION_QUERY = new Condition <>(
61
+ s -> s .toLowerCase ().startsWith ( "create table" ) && s .contains ( "stringArray varchar(255) array," ),
62
+ "generated query for PostgreSQL `create table...`"
63
+ );
64
+
65
+ @ Override
66
+ protected Configuration constructConfiguration () {
67
+ Configuration configuration = super .constructConfiguration ();
68
+ sqlTracker = new SqlStatementTracker ( JavaTypesArrayTest ::filterCreateTable , configuration .getProperties () );
69
+ return configuration ;
70
+ }
71
+
72
+ @ Override
73
+ protected void addServices (StandardServiceRegistryBuilder builder ) {
74
+ sqlTracker .registerService ( builder );
75
+ }
76
+
77
+ private static boolean filterCreateTable (String s ) {
78
+ return s .toLowerCase ().startsWith ( "create table" );
79
+ }
80
+
45
81
@ Override
46
82
protected Set <Class <?>> annotatedEntities () {
47
83
return Set .of ( Basic .class );
@@ -64,12 +100,59 @@ private void testField(
64
100
@ Test
65
101
public void testStringArrayType (VertxTestContext context ) {
66
102
Basic basic = new Basic ();
67
- String [] dataArray = {"Hello world!" , "Hello earth" };
103
+ String [] dataArray = { "Hello world!" , "Hello earth" };
68
104
basic .stringArray = dataArray ;
69
105
70
- testField ( context , basic , found -> assertArrayEquals ( dataArray , found .stringArray ) );
106
+ testField ( context , basic , found -> {
107
+ assertArrayEquals ( dataArray , found .stringArray );
108
+ // PostgreSQL is the only DB that changes it's `create table...` statement to include array information
109
+ // This test checks that the logged query is correct and contains "array[100]"
110
+ if ( dbType () == POSTGRESQL ) {
111
+ assertThat ( sqlTracker .getLoggedQueries () ).have ( IS_PG_CREATE_TABLE_NO_ARRAY_ANNOTATION_QUERY );
112
+ }
113
+ } );
114
+ }
115
+
116
+ @ Test
117
+ public void testStringArrayTypeWithArrayAnnotation (VertxTestContext context ) {
118
+ Basic basic = new Basic ();
119
+ String [] dataArray = {"Hello world!" , "Hello earth" };
120
+ basic .stringArrayWithArrayAnnotation = dataArray ;
121
+
122
+ testField ( context , basic , found -> {
123
+ assertArrayEquals ( dataArray , found .stringArrayWithArrayAnnotation );
124
+ // PostgreSQL is the only DB that changes it's `create table...` statement to include array information
125
+ // This test checks that the logged query is correct and contains "array[100]"
126
+ if ( dbType () == POSTGRESQL ) {
127
+ assertThat ( sqlTracker .getLoggedQueries () ).have ( IS_PG_CREATE_TABLE_QUERY );
128
+ }
129
+ } );
71
130
}
72
131
132
+ @ Test
133
+ public void testStringArrayTypeWithColumnAnnotation (VertxTestContext context ) {
134
+ Basic basic = new Basic ();
135
+ String [] dataArray = { "Hello world!" , "Hello earth" };
136
+ basic .stringArrayWithColumnAnnotation = dataArray ;
137
+
138
+ testField ( context , basic , found -> {
139
+ assertArrayEquals ( dataArray , found .stringArrayWithColumnAnnotation );
140
+ } );
141
+ }
142
+
143
+ @ Test
144
+ public void testStringArrayTypeWithBothAnnotations (VertxTestContext context ) {
145
+ Basic basic = new Basic ();
146
+ String [] dataArray = { "Hello world!" , "Hello earth" };
147
+ basic .stringArrayWithBothAnnotations = dataArray ;
148
+
149
+ testField ( context , basic , found -> {
150
+ assertArrayEquals ( dataArray , found .stringArrayWithBothAnnotations );
151
+ } );
152
+ }
153
+
154
+
155
+
73
156
@ Test
74
157
public void testBooleanArrayType (VertxTestContext context ) {
75
158
Basic basic = new Basic ();
@@ -277,13 +360,33 @@ public void testBigDecimalArrayType(VertxTestContext context) {
277
360
} );
278
361
}
279
362
363
+ @ Test
364
+ public void testBigDecimalArrayTypeWithArrayAnnotation (VertxTestContext context ) {
365
+ Basic basic = new Basic ();
366
+ BigDecimal [] dataArray = {BigDecimal .valueOf ( 123384967L ), BigDecimal .ZERO };
367
+ basic .bigDecimalArrayWithArrayAnnotation = dataArray ;
368
+
369
+ testField ( context , basic , found -> {
370
+ assertEquals ( dataArray .length , found .bigDecimalArrayWithArrayAnnotation .length );
371
+ assertEquals ( dataArray [0 ].compareTo ( found .bigDecimalArrayWithArrayAnnotation [0 ] ), 0 );
372
+ assertEquals ( dataArray [1 ].compareTo ( found .bigDecimalArrayWithArrayAnnotation [1 ] ), 0 );
373
+ } );
374
+ }
375
+
280
376
@ Entity (name = "Basic" )
281
377
@ Table (name = "Basic" )
282
378
private static class Basic {
283
379
@ Id
284
380
@ GeneratedValue
285
381
Integer id ;
286
382
String [] stringArray ;
383
+ @ Array (length = 5 )
384
+ String [] stringArrayWithArrayAnnotation ;
385
+ @ Column (length = 255 )
386
+ String [] stringArrayWithColumnAnnotation ;
387
+ @ Array (length = 5 )
388
+ @ Column (length = 255 )
389
+ String [] stringArrayWithBothAnnotations ;
287
390
Boolean [] booleanArray ;
288
391
boolean [] primitiveBooleanArray ;
289
392
Integer [] integerArray ;
@@ -309,6 +412,9 @@ private static class Basic {
309
412
BigInteger [] bigIntegerArray ;
310
413
@ Column (length = 5000 )
311
414
BigDecimal [] bigDecimalArray ;
415
+ @ Array (length = 5 )
416
+ @ Column (length = 5000 )
417
+ BigDecimal [] bigDecimalArrayWithArrayAnnotation ;
312
418
}
313
419
314
420
enum AnEnum {FIRST , SECOND , THIRD , FOURTH }
0 commit comments