HHH-9064 Simplify/improve the hibernate-osgi tests
This commit is contained in:
parent
1178e70034
commit
fc577724fb
|
@ -4,18 +4,12 @@ configurations {
|
|||
}
|
||||
|
||||
sourceSets {
|
||||
testResult
|
||||
testClientBundle
|
||||
}
|
||||
|
||||
sourceSets.test {
|
||||
compileClasspath += sourceSets.testResult.output
|
||||
runtimeClasspath += sourceSets.testResult.output
|
||||
}
|
||||
|
||||
sourceSets.testClientBundle {
|
||||
compileClasspath += sourceSets.testResult.output
|
||||
runtimeClasspath += sourceSets.testResult.output
|
||||
compileClasspath += sourceSets.testClientBundle.output
|
||||
runtimeClasspath += sourceSets.testClientBundle.output
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -133,7 +127,7 @@ task felixProperties << {
|
|||
}
|
||||
|
||||
task testClientBundleJar(type: Jar) {
|
||||
from sourceSets.testClientBundle.output, sourceSets.testResult.output
|
||||
from sourceSets.testClientBundle.output
|
||||
destinationDir new File("$buildDir/osgi-lib")
|
||||
archiveName "testClientBundle.jar"
|
||||
|
||||
|
|
|
@ -19,13 +19,18 @@ 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 static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
|
||||
import org.hibernate.integrator.spi.Integrator;
|
||||
import org.hibernate.metamodel.spi.TypeContributor;
|
||||
import org.hibernate.osgi.OsgiPersistenceProviderService;
|
||||
import org.hibernate.osgi.OsgiSessionFactoryService;
|
||||
import org.hibernate.osgi.test.result.OsgiTestResults;
|
||||
import org.hibernate.osgi.test.client.DataPoint;
|
||||
import org.hibernate.osgi.test.client.TestService;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.arquillian.junit.InSequence;
|
||||
|
@ -41,21 +46,15 @@ import org.osgi.framework.BundleContext;
|
|||
import org.osgi.framework.ServiceReference;
|
||||
|
||||
/**
|
||||
* A separate sourceset, testClientBundle, contains a persistence unit and multiple uses of Native and JPA functionality.
|
||||
* Any failures that occur are logged in the OsgiTestResult service, contained in another sourceset (testResult).
|
||||
*
|
||||
* The "unit tests" MUST reside in testClientBundle, rather than attempting to programmatically create a bundle and obtain an SF/EMF here. There are
|
||||
* A separate sourceset, testClientBundle, contains a persistence unit and an OSGi service interface providing
|
||||
* multiple uses of Native and JPA functionality. The use of a SF/EMF must occur in that separate bundle, rather than
|
||||
* attempting to programmatically create a bundle and obtain/use an SF/EMF here. There are
|
||||
* MANY ClassLoader issues with that sort of setup. JPA annotations are "stripped", since one ClassLoader is used here
|
||||
* to create the entity's stream and another is used to parse it within core. Further, the entire Felix framework
|
||||
* is given to hibernate-osgi as the "requestingBundle" in that setup, regardless of Arquillian vs. Pax Exam. That
|
||||
* causes another slew of ClassLoader issues as well.
|
||||
*
|
||||
* It is also important to keep OsgiTestResult in a third sourceset, rather than attempting to put it in test or
|
||||
* testClientBundle. Adding testClientBundle to test's classpath causes more ClassLoader issues during runtime (and
|
||||
* vice versa), similar to the above.
|
||||
*
|
||||
* The bottom line is that many, many alternatives were prototyped and all of them eventually hit brick walls.
|
||||
* Regardless, this is the most "realistic" type of test anyway with a *real* client bundle.
|
||||
* This is the most "realistic" type of test anyway with a *real* client bundle.
|
||||
*
|
||||
* IMPORTANT: There are a few maintenance points that need addressed for new versions of Hibernate and library upgrades:
|
||||
* 1.) Updated library versions in hibernate-osgi.gradle. libraries.gradle is used wherever possible. But, there
|
||||
|
@ -71,8 +70,8 @@ import org.osgi.framework.ServiceReference;
|
|||
* stripped of the javax.transaction nonsense. This may need to be repeated if Felix is ever updated in ORM
|
||||
* (should be rare).
|
||||
*
|
||||
* This should largerly be considered an integration test, rather than a granular unit test. Depending on how you setup
|
||||
* the source directories and classpaths, this may not work in your IDE.
|
||||
* This should largerly be considered an integration test, rather than a granular unit test. Also, this is almost
|
||||
* guaranteed to not work in your IDE.
|
||||
*
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
|
@ -97,8 +96,12 @@ public class OsgiTestCase {
|
|||
final OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
|
||||
builder.addBundleSymbolicName( archive.getName() );
|
||||
builder.addBundleManifestVersion( 2 );
|
||||
builder.addImportPackages( OsgiTestResults.class );
|
||||
// needed primarily to test service cleanup in #testStop
|
||||
builder.addImportPackages( TestService.class );
|
||||
// ORM packages needed in the tests
|
||||
builder.addImportPackages( Hibernate.class );
|
||||
builder.addImportPackages( Integrator.class );
|
||||
builder.addImportPackages( StrategyRegistrationProvider.class );
|
||||
builder.addImportPackages( TypeContributor.class );
|
||||
builder.addImportPackages( OsgiSessionFactoryService.class );
|
||||
return builder.openStream();
|
||||
}
|
||||
|
@ -121,13 +124,97 @@ public class OsgiTestCase {
|
|||
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 OsgiTestResults testResults = (OsgiTestResults) context.getService( serviceReference );
|
||||
@Test
|
||||
@InSequence(2)
|
||||
public void testJpa() throws Exception {
|
||||
commonTests();
|
||||
|
||||
if ( testResults.getFailures().size() > 0 ) {
|
||||
fail( testResults.getFailures().get( 0 ).getFailure() );
|
||||
}
|
||||
final TestService testService = getTestService();
|
||||
|
||||
DataPoint dp = new DataPoint();
|
||||
dp.setName( "Brett" );
|
||||
testService.saveJpa( dp );
|
||||
|
||||
dp = testService.getJpa(dp.getId());
|
||||
assertNotNull( dp );
|
||||
assertEquals( "Brett", dp.getName() );
|
||||
|
||||
dp.setName( "Brett2" );
|
||||
testService.updateJpa( dp );
|
||||
|
||||
dp = testService.getJpa(dp.getId());
|
||||
assertNotNull( dp );
|
||||
assertEquals( "Brett2", dp.getName() );
|
||||
|
||||
testService.deleteJpa();
|
||||
|
||||
dp = testService.getJpa(dp.getId());
|
||||
assertNull( dp );
|
||||
}
|
||||
|
||||
@Test
|
||||
@InSequence(2)
|
||||
public void testNative() throws Exception {
|
||||
commonTests();
|
||||
|
||||
final TestService testService = getTestService();
|
||||
|
||||
DataPoint dp = new DataPoint();
|
||||
dp.setName( "Brett" );
|
||||
testService.saveNative( dp );
|
||||
|
||||
dp = testService.getNative(dp.getId());
|
||||
assertNotNull( dp );
|
||||
assertEquals( "Brett", dp.getName() );
|
||||
|
||||
dp.setName( "Brett2" );
|
||||
testService.updateNative( dp );
|
||||
|
||||
dp = testService.getNative(dp.getId());
|
||||
assertNotNull( dp );
|
||||
assertEquals( "Brett2", dp.getName() );
|
||||
|
||||
testService.deleteNative();
|
||||
|
||||
dp = testService.getNative(dp.getId());
|
||||
assertNull( dp );
|
||||
}
|
||||
|
||||
@Test
|
||||
@InSequence(2)
|
||||
public void testLazyLoading() throws Exception {
|
||||
commonTests();
|
||||
|
||||
final TestService testService = getTestService();
|
||||
|
||||
DataPoint dp = new DataPoint();
|
||||
dp.setName( "Brett" );
|
||||
testService.saveNative( dp );
|
||||
|
||||
// #lazyLoad will init dp on its own
|
||||
dp = testService.lazyLoad( dp.getId() );
|
||||
assertNotNull( dp );
|
||||
assertTrue( Hibernate.isInitialized( dp ) );
|
||||
assertEquals( "Brett", dp.getName() );
|
||||
}
|
||||
|
||||
@Test
|
||||
@InSequence(2)
|
||||
public void testExtensionPoints() throws Exception {
|
||||
commonTests();
|
||||
|
||||
final TestService testService = getTestService();
|
||||
|
||||
assertNotNull( testService.getTestIntegrator() );
|
||||
assertTrue( testService.getTestIntegrator().passed() );
|
||||
|
||||
assertNotNull( testService.getTestStrategyRegistrationProvider() );
|
||||
assertTrue( testService.getTestStrategyRegistrationProvider().passed() );
|
||||
|
||||
assertNotNull( testService.getTestTypeContributor() );
|
||||
assertTrue( testService.getTestTypeContributor().passed() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -140,7 +227,7 @@ public class OsgiTestCase {
|
|||
@Test
|
||||
// Arquillian does not restart the container between runs (afaik). Without the ordering, the tests will
|
||||
// intermittently fail since this method stops the bundle.
|
||||
@InSequence(2)
|
||||
@InSequence(3)
|
||||
public void testStop() throws Exception {
|
||||
commonTests();
|
||||
|
||||
|
@ -175,4 +262,9 @@ public class OsgiTestCase {
|
|||
assertNotNull( "Bundle " + symbolicName + " was not found!", bundle );
|
||||
assertEquals( "Bundle " + symbolicName + " was not in the expected state!", state, bundle.getState() );
|
||||
}
|
||||
|
||||
private TestService getTestService() {
|
||||
final ServiceReference<?> serviceReference = context.getServiceReference( TestService.class.getName() );
|
||||
return (TestService) context.getService( serviceReference );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,54 +21,35 @@
|
|||
package org.hibernate.osgi.test.client;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.spi.PersistenceProvider;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.integrator.spi.Integrator;
|
||||
import org.hibernate.metamodel.spi.TypeContributor;
|
||||
import org.hibernate.osgi.test.result.OsgiTestResults;
|
||||
import org.hibernate.osgi.test.result.OsgiTestResultsImpl;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
|
||||
/**
|
||||
* This is a BundleActivate for the testClientBundle, but realistically it's the actual unit test. See the note on
|
||||
* OsgiTestCase.
|
||||
*
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class OsgiTestActivator implements BundleActivator {
|
||||
|
||||
private OsgiTestResults testResult = new OsgiTestResultsImpl();
|
||||
|
||||
private TestIntegrator integrator = new TestIntegrator();
|
||||
private TestStrategyRegistrationProvider strategyRegistrationProvider = new TestStrategyRegistrationProvider();
|
||||
private TestTypeContributor typeContributor = new TestTypeContributor();
|
||||
private TestService testService;
|
||||
|
||||
@Override
|
||||
public void start(BundleContext context) throws Exception {
|
||||
|
||||
// register the test result service
|
||||
context.registerService( OsgiTestResults.class, testResult, new Hashtable() );
|
||||
final TestIntegrator integrator = new TestIntegrator();
|
||||
final TestStrategyRegistrationProvider strategyRegistrationProvider = new TestStrategyRegistrationProvider();
|
||||
final TestTypeContributor typeContributor = new TestTypeContributor();
|
||||
|
||||
// register example extension point services
|
||||
context.registerService( Integrator.class, integrator, new Hashtable() );
|
||||
context.registerService( StrategyRegistrationProvider.class, strategyRegistrationProvider, new Hashtable() );
|
||||
context.registerService( TypeContributor.class, typeContributor, new Hashtable() );
|
||||
|
||||
testUnmanagedJpa( context );
|
||||
testUnmanagedNative( context );
|
||||
testLazyLoading( context );
|
||||
testExtensionPoints( context );
|
||||
// register the test result service
|
||||
testService = new TestServiceImpl(context, integrator, strategyRegistrationProvider, typeContributor);
|
||||
context.registerService( TestService.class, testService, new Hashtable() );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -76,156 +57,4 @@ public class OsgiTestActivator implements BundleActivator {
|
|||
|
||||
}
|
||||
|
||||
private void testUnmanagedJpa(BundleContext context) {
|
||||
try {
|
||||
final ServiceReference serviceReference = context.getServiceReference( PersistenceProvider.class.getName() );
|
||||
final PersistenceProvider persistenceProvider = (PersistenceProvider) context.getService( serviceReference );
|
||||
final EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory( "hibernate-osgi-test", null );
|
||||
final EntityManager em = emf.createEntityManager();
|
||||
|
||||
DataPoint dp = new DataPoint();
|
||||
dp.setName( "Brett" );
|
||||
em.getTransaction().begin();
|
||||
em.persist( dp );
|
||||
em.getTransaction().commit();
|
||||
em.clear();
|
||||
|
||||
em.getTransaction().begin();
|
||||
List<DataPoint> results = em.createQuery( "from DataPoint" ).getResultList();
|
||||
if ( results.size() == 0 || !results.get( 0 ).getName().equals( "Brett" ) ) {
|
||||
testResult.addFailure( "Unmanaged JPA: Unexpected data returned!" );
|
||||
}
|
||||
dp = results.get( 0 );
|
||||
dp.setName( "Brett2" );
|
||||
em.merge( dp );
|
||||
em.getTransaction().commit();
|
||||
em.clear();
|
||||
|
||||
em.getTransaction().begin();
|
||||
results = em.createQuery( "from DataPoint" ).getResultList();
|
||||
if ( results.size() == 0 || !results.get( 0 ).getName().equals( "Brett2" ) ) {
|
||||
testResult.addFailure( "Unmanaged JPA: The update/merge failed!" );
|
||||
}
|
||||
em.getTransaction().commit();
|
||||
em.clear();
|
||||
|
||||
em.getTransaction().begin();
|
||||
em.createQuery( "delete from DataPoint" ).executeUpdate();
|
||||
em.getTransaction().commit();
|
||||
em.clear();
|
||||
|
||||
em.getTransaction().begin();
|
||||
results = em.createQuery( "from DataPoint" ).getResultList();
|
||||
if ( results.size() > 0 ) {
|
||||
testResult.addFailure( "Unmanaged JPA: The delete failed!" );
|
||||
}
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
testResult.addFailure( "Exception: " + e.getMessage(), e );
|
||||
}
|
||||
}
|
||||
|
||||
private void testUnmanagedNative(BundleContext context) {
|
||||
try {
|
||||
final ServiceReference sr = context.getServiceReference( SessionFactory.class.getName() );
|
||||
final SessionFactory sf = (SessionFactory) context.getService( sr );
|
||||
final Session s = sf.openSession();
|
||||
|
||||
DataPoint dp = new DataPoint();
|
||||
dp.setName( "Brett" );
|
||||
s.getTransaction().begin();
|
||||
s.persist( dp );
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.getTransaction().begin();
|
||||
List<DataPoint> results = s.createQuery( "from DataPoint" ).list();
|
||||
if ( results.size() == 0 || !results.get( 0 ).getName().equals( "Brett" ) ) {
|
||||
testResult.addFailure( "Native Hibernate: Unexpected data returned!" );
|
||||
}
|
||||
dp = results.get( 0 );
|
||||
dp.setName( "Brett2" );
|
||||
s.update( dp );
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.getTransaction().begin();
|
||||
results = s.createQuery( "from DataPoint" ).list();
|
||||
if ( results.size() == 0 || !results.get( 0 ).getName().equals( "Brett2" ) ) {
|
||||
testResult.addFailure( "Native Hibernate: The update/merge failed!" );
|
||||
}
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.getTransaction().begin();
|
||||
s.createQuery( "delete from DataPoint" ).executeUpdate();
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.getTransaction().begin();
|
||||
results = s.createQuery( "from DataPoint" ).list();
|
||||
if ( results.size() > 0 ) {
|
||||
testResult.addFailure( "Native Hibernate: The delete failed!" );
|
||||
}
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
testResult.addFailure( "Exception: " + e.getMessage(), e );
|
||||
}
|
||||
}
|
||||
|
||||
private void testLazyLoading(BundleContext context) {
|
||||
try {
|
||||
final ServiceReference sr = context.getServiceReference( SessionFactory.class.getName() );
|
||||
final SessionFactory sf = (SessionFactory) context.getService( sr );
|
||||
final Session s = sf.openSession();
|
||||
|
||||
DataPoint dp = new DataPoint();
|
||||
dp.setName( "Brett" );
|
||||
s.getTransaction().begin();
|
||||
s.persist( dp );
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.getTransaction().begin();
|
||||
// ensure the proxy comes through ok
|
||||
dp = (DataPoint) s.load( DataPoint.class, new Long( dp.getId() ) );
|
||||
// initialize and test
|
||||
if ( dp == null || !dp.getName().equals( "Brett" ) ) {
|
||||
testResult.addFailure( "Native Hibernate: Lazy loading/proxy failed!" );
|
||||
}
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
testResult.addFailure( "Exception: " + e.getMessage(), e );
|
||||
}
|
||||
}
|
||||
|
||||
private void testExtensionPoints(BundleContext context) {
|
||||
try {
|
||||
final ServiceReference serviceReference = context.getServiceReference( SessionFactory.class.getName() );
|
||||
final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) context.getService(
|
||||
serviceReference );
|
||||
final ServiceRegistry serviceRegistry = sessionFactory.getServiceRegistry();
|
||||
|
||||
// test extension points in the client bundle
|
||||
if (!integrator.passed) {
|
||||
testResult.addFailure( "Could not discover " + integrator.getClass() );
|
||||
}
|
||||
if (!strategyRegistrationProvider.passed) {
|
||||
testResult.addFailure( "Could not discover " + strategyRegistrationProvider.getClass() );
|
||||
}
|
||||
if (!typeContributor.passed) {
|
||||
testResult.addFailure( "Could not discover " + typeContributor.getClass() );
|
||||
}
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
testResult.addFailure( "Exception: " + e.getMessage(), e );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import org.hibernate.service.spi.SessionFactoryServiceRegistry;
|
|||
*/
|
||||
public class TestIntegrator implements Integrator {
|
||||
|
||||
public boolean passed = false;
|
||||
private boolean passed = false;
|
||||
|
||||
public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
|
||||
passed = true;
|
||||
|
@ -46,4 +46,8 @@ public class TestIntegrator implements Integrator {
|
|||
passed = true;
|
||||
}
|
||||
|
||||
public boolean passed() {
|
||||
return passed;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* Copyright 2014 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
|
@ -18,39 +18,39 @@
|
|||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgi.test.result;
|
||||
package org.hibernate.osgi.test.client;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* OSGi service interface providing multiple uses of Native and JPA functionality. The use of a SF/EMF must occur in
|
||||
* this separate bundle, rather than attempting to programmatically create a bundle and obtain/use an SF/EMF there.
|
||||
* See comments on OsgiTestCase
|
||||
*
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class OsgiTestFailure {
|
||||
public interface TestService {
|
||||
public void saveJpa(DataPoint dp);
|
||||
|
||||
private String failure;
|
||||
public DataPoint getJpa(long id);
|
||||
|
||||
private Throwable cause;
|
||||
public void updateJpa(DataPoint dp);
|
||||
|
||||
public OsgiTestFailure( String failure ) {
|
||||
this.failure = failure;
|
||||
}
|
||||
public void deleteJpa();
|
||||
|
||||
public OsgiTestFailure( String failure, Throwable cause ) {
|
||||
this( failure );
|
||||
this.cause = cause;
|
||||
}
|
||||
public void saveNative(DataPoint dp);
|
||||
|
||||
public String getFailure() {
|
||||
return failure;
|
||||
}
|
||||
public DataPoint getNative(long id);
|
||||
|
||||
public void setFailure(String failure) {
|
||||
this.failure = failure;
|
||||
}
|
||||
public void updateNative(DataPoint dp);
|
||||
|
||||
public Throwable getCause() {
|
||||
return cause;
|
||||
}
|
||||
public void deleteNative();
|
||||
|
||||
public void setCause(Throwable cause) {
|
||||
this.cause = cause;
|
||||
}
|
||||
public DataPoint lazyLoad(long id);
|
||||
|
||||
public TestIntegrator getTestIntegrator();
|
||||
|
||||
public TestStrategyRegistrationProvider getTestStrategyRegistrationProvider();
|
||||
|
||||
public TestTypeContributor getTestTypeContributor();
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2014 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgi.test.client;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.spi.PersistenceProvider;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class TestServiceImpl implements TestService {
|
||||
|
||||
private final EntityManagerFactory emf;
|
||||
|
||||
private final SessionFactory sf;
|
||||
|
||||
private final TestIntegrator testIntegrator;
|
||||
|
||||
private final TestStrategyRegistrationProvider testStrategyRegistrationProvider;
|
||||
|
||||
private final TestTypeContributor testTypeContributor;
|
||||
|
||||
public TestServiceImpl(BundleContext context, TestIntegrator testIntegrator,
|
||||
TestStrategyRegistrationProvider testStrategyRegistrationProvider, TestTypeContributor testTypeContributor) {
|
||||
final ServiceReference serviceReference = context.getServiceReference( PersistenceProvider.class.getName() );
|
||||
final PersistenceProvider persistenceProvider = (PersistenceProvider) context.getService( serviceReference );
|
||||
emf = persistenceProvider.createEntityManagerFactory( "hibernate-osgi-test", null );
|
||||
|
||||
final ServiceReference sr = context.getServiceReference( SessionFactory.class.getName() );
|
||||
sf = (SessionFactory) context.getService( sr );
|
||||
|
||||
this.testIntegrator = testIntegrator;
|
||||
this.testStrategyRegistrationProvider = testStrategyRegistrationProvider;
|
||||
this.testTypeContributor = testTypeContributor;
|
||||
}
|
||||
|
||||
public void saveJpa(DataPoint dp) {
|
||||
final EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.persist( dp );
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public DataPoint getJpa(long id) {
|
||||
final EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
DataPoint dp = em.find(DataPoint.class, id);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
return dp;
|
||||
}
|
||||
|
||||
public void updateJpa(DataPoint dp) {
|
||||
final EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.merge( dp );
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void deleteJpa() {
|
||||
final EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.createQuery( "delete from DataPoint" ).executeUpdate();
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void saveNative(DataPoint dp) {
|
||||
final Session s = sf.openSession();
|
||||
s.getTransaction().begin();
|
||||
s.persist( dp );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
public DataPoint getNative(long id) {
|
||||
final Session s = sf.openSession();
|
||||
s.getTransaction().begin();
|
||||
DataPoint dp = (DataPoint) s.get( DataPoint.class, id );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
return dp;
|
||||
}
|
||||
|
||||
public void updateNative(DataPoint dp) {
|
||||
final Session s = sf.openSession();
|
||||
s.getTransaction().begin();
|
||||
s.update( dp );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
public void deleteNative() {
|
||||
final Session s = sf.openSession();
|
||||
s.getTransaction().begin();
|
||||
s.createQuery( "delete from DataPoint" ).executeUpdate();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
public DataPoint lazyLoad(long id) {
|
||||
final Session s = sf.openSession();
|
||||
s.getTransaction().begin();
|
||||
final DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( id ) );
|
||||
Hibernate.initialize( dp );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
return dp;
|
||||
}
|
||||
|
||||
public TestIntegrator getTestIntegrator() {
|
||||
return testIntegrator;
|
||||
}
|
||||
|
||||
public TestStrategyRegistrationProvider getTestStrategyRegistrationProvider() {
|
||||
return testStrategyRegistrationProvider;
|
||||
}
|
||||
|
||||
public TestTypeContributor getTestTypeContributor() {
|
||||
return testTypeContributor;
|
||||
}
|
||||
}
|
|
@ -30,10 +30,14 @@ import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
|
|||
*/
|
||||
public class TestStrategyRegistrationProvider implements StrategyRegistrationProvider {
|
||||
|
||||
public boolean passed = false;
|
||||
private boolean passed = false;
|
||||
|
||||
public Iterable<StrategyRegistration> getStrategyRegistrations() {
|
||||
passed = true;
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
public boolean passed() {
|
||||
return passed;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,10 +30,14 @@ import org.hibernate.service.ServiceRegistry;
|
|||
*/
|
||||
public class TestTypeContributor implements TypeContributor {
|
||||
|
||||
public boolean passed = false;
|
||||
private boolean passed = false;
|
||||
|
||||
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
|
||||
passed = true;
|
||||
}
|
||||
|
||||
public boolean passed() {
|
||||
return passed;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgi.test.result;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Defines an OSGi service to provide test results to the main test class, without mucking up the classpath
|
||||
* and causing ClassLoader issues.
|
||||
*
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public interface OsgiTestResults {
|
||||
|
||||
public void addFailure(String failure);
|
||||
|
||||
public void addFailure(String failure, Throwable cause);
|
||||
|
||||
public List<OsgiTestFailure> getFailures();
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgi.test.result;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Implementation of the OsgiTestResult OSGi service.
|
||||
*
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class OsgiTestResultsImpl implements OsgiTestResults {
|
||||
|
||||
private List<OsgiTestFailure> failures = new ArrayList<OsgiTestFailure>();
|
||||
|
||||
@Override
|
||||
public void addFailure(String failure) {
|
||||
failures.add( new OsgiTestFailure( failure ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFailure(String failure, Throwable cause) {
|
||||
failures.add( new OsgiTestFailure( failure, cause) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OsgiTestFailure> getFailures() {
|
||||
return failures;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue