Skip to content

Commit

Permalink
HHH-6020: some refactoring on how the JTA tests are done. All hiberna…
Browse files Browse the repository at this point in the history
…te config is in the AbstractEntityTest now, instead of loading an xml file.
  • Loading branch information
adamw committed Apr 2, 2011
1 parent e9f16e4 commit 8c23331
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 91 deletions.
Expand Up @@ -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)
Expand Down Expand Up @@ -84,22 +78,26 @@ protected void init(boolean audited, String auditStrategy) throws IOException {
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 );

Expand All @@ -124,9 +122,4 @@ public AuditReader getAuditReader() {
public Ejb3Configuration getCfg() {
return cfg;
}

protected TransactionManager addJTAConfig(Ejb3Configuration cfg) {
cfg.getProperties().put(AvailableSettings.TRANSACTION_TYPE, "JTA");
return EnversTestingJtaBootstrap.updateConfigAndCreateTM(cfg.getProperties());
}
}
Expand Up @@ -5,16 +5,15 @@
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
Expand Down Expand Up @@ -48,10 +47,24 @@ public static TransactionManager updateConfigAndCreateTM(Map configValues) {
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();
}
}
}
Expand Up @@ -32,6 +32,8 @@
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)
Expand All @@ -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);
}
}
}
Expand Up @@ -2,6 +2,7 @@

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;
Expand All @@ -10,6 +11,8 @@
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)
Expand All @@ -20,33 +23,38 @@ public class JtaTransaction extends AbstractEntityTest {

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

tm = addJTAConfig(cfg);
tm = EnversTestingJtaBootstrap.updateConfigAndCreateTM(cfg.getProperties());
}

@Test
@Priority(10)
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
Expand Down
30 changes: 0 additions & 30 deletions hibernate-envers/src/test/resources/hibernate.test.cfg.xml

This file was deleted.

0 comments on commit 8c23331

Please sign in to comment.