Updating the failing JtaTransaction test - for some reason, only with the JTA config, the test entity table isn't created, although the log contains the schema export messages, create table statements etc.

This commit is contained in:
adamw 2011-03-31 18:31:41 +02:00
parent 7a5c85823b
commit 8e2619c31e
4 changed files with 84 additions and 22 deletions

View File

@ -25,8 +25,12 @@ package org.hibernate.envers.test;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.transaction.TransactionManager;
import java.io.IOException; import java.io.IOException;
import org.hibernate.ejb.AvailableSettings;
import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory;
import org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
@ -41,8 +45,6 @@ import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.event.EnversIntegrator; import org.hibernate.envers.event.EnversIntegrator;
import org.hibernate.service.internal.BasicServiceRegistryImpl; import org.hibernate.service.internal.BasicServiceRegistryImpl;
import org.hibernate.testing.jta.TestingJtaBootstrap;
/** /**
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
*/ */
@ -99,8 +101,6 @@ public abstract class AbstractEntityTest {
configure( cfg ); configure( cfg );
cfg.configure( cfg.getHibernateConfiguration().getProperties() );
serviceRegistry = new BasicServiceRegistryImpl( cfg.getProperties() ); serviceRegistry = new BasicServiceRegistryImpl( cfg.getProperties() );
emf = cfg.buildEntityManagerFactory( serviceRegistry ); emf = cfg.buildEntityManagerFactory( serviceRegistry );
@ -127,10 +127,8 @@ public abstract class AbstractEntityTest {
return cfg; return cfg;
} }
protected void addJTAConfig(Ejb3Configuration cfg) { protected TransactionManager addJTAConfig(Ejb3Configuration cfg) {
TestingJtaBootstrap.prepare( cfg.getProperties() ); cfg.getProperties().put(AvailableSettings.TRANSACTION_TYPE, "JTA");
cfg.getProperties().remove( Environment.USER ); return EnversTestingJtaBootstrap.updateConfigAndCreateTM(cfg.getProperties());
cfg.getProperties().remove( Environment.PASS );
cfg.setProperty( AvailableSettings.TRANSACTION_TYPE, "JTA" );
} }
} }

View File

@ -0,0 +1,57 @@
package org.hibernate.envers.test;
import com.arjuna.ats.arjuna.common.ObjectStoreEnvironmentBean;
import com.arjuna.ats.internal.arjuna.objectstore.VolatileStore;
import com.arjuna.common.internal.util.propertyservice.BeanPopulator;
import org.enhydra.jdbc.standard.StandardXADataSource;
import org.hibernate.cfg.Environment;
import org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl;
import org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform;
import org.hibernate.service.jta.platform.internal.JtaPlatformInitiator;
import javax.sql.DataSource;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
/**
* Copied from {@link org.hibernate.testing.jta.TestingJtaBootstrap}, as Envers tests use a different URL for
* testing databases.
* @author Adam Warski (adam at warski dot org)
*/
public class EnversTestingJtaBootstrap {
public static TransactionManager updateConfigAndCreateTM(Map configValues) {
BeanPopulator
.getDefaultInstance(ObjectStoreEnvironmentBean.class)
.setObjectStoreType( VolatileStore.class.getName() );
BeanPopulator
.getNamedInstance( ObjectStoreEnvironmentBean.class, "communicationStore" )
.setObjectStoreType( VolatileStore.class.getName() );
BeanPopulator
.getNamedInstance( ObjectStoreEnvironmentBean.class, "stateStore" )
.setObjectStoreType( VolatileStore.class.getName() );
TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
StandardXADataSource dataSource = new StandardXADataSource();
dataSource.setTransactionManager( transactionManager );
try {
dataSource.setDriverName( configValues.get(Environment.DRIVER).toString() );
}
catch (SQLException e) {
throw new RuntimeException( "Unable to set DataSource JDBC driver name", e );
}
dataSource.setUrl(configValues.get(Environment.URL).toString());
dataSource.setUser(configValues.get(Environment.USER).toString());
configValues.put( JtaPlatformInitiator.JTA_PLATFORM, new JBossStandAloneJtaPlatform() );
configValues.put( Environment.CONNECTION_PROVIDER, DatasourceConnectionProviderImpl.class.getName() );
configValues.put( Environment.DATASOURCE, dataSource );
return transactionManager;
}
}

View File

