HHH-6020: some refactoring on how the JTA tests are done. All hibernate config is in the AbstractEntityTest now, instead of loading an xml file.
This commit is contained in:
parent
e9f16e4a76
commit
8c233312ce
|
@ -23,26 +23,20 @@
|
|||
*/
|
||||
package org.hibernate.envers.test;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.transaction.TransactionManager;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.ejb.AvailableSettings;
|
||||
import org.hibernate.testing.AfterClassOnce;
|
||||
import org.hibernate.testing.BeforeClassOnce;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.ejb.Ejb3Configuration;
|
||||
import org.hibernate.envers.AuditReader;
|
||||
import org.hibernate.envers.AuditReaderFactory;
|
||||
import org.hibernate.envers.event.EnversIntegrator;
|
||||
import org.hibernate.service.internal.BasicServiceRegistryImpl;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.hibernate.testing.AfterClassOnce;
|
||||
import org.hibernate.testing.BeforeClassOnce;
|
||||
import org.junit.Before;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
|
@ -84,22 +78,26 @@ public abstract class AbstractEntityTest extends AbstractEnversTest {
|
|||
this.audited = audited;
|
||||
|
||||
cfg = new Ejb3Configuration();
|
||||
if ( ! audited ) {
|
||||
cfg.setProperty( EnversIntegrator.AUTO_REGISTER, "false" );
|
||||
Properties configValues = cfg.getProperties();
|
||||
if (!audited) {
|
||||
configValues.setProperty(EnversIntegrator.AUTO_REGISTER, "false");
|
||||
}
|
||||
|
||||
cfg.configure( "hibernate.test.cfg.xml" );
|
||||
configValues.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
|
||||
configValues.setProperty(Environment.DIALECT, "org.hibernate.dialect.H2Dialect");
|
||||
configValues.setProperty(Environment.DRIVER, "org.h2.Driver");
|
||||
configValues.setProperty(Environment.USER, "sa");
|
||||
|
||||
// Separate database for each test class
|
||||
configValues.setProperty(Environment.URL, "jdbc:h2:mem:" + this.getClass().getName());
|
||||
|
||||
if (auditStrategy != null && !"".equals(auditStrategy)) {
|
||||
cfg.setProperty("org.hibernate.envers.audit_strategy", auditStrategy);
|
||||
}
|
||||
|
||||
// Separate database for each test class
|
||||
cfg.setProperty( Environment.URL, "jdbc:h2:mem:" + this.getClass().getName() );
|
||||
|
||||
configure( cfg );
|
||||
|
||||
serviceRegistry = new BasicServiceRegistryImpl( cfg.getProperties() );
|
||||
serviceRegistry = new BasicServiceRegistryImpl(configValues);
|
||||
|
||||
emf = cfg.buildEntityManagerFactory( serviceRegistry );
|
||||
|
||||
|
@ -124,9 +122,4 @@ public abstract class AbstractEntityTest extends AbstractEnversTest {
|
|||
public Ejb3Configuration getCfg() {
|
||||
return cfg;
|
||||
}
|
||||
|
||||
protected TransactionManager addJTAConfig(Ejb3Configuration cfg) {
|
||||
cfg.getProperties().put(AvailableSettings.TRANSACTION_TYPE, "JTA");
|
||||
return EnversTestingJtaBootstrap.updateConfigAndCreateTM(cfg.getProperties());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,16 +5,15 @@ 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.ejb.AvailableSettings;
|
||||
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.Status;
|
||||
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
|
||||
|
@ -48,10 +47,24 @@ public class EnversTestingJtaBootstrap {
|
|||
dataSource.setUrl(configValues.get(Environment.URL).toString());
|
||||
dataSource.setUser(configValues.get(Environment.USER).toString());
|
||||
|
||||
configValues.remove(Environment.URL);
|
||||
configValues.remove(Environment.USER);
|
||||
configValues.remove(Environment.DRIVER);
|
||||
|
||||
configValues.put( JtaPlatformInitiator.JTA_PLATFORM, new JBossStandAloneJtaPlatform() );
|
||||
configValues.put( Environment.CONNECTION_PROVIDER, DatasourceConnectionProviderImpl.class.getName() );
|
||||
configValues.put( Environment.DATASOURCE, dataSource );
|
||||
|
||||
configValues.put(AvailableSettings.TRANSACTION_TYPE, "JTA");
|
||||
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
public static void tryCommit(TransactionManager tm) throws Exception {
|
||||
if (tm.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
|
||||
tm.rollback();
|
||||
} else {
|
||||
tm.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,8 @@ import org.junit.Test;
|
|||
import javax.persistence.EntityManager;
|
||||
import javax.transaction.TransactionManager;
|
||||
|
||||
import static org.hibernate.envers.test.EnversTestingJtaBootstrap.*;
|
||||
|
||||
/**
|
||||
* Same as {@link org.hibernate.envers.test.integration.reventity.ExceptionListener}, but in a JTA environment.
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
|
@ -40,36 +42,40 @@ public class JtaExceptionListener extends AbstractEntityTest {
|
|||
private TransactionManager tm;
|
||||
|
||||
public void configure(Ejb3Configuration cfg) {
|
||||
tm = updateConfigAndCreateTM(cfg.getProperties());
|
||||
|
||||
cfg.addAnnotatedClass(StrTestEntity.class);
|
||||
cfg.addAnnotatedClass(ExceptionListenerRevEntity.class);
|
||||
|
||||
tm = addJTAConfig(cfg);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void testTransactionRollback() throws Exception {
|
||||
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();
|
||||
StrTestEntity te = new StrTestEntity("x");
|
||||
em.persist(te);
|
||||
|
||||
tm.commit();
|
||||
try {
|
||||
// Trying to persist an entity - however the listener should throw an exception, so the entity
|
||||
// shouldn't be persisted
|
||||
newEntityManager();
|
||||
EntityManager em = getEntityManager();
|
||||
StrTestEntity te = new StrTestEntity("x");
|
||||
em.persist(te);
|
||||
} finally {
|
||||
tryCommit(tm);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDataNotPersisted() throws Exception {
|
||||
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;
|
||||
|
||||
tm.commit();
|
||||
try {
|
||||
// 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;
|
||||
} finally {
|
||||
tryCommit(tm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.hibernate.envers.test.integration.jta;
|
|||
|
||||
import org.hibernate.ejb.Ejb3Configuration;
|
||||
import org.hibernate.envers.test.AbstractEntityTest;
|
||||
import org.hibernate.envers.test.EnversTestingJtaBootstrap;
|
||||
import org.hibernate.envers.test.Priority;
|
||||
import org.hibernate.envers.test.entities.IntTestEntity;
|
||||
import org.junit.Test;
|
||||
|
@ -10,6 +11,8 @@ import javax.persistence.EntityManager;
|
|||
import javax.transaction.TransactionManager;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.hibernate.envers.test.EnversTestingJtaBootstrap.*;
|
||||
|
||||
/**
|
||||
* Same as {@link org.hibernate.envers.test.integration.basic.Simple}, but in a JTA environment.
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
|
@ -20,8 +23,7 @@ public class JtaTransaction extends AbstractEntityTest {
|
|||
|
||||
public void configure(Ejb3Configuration cfg) {
|
||||
cfg.addAnnotatedClass(IntTestEntity.class);
|
||||
|
||||
tm = addJTAConfig(cfg);
|
||||
tm = EnversTestingJtaBootstrap.updateConfigAndCreateTM(cfg.getProperties());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -29,24 +31,30 @@ public class JtaTransaction extends AbstractEntityTest {
|
|||
public void initData() throws Exception {
|
||||
tm.begin();
|
||||
|
||||
newEntityManager();
|
||||
EntityManager em = getEntityManager();
|
||||
IntTestEntity ite = new IntTestEntity(10);
|
||||
em.persist(ite);
|
||||
id1 = ite.getId();
|
||||
|
||||
tm.commit();
|
||||
EntityManager em;
|
||||
IntTestEntity ite;
|
||||
try {
|
||||
newEntityManager();
|
||||
em = getEntityManager();
|
||||
ite = new IntTestEntity(10);
|
||||
em.persist(ite);
|
||||
id1 = ite.getId();
|
||||
} finally {
|
||||
tryCommit(tm);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
tm.begin();
|
||||
|
||||
newEntityManager();
|
||||
em = getEntityManager();
|
||||
ite = em.find(IntTestEntity.class, id1);
|
||||
ite.setNumber(20);
|
||||
|
||||
tm.commit();
|
||||
try {
|
||||
newEntityManager();
|
||||
em = getEntityManager();
|
||||
ite = em.find(IntTestEntity.class, id1);
|
||||
ite.setNumber(20);
|
||||
} finally {
|
||||
tryCommit(tm);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
|
||||
<!--suppress HibernateConfigDomInspection -->
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="hbm2ddl.auto">create-drop</property>
|
||||
|
||||
<property name="show_sql">false</property>
|
||||
<property name="format_sql">true</property>
|
||||
|
||||
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
|
||||
<property name="connection.driver_class">org.h2.Driver</property>
|
||||
<property name="connection.username">sa</property>
|
||||
<property name="connection.password"></property>
|
||||
<!-- The connection URL is set in AbstractEntityTest -->
|
||||
|
||||
<!--<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>-->
|
||||
<!--<property name="connection.url">jdbc:mysql:///hibernate_tests?useUnicode=true&characterEncoding=UTF-8</property>-->
|
||||
<!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property>-->
|
||||
<!--<property name="connection.username">root</property>-->
|
||||
<!--<property name="connection.password"></property>-->
|
||||
|
||||
<!--<property name="org.hibernate.envers.audit_strategy">org.hibernate.envers.strategy.ValidityAuditStrategy</property>-->
|
||||
|
||||
<!--<property name="hibernate.jdbc.batch_size">100</property>-->
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
Loading…
Reference in New Issue