HHH-8378 Cleanup during Bundle#stop. Added shutdown unit test.

This commit is contained in:
Brett Meyer 2013-07-24 11:43:20 -04:00
parent 3ffc6ce6e8
commit c620eaa952
2 changed files with 49 additions and 11 deletions

View File

@ -35,6 +35,7 @@ import org.hibernate.jpa.HibernatePersistenceProvider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
/**
* This BundleActivator provides three different uses of Hibernate in OSGi
@ -59,6 +60,10 @@ import org.osgi.framework.FrameworkUtil;
*/
@SuppressWarnings("UnusedDeclaration")
public class HibernateBundleActivator implements BundleActivator {
private ServiceRegistration<?> persistenceProviderService;
private ServiceRegistration<?> sessionFactoryService;
@Override
@SuppressWarnings("unchecked")
public void start(BundleContext context) throws Exception {
@ -75,12 +80,12 @@ public class HibernateBundleActivator implements BundleActivator {
final Dictionary properties = new Hashtable();
// In order to support existing persistence.xml files, register using the legacy provider name.
properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() );
context.registerService(
persistenceProviderService = context.registerService(
PersistenceProvider.class.getName(),
new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ),
properties
);
context.registerService(
sessionFactoryService = context.registerService(
SessionFactory.class.getName(),
new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ),
new Hashtable()
@ -89,6 +94,11 @@ public class HibernateBundleActivator implements BundleActivator {
@Override
public void stop(BundleContext context) throws Exception {
// Nothing else to do?
persistenceProviderService.unregister();
persistenceProviderService = null;
sessionFactoryService.unregister();
sessionFactoryService = null;
ClassLoaderHelper.overridenClassLoader = null;
}
}

View File

@ -18,10 +18,13 @@ package org.hibernate.osgi.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.io.InputStream;
import org.hibernate.osgi.OsgiPersistenceProviderService;
import org.hibernate.osgi.OsgiSessionFactoryService;
import org.hibernate.osgi.test.result.OsgiTestResults;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
@ -94,6 +97,8 @@ public class OsgiTestCase {
builder.addBundleSymbolicName( archive.getName() );
builder.addBundleManifestVersion( 2 );
builder.addImportPackages( OsgiTestResults.class );
// needed primarily to test service cleanup in #testStop
builder.addImportPackages( OsgiSessionFactoryService.class );
return builder.openStream();
}
} );
@ -108,18 +113,14 @@ public class OsgiTestCase {
*/
@Test
public void testClientBundle() throws Exception {
assertNotNull( "BundleContext injected", context );
assertEquals( "System Bundle ID", 0, context.getBundle().getBundleId() );
testHibernateBundle( "org.hibernate.core" );
testHibernateBundle( "org.hibernate.entitymanager" );
commonTests();
final Bundle testClientBundle = findHibernateBundle( "testClientBundle" );
assertNotNull( "The test client bundle was not found!", testClientBundle );
testClientBundle.start();
assertEquals( "The test client bundle was not activated!", Bundle.ACTIVE, testClientBundle.getState() );
final ServiceReference serviceReference = context.getServiceReference( OsgiTestResults.class.getName() );
final ServiceReference<?> serviceReference = context.getServiceReference( OsgiTestResults.class.getName() );
final OsgiTestResults testResults = (OsgiTestResults) context.getService( serviceReference );
if ( testResults.getFailures().size() > 0 ) {
@ -127,6 +128,33 @@ public class OsgiTestCase {
}
}
/**
* Test that stopping the hibernate-osgi bundle happens cleanly.
*
* TODO: This will be really simplistic at first, but should be expanded upon.
*
* @throws Exception
*/
@Test
public void testStop() throws Exception {
commonTests();
findHibernateBundle( "org.hibernate.osgi" ).stop();
testHibernateBundle( "org.hibernate.osgi", Bundle.RESOLVED );
assertNull( context.getServiceReference( OsgiSessionFactoryService.class ) );
assertNull( context.getServiceReference( OsgiPersistenceProviderService.class ) );
}
private void commonTests() {
assertNotNull( "BundleContext injected", context );
assertEquals( "System Bundle ID", 0, context.getBundle().getBundleId() );
testHibernateBundle( "org.hibernate.core", Bundle.ACTIVE );
testHibernateBundle( "org.hibernate.entitymanager", Bundle.ACTIVE );
testHibernateBundle( "org.hibernate.osgi", Bundle.ACTIVE );
}
private Bundle findHibernateBundle(String symbolicName) {
for ( Bundle bundle : context.getBundles() ) {
if ( bundle.getSymbolicName().equals( symbolicName ) ) {
@ -136,10 +164,10 @@ public class OsgiTestCase {
return null;
}
private void testHibernateBundle(String symbolicName) {
private void testHibernateBundle(String symbolicName, int state) {
final Bundle bundle = findHibernateBundle( symbolicName );
assertNotNull( "Bundle " + symbolicName + " was not found!", bundle );
assertEquals( "Bundle " + symbolicName + " was not activated!", Bundle.ACTIVE, bundle.getState() );
assertEquals( "Bundle " + symbolicName + " was not in the expected state!", state, bundle.getState() );
}
}