HHH-8378 Cleanup during Bundle#stop. Added shutdown unit test.
This commit is contained in:
parent
3ffc6ce6e8
commit
c620eaa952
|
@ -35,6 +35,7 @@ import org.hibernate.jpa.HibernatePersistenceProvider;
|
||||||
import org.osgi.framework.BundleActivator;
|
import org.osgi.framework.BundleActivator;
|
||||||
import org.osgi.framework.BundleContext;
|
import org.osgi.framework.BundleContext;
|
||||||
import org.osgi.framework.FrameworkUtil;
|
import org.osgi.framework.FrameworkUtil;
|
||||||
|
import org.osgi.framework.ServiceRegistration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This BundleActivator provides three different uses of Hibernate in OSGi
|
* This BundleActivator provides three different uses of Hibernate in OSGi
|
||||||
|
@ -59,6 +60,10 @@ import org.osgi.framework.FrameworkUtil;
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("UnusedDeclaration")
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
public class HibernateBundleActivator implements BundleActivator {
|
public class HibernateBundleActivator implements BundleActivator {
|
||||||
|
|
||||||
|
private ServiceRegistration<?> persistenceProviderService;
|
||||||
|
private ServiceRegistration<?> sessionFactoryService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void start(BundleContext context) throws Exception {
|
public void start(BundleContext context) throws Exception {
|
||||||
|
@ -75,12 +80,12 @@ public class HibernateBundleActivator implements BundleActivator {
|
||||||
final Dictionary properties = new Hashtable();
|
final Dictionary properties = new Hashtable();
|
||||||
// In order to support existing persistence.xml files, register using the legacy provider name.
|
// In order to support existing persistence.xml files, register using the legacy provider name.
|
||||||
properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() );
|
properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() );
|
||||||
context.registerService(
|
persistenceProviderService = context.registerService(
|
||||||
PersistenceProvider.class.getName(),
|
PersistenceProvider.class.getName(),
|
||||||
new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ),
|
new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ),
|
||||||
properties
|
properties
|
||||||
);
|
);
|
||||||
context.registerService(
|
sessionFactoryService = context.registerService(
|
||||||
SessionFactory.class.getName(),
|
SessionFactory.class.getName(),
|
||||||
new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ),
|
new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ),
|
||||||
new Hashtable()
|
new Hashtable()
|
||||||
|
@ -89,6 +94,11 @@ public class HibernateBundleActivator implements BundleActivator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop(BundleContext context) throws Exception {
|
public void stop(BundleContext context) throws Exception {
|
||||||
// Nothing else to do?
|
persistenceProviderService.unregister();
|
||||||
|
persistenceProviderService = null;
|
||||||
|
sessionFactoryService.unregister();
|
||||||
|
sessionFactoryService = null;
|
||||||
|
|
||||||
|
ClassLoaderHelper.overridenClassLoader = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,10 +18,13 @@ package org.hibernate.osgi.test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import org.hibernate.osgi.OsgiPersistenceProviderService;
|
||||||
|
import org.hibernate.osgi.OsgiSessionFactoryService;
|
||||||
import org.hibernate.osgi.test.result.OsgiTestResults;
|
import org.hibernate.osgi.test.result.OsgiTestResults;
|
||||||
import org.jboss.arquillian.container.test.api.Deployment;
|
import org.jboss.arquillian.container.test.api.Deployment;
|
||||||
import org.jboss.arquillian.junit.Arquillian;
|
import org.jboss.arquillian.junit.Arquillian;
|
||||||
|
@ -94,6 +97,8 @@ public class OsgiTestCase {
|
||||||
builder.addBundleSymbolicName( archive.getName() );
|
builder.addBundleSymbolicName( archive.getName() );
|
||||||
builder.addBundleManifestVersion( 2 );
|
builder.addBundleManifestVersion( 2 );
|
||||||
builder.addImportPackages( OsgiTestResults.class );
|
builder.addImportPackages( OsgiTestResults.class );
|
||||||
|
// needed primarily to test service cleanup in #testStop
|
||||||
|
builder.addImportPackages( OsgiSessionFactoryService.class );
|
||||||
return builder.openStream();
|
return builder.openStream();
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
@ -108,24 +113,47 @@ public class OsgiTestCase {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testClientBundle() throws Exception {
|
public void testClientBundle() throws Exception {
|
||||||
assertNotNull( "BundleContext injected", context );
|
commonTests();
|
||||||
assertEquals( "System Bundle ID", 0, context.getBundle().getBundleId() );
|
|
||||||
|
|
||||||
testHibernateBundle( "org.hibernate.core" );
|
|
||||||
testHibernateBundle( "org.hibernate.entitymanager" );
|
|
||||||
|
|
||||||
final Bundle testClientBundle = findHibernateBundle( "testClientBundle" );
|
final Bundle testClientBundle = findHibernateBundle( "testClientBundle" );
|
||||||
assertNotNull( "The test client bundle was not found!", testClientBundle );
|
assertNotNull( "The test client bundle was not found!", testClientBundle );
|
||||||
testClientBundle.start();
|
testClientBundle.start();
|
||||||
assertEquals( "The test client bundle was not activated!", Bundle.ACTIVE, testClientBundle.getState() );
|
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 );
|
final OsgiTestResults testResults = (OsgiTestResults) context.getService( serviceReference );
|
||||||
|
|
||||||
if ( testResults.getFailures().size() > 0 ) {
|
if ( testResults.getFailures().size() > 0 ) {
|
||||||
fail( testResults.getFailures().get( 0 ).getFailure() );
|
fail( testResults.getFailures().get( 0 ).getFailure() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
private Bundle findHibernateBundle(String symbolicName) {
|
||||||
for ( Bundle bundle : context.getBundles() ) {
|
for ( Bundle bundle : context.getBundles() ) {
|
||||||
|
@ -136,10 +164,10 @@ public class OsgiTestCase {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testHibernateBundle(String symbolicName) {
|
private void testHibernateBundle(String symbolicName, int state) {
|
||||||
final Bundle bundle = findHibernateBundle( symbolicName );
|
final Bundle bundle = findHibernateBundle( symbolicName );
|
||||||
|
|
||||||
assertNotNull( "Bundle " + symbolicName + " was not found!", bundle );
|
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() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue