Skip to content

Commit 4437ae9

Browse files
committed
[#1641] tests for @array annotations
1 parent e88997d commit 4437ae9

File tree

8 files changed

+372
-11
lines changed

8 files changed

+372
-11
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, "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/OracleDatabase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ 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" );
89+
8490
}
8591
}
8692

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
/**
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.schema;
7+
8+
import java.math.BigDecimal;
9+
import java.math.BigInteger;
10+
11+
import org.hibernate.annotations.Array;
12+
13+
import jakarta.persistence.Entity;
14+
import jakarta.persistence.GeneratedValue;
15+
import jakarta.persistence.Id;
16+
import jakarta.persistence.Table;
17+
import jakarta.persistence.Version;
18+
19+
20+
@Entity(name = "ArrayTypesTestEntity")
21+
@Table(name = ArrayTypesTestEntity.TABLE_NAME)
22+
public class ArrayTypesTestEntity {
23+
24+
public static final String TABLE_NAME = "ARRAY_TYPES_TABLE";
25+
26+
@Id
27+
@GeneratedValue
28+
Integer id;
29+
@Version
30+
Integer version;
31+
32+
String[] stringArray;
33+
34+
@Array( length = 5 )
35+
String[] stringArrayAnnotated;
36+
37+
Long[] longArray;
38+
39+
@Array( length = 5 )
40+
Long[] longArrayAnnotated;
41+
42+
BigDecimal[] bigDecimalArray;
43+
44+
@Array( length = 5 )
45+
BigDecimal[] bigDecimalArrayAnnotated;
46+
47+
BigInteger[] bigIntegerArray;
48+
49+
@Array( length = 5 )
50+
BigInteger[] bigIntegerArrayAnnotated;
51+
52+
Boolean[] fieldBooleanArray;
53+
54+
@Array( length = 5 )
55+
Boolean[] fieldBooleanArrayAnnotated;
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.schema;
7+
8+
9+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
10+
import org.hibernate.cfg.Configuration;
11+
import org.hibernate.reactive.BaseReactiveTest;
12+
import org.hibernate.reactive.annotations.DisabledFor;
13+
import org.hibernate.reactive.provider.Settings;
14+
import org.hibernate.tool.schema.spi.SchemaManagementException;
15+
16+
import org.junit.jupiter.api.AfterEach;
17+
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
21+
import io.vertx.junit5.Timeout;
22+
import io.vertx.junit5.VertxTestContext;
23+
import jakarta.persistence.Entity;
24+
import jakarta.persistence.GeneratedValue;
25+
import jakarta.persistence.Id;
26+
import jakarta.persistence.Table;
27+
28+
import static java.util.concurrent.TimeUnit.MINUTES;
29+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
30+
import static org.hibernate.tool.schema.JdbcMetadaAccessStrategy.GROUPED;
31+
import static org.hibernate.tool.schema.JdbcMetadaAccessStrategy.INDIVIDUALLY;
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertNotNull;
34+
35+
/**
36+
* Test schema validation at startup for all the supported types:
37+
* - Missing table validation error
38+
* - No validation error when everything is fine
39+
* - TODO: Missing column
40+
* - TODO: Wrong column type
41+
*/
42+
@DisabledFor(value = DB2, reason = "No InformationExtractor for Dialect [org.hibernate.dialect.DB2Dialect..]")
43+
public abstract class SchemaArrayTypesValidationTestBase extends BaseReactiveTest {
44+
45+
public static class IndividuallyStrategyTest extends SchemaArrayTypesValidationTestBase {
46+
47+
@Override
48+
protected Configuration constructConfiguration(String hbm2DdlOption) {
49+
final Configuration configuration = super.constructConfiguration( hbm2DdlOption );
50+
configuration.setProperty( Settings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, INDIVIDUALLY.toString() );
51+
configuration.setProperty( Settings.SHOW_SQL, System.getProperty( Settings.SHOW_SQL, "true" ) );
52+
return configuration;
53+
}
54+
}
55+
56+
public static class GroupedStrategyTest extends SchemaArrayTypesValidationTestBase {
57+
58+
@Override
59+
protected Configuration constructConfiguration(String hbm2DdlOption) {
60+
final Configuration configuration = super.constructConfiguration( hbm2DdlOption );
61+
configuration.setProperty( Settings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, GROUPED.toString() );
62+
configuration.setProperty( Settings.SHOW_SQL, System.getProperty( Settings.SHOW_SQL, "true" ) );
63+
return configuration;
64+
}
65+
}
66+
67+
protected Configuration constructConfiguration(String action) {
68+
Configuration configuration = super.constructConfiguration();
69+
configuration.setProperty( Settings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, INDIVIDUALLY.toString() );
70+
configuration.setProperty( Settings.HBM2DDL_AUTO, action );
71+
return configuration;
72+
}
73+
74+
@BeforeEach
75+
@Override
76+
public void before(VertxTestContext context) {
77+
Configuration createConf = constructConfiguration( "create" );
78+
createConf.addAnnotatedClass( ArrayTypesTestEntity.class );
79+
80+
// Make sure that the extra table is not in the db
81+
Configuration dropConf = constructConfiguration( "drop" );
82+
dropConf.addAnnotatedClass( Extra.class );
83+
84+
test( context, setupSessionFactory( dropConf )
85+
.thenCompose( v -> factoryManager.stop() )
86+
.thenCompose( v -> setupSessionFactory( createConf ) )
87+
.thenCompose( v -> factoryManager.stop() )
88+
);
89+
}
90+
91+
@AfterEach
92+
@Override
93+
public void after(VertxTestContext context) {
94+
super.after( context );
95+
closeFactory( context );
96+
}
97+
98+
// When we have created the table, the validation should pass
99+
@Test
100+
@Timeout(value = 10, timeUnit = MINUTES)
101+
public void testValidationSucceeds(VertxTestContext context) {
102+
Configuration validateConf = constructConfiguration( "validate" );
103+
validateConf.addAnnotatedClass( ArrayTypesTestEntity.class );
104+
105+
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
106+
.applySettings( validateConf.getProperties() );
107+
test( context, setupSessionFactory( validateConf ) );
108+
}
109+
110+
// Validation should fail if a table is missing
111+
@Test
112+
@Timeout(value = 10, timeUnit = MINUTES)
113+
public void testValidationFails(VertxTestContext context) {
114+
Configuration validateConf = constructConfiguration( "validate" );
115+
validateConf.addAnnotatedClass( ArrayTypesTestEntity.class );
116+
// The table mapping this entity shouldn't be in the db
117+
validateConf.addAnnotatedClass( Extra.class );
118+
119+
final String errorMessage = "Schema-validation: missing table [" + Extra.TABLE_NAME + "]";
120+
test( context, setupSessionFactory( validateConf )
121+
.handle( (unused, throwable) -> {
122+
assertNotNull( throwable );
123+
assertEquals( throwable.getClass(), SchemaManagementException.class );
124+
assertEquals( throwable.getMessage(), errorMessage );
125+
return null;
126+
} )
127+
);
128+
}
129+
130+
/**
131+
* An extra entity used for validation,
132+
* it should not be created at start up
133+
*/
134+
@Entity(name = "Extra")
135+
@Table(name = Extra.TABLE_NAME)
136+
public static class Extra {
137+
public static final String TABLE_NAME = "EXTRA_TABLE";
138+
@Id
139+
@GeneratedValue
140+
private Integer id;
141+
142+
private String description;
143+
}
144+
}

0 commit comments

Comments
 (0)