Skip to content

Commit c950801

Browse files
committed
[#1641] tests for @array annotations
1 parent 427aa62 commit c950801

File tree

7 files changed

+162
-3
lines changed

7 files changed

+162
-3
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/DB2Database.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class DB2Database implements TestableDatabase {
7373
expectedDBTypeForClass.put( Character.class, "CHARACTER" );
7474
expectedDBTypeForClass.put( char.class, "CHARACTER" );
7575
expectedDBTypeForClass.put( String.class, "VARCHAR" );
76+
expectedDBTypeForClass.put( String[].class, "VARBINARY" );
77+
expectedDBTypeForClass.put( Long[].class, "VARBINARY" );
78+
expectedDBTypeForClass.put( BigDecimal[].class, "VARBINARY" );
79+
expectedDBTypeForClass.put( BigInteger[].class, "VARBINARY" );
80+
expectedDBTypeForClass.put( Boolean[].class, "VARBINARY" );
7681
}}
7782

7883
/**

hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/MSSQLServerDatabase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class MSSQLServerDatabase implements TestableDatabase {
8282
expectedDBTypeForClass.put( Character.class, "char" );
8383
expectedDBTypeForClass.put( char.class, "char" );
8484
expectedDBTypeForClass.put( String.class, "varchar" );
85+
expectedDBTypeForClass.put( String[].class, "varbinary" );
86+
expectedDBTypeForClass.put( Long[].class, "varbinary" );
87+
expectedDBTypeForClass.put( BigDecimal[].class, "varbinary" );
88+
expectedDBTypeForClass.put( BigInteger[].class, "varbinary" );
89+
expectedDBTypeForClass.put( Boolean[].class, "varbinary" );
8590
}}
8691

8792
/**

hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/MySQLDatabase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class MySQLDatabase implements TestableDatabase {
7373
expectedDBTypeForClass.put( Character.class, "char" );
7474
expectedDBTypeForClass.put( char.class, "char" );
7575
expectedDBTypeForClass.put( String.class, "varchar" );
76+
expectedDBTypeForClass.put( String[].class, "varchar" );
77+
expectedDBTypeForClass.put( Long[].class, "varbinary" );
78+
expectedDBTypeForClass.put( BigDecimal[].class, "varbinary" );
79+
expectedDBTypeForClass.put( BigInteger[].class, "varbinary" );
80+
expectedDBTypeForClass.put( Boolean[].class, "varbinary" );
7681
}};
7782

7883
/**

hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/OracleDatabase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ class OracleDatabase implements TestableDatabase {
8181
expectedDBTypeForClass.put( Character.class, "CHAR" );
8282
expectedDBTypeForClass.put( char.class, "CHAR" );
8383
expectedDBTypeForClass.put( String.class, "VARCHAR2" );
84+
expectedDBTypeForClass.put( String[].class, "STRINGARRAY" );
85+
expectedDBTypeForClass.put( Long[].class, "LONGARRAY" );
86+
expectedDBTypeForClass.put( BigDecimal[].class, "BIGDECIMALARRAY" );
87+
expectedDBTypeForClass.put( BigInteger[].class, "BIGINTEGERARRAY" );
88+
expectedDBTypeForClass.put( Boolean[].class, "BOOLEANARRAY" );
8489
}
8590
}
8691

hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/PostgreSQLDatabase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class PostgreSQLDatabase implements TestableDatabase {
7373
expectedDBTypeForClass.put( Character.class, "character" );
7474
expectedDBTypeForClass.put( char.class, "character" );
7575
expectedDBTypeForClass.put( String.class, "character varying" );
76+
expectedDBTypeForClass.put( String[].class, "ARRAY" );
77+
expectedDBTypeForClass.put( Long[].class, "ARRAY" );
78+
expectedDBTypeForClass.put( BigDecimal[].class, "ARRAY" );
79+
expectedDBTypeForClass.put( BigInteger[].class, "ARRAY" );
80+
expectedDBTypeForClass.put( Boolean[].class, "ARRAY" );
7681
}}
7782

7883
/**

hibernate-reactive-core/src/test/java/org/hibernate/reactive/schema/BasicTypesTestEntity.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import java.util.Date;
1919
import java.util.TimeZone;
2020
import java.util.UUID;
21+
22+
import org.hibernate.annotations.Array;
23+
2124
import jakarta.persistence.Column;
2225
import jakarta.persistence.Convert;
2326
import jakarta.persistence.Entity;
@@ -104,6 +107,31 @@ public class BasicTypesTestEntity {
104107

105108
Serializable serializable;
106109

110+
String[] stringArray;
111+
112+
@Array(length = 5)
113+
String[] stringArrayAnnotated;
114+
115+
Long[] longArray;
116+
117+
@Array(length = 5)
118+
Long[] longArrayAnnotated;
119+
120+
BigDecimal[] bigDecimalArray;
121+
122+
@Array(length = 5)
123+
BigDecimal[] bigDecimalArrayAnnotated;
124+
125+
BigInteger[] bigIntegerArray;
126+
127+
@Array(length = 5)
128+
BigInteger[] bigIntegerArrayAnnotated;
129+
130+
Boolean[] fieldBooleanArray;
131+
132+
@Array(length = 5)
133+
Boolean[] fieldBooleanArrayAnnotated;
134+
107135
public BasicTypesTestEntity() {
108136
}
109137
public BasicTypesTestEntity(String name) {

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

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
import java.util.UUID;
1818
import java.util.function.Consumer;
1919

20+
import org.hibernate.annotations.Array;
21+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
22+
import org.hibernate.cfg.Configuration;
2023
import org.hibernate.reactive.BaseReactiveTest;
2124
import org.hibernate.reactive.annotations.DisabledFor;
25+
import org.hibernate.reactive.testing.SqlStatementTracker;
2226

2327
import org.junit.jupiter.api.Test;
2428

@@ -29,19 +33,51 @@
2933
import jakarta.persistence.GeneratedValue;
3034
import jakarta.persistence.Id;
3135
import jakarta.persistence.Table;
36+
import org.assertj.core.api.Condition;
3237

3338
import static java.lang.Boolean.FALSE;
3439
import static java.lang.Boolean.TRUE;
3540
import static java.util.concurrent.TimeUnit.MINUTES;
41+
import static org.assertj.core.api.Assertions.assertThat;
3642
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;
3745
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3846
import static org.junit.jupiter.api.Assertions.assertEquals;
3947
import 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")
4351
public 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

Comments
 (0)