Skip to content

Commit

Permalink
OGM-1496 Add helpers to test SessionFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever committed Jul 2, 2018
1 parent 53d943a commit eef6cc3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Hibernate OGM, Domain model persistence for NoSQL datastores
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.ogm.sessionfactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.ogm.cfg.OgmConfiguration;
import org.hibernate.ogm.utils.TestHelper;

/**
* Builder class to create a test configuration.
*
* @author Fabio Massimo Ercoli
*/
public class SessionFactoryBuilder {

// input
private final Class<?>[] entities;
private final Map<String, Object> customProperties = new HashMap<>();

// output
private Configuration configuration;
private StandardServiceRegistry serviceRegistry;

private SessionFactoryBuilder(Class<?>[] entities) {
this.entities = entities;
}

public static SessionFactoryBuilder entities(Class... entities) {
return new SessionFactoryBuilder( entities );
}

public SessionFactoryBuilder property(String key, Object value) {
customProperties.put( key, value );
return this;
}

public SessionFactory build() {
Properties properties = new Properties();
properties.putAll( TestHelper.getDefaultTestSettings() );
properties.putAll( customProperties );

configuration = new OgmConfiguration();
for ( Class entity : entities ) {
configuration.addAnnotatedClass( entity );
}
configuration.addProperties( properties );
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings( configuration.getProperties() )
.build();
return configuration.buildSessionFactory( serviceRegistry );
}
}
25 changes: 25 additions & 0 deletions core/src/test/java/org/hibernate/ogm/utils/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
Expand All @@ -44,6 +45,7 @@
import org.hibernate.ogm.util.impl.LoggerFactory;
import java.lang.invoke.MethodHandles;
import java.util.ServiceLoader;
import java.util.function.Consumer;

import com.arjuna.ats.arjuna.coordinator.TxControl;

Expand Down Expand Up @@ -428,4 +430,27 @@ public static void enableCountersForInfinispan(@SuppressWarnings("rawtypes") Map
cfg.put( "hibernate.ogm.infinispan.configuration_resource_name", "infinispan-dist.xml" );
}

/**
* Provides a static version of {@link org.hibernate.ogm.utils.OgmTestCase#inTransaction(Consumer)}.
* It can be useful for test cases that do not extend OgmTestCase or {@link org.hibernate.ogm.utils.jpa.OgmJpaTestCase}
*
* @param sessionFactory already created session factory instance
* @param consumer code to execute inside the transaction boundary
*/
public static void inTransaction(SessionFactory sessionFactory, Consumer<Session> consumer) {
try ( Session session = sessionFactory.openSession() ) {
Transaction transaction = session.beginTransaction();

try {
consumer.accept( session );
transaction.commit();
}
catch (Throwable t) {
if ( transaction.isActive() ) {
transaction.rollback();
}
throw t;
}
}
}
}

0 comments on commit eef6cc3

Please sign in to comment.