HHH-7266 - Move away from use of Enhydra developed DataSource for JTA testing

This commit is contained in:
Steve Ebersole 2012-04-20 18:59:06 -05:00
parent 70847a2331
commit 3cd58b6e79
7 changed files with 70 additions and 44 deletions

View File

@ -36,6 +36,7 @@ import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.transaction.internal.jta.CMTTransaction; import org.hibernate.engine.transaction.internal.jta.CMTTransaction;
import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper; import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaBootstrap;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -55,7 +56,7 @@ public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase
@Test @Test
public void testExplicitJoining() throws Exception { public void testExplicitJoining() throws Exception {
assertFalse( JtaStatusHelper.isActive( TestingJtaBootstrap.INSTANCE.getTransactionManager() ) ); assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) );
EntityManager entityManager = entityManagerFactory().createEntityManager(); EntityManager entityManager = entityManagerFactory().createEntityManager();
SessionImplementor session = entityManager.unwrap( SessionImplementor.class ); SessionImplementor session = entityManager.unwrap( SessionImplementor.class );
@ -68,34 +69,34 @@ public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase
assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() ); assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() );
assertFalse( hibernateTransaction.isParticipating() ); assertFalse( hibernateTransaction.isParticipating() );
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
assertTrue( JtaStatusHelper.isActive( TestingJtaBootstrap.INSTANCE.getTransactionManager() ) ); assertTrue( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) );
assertTrue( hibernateTransaction.isActive() ); assertTrue( hibernateTransaction.isActive() );
assertFalse( hibernateTransaction.isParticipating() ); assertFalse( hibernateTransaction.isParticipating() );
assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() ); assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() );
session.getFlushMode(); session.getFlushMode();
assertTrue( JtaStatusHelper.isActive( TestingJtaBootstrap.INSTANCE.getTransactionManager() ) ); assertTrue( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) );
assertTrue( hibernateTransaction.isActive() ); assertTrue( hibernateTransaction.isActive() );
assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() ); assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() );
assertFalse( hibernateTransaction.isParticipating() ); assertFalse( hibernateTransaction.isParticipating() );
entityManager.joinTransaction(); entityManager.joinTransaction();
assertTrue( JtaStatusHelper.isActive( TestingJtaBootstrap.INSTANCE.getTransactionManager() ) ); assertTrue( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) );
assertTrue( hibernateTransaction.isActive() ); assertTrue( hibernateTransaction.isActive() );
assertTrue( session.getTransactionCoordinator().isSynchronizationRegistered() ); assertTrue( session.getTransactionCoordinator().isSynchronizationRegistered() );
assertTrue( hibernateTransaction.isParticipating() ); assertTrue( hibernateTransaction.isParticipating() );
entityManager.close(); entityManager.close();
TestingJtaBootstrap.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
} }
@Test @Test
public void testImplicitJoining() throws Exception { public void testImplicitJoining() throws Exception {
assertFalse( JtaStatusHelper.isActive( TestingJtaBootstrap.INSTANCE.getTransactionManager() ) ); assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) );
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
EntityManager entityManager = entityManagerFactory().createEntityManager(); EntityManager entityManager = entityManagerFactory().createEntityManager();
SessionImplementor session = entityManager.unwrap( SessionImplementor.class ); SessionImplementor session = entityManager.unwrap( SessionImplementor.class );
Transaction hibernateTransaction = ( (Session) session ).getTransaction(); Transaction hibernateTransaction = ( (Session) session ).getTransaction();
@ -105,6 +106,6 @@ public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase
entityManager.close(); entityManager.close();
TestingJtaBootstrap.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
} }
} }

View File

@ -1,15 +1,37 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* 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, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY 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
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test; package org.hibernate.envers.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.SystemException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.SystemException;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.junit.After;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
@ -26,9 +48,12 @@ import org.hibernate.internal.util.StringHelper;
import org.hibernate.service.BootstrapServiceRegistryBuilder; import org.hibernate.service.BootstrapServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.internal.StandardServiceRegistryImpl; import org.hibernate.service.internal.StandardServiceRegistryImpl;
import org.junit.After;
import org.hibernate.testing.AfterClassOnce; import org.hibernate.testing.AfterClassOnce;
import org.hibernate.testing.BeforeClassOnce; import org.hibernate.testing.BeforeClassOnce;
import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaPlatformImpl;
/** /**
* @author Strong Liu (stliu@hibernate.org) * @author Strong Liu (stliu@hibernate.org)
@ -222,10 +247,10 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest
em = null; em = null;
return; return;
} }
if ( JtaStatusHelper.isActive( TestingJtaBootstrap.INSTANCE.getTransactionManager() ) ) { if ( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ) {
log.warn( "Cleaning up unfinished transaction" ); log.warn( "Cleaning up unfinished transaction" );
try { try {
TestingJtaBootstrap.INSTANCE.getTransactionManager().rollback(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
} }
catch (SystemException ignored) { catch (SystemException ignored) {
} }

View File

@ -37,6 +37,7 @@ import org.hibernate.envers.test.entities.StrTestEntity;
import org.hibernate.envers.test.integration.reventity.ExceptionListenerRevEntity; import org.hibernate.envers.test.integration.reventity.ExceptionListenerRevEntity;
import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaBootstrap;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
/** /**
* Same as {@link org.hibernate.envers.test.integration.reventity.ExceptionListener}, but in a JTA environment. * Same as {@link org.hibernate.envers.test.integration.reventity.ExceptionListener}, but in a JTA environment.
@ -56,7 +57,7 @@ public class JtaExceptionListener extends BaseEnversJPAFunctionalTestCase {
@Test(expected = RollbackException.class) @Test(expected = RollbackException.class)
@Priority(5) // must run before testDataNotPersisted() @Priority(5) // must run before testDataNotPersisted()
public void testTransactionRollback() throws Exception { public void testTransactionRollback() throws Exception {
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
try { try {
EntityManager em = getEntityManager(); EntityManager em = getEntityManager();
@ -66,13 +67,13 @@ public class JtaExceptionListener extends BaseEnversJPAFunctionalTestCase {
StrTestEntity te = new StrTestEntity("x"); StrTestEntity te = new StrTestEntity("x");
em.persist(te); em.persist(te);
} finally { } finally {
TestingJtaBootstrap.tryCommit(); TestingJtaPlatformImpl.tryCommit();
} }
} }
@Test @Test
public void testDataNotPersisted() throws Exception { public void testDataNotPersisted() throws Exception {
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
try { try {
// Checking if the entity became persisted // Checking if the entity became persisted
@ -80,7 +81,7 @@ public class JtaExceptionListener extends BaseEnversJPAFunctionalTestCase {
long count = em.createQuery("from StrTestEntity s where s.str = 'x'").getResultList().size(); long count = em.createQuery("from StrTestEntity s where s.str = 'x'").getResultList().size();
Assert.assertEquals( 0, count ); Assert.assertEquals( 0, count );
} finally { } finally {
TestingJtaBootstrap.tryCommit(); TestingJtaPlatformImpl.tryCommit();
} }
} }
} }

View File

@ -12,6 +12,7 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaBootstrap;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
/** /**
* Same as {@link org.hibernate.envers.test.integration.basic.Simple}, but in a JTA environment. * Same as {@link org.hibernate.envers.test.integration.basic.Simple}, but in a JTA environment.
@ -33,7 +34,7 @@ public class JtaTransaction extends BaseEnversJPAFunctionalTestCase {
@Test @Test
@Priority(10) @Priority(10)
public void initData() throws Exception { public void initData() throws Exception {
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
EntityManager em; EntityManager em;
IntTestEntity ite; IntTestEntity ite;
@ -43,18 +44,18 @@ public class JtaTransaction extends BaseEnversJPAFunctionalTestCase {
em.persist(ite); em.persist(ite);
id1 = ite.getId(); id1 = ite.getId();
} finally { } finally {
TestingJtaBootstrap.tryCommit(); TestingJtaPlatformImpl.tryCommit();
} }
em.close(); em.close();
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
try { try {
em = getEntityManager(); em = getEntityManager();
ite = em.find(IntTestEntity.class, id1); ite = em.find(IntTestEntity.class, id1);
ite.setNumber(20); ite.setNumber(20);
} finally { } finally {
TestingJtaBootstrap.tryCommit(); TestingJtaPlatformImpl.tryCommit();
} }
em.close(); em.close();
} }

View File

@ -73,6 +73,7 @@ public class JtaAwareConnectionProviderImpl implements ConnectionProvider, Confi
transferSetting( Environment.CONNECTION_PREFIX + '.' + setting, configurationValues, connectionSettings ); transferSetting( Environment.CONNECTION_PREFIX + '.' + setting, configurationValues, connectionSettings );
} }
} }
connectionSettings.setProperty( Environment.AUTOCOMMIT, "false" );
delegate = new DriverManagerConnectionProviderImpl(); delegate = new DriverManagerConnectionProviderImpl();
delegate.configure( connectionSettings ); delegate.configure( connectionSettings );

View File

@ -23,24 +23,17 @@
*/ */
package org.hibernate.testing.jta; package org.hibernate.testing.jta;
import javax.sql.DataSource;
import javax.transaction.Status;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import java.util.Map; import java.util.Map;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
/** /**
* Manages the {@link TransactionManager}, {@link UserTransaction} and {@link DataSource} instances used for testing.
*
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class TestingJtaBootstrap { public class TestingJtaBootstrap {
public static final TestingJtaBootstrap INSTANCE = new TestingJtaBootstrap(); public static final TestingJtaBootstrap INSTANCE = new TestingJtaBootstrap();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void prepare(Map configValues) { public static void prepare(Map configValues) {
configValues.put( AvailableSettings.JTA_PLATFORM, TestingJtaPlatformImpl.INSTANCE ); configValues.put( AvailableSettings.JTA_PLATFORM, TestingJtaPlatformImpl.INSTANCE );
@ -48,15 +41,4 @@ public class TestingJtaBootstrap {
configValues.put( "javax.persistence.transactionType", "JTA" ); configValues.put( "javax.persistence.transactionType", "JTA" );
} }
/**
* Used by envers...
*/
public static void tryCommit() throws Exception {
if ( TestingJtaPlatformImpl.transactionManager().getStatus() == Status.STATUS_MARKED_ROLLBACK ) {
TestingJtaPlatformImpl.transactionManager().rollback();
}
else {
TestingJtaPlatformImpl.transactionManager().commit();
}
}
} }

