Skip to content

Commit d670658

Browse files
committed
Test refactoring
1 parent ed1207b commit d670658

File tree

1 file changed

+58
-30
lines changed

1 file changed

+58
-30
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JavaTypesArrayTest.java

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@
4747
import static org.junit.jupiter.api.Assertions.assertNotNull;
4848

4949
/**
50-
* Test that we handle arrays as basic types and the @{@link Array} annotation.
50+
* Test that we handle arrays as basic types and the @{@link Array} annotation in combination with @{@link Column}.
5151
* <p>
5252
* 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.
53+
* We use it when creating the table with Postgres, but Postgres ignore it anyway.
5554
*/
5655
@Timeout(value = 10, timeUnit = MINUTES)
5756
@DisabledFor(value = ORACLE, reason = "Vert.x does not support arrays for Oracle")
@@ -72,7 +71,7 @@ protected void addServices(StandardServiceRegistryBuilder builder) {
7271
}
7372

7473
private static boolean filterCreateTable(String s) {
75-
return s.toLowerCase().startsWith( "create table basic" );
74+
return s.toLowerCase().startsWith( "create table basic " );
7675
}
7776

7877
@Override
@@ -387,7 +386,7 @@ public void testBigIntegerArrayType(VertxTestContext context) {
387386

388387
testField( context, basic, found -> {
389388
assertArrayEquals( dataArray, found.bigIntegerArray );
390-
validateArrayColumn( "bigIntegerArray", null, null );
389+
validateArrayColumn( "bigIntegerArray", null, 5000 );
391390
} );
392391
}
393392

@@ -401,7 +400,7 @@ public void testBigDecimalArrayType(VertxTestContext context) {
401400
assertEquals( dataArray.length, found.bigDecimalArray.length );
402401
assertEquals( 0, dataArray[0].compareTo( found.bigDecimalArray[0] ) );
403402
assertEquals( 0, dataArray[1].compareTo( found.bigDecimalArray[1] ) );
404-
validateArrayColumn( "bigDecimalArray", null, null );
403+
validateArrayColumn( "bigDecimalArray", null, 5000 );
405404
} );
406405
}
407406

@@ -415,6 +414,7 @@ public void testBigDecimalArrayTypeWithArrayAnnotation(VertxTestContext context)
415414
assertEquals( dataArray.length, found.bigDecimalArrayWithArrayAnnotation.length );
416415
assertEquals( 0, dataArray[0].compareTo( found.bigDecimalArrayWithArrayAnnotation[0] ) );
417416
assertEquals( 0, dataArray[1].compareTo( found.bigDecimalArrayWithArrayAnnotation[1] ) );
417+
validateArrayColumn( "bigDecimalArrayWithArrayAnnotation", 5, 5000 );
418418
} );
419419
}
420420

@@ -426,32 +426,60 @@ private void validateArrayColumn(String columnName, Integer arrayLength, Integer
426426

427427
// A predicate that checks we apply the right size to the array when required
428428
private static Predicate<String> arrayColumnPredicate(String columnName, Integer arrayLength, Integer columnLength) {
429-
final StringBuilder conditionBuilder = new StringBuilder();
430-
// Only Postgres has a different behaviour if @Array is used
431-
if ( dbType() == POSTGRESQL ) {
432-
conditionBuilder.append( ".*" );
433-
// Postgres will ignore the length of the array, but we can test the table creation sql query
434-
// and make sure that the limit is defined
435-
436-
// Example of correct query definition: columnName varchar(123) array[2]
437-
conditionBuilder.append( columnName ).append( " \\w+" );
438-
if ( columnLength != null ) {
439-
conditionBuilder.append( "\\(" ).append( columnLength ).append( "\\)" );
440-
}
441-
else {
442-
// for some types we have a default size. For example: `varchar(255)` or `numeric(38,0)`
443-
conditionBuilder.append( "(\\(\\d+(,\\d+)?\\))?" );
444-
}
445-
conditionBuilder.append( " array" );
446-
if ( arrayLength != null ) {
447-
conditionBuilder.append( "\\[" ).append( arrayLength ).append( "\\]" );
448-
}
449-
conditionBuilder.append( ".*" );
429+
switch ( dbType() ) {
430+
case POSTGRESQL:
431+
case COCKROACHDB:
432+
return postgresPredicate( columnName, arrayLength, columnLength );
433+
case MYSQL:
434+
case MARIA:
435+
case SQLSERVER:
436+
case DB2:
437+
return mysqlPredicate( columnName, columnLength );
438+
default:
439+
return s -> true;
450440
}
451-
if ( conditionBuilder.length() == 0 ) {
452-
return s -> true;
441+
}
442+
443+
/**
444+
* For Postgres, we expect arrays to be defined as {@code array}.
445+
* <p>
446+
* For example: {@code varchar(255) array[2]}
447+
* </p>
448+
*/
449+
private static Predicate<String> postgresPredicate(String columnName, Integer arrayLength, Integer columnLength) {
450+
StringBuilder regexBuilder = new StringBuilder();
451+
regexBuilder.append( ".*" );
452+
453+
regexBuilder.append( columnName ).append( " \\w+" );
454+
// Column length only affect String types
455+
if ( columnLength != null && columnName.startsWith( "string" ) ) {
456+
regexBuilder.append( "\\(" ).append( columnLength ).append( "\\)" );
457+
}
458+
else {
459+
// for some types we have a default size. For example: `varchar(255)` or `numeric(38,0)`
460+
regexBuilder.append( "(\\(\\d+(,\\d+)?\\))?" );
461+
}
462+
regexBuilder.append( " array" );
463+
if ( arrayLength != null ) {
464+
regexBuilder.append( "\\[" ).append( arrayLength ).append( "\\]" );
465+
}
466+
regexBuilder.append( ".*" );
467+
return s -> s.matches( regexBuilder.toString() );
468+
}
469+
470+
private static Predicate<String> mysqlPredicate(String columnName, Integer columnLength) {
471+
StringBuilder regexBuilder = new StringBuilder();
472+
// Example of correct query definition: columnName varbinary(255)
473+
regexBuilder.append( columnName ).append( " varbinary" ).append( "(" );
474+
if ( columnLength != null ) {
475+
regexBuilder.append( columnLength );
476+
}
477+
else {
478+
// Default size
479+
regexBuilder.append( 255 );
453480
}
454-
return s -> s.matches( conditionBuilder.toString() );
481+
regexBuilder.append( ")" );
482+
return s -> s.contains( regexBuilder.toString() );
455483
}
456484

457485
@Entity(name = "Basic")

0 commit comments

Comments
 (0)