46
46
import static org .junit .jupiter .api .Assertions .assertEquals ;
47
47
import static org .junit .jupiter .api .Assertions .assertNotNull ;
48
48
49
+ /**
50
+ * Test that we handle arrays as basic types and the @{@link Array} annotation.
51
+ * <p>
52
+ * Specifying the length doesn't seem to have any effect at the moment.
53
+ * Except for Postgres: Hibernate ORM will use a different SQL query for the creation of the table, but Postgres will
54
+ * ignore the limit anyway.
55
+ */
49
56
@ Timeout (value = 10 , timeUnit = MINUTES )
50
57
@ DisabledFor (value = ORACLE , reason = "Vert.x does not support arrays for Oracle" )
51
58
public class JavaTypesArrayTest extends BaseReactiveTest {
52
59
53
60
private static SqlStatementTracker sqlTracker ;
54
61
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
62
@ Override
66
63
protected Configuration constructConfiguration () {
67
64
Configuration configuration = super .constructConfiguration ();
@@ -75,7 +72,7 @@ protected void addServices(StandardServiceRegistryBuilder builder) {
75
72
}
76
73
77
74
private static boolean filterCreateTable (String s ) {
78
- return s .toLowerCase ().startsWith ( "create table" );
75
+ return s .toLowerCase ().startsWith ( "create table " );
79
76
}
80
77
81
78
@ Override
@@ -100,16 +97,12 @@ private void testField(
100
97
@ Test
101
98
public void testStringArrayType (VertxTestContext context ) {
102
99
Basic basic = new Basic ();
103
- String [] dataArray = { "Hello world!" , "Hello earth" };
100
+ String [] dataArray = {"Hello world!" , "Hello earth" };
104
101
basic .stringArray = dataArray ;
105
102
106
103
testField ( context , basic , found -> {
107
104
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
- }
105
+ validateStringColumns ( "stringArray" , null , 255 );
113
106
} );
114
107
}
115
108
@@ -121,37 +114,60 @@ public void testStringArrayTypeWithArrayAnnotation(VertxTestContext context) {
121
114
122
115
testField ( context , basic , found -> {
123
116
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
- }
117
+ validateStringColumns ( "stringArrayWithArrayAnnotation" , 5 , null );
129
118
} );
130
119
}
131
120
132
121
@ Test
133
122
public void testStringArrayTypeWithColumnAnnotation (VertxTestContext context ) {
134
123
Basic basic = new Basic ();
135
- String [] dataArray = { "Hello world!" , "Hello earth" };
124
+ String [] dataArray = {"Hello world!" , "Hello earth" };
136
125
basic .stringArrayWithColumnAnnotation = dataArray ;
137
126
138
127
testField ( context , basic , found -> {
139
128
assertArrayEquals ( dataArray , found .stringArrayWithColumnAnnotation );
129
+ validateStringColumns ( "stringArrayWithColumnAnnotation" , null , 25 );
140
130
} );
141
131
}
142
132
143
133
@ Test
144
134
public void testStringArrayTypeWithBothAnnotations (VertxTestContext context ) {
145
135
Basic basic = new Basic ();
146
- String [] dataArray = { "Hello world!" , "Hello earth" };
136
+ String [] dataArray = {"Hello world!" , "Hello earth" };
147
137
basic .stringArrayWithBothAnnotations = dataArray ;
148
138
149
139
testField ( context , basic , found -> {
150
140
assertArrayEquals ( dataArray , found .stringArrayWithBothAnnotations );
141
+ validateStringColumns ( "stringArrayWithBothAnnotations" , 5 , 25 );
151
142
} );
152
143
}
153
144
145
+ private void validateStringColumns (String columnName , Integer arrayLength , Integer columnLength ) {
146
+ // Only Postgres has a different behaviour if @Array is used
147
+ if ( dbType () == POSTGRESQL ) {
148
+ // Postgres will ignore the length of the array, but we can test the table creation sql query
149
+ // and make sure that the limit is defined
154
150
151
+ // Example of correct query definition: columnName varchar(123) array[2]
152
+ final StringBuilder conditionBuilder = new StringBuilder ( columnName + " varchar(" );
153
+ if ( columnLength != null ) {
154
+ conditionBuilder .append ( columnLength );
155
+ }
156
+ else {
157
+ // Default varchar length
158
+ conditionBuilder .append ( 255 );
159
+ }
160
+ conditionBuilder .append ( ") array" );
161
+ if ( arrayLength != null ) {
162
+ conditionBuilder .append ( "[" ).append ( arrayLength ).append ( "]" );
163
+ }
164
+ Condition <String > condition = new Condition <>(
165
+ s -> s .contains ( conditionBuilder ),
166
+ "Should contains the right column definition"
167
+ );
168
+ assertThat ( sqlTracker .getLoggedQueries () ).have ( condition );
169
+ }
170
+ }
155
171
156
172
@ Test
157
173
public void testBooleanArrayType (VertxTestContext context ) {
@@ -382,19 +398,24 @@ private static class Basic {
382
398
String [] stringArray ;
383
399
@ Array (length = 5 )
384
400
String [] stringArrayWithArrayAnnotation ;
385
- @ Column (length = 255 )
401
+ @ Column (length = 25 )
386
402
String [] stringArrayWithColumnAnnotation ;
387
403
@ Array (length = 5 )
388
- @ Column (length = 255 )
404
+ @ Column (length = 25 )
389
405
String [] stringArrayWithBothAnnotations ;
406
+
390
407
Boolean [] booleanArray ;
391
408
boolean [] primitiveBooleanArray ;
409
+
392
410
Integer [] integerArray ;
393
411
int [] primitiveIntegerArray ;
412
+
394
413
Long [] longArray ;
395
414
long [] primitiveLongArray ;
415
+
396
416
Float [] floatArray ;
397
417
float [] primitiveFloatArray ;
418
+
398
419
Double [] doubleArray ;
399
420
double [] primitiveDoubleArray ;
400
421
UUID [] uuidArray ;
0 commit comments