@ -24,7 +24,9 @@
package org.hibernate.envers.test.integration.jta; package org.hibernate.envers.test.integration.jta;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.transaction.TransactionManager;
import org.hibernate.envers.test.EnversTestingJtaBootstrap;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.hibernate.ejb.Ejb3Configuration; import org.hibernate.ejb.Ejb3Configuration;
@ -39,31 +41,32 @@ import org.hibernate.testing.jta.TestingJtaBootstrap;
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
*/ */
public class JtaExceptionListener extends AbstractEntityTest { public class JtaExceptionListener extends AbstractEntityTest {
private TransactionManager tm;
public void configure(Ejb3Configuration cfg) { public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(StrTestEntity.class); cfg.addAnnotatedClass(StrTestEntity.class);
cfg.addAnnotatedClass(ExceptionListenerRevEntity.class); cfg.addAnnotatedClass(ExceptionListenerRevEntity.class);
addJTAConfig(cfg); tm = addJTAConfig(cfg);
} }
@Test(expectedExceptions = RuntimeException.class) @Test(expectedExceptions = RuntimeException.class)
public void testTransactionRollback() throws Exception { public void testTransactionRollback() throws Exception {
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); tm.begin();
// Trying to persist an entity - however the listener should throw an exception, so the entity // Trying to persist an entity - however the listener should throw an exception, so the entity
// shouldn't be persisted // shouldn't be persisted
newEntityManager(); newEntityManager();
EntityManager em = getEntityManager(); EntityManager em = getEntityManager();
em.getTransaction().begin();
StrTestEntity te = new StrTestEntity("x"); StrTestEntity te = new StrTestEntity("x");
em.persist(te); em.persist(te);
TestingJtaBootstrap.INSTANCE.getTransactionManager().commit(); tm.commit();
} }
@Test(dependsOnMethods = "testTransactionRollback") @Test(dependsOnMethods = "testTransactionRollback")
public void testDataNotPersisted() throws Exception { public void testDataNotPersisted() throws Exception {
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); tm.begin();
// Checking if the entity became persisted // Checking if the entity became persisted
newEntityManager(); newEntityManager();
@ -71,6 +74,6 @@ public class JtaExceptionListener extends AbstractEntityTest {
Long count = (Long) em.createQuery("select count(s) from StrTestEntity s where s.str = 'x'").getSingleResult(); Long count = (Long) em.createQuery("select count(s) from StrTestEntity s where s.str = 'x'").getSingleResult();
assert count == 0l; assert count == 0l;
TestingJtaBootstrap.INSTANCE.getTransactionManager().commit(); tm.commit();
} }
} }

View File

@ -1,8 +1,12 @@
package org.hibernate.envers.test.integration.jta; package org.hibernate.envers.test.integration.jta;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.TransactionManager;
import java.util.Arrays; import java.util.Arrays;
import org.hibernate.envers.test.EnversTestingJtaBootstrap;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.hibernate.ejb.Ejb3Configuration; import org.hibernate.ejb.Ejb3Configuration;
@ -16,37 +20,37 @@ import org.hibernate.testing.jta.TestingJtaBootstrap;
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
*/ */
public class JtaTransaction extends AbstractEntityTest { public class JtaTransaction extends AbstractEntityTest {
private TransactionManager tm;
private Integer id1; private Integer id1;
public void configure(Ejb3Configuration cfg) { public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(IntTestEntity.class); cfg.addAnnotatedClass(IntTestEntity.class);
addJTAConfig(cfg); tm = addJTAConfig(cfg);
} }
@Test @Test
public void initData() throws Exception { public void initData() throws Exception {
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); tm.begin();
newEntityManager(); newEntityManager();
EntityManager em = getEntityManager(); EntityManager em = getEntityManager();
em.joinTransaction();
IntTestEntity ite = new IntTestEntity(10); IntTestEntity ite = new IntTestEntity(10);
em.persist(ite); em.persist(ite);
id1 = ite.getId(); id1 = ite.getId();
TestingJtaBootstrap.INSTANCE.getTransactionManager().commit(); tm.commit();
// //
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin(); tm.begin();
newEntityManager(); newEntityManager();
em = getEntityManager(); em = getEntityManager();
ite = em.find(IntTestEntity.class, id1); ite = em.find(IntTestEntity.class, id1);
ite.setNumber(20); ite.setNumber(20);
TestingJtaBootstrap.INSTANCE.getTransactionManager().commit(); tm.commit();
} }
@Test(dependsOnMethods = "initData") @Test(dependsOnMethods = "initData")