Skip to content

Instantly share code, notes, and snippets.

@hferentschik
Created August 10, 2011 16:20
Show Gist options
  • Save hferentschik/1137321 to your computer and use it in GitHub Desktop.
Save hferentschik/1137321 to your computer and use it in GitHub Desktop.
public abstract class SearchTestCase extends TestCase {
...
protected void buildConfiguration() {
try {
setCfg( new Configuration() );
configure( cfg );
if ( recreateSchema() ) {
cfg.setProperty( org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create-drop" );
}
for ( String aPackage : getAnnotatedPackages() ) {
getCfg().addPackage( aPackage );
}
for ( Class<?> aClass : getAnnotatedClasses() ) {
getCfg().addAnnotatedClass( aClass );
}
for ( String xmlFile : getXmlFiles() ) {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFile );
getCfg().addInputStream( is );
}
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder( cfg.getProperties() ).buildServiceRegistry();
EventListenerRegistry registry = serviceRegistry.getService( EventListenerRegistry.class );
registerCustomListeners(registry);
setSessions( getCfg().buildSessionFactory( serviceRegistry ));
}
catch ( HibernateException e ) {
e.printStackTrace();
throw e;
}
catch ( SearchException e ) {
e.printStackTrace();
throw e;
}
catch ( Exception e ) {
e.printStackTrace();
throw new RuntimeException( e );
}
}
protected void registerCustomListeners(EventListenerRegistry registry) {
// per default do nothing
}
}
@hferentschik
Copy link
Author

This should do it :

protected void buildConfiguration() {
    try {
        setCfg( new Configuration() );
        configure( cfg );
        if ( recreateSchema() ) {
            cfg.setProperty( org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create-drop" );
        }
        for ( String aPackage : getAnnotatedPackages() ) {
            getCfg().addPackage( aPackage );
        }
        for ( Class<?> aClass : getAnnotatedClasses() ) {
            getCfg().addAnnotatedClass( aClass );
        }
        for ( String xmlFile : getXmlFiles() ) {
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFile );
            getCfg().addInputStream( is );
        }

        final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder( cfg.getProperties() ).buildServiceRegistry();
        SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) getCfg().buildSessionFactory( serviceRegistry );
        ServiceRegistryImplementor serviceRegistryImplementor = sessionFactoryImpl.getServiceRegistry();
        EventListenerRegistry registry = serviceRegistryImplementor.getService( EventListenerRegistry.class );
        registerCustomListeners( registry );
        setSessions( sessionFactoryImpl );
    }
    catch ( HibernateException e ) {
        e.printStackTrace();
        throw e;
    }
    catch ( SearchException e ) {
        e.printStackTrace();
        throw e;
    }
    catch ( Exception e ) {
        e.printStackTrace();
        throw new RuntimeException( e );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment