1717import java .util .UUID ;
1818import java .util .function .Consumer ;
1919
20+ import org .hibernate .annotations .Array ;
21+ import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
22+ import org .hibernate .cfg .Configuration ;
2023import org .hibernate .reactive .BaseReactiveTest ;
2124import org .hibernate .reactive .annotations .DisabledFor ;
25+ import org .hibernate .reactive .testing .SqlStatementTracker ;
2226
2327import org .junit .jupiter .api .Test ;
2428
2933import jakarta .persistence .GeneratedValue ;
3034import jakarta .persistence .Id ;
3135import jakarta .persistence .Table ;
36+ import org .assertj .core .api .Condition ;
3237
3338import static java .lang .Boolean .FALSE ;
3439import static java .lang .Boolean .TRUE ;
3540import static java .util .concurrent .TimeUnit .MINUTES ;
41+ import static org .assertj .core .api .Assertions .assertThat ;
3642import 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 ;
3745import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
3846import static org .junit .jupiter .api .Assertions .assertEquals ;
3947import static org .junit .jupiter .api .Assertions .assertNotNull ;
4048
4149@ 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" )
4351public class JavaTypesArrayTest extends BaseReactiveTest {
4452
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+
4581 @ Override
4682 protected Set <Class <?>> annotatedEntities () {
4783 return Set .of ( Basic .class );
@@ -64,12 +100,59 @@ private void testField(
64100 @ Test
65101 public void testStringArrayType (VertxTestContext context ) {
66102 Basic basic = new Basic ();
67- String [] dataArray = {"Hello world!" , "Hello earth" };
103+ String [] dataArray = { "Hello world!" , "Hello earth" };
68104 basic .stringArray = dataArray ;
69105
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+ } );
71130 }
72131
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+
73156 @ Test
74157 public void testBooleanArrayType (VertxTestContext context ) {
75158 Basic basic = new Basic ();
@@ -277,13 +360,33 @@ public void testBigDecimalArrayType(VertxTestContext context) {
277360 } );
278361 }
279362
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+
280376 @ Entity (name = "Basic" )
281377 @ Table (name = "Basic" )
282378 private static class Basic {
283379 @ Id
284380 @ GeneratedValue
285381 Integer id ;
286382 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 ;
287390 Boolean [] booleanArray ;
288391 boolean [] primitiveBooleanArray ;
289392 Integer [] integerArray ;
@@ -309,6 +412,9 @@ private static class Basic {
309412 BigInteger [] bigIntegerArray ;
310413 @ Column (length = 5000 )
311414 BigDecimal [] bigDecimalArray ;
415+ @ Array (length = 5 )
416+ @ Column (length = 5000 )
417+ BigDecimal [] bigDecimalArrayWithArrayAnnotation ;
312418 }
313419
314420 enum AnEnum {FIRST , SECOND , THIRD , FOURTH }
0 commit comments