Skip to content

Commit

Permalink
HSEARCH-1874 Rethink resource management in testing utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne authored and DavideD committed Jun 4, 2015
1 parent 22ff199 commit ccf8ece
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 62 deletions.
Expand Up @@ -38,7 +38,6 @@ public void testInjectionHappened() throws Exception {
public void setUp() throws Exception {
masterChannel = createChannel();
slaveChannel = createChannel();
forceConfigurationRebuild();
super.setUp();
}

Expand Down
Expand Up @@ -45,7 +45,6 @@ public void testMuxDispatcher() throws Exception {
public void setUp() throws Exception {
muxId = (short) new Random().nextInt();
channels = createChannels();
forceConfigurationRebuild();
super.setUp();
}

Expand Down
Expand Up @@ -36,7 +36,6 @@ public class OptimizerPerformanceTest extends SearchTestBase {
@Override
@Before
public void setUp() throws Exception {
forceConfigurationRebuild();
String indexBase = TestConstants.getIndexDirectory( OptimizerPerformanceTest.class );
File indexDir = new File(indexBase);
FileHelper.delete( indexDir );
Expand Down
Expand Up @@ -48,7 +48,6 @@ public abstract class ReaderPerformanceTestCase extends SearchTestBase {
@Override
@Before
public void setUp() throws Exception {
forceConfigurationRebuild();
String indexBase = TestConstants.getIndexDirectory( ReaderPerformanceTestCase.class );
File indexDir = new File(indexBase);
indexDir.mkdir();
Expand Down
Expand Up @@ -17,6 +17,7 @@
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.jdbc.Work;
import org.hibernate.search.FullTextSession;
Expand All @@ -36,6 +37,10 @@
/**
* Test utility class for managing ORM and Search test resources.
*
* The Configuration instance cfg has a lifecycle coupled to the SessionFactory: it's
* created when the SessionFactory is started, and nulled when it's closed.
* It is not exposed for modification after the SessionFactory is started.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
Expand All @@ -50,21 +55,16 @@ public final class DefaultTestResourceManager implements TestResourceManager {
private SessionFactory sessionFactory;
private Session session;
private SearchFactory searchFactory;
private boolean needsConfigurationRebuild;

public DefaultTestResourceManager(Class<?>[] annotatedClasses) {
this.annotatedClasses = annotatedClasses == null ? new Class<?>[0] : annotatedClasses;
this.cfg = new Configuration();
this.baseIndexDir = createBaseIndexDir();
this.needsConfigurationRebuild = true;
}

@Override
public void openSessionFactory() {
if ( sessionFactory == null ) {
if ( cfg == null ) {
throw new IllegalStateException( "configuration was not built" );
}
buildConfiguration();
setSessionFactory( cfg.buildSessionFactory( /*new TestInterceptor()*/ ) );
}
else {
Expand All @@ -78,11 +78,7 @@ public void closeSessionFactory() {
sessionFactory.close();
sessionFactory = null;
}
}

@Override
public Configuration getCfg() {
return cfg;
cfg = null;
}

@Override
Expand All @@ -98,9 +94,7 @@ public Session getSession() {

@Override
public SessionFactory getSessionFactory() {
if ( cfg == null ) {
throw new IllegalStateException( "Configuration should be already defined at this point" );
}
requireConfiguration();
if ( sessionFactory == null ) {
throw new IllegalStateException( "SessionFactory should be already defined at this point" );
}
Expand All @@ -117,7 +111,7 @@ public Directory getDirectory(Class<?> clazz) {

@Override
public void ensureIndexesAreEmpty() {
if ( "jms".equals( getCfg().getProperty( "hibernate.search.worker.backend" ) ) ) {
if ( "jms".equals( getConfigurationProperty( "hibernate.search.worker.backend" ) ) ) {
log.debug( "JMS based test. Skipping index emptying" );
return;
}
Expand Down Expand Up @@ -145,17 +139,6 @@ public File getBaseIndexDir() {
return baseIndexDir;
}

@Override
public void forceConfigurationRebuild() {
this.needsConfigurationRebuild = true;
this.cfg = new Configuration();
}

@Override
public boolean needsConfigurationRebuild() {
return this.needsConfigurationRebuild;
}

public void defaultTearDown() throws Exception {
handleUnclosedResources();
closeSessionFactory();
Expand Down Expand Up @@ -193,10 +176,14 @@ public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public void buildConfiguration() {
private void buildConfiguration() {
if ( cfg != null ) {
throw new IllegalStateException( "Configuration was already created!" );
}
cfg = new Configuration();
try {
for ( Class<?> aClass : annotatedClasses ) {
getCfg().addAnnotatedClass( aClass );
cfg.addAnnotatedClass( aClass );
}
}
catch (HibernateException e) {
Expand All @@ -211,7 +198,6 @@ public void buildConfiguration() {
e.printStackTrace();
throw new RuntimeException( e );
}
needsConfigurationRebuild = false;
}

private File createBaseIndexDir() {
Expand All @@ -233,4 +219,29 @@ public void execute(Connection connection) throws SQLException {
connection.rollback();
}
}

@Override
public String getConfigurationProperty(String propertyKey) {
requireConfiguration();
return cfg.getProperty( propertyKey );
}

private void requireConfiguration() {
if ( cfg == null ) {
throw new IllegalStateException( "Configuration should be already defined at this point" );
}
}

@Override
public String[] generateDropSchemaScript(Dialect d) {
requireConfiguration();
return cfg.generateDropSchemaScript( d );
}

@Override
public String[] generateSchemaCreationScript(Dialect d) {
requireConfiguration();
return cfg.generateSchemaCreationScript( d );
}

}
21 changes: 8 additions & 13 deletions orm/src/test/java/org/hibernate/search/test/SearchTestBase.java
Expand Up @@ -12,8 +12,8 @@

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.search.SearchFactory;
import org.hibernate.search.engine.integration.impl.ExtendedSearchIntegrator;
import org.hibernate.search.testsupport.TestConstants;
Expand All @@ -37,12 +37,7 @@ public abstract class SearchTestBase implements TestResourceManager {

@Before
public void setUp() throws Exception {
DefaultTestResourceManager testResourceManager = getTestResourceManager();
if ( testResourceManager.needsConfigurationRebuild() ) {
configure( testResourceManager.getCfg() );
testResourceManager.buildConfiguration();
}
testResourceManager.openSessionFactory();
getTestResourceManager().openSessionFactory();
}

@After
Expand All @@ -51,8 +46,8 @@ public void tearDown() throws Exception {
}

@Override
public final Configuration getCfg() {
return getTestResourceManager().getCfg();
public String getConfigurationProperty(String propertyKey) {
return getTestResourceManager().getConfigurationProperty( propertyKey );
}

@Override
Expand Down Expand Up @@ -106,13 +101,13 @@ public Directory getDirectory(Class<?> clazz) {
}

@Override
public void forceConfigurationRebuild() {
getTestResourceManager().forceConfigurationRebuild();
public String[] generateDropSchemaScript(Dialect d) {
return getTestResourceManager().generateDropSchemaScript( d );
}

@Override
public boolean needsConfigurationRebuild() {
return getTestResourceManager().needsConfigurationRebuild();
public String[] generateSchemaCreationScript(Dialect d) {
return getTestResourceManager().generateSchemaCreationScript( d );
}

protected abstract Class<?>[] getAnnotatedClasses();
Expand Down
Expand Up @@ -13,8 +13,7 @@

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.search.SearchFactory;
import org.hibernate.search.spi.SearchIntegrator;

Expand All @@ -25,7 +24,7 @@
*/
public interface TestResourceManager {

Configuration getCfg();
String getConfigurationProperty(String propertyKey);

void openSessionFactory();

Expand All @@ -47,7 +46,8 @@ public interface TestResourceManager {

File getBaseIndexDir();

void forceConfigurationRebuild();
String[] generateDropSchemaScript(Dialect d);

String[] generateSchemaCreationScript(Dialect d);

boolean needsConfigurationRebuild();
}
Expand Up @@ -102,8 +102,8 @@ public void setUp() throws Exception {
ClockMultitenantConnectionProvider.start();
super.setUp();

exportSchema( ClockMultitenantConnectionProvider.GEOCHRON_PROVIDER, getCfg() );
exportSchema( ClockMultitenantConnectionProvider.METAMEC_PROVIDER, getCfg() );
exportSchema( ClockMultitenantConnectionProvider.GEOCHRON_PROVIDER );
exportSchema( ClockMultitenantConnectionProvider.METAMEC_PROVIDER );

Session sessionMetamec = openSessionWithTenantId( METAMEC_TID );
persist( sessionMetamec, METAMEC_MODELS );
Expand Down Expand Up @@ -289,9 +289,9 @@ private void deleteClocks(Session session) {
* Hibernate does not generate the schema when using multi-tenancy.
* We have to call the SchemaExport class explicitly.
*/
private void exportSchema(final ConnectionProvider provider, Configuration cfg) {
String[] generateDropSchemaScript = cfg.generateDropSchemaScript( DIALECT );
String[] generateSchemaCreationScript = cfg.generateSchemaCreationScript( DIALECT );
private void exportSchema(final ConnectionProvider provider) {
String[] dropSchemaScript = generateDropSchemaScript( DIALECT );
String[] createSchemaScript = generateSchemaCreationScript( DIALECT );
new SchemaExport( new ConnectionHelper() {

private Connection connection;
Expand All @@ -312,7 +312,7 @@ public void release() throws SQLException {
}
},

generateDropSchemaScript, generateSchemaCreationScript ).execute( false, true, false, false );
dropSchemaScript, createSchemaScript ).execute( false, true, false, false );
}

public static class ClockMultitenantConnectionProvider extends AbstractMultiTenantConnectionProvider {
Expand Down
Expand Up @@ -114,7 +114,6 @@ public void testIndexAndPurge() throws Exception {
@Override
@Before
public void setUp() throws Exception {
forceConfigurationRebuild();
super.setUp();
mbeanServer = ManagementFactory.getPlatformMBeanServer();
statisticsBeanObjectName = new ObjectName( StatisticsInfoMBean.STATISTICS_MBEAN_OBJECT_NAME );
Expand Down
Expand Up @@ -44,9 +44,8 @@ public void testIndexCtrlMBeanRegistered() throws Exception {
@Override
@Before
public void setUp() throws Exception {
forceConfigurationRebuild();
super.setUp();
String suffix = getCfg().getProperty( Environment.JMX_BEAN_SUFFIX );
String suffix = getConfigurationProperty( Environment.JMX_BEAN_SUFFIX );
mbeanServer = ManagementFactory.getPlatformMBeanServer();
indexBeanObjectName = new ObjectName(
JMXRegistrar.buildMBeanName(
Expand Down
Expand Up @@ -75,8 +75,6 @@ public void setUp() throws Exception {
mbeanServer.unregisterMBean( indexBeanObjectName );
}

// build the new configuration
forceConfigurationRebuild();
super.setUp();
}

Expand Down

0 comments on commit ccf8ece

Please sign in to comment.