Skip to content

Commit d698931

Browse files
committed
Update tests
1 parent c061826 commit d698931

File tree

1 file changed

+47
-26
lines changed

1 file changed

+47
-26
lines changed

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

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,19 @@
4646
import static org.junit.jupiter.api.Assertions.assertEquals;
4747
import static org.junit.jupiter.api.Assertions.assertNotNull;
4848

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+
*/
4956
@Timeout(value = 10, timeUnit = MINUTES)
5057
@DisabledFor(value = ORACLE, reason = "Vert.x does not support arrays for Oracle")
5158
public class JavaTypesArrayTest extends BaseReactiveTest {
5259

5360
private static SqlStatementTracker sqlTracker;
5461

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-
6562
@Override
6663
protected Configuration constructConfiguration() {
6764
Configuration configuration = super.constructConfiguration();
@@ -75,7 +72,7 @@ protected void addServices(StandardServiceRegistryBuilder builder) {
7572
}
7673

7774
private static boolean filterCreateTable(String s) {
78-
return s.toLowerCase().startsWith( "create table" );
75+
return s.toLowerCase().startsWith( "create table " );
7976
}
8077

8178
@Override
@@ -100,16 +97,12 @@ private void testField(
10097
@Test
10198
public void testStringArrayType(VertxTestContext context) {
10299
Basic basic = new Basic();
103-
String[] dataArray = { "Hello world!", "Hello earth" };
100+
String[] dataArray = {"Hello world!", "Hello earth"};
104101
basic.stringArray = dataArray;
105102

106103
testField( context, basic, found -> {
107104
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 );
113106
} );
114107
}
115108

@@ -121,37 +114,60 @@ public void testStringArrayTypeWithArrayAnnotation(VertxTestContext context) {
121114

122115
testField( context, basic, found -> {
123116
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 );
129118
} );
130119
}
131120

132121
@Test
133122
public void testStringArrayTypeWithColumnAnnotation(VertxTestContext context) {
134123
Basic basic = new Basic();
135-
String[] dataArray = { "Hello world!", "Hello earth" };
124+
String[] dataArray = {"Hello world!", "Hello earth"};
136125
basic.stringArrayWithColumnAnnotation = dataArray;
137126

138127
testField( context, basic, found -> {
139128
assertArrayEquals( dataArray, found.stringArrayWithColumnAnnotation );
129+
validateStringColumns( "stringArrayWithColumnAnnotation", null, 25 );
140130
} );
141131
}
142132

143133
@Test
144134
public void testStringArrayTypeWithBothAnnotations(VertxTestContext context) {
145135
Basic basic = new Basic();
146-
String[] dataArray = { "Hello world!", "Hello earth" };
136+
String[] dataArray = {"Hello world!", "Hello earth"};
147137
basic.stringArrayWithBothAnnotations = dataArray;
148138

149139
testField( context, basic, found -> {
150140
assertArrayEquals( dataArray, found.stringArrayWithBothAnnotations );
141+
validateStringColumns( "stringArrayWithBothAnnotations", 5, 25 );
151142
} );
152143
}
153144

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
154150

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+
}
155171

156172
@Test
157173
public void testBooleanArrayType(VertxTestContext context) {
@@ -382,19 +398,24 @@ private static class Basic {
382398
String[] stringArray;
383399
@Array(length = 5)
384400
String[] stringArrayWithArrayAnnotation;
385-
@Column(length = 255)
401+
@Column(length = 25)
386402
String[] stringArrayWithColumnAnnotation;
387403
@Array(length = 5)
388-
@Column(length = 255)
404+
@Column(length = 25)
389405
String[] stringArrayWithBothAnnotations;
406+
390407
Boolean[] booleanArray;
391408
boolean[] primitiveBooleanArray;
409+
392410
Integer[] integerArray;
393411
int[] primitiveIntegerArray;
412+
394413
Long[] longArray;
395414
long[] primitiveLongArray;
415+
396416
Float[] floatArray;
397417
float[] primitiveFloatArray;
418+
398419
Double[] doubleArray;
399420
double[] primitiveDoubleArray;
400421
UUID[] uuidArray;

0 commit comments

Comments
 (0)