Skip to content

Commit

Permalink
remove ConfigurationBuilder and replace it with a static method
Browse files Browse the repository at this point in the history
The previous design made it harder to override things in subclasses
of BaseReactiveTest. This design is actually every bit as simple for
NoVertxContextTest, but more flexible for our regular tests.

Also add ability to specify XML mappings.
  • Loading branch information
gavinking committed Nov 1, 2022
1 parent 0005116 commit f8af474
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,26 @@
public abstract class BaseReactiveTest {

/**
* When we need to write a test that doesn't fit with {@link BaseReactiveTest}, we can use
* this builder to get a default configuration that's compatible with the build properties.
* Configure properties defined in {@link Settings}.
*/
public static class ConfigurationBuilder {

public Configuration build(Collection<Class<?>> entityTypes) {
Configuration configuration = new Configuration();
entityTypes.forEach( configuration::addAnnotatedClass );
configuration.setProperty( Settings.HBM2DDL_AUTO, "create" );
configuration.setProperty( Settings.URL, DatabaseConfiguration.getJdbcUrl() );
if ( DatabaseConfiguration.dbType() == DBType.DB2 && !doneTablespace ) {
configuration.setProperty( Settings.HBM2DDL_IMPORT_FILES, "/db2.sql" );
doneTablespace = true;
}
//Use JAVA_TOOL_OPTIONS='-Dhibernate.show_sql=true'
configuration.setProperty( Settings.SHOW_SQL, System.getProperty( Settings.SHOW_SQL, "false" ) );
configuration.setProperty( Settings.FORMAT_SQL, System.getProperty( Settings.FORMAT_SQL, "false" ) );
configuration.setProperty( Settings.HIGHLIGHT_SQL, System.getProperty( Settings.HIGHLIGHT_SQL, "true" ) );
return configuration;
protected void setProperties(Configuration configuration) {
setDefaultProperties( configuration );
}

/**
* Configure default properties common to most tests.
*/
public static void setDefaultProperties(Configuration configuration) {
configuration.setProperty( Settings.HBM2DDL_AUTO, "create" );
configuration.setProperty( Settings.URL, DatabaseConfiguration.getJdbcUrl() );
if ( DatabaseConfiguration.dbType() == DBType.DB2 && !doneTablespace ) {
configuration.setProperty( Settings.HBM2DDL_IMPORT_FILES, "/db2.sql" );
doneTablespace = true;
}
//Use JAVA_TOOL_OPTIONS='-Dhibernate.show_sql=true'
configuration.setProperty( Settings.SHOW_SQL, System.getProperty( Settings.SHOW_SQL, "false" ) );
configuration.setProperty( Settings.FORMAT_SQL, System.getProperty( Settings.FORMAT_SQL, "false" ) );
configuration.setProperty( Settings.HIGHLIGHT_SQL, System.getProperty( Settings.HIGHLIGHT_SQL, "true" ) );
}

public static SessionFactoryManager factoryManager = new SessionFactoryManager();
Expand All @@ -111,16 +111,23 @@ protected static void test(TestContext context, CompletionStage<?> work) {
}

/**
* These entities will be added to the configuration of the factory and the rows in the mapping tables delted after
* each test.
* These entities will be added to the configuration of the factory and
* the rows in the mapping tables deleted after each test.
* <p>
* For more complicated configuration see {@link #constructConfiguration()} or {@link #cleanDb()}
* </p>
* For more complicated configuration see {@link #constructConfiguration()}
* or {@link #cleanDb()}.
*/
protected Collection<Class<?>> annotatedEntities() {
return List.of();
}

/**
* XML mapping documents to be added to the configuration.
*/
protected Collection<String> mappings() {
return List.of();
}

/**
* For when we need to create the {@link Async} in advance
*/
Expand All @@ -143,16 +150,25 @@ protected static void test(TestContext context, Uni<?> uni) {
* For when we need to create the {@link Async} in advance
*/
public static void test(Async async, TestContext context, Uni<?> uni) {
uni.subscribe().with(
res -> async.complete(),
context::fail
);
uni.subscribe().with( res -> async.complete(), context::fail );
}

private static boolean doneTablespace;

protected Configuration constructConfiguration() {
return new ConfigurationBuilder().build( annotatedEntities() );
Configuration configuration = new Configuration();
addEntities( configuration );
setProperties( configuration );
return configuration;
}

protected void addEntities(Configuration configuration) {
for ( Class<?> entity: annotatedEntities() ) {
configuration.addAnnotatedClass( entity );
}
for ( String mapping: mappings() ) {
configuration.addResource( mapping );
}
}

public CompletionStage<Void> deleteEntities(Class<?>... entities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package org.hibernate.reactive;

import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import javax.persistence.Column;
Expand Down Expand Up @@ -51,8 +50,9 @@ public class NoVertxContextTest {

@BeforeClass
public static void setupSessionFactory() {
final Configuration configuration = new BaseReactiveTest.ConfigurationBuilder()
.build( List.of( GameCharacter.class ) );
Configuration configuration = new Configuration();
configuration.addAnnotatedClass( GameCharacter.class );
BaseReactiveTest.setDefaultProperties( configuration );
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder()
.applySettings( configuration.getProperties() );
StandardServiceRegistry registry = builder.build();
Expand Down

1 comment on commit f8af474

@gavinking
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.