Skip to content

Commit

Permalink
Updating the failing JtaTransaction test - for some reason, only with…
Browse files Browse the repository at this point in the history
… the JTA config, the test entity table isn't created, although the log contains the schema export messages, create table statements etc.
  • Loading branch information
adamw committed Mar 31, 2011
1 parent 7a5c858 commit 8e2619c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 22 deletions.
Expand Up @@ -25,8 +25,12 @@

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.TransactionManager;
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.BeforeClass;
import org.testng.annotations.BeforeMethod;
Expand All @@ -41,8 +45,6 @@
import org.hibernate.envers.event.EnversIntegrator;
import org.hibernate.service.internal.BasicServiceRegistryImpl;

import org.hibernate.testing.jta.TestingJtaBootstrap;

/**
* @author Adam Warski (adam at warski dot org)
*/
Expand Down Expand Up @@ -99,8 +101,6 @@ protected void init(boolean audited, String auditStrategy) throws IOException {

configure( cfg );

cfg.configure( cfg.getHibernateConfiguration().getProperties() );

serviceRegistry = new BasicServiceRegistryImpl( cfg.getProperties() );

emf = cfg.buildEntityManagerFactory( serviceRegistry );
Expand All @@ -127,10 +127,8 @@ public Ejb3Configuration getCfg() {
return cfg;
}

protected void addJTAConfig(Ejb3Configuration cfg) {
TestingJtaBootstrap.prepare( cfg.getProperties() );
cfg.getProperties().remove( Environment.USER );
cfg.getProperties().remove( Environment.PASS );
cfg.setProperty( AvailableSettings.TRANSACTION_TYPE, "JTA" );
protected TransactionManager addJTAConfig(Ejb3Configuration cfg) {
cfg.getProperties().put(AvailableSettings.TRANSACTION_TYPE, "JTA");
return EnversTestingJtaBootstrap.updateConfigAndCreateTM(cfg.getProperties());
}
}
@@ -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;
}
}
Expand Up @@ -24,7 +24,9 @@
package org.hibernate.envers.test.integration.jta;

import javax.persistence.EntityManager;
import javax.transaction.TransactionManager;

import org.hibernate.envers.test.EnversTestingJtaBootstrap;
import org.testng.annotations.Test;

import org.hibernate.ejb.Ejb3Configuration;
Expand All @@ -39,38 +41,39 @@
* @author Adam Warski (adam at warski dot org)
*/
public class JtaExceptionListener extends AbstractEntityTest {
private TransactionManager tm;

public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(StrTestEntity.class);
cfg.addAnnotatedClass(ExceptionListenerRevEntity.class);

addJTAConfig(cfg);
tm = addJTAConfig(cfg);
}

@Test(expectedExceptions = RuntimeException.class)
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
// shouldn't be persisted
newEntityManager();
EntityManager em = getEntityManager();
em.getTransaction().begin();
StrTestEntity te = new StrTestEntity("x");
em.persist(te);

TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();
tm.commit();
}

@Test(dependsOnMethods = "testTransactionRollback")
public void testDataNotPersisted() throws Exception {
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
tm.begin();

// Checking if the entity became persisted
newEntityManager();
EntityManager em = getEntityManager();
Long count = (Long) em.createQuery("select count(s) from StrTestEntity s where s.str = 'x'").getSingleResult();
assert count == 0l;

TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();
tm.commit();
}
}
@@ -1,8 +1,12 @@
package org.hibernate.envers.test.integration.jta;

import javax.persistence.EntityManager;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.TransactionManager;
import java.util.Arrays;

import org.hibernate.envers.test.EnversTestingJtaBootstrap;
import org.testng.annotations.Test;

import org.hibernate.ejb.Ejb3Configuration;
Expand All @@ -16,42 +20,42 @@
* @author Adam Warski (adam at warski dot org)
*/
public class JtaTransaction extends AbstractEntityTest {
private TransactionManager tm;
private Integer id1;

public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(IntTestEntity.class);

addJTAConfig(cfg);
tm = addJTAConfig(cfg);
}

@Test
public void initData() throws Exception {
TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
tm.begin();

newEntityManager();
EntityManager em = getEntityManager();
em.joinTransaction();
IntTestEntity ite = new IntTestEntity(10);
em.persist(ite);
id1 = ite.getId();

TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();
tm.commit();

//

TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
tm.begin();

newEntityManager();
em = getEntityManager();
ite = em.find(IntTestEntity.class, id1);
ite.setNumber(20);

TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();
tm.commit();
}

@Test(dependsOnMethods = "initData")
public void testRevisionsCounts() throws Exception {
assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(IntTestEntity.class, id1));
assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(IntTestEntity.class, id1));
}

@Test(dependsOnMethods = "initData")
Expand Down

0 comments on commit 8e2619c

Please sign in to comment.