Skip to content

Commit 21edd60

Browse files
committed
[#2738] Test column type validation with Oracle
Test
1 parent c272d03 commit 21edd60

File tree

3 files changed

+93
-116
lines changed

3 files changed

+93
-116
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/ORMReactivePersistenceTest.java

Lines changed: 11 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,31 @@
55
*/
66
package org.hibernate.reactive;
77

8+
import java.net.URL;
89
import java.util.Collection;
910
import java.util.List;
10-
import java.util.Objects;
1111

12-
import org.hibernate.Session;
1312
import org.hibernate.SessionFactory;
1413
import org.hibernate.boot.registry.StandardServiceRegistry;
1514
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
15+
import org.hibernate.cfg.AvailableSettings;
1616
import org.hibernate.cfg.Configuration;
1717
import org.hibernate.reactive.annotations.DisabledFor;
18+
import org.hibernate.reactive.provider.Settings;
19+
import org.hibernate.reactive.schema.BasicTypesTestEntity;
1820

1921
import org.junit.jupiter.api.AfterEach;
2022
import org.junit.jupiter.api.BeforeEach;
2123
import org.junit.jupiter.api.Test;
2224

2325
import io.vertx.junit5.Timeout;
24-
import io.vertx.junit5.VertxTestContext;
25-
import jakarta.persistence.Entity;
26-
import jakarta.persistence.Id;
27-
import jakarta.persistence.Table;
2826

2927
import static java.util.concurrent.TimeUnit.MINUTES;
3028
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB;
3129
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
3230
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
3331
import static org.hibernate.reactive.provider.Settings.DIALECT;
3432
import static org.hibernate.reactive.provider.Settings.DRIVER;
35-
import static org.junit.jupiter.api.Assertions.assertEquals;
3633

3734
@Timeout(value = 10, timeUnit = MINUTES)
3835

@@ -48,14 +45,19 @@ public class ORMReactivePersistenceTest extends BaseReactiveTest {
4845

4946
@Override
5047
protected Collection<Class<?>> annotatedEntities() {
51-
return List.of( Flour.class );
48+
return List.of( BasicTypesTestEntity.class );
5249
}
5350

5451
@BeforeEach
5552
public void prepareOrmFactory() {
5653
Configuration configuration = constructConfiguration();
54+
configuration.setProperty( Settings.HBM2DDL_AUTO, "validate" );
5755
configuration.setProperty( DRIVER, dbType().getJdbcDriver() );
5856
configuration.setProperty( DIALECT, dbType().getDialectClass().getName() );
57+
final URL importFileURL = Thread.currentThread()
58+
.getContextClassLoader()
59+
.getResource( "oracleSchemaValidationTest.sql" );
60+
configuration.setProperty( AvailableSettings.JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE, importFileURL.getFile() );
5961

6062
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
6163
.applySettings( configuration.getProperties() );
@@ -70,112 +72,7 @@ public void closeOrmFactory() {
7072
}
7173

7274
@Test
73-
public void testORMWithStageSession(VertxTestContext context) {
74-
final Flour almond = new Flour( 1, "Almond", "made from ground almonds.", "Gluten free" );
75+
public void testORMWithStageSession() {
7576

76-
try (Session session = ormFactory.openSession()) {
77-
session.beginTransaction();
78-
session.persist( almond );
79-
session.getTransaction().commit();
80-
}
81-
82-
// Check database with Stage session and verify 'almond' flour exists
83-
test( context, openSession()
84-
.thenCompose( stageSession -> stageSession.find( Flour.class, almond.id ) )
85-
.thenAccept( entityFound -> assertEquals( almond, entityFound ) )
86-
);
87-
}
88-
89-
@Test
90-
public void testORMWitMutinySession(VertxTestContext context) {
91-
final Flour rose = new Flour( 2, "Rose", "made from ground rose pedals.", "Full fragrance" );
92-
93-
try (Session ormSession = ormFactory.openSession()) {
94-
ormSession.beginTransaction();
95-
ormSession.persist( rose );
96-
ormSession.getTransaction().commit();
97-
}
98-
99-
// Check database with Mutiny session and verify 'rose' flour exists
100-
test( context, openMutinySession()
101-
.chain( session -> session.find( Flour.class, rose.id ) )
102-
.invoke( foundRose -> assertEquals( rose, foundRose ) )
103-
);
104-
}
105-
106-
@Entity(name = "Flour")
107-
@Table(name = "Flour")
108-
public static class Flour {
109-
@Id
110-
private Integer id;
111-
private String name;
112-
private String description;
113-
private String type;
114-
115-
public Flour() {
116-
}
117-
118-
public Flour(Integer id, String name, String description, String type) {
119-
this.id = id;
120-
this.name = name;
121-
this.description = description;
122-
this.type = type;
123-
}
124-
125-
public Integer getId() {
126-
return id;
127-
}
128-
129-
public void setId(Integer id) {
130-
this.id = id;
131-
}
132-
133-
public String getName() {
134-
return name;
135-
}
136-
137-
public void setName(String name) {
138-
this.name = name;
139-
}
140-
141-
public String getDescription() {
142-
return description;
143-
}
144-
145-
public void setDescription(String description) {
146-
this.description = description;
147-
}
148-
149-
public String getType() {
150-
return type;
151-
}
152-
153-
public void setType(String type) {
154-
this.type = type;
155-
}
156-
157-
@Override
158-
public String toString() {
159-
return name;
160-
}
161-
162-
@Override
163-
public boolean equals(Object o) {
164-
if ( this == o ) {
165-
return true;
166-
}
167-
if ( o == null || getClass() != o.getClass() ) {
168-
return false;
169-
}
170-
Flour flour = (Flour) o;
171-
return Objects.equals( name, flour.name ) &&
172-
Objects.equals( description, flour.description ) &&
173-
Objects.equals( type, flour.type );
174-
}
175-
176-
@Override
177-
public int hashCode() {
178-
return Objects.hash( name, description, type );
179-
}
18077
}
18178
}

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

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
package org.hibernate.reactive.schema;
77

88

9+
import java.net.URL;
910
import java.util.concurrent.CompletionStage;
1011
import java.util.stream.Stream;
1112

1213
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
14+
import org.hibernate.cfg.AvailableSettings;
1315
import org.hibernate.cfg.Configuration;
1416
import org.hibernate.reactive.BaseReactiveTest;
1517
import org.hibernate.reactive.annotations.DisabledFor;
18+
import org.hibernate.reactive.annotations.EnabledFor;
1619
import org.hibernate.reactive.provider.Settings;
1720
import org.hibernate.tool.schema.spi.SchemaManagementException;
1821

@@ -23,6 +26,7 @@
2326

2427
import io.vertx.junit5.Timeout;
2528
import io.vertx.junit5.VertxTestContext;
29+
import jakarta.persistence.Column;
2630
import jakarta.persistence.Entity;
2731
import jakarta.persistence.GeneratedValue;
2832
import jakarta.persistence.Id;
@@ -34,6 +38,7 @@
3438
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
3539
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA;
3640
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL;
41+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE;
3742
import static org.hibernate.reactive.testing.ReactiveAssertions.assertThrown;
3843
import static org.hibernate.tool.schema.JdbcMetadataAccessStrategy.GROUPED;
3944
import static org.hibernate.tool.schema.JdbcMetadataAccessStrategy.INDIVIDUALLY;
@@ -47,7 +52,7 @@
4752
* - TODO: Test that validation fails when a column is the wrong type
4853
*/
4954
@DisabledFor(value = DB2, reason = "We don't have an information extractor. See https://github.com/hibernate/hibernate-reactive/issues/911")
50-
@DisabledFor(value = {MARIA, MYSQL}, reason = "HHH-18869: Schema creation creates an invalid schema")
55+
@DisabledFor(value = { MARIA, MYSQL }, reason = "HHH-18869: Schema creation creates an invalid schema")
5156
public class SchemaValidationTest extends BaseReactiveTest {
5257

5358
static Stream<Arguments> settings() {
@@ -77,9 +82,18 @@ public void before(VertxTestContext context) {
7782
}
7883

7984
public CompletionStage<Void> setupFactory(String strategy, String type) {
85+
return setupFactory( strategy, type, null );
86+
}
87+
88+
public CompletionStage<Void> setupFactory(String strategy, String type, String importFile) {
8089
Configuration createConf = constructConfiguration( "create", strategy, type );
8190
createConf.addAnnotatedClass( BasicTypesTestEntity.class );
82-
91+
if ( importFile != null ) {
92+
final URL importFileURL = Thread.currentThread()
93+
.getContextClassLoader()
94+
.getResource( importFile );
95+
createConf.setProperty( AvailableSettings.JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE, importFileURL.getFile() );
96+
}
8397
// Make sure that the extra table is not in the db
8498
Configuration dropConf = constructConfiguration( "drop", strategy, type );
8599
dropConf.addAnnotatedClass( Extra.class );
@@ -97,6 +111,21 @@ public void after(VertxTestContext context) {
97111
closeFactory( context );
98112
}
99113

114+
@ParameterizedTest
115+
@MethodSource("settings")
116+
@EnabledFor( ORACLE )
117+
public void testOracleColumnTypeValidation(final String strategy, final String type, VertxTestContext context) {
118+
test(
119+
context, setupFactory( strategy, type, "oracle-SchemaValidationTest.sql" )
120+
.thenCompose( v -> {
121+
Configuration validateConf = constructConfiguration( "validate", strategy, type );
122+
validateConf.addAnnotatedClass( Fruit.class );
123+
new StandardServiceRegistryBuilder().applySettings( validateConf.getProperties() );
124+
return setupSessionFactory( validateConf );
125+
} )
126+
);
127+
}
128+
100129
// When we have created the table, the validation should pass
101130
@ParameterizedTest
102131
@MethodSource("settings")
@@ -147,4 +176,43 @@ public static class Extra {
147176

148177
private String description;
149178
}
179+
180+
@Entity(name = "Fruit")
181+
public static class Fruit {
182+
183+
@Id
184+
@GeneratedValue
185+
private Integer id;
186+
187+
@Column(name = "something_name", nullable = false, updatable = false)
188+
private String name;
189+
190+
public Fruit() {
191+
}
192+
193+
public Fruit(String name) {
194+
this.name = name;
195+
}
196+
197+
public Integer getId() {
198+
return id;
199+
}
200+
201+
public void setId(Integer id) {
202+
this.id = id;
203+
}
204+
205+
public String getName() {
206+
return name;
207+
}
208+
209+
public void setName(String name) {
210+
this.name = name;
211+
}
212+
213+
@Override
214+
public String toString() {
215+
return "Fruit{" + id + "," + name + '}';
216+
}
217+
}
150218
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Import file for testing schema validation in SchemaValidationTest
2+
drop table if exists Fruit cascade constraints
3+
drop sequence if exists Fruit_SEQ
4+
5+
-- Create the table manually, so that we can check if the validation succeeds
6+
create sequence fruit_seq start with 1 increment by 50;
7+
create table Fruit (id number(10,0) not null, something_name nvarchar2(20) not null, primary key (id))
8+
9+
INSERT INTO fruit(id, something_name) VALUES (1, 'Cherry');
10+
INSERT INTO fruit(id, something_name) VALUES (2, 'Apple');
11+
INSERT INTO fruit(id, something_name) VALUES (3, 'Banana');
12+
ALTER SEQUENCE fruit_seq RESTART start with 4;

0 commit comments

Comments
 (0)