View File

@ -23,6 +23,7 @@
*/ */
package org.hibernate.testing.jta; package org.hibernate.testing.jta;
import javax.transaction.Status;
import javax.transaction.TransactionManager; import javax.transaction.TransactionManager;
import javax.transaction.TransactionSynchronizationRegistry; import javax.transaction.TransactionSynchronizationRegistry;
import javax.transaction.UserTransaction; import javax.transaction.UserTransaction;
@ -37,8 +38,10 @@ import org.hibernate.service.jta.platform.internal.SynchronizationRegistryAccess
import org.hibernate.service.jta.platform.internal.SynchronizationRegistryBasedSynchronizationStrategy; import org.hibernate.service.jta.platform.internal.SynchronizationRegistryBasedSynchronizationStrategy;
/** /**
* @author Steve Ebersole * A test-specific implementation of the JtaPlatform contract for testing JTA-based functionality.
*/ *
* @author Steve Ebersole
*/
public class TestingJtaPlatformImpl extends AbstractJtaPlatform { public class TestingJtaPlatformImpl extends AbstractJtaPlatform {
public static final TestingJtaPlatformImpl INSTANCE = new TestingJtaPlatformImpl(); public static final TestingJtaPlatformImpl INSTANCE = new TestingJtaPlatformImpl();
@ -88,6 +91,18 @@ public class TestingJtaPlatformImpl extends AbstractJtaPlatform {
return INSTANCE.synchronizationRegistry; return INSTANCE.synchronizationRegistry;
} }
/**
* Used by envers...
*/
public static void tryCommit() throws Exception {
if ( transactionManager().getStatus() == Status.STATUS_MARKED_ROLLBACK ) {
transactionManager().rollback();
}
else {
transactionManager().commit();
}
}
@Override @Override
protected TransactionManager locateTransactionManager() { protected TransactionManager locateTransactionManager() {
return transactionManager; return transactionManager;