Skip to content

Commit

Permalink
OGM-1308 Add prepareDatabase method to GridDialectTestHelper interface
Browse files Browse the repository at this point in the history
  Allow dialect to initialize the database at startup in case we need some specific configuration
  for the tests.
  • Loading branch information
DavideD committed Sep 6, 2017
1 parent ab11e28 commit 0890c08
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 1 deletion.
Expand Up @@ -79,6 +79,13 @@ public interface GridDialectTestHelper {
*/
boolean backendSupportsTransactions();

/**
* Initialize the database on start-up.
*
* @param sessionFactory
*/
void prepareDatabase(SessionFactory sessionFactory);

/**
* Used to clean up all the stored data. The cleaning can be done by dropping
* the database and/or the schema.
Expand Down
Expand Up @@ -75,6 +75,11 @@ public boolean backendSupportsTransactions() {
return false;
}

@Override
public void prepareDatabase(SessionFactory sessionFactory) {
// Nothing to do
}

@Override
public void dropSchemaAndDatabase(SessionFactory sessionFactory) {
// Nothing to do
Expand Down
20 changes: 20 additions & 0 deletions core/src/test/java/org/hibernate/ogm/utils/TestHelper.java
Expand Up @@ -161,6 +161,10 @@ public static Map<String, Object> extractEntityTuple(Session session, EntityKey
return HELPER.extractEntityTuple( session, key );
}

public static long getNumberOfAssociations(EntityManager em) {
return getNumberOfAssociations( em.unwrap( Session.class ) );
}

public static long getNumberOfAssociations(Session session) {
return HELPER.getNumberOfAssociations( session );
}
Expand Down Expand Up @@ -210,6 +214,22 @@ public static void dropSchemaAndDatabase(SessionFactory sessionFactory) {
}
}

public static void prepareDatabase(EntityManagerFactory emf) {
prepareDatabase( ( (HibernateEntityManagerFactory) emf ).getSessionFactory() );
}

public static void prepareDatabase(SessionFactory sessionFactory) {
// if the factory is closed, we don't have access to the service registry
if ( sessionFactory != null && !sessionFactory.isClosed() ) {
try {
HELPER.prepareDatabase( sessionFactory );
}
catch ( Exception e ) {
log.warn( "Exception while preparing schema and database in test", e );
}
}
}

public static void checkCleanCache(SessionFactory sessionFactory) {
assertThat( getNumberOfEntities( sessionFactory ) ).as( "Entity cache should be empty" ).isEqualTo( 0 );
assertThat( getNumberOfAssociations( sessionFactory ) ).as( "Association cache should be empty" ).isEqualTo( 0 );
Expand Down
Expand Up @@ -127,6 +127,7 @@ public void run(RunNotifier notifier) {
if ( isTestScopedEntityManagerFactoryRequired() ) {
testScopedEntityManagerFactory = buildEntityManagerFactory();
injectEntityManagerFactory( null, testScopedFactoryFields, testScopedEntityManagerFactory );
TestHelper.prepareDatabase( testScopedEntityManagerFactory );
}

try {
Expand Down
Expand Up @@ -70,6 +70,10 @@ public boolean backendSupportsTransactions() {
return false;
}

@Override
public void prepareDatabase(SessionFactory sessionFactory) {
}

@Override
public void dropSchemaAndDatabase(SessionFactory sessionFactory) {
final InfinispanRemoteDatastoreProvider datastoreProvider = getProvider( sessionFactory );
Expand Down Expand Up @@ -171,5 +175,4 @@ public static InfinispanRemoteDatastoreProvider getProvider(SessionFactory sessi
}
return InfinispanRemoteDatastoreProvider.class.cast( provider );
}

}
Expand Up @@ -107,6 +107,11 @@ public boolean backendSupportsTransactions() {
return true;
}

@Override
public void prepareDatabase(SessionFactory sessionFactory) {
// Nothing to do
}

@Override
public void dropSchemaAndDatabase(SessionFactory sessionFactory) {
// Nothing to do
Expand Down
Expand Up @@ -228,6 +228,10 @@ private static MongoDBDatastoreProvider getProvider(SessionFactory sessionFactor
return MongoDBDatastoreProvider.class.cast( provider );
}

@Override
public void prepareDatabase(SessionFactory sessionFactory) {
}

@Override
public void dropSchemaAndDatabase(SessionFactory sessionFactory) {
MongoDBDatastoreProvider provider = getProvider( sessionFactory );
Expand Down
Expand Up @@ -108,6 +108,10 @@ public boolean backendSupportsTransactions() {
return true;
}

@Override
public void prepareDatabase(SessionFactory sessionFactory) {
}

@Override
public void dropSchemaAndDatabase(SessionFactory sessionFactory) {
DatastoreProvider datastoreProvider = getDatastoreProvider( sessionFactory );
Expand Down

0 comments on commit 0890c08

Please sign in to comment.