From 58473c87dd6bc0a0c420ab2c75a113e886650712 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Fri, 6 May 2016 14:37:00 -0500 Subject: [PATCH] HHH-10664 - Prep 5.2 feature branch - Envers --- hibernate-envers/hibernate-envers.gradle | 4 +- .../internal/entities/RevisionTypeType.java | 6 +- .../synchronization/AuditProcess.java | 5 +- .../synchronization/AuditProcessManager.java | 5 +- .../synchronization/SessionCacheCleaner.java | 4 +- .../envers/internal/tools/EntityTools.java | 4 +- .../test/BaseEnversJPAFunctionalTestCase.java | 19 ++- .../customtype/CompositeTestUserType.java | 13 +- .../customtype/ParametrizedTestUserType.java | 6 +- .../test/entities/ids/CustomEnumUserType.java | 6 +- .../basic/OutsideTransactionTest.java | 14 +- .../basic/RegisterUserEventListenersTest.java | 4 +- .../basic/TransactionRollbackBehaviour.java | 121 +++++++++--------- .../DetachedMultipleCollectionChangeTest.java | 2 + .../components/dynamic/AgeType.java | 6 +- ...tedDynamicComponentsAdvancedCasesTest.java | 14 +- .../components/dynamic/SanityCheckTest.java | 24 ++-- .../customtype/ObjectUserType.java | 6 +- .../customtype/UnspecifiedEnumTypeTest.java | 19 ++- .../test/integration/jta/JtaTransaction.java | 2 + .../modifiedflags/HasChangedComponents.java | 4 +- .../HasChangedForDefaultNotUsing.java | 6 +- .../HasChangedUnversionedProperties.java | 4 +- 23 files changed, 143 insertions(+), 155 deletions(-) diff --git a/hibernate-envers/hibernate-envers.gradle b/hibernate-envers/hibernate-envers.gradle index bedfb3a882..2a6701ba31 100644 --- a/hibernate-envers/hibernate-envers.gradle +++ b/hibernate-envers/hibernate-envers.gradle @@ -13,8 +13,8 @@ dependencies { provided( libraries.ant ) provided( project( ':hibernate-jpamodelgen' ) ) - testCompile( project(':hibernate-testing') ) - + testCompile( project( ':hibernate-testing' ) ) + testCompile( project( path: ':hibernate-core', configuration: 'tests' ) ) testRuntime( libraries.javassist ) } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/RevisionTypeType.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/RevisionTypeType.java index 50d8322561..a6b964af85 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/RevisionTypeType.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/RevisionTypeType.java @@ -13,7 +13,7 @@ import java.sql.SQLException; import java.sql.Types; import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.envers.RevisionType; import org.hibernate.internal.util.compare.EqualsHelper; import org.hibernate.type.IntegerType; @@ -40,7 +40,7 @@ public class RevisionTypeType implements UserType, Serializable { } @Override - public RevisionType nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner) + public RevisionType nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { final Integer representationInt = IntegerType.INSTANCE.nullSafeGet( resultSet, names[0], session ); return representationInt == null ? @@ -49,7 +49,7 @@ public class RevisionTypeType implements UserType, Serializable { } @Override - public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SessionImplementor session) + public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { IntegerType.INSTANCE.nullSafeSet( preparedStatement, diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcess.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcess.java index 9d4c220f37..68dd09f917 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcess.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcess.java @@ -10,16 +10,15 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; +import javax.persistence.FlushModeType; import org.hibernate.ConnectionReleaseMode; -import org.hibernate.FlushMode; import org.hibernate.Session; import org.hibernate.action.spi.BeforeTransactionCompletionProcess; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.envers.internal.revisioninfo.RevisionInfoGenerator; import org.hibernate.envers.internal.synchronization.work.AuditWorkUnit; import org.hibernate.envers.tools.Pair; - import org.jboss.logging.Logger; /** @@ -134,7 +133,7 @@ public class AuditProcess implements BeforeTransactionCompletionProcess { } // see: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431 - if ( FlushMode.isManualFlushMode( session.getFlushMode() ) ) { + if ( FlushModeType.COMMIT.equals ( session.getFlushMode() ) ) { Session temporarySession = null; try { temporarySession = ((Session) session).sessionWithOptions() diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcessManager.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcessManager.java index e7cf12a6fc..fed261f9b9 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcessManager.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcessManager.java @@ -13,6 +13,7 @@ import org.hibernate.Transaction; import org.hibernate.action.spi.AfterTransactionCompletionProcess; import org.hibernate.action.spi.BeforeTransactionCompletionProcess; import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.envers.internal.revisioninfo.RevisionInfoGenerator; import org.hibernate.event.spi.EventSource; @@ -30,7 +31,7 @@ public class AuditProcessManager { } public AuditProcess get(EventSource session) { - final Transaction transaction = session.getTransaction(); + final Transaction transaction = session.accessTransaction(); AuditProcess auditProcess = auditProcesses.get( transaction ); if ( auditProcess == null ) { @@ -51,7 +52,7 @@ public class AuditProcessManager { session.getActionQueue().registerProcess( new AfterTransactionCompletionProcess() { - public void doAfterTransactionCompletion(boolean success, SessionImplementor session) { + public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) { auditProcesses.remove( transaction ); } } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/SessionCacheCleaner.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/SessionCacheCleaner.java index 21a21a7470..30e98ddc92 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/SessionCacheCleaner.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/SessionCacheCleaner.java @@ -9,7 +9,7 @@ package org.hibernate.envers.internal.synchronization; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.action.spi.AfterTransactionCompletionProcess; -import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.event.spi.EventSource; /** @@ -29,7 +29,7 @@ public class SessionCacheCleaner { public void scheduleAuditDataRemoval(final Session session, final Object data) { ((EventSource) session).getActionQueue().registerProcess( new AfterTransactionCompletionProcess() { - public void doAfterTransactionCompletion(boolean success, SessionImplementor sessionImplementor) { + public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor sessionImplementor) { if ( !sessionImplementor.isClosed() ) { try { ( (Session) sessionImplementor ).evict( data ); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/tools/EntityTools.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/tools/EntityTools.java index e2dfc52751..9dd1886a72 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/tools/EntityTools.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/tools/EntityTools.java @@ -7,10 +7,10 @@ package org.hibernate.envers.internal.tools; import javassist.util.proxy.ProxyFactory; - import org.hibernate.Session; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.proxy.HibernateProxy; @@ -43,7 +43,7 @@ public abstract class EntityTools { return proxy.getHibernateLazyInitializer().getImplementation(); } - final SessionImplementor sessionImplementor = proxy.getHibernateLazyInitializer().getSession(); + final SharedSessionContractImplementor sessionImplementor = proxy.getHibernateLazyInitializer().getSession(); final Session tempSession = sessionImplementor == null ? sessionFactoryImplementor.openTemporarySession() : sessionImplementor.getFactory().openTemporarySession(); diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/BaseEnversJPAFunctionalTestCase.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/BaseEnversJPAFunctionalTestCase.java index f047a5e0c5..28bac8234f 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/BaseEnversJPAFunctionalTestCase.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/BaseEnversJPAFunctionalTestCase.java @@ -12,13 +12,14 @@ import java.util.HashMap; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; -import javax.persistence.spi.PersistenceUnitTransactionType; import javax.transaction.SystemException; import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.H2Dialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper; import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReaderFactory; @@ -26,19 +27,16 @@ import org.hibernate.envers.boot.internal.EnversIntegrator; import org.hibernate.envers.configuration.EnversSettings; import org.hibernate.internal.util.StringHelper; import org.hibernate.jpa.AvailableSettings; -import org.hibernate.jpa.HibernateEntityManagerFactory; import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; import org.hibernate.jpa.boot.spi.Bootstrap; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor; import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; - import org.hibernate.testing.AfterClassOnce; import org.hibernate.testing.BeforeClassOnce; import org.hibernate.testing.jta.TestingJtaPlatformImpl; import org.hibernate.testing.junit4.Helper; -import org.junit.After; - import org.jboss.logging.Logger; +import org.junit.After; /** * @author Strong Liu (stliu@hibernate.org) @@ -50,7 +48,7 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest private EntityManagerFactoryBuilderImpl entityManagerFactoryBuilder; private StandardServiceRegistryImpl serviceRegistry; - private HibernateEntityManagerFactory entityManagerFactory; + private SessionFactoryImplementor entityManagerFactory; private EntityManager em; private AuditReader auditReader; @@ -81,10 +79,9 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest buildPersistenceUnitDescriptor(), buildSettings() ); - entityManagerFactory = entityManagerFactoryBuilder.build().unwrap( HibernateEntityManagerFactory.class ); + entityManagerFactory = entityManagerFactoryBuilder.build().unwrap( SessionFactoryImplementor.class ); - serviceRegistry = (StandardServiceRegistryImpl) entityManagerFactory.getSessionFactory() - .getServiceRegistry() + serviceRegistry = (StandardServiceRegistryImpl) entityManagerFactory.getServiceRegistry() .getParentServiceRegistry(); afterEntityManagerFactoryBuilt(); @@ -265,9 +262,9 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest protected AuditReader getAuditReader() { EntityManager entityManager = getOrCreateEntityManager(); - PersistenceUnitTransactionType transactionType = ((EntityManagerImpl) entityManager).getTransactionType(); + SessionImplementor sessionImplementor = entityManager.unwrap( SessionImplementor.class ); - if ( transactionType == PersistenceUnitTransactionType.JTA ) { + if ( sessionImplementor.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta() ) { if ( !JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ) { try { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/customtype/CompositeTestUserType.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/customtype/CompositeTestUserType.java index d594ee5c83..09e33c8093 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/customtype/CompositeTestUserType.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/customtype/CompositeTestUserType.java @@ -12,7 +12,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.type.IntegerType; import org.hibernate.type.StringType; import org.hibernate.type.Type; @@ -75,7 +75,7 @@ public class CompositeTestUserType implements CompositeUserType { public Object nullSafeGet( final ResultSet rs, final String[] names, - final SessionImplementor session, + final SharedSessionContractImplementor session, final Object owner) throws HibernateException, SQLException { final String prop1 = rs.getString( names[0] ); if ( prop1 == null ) { @@ -88,7 +88,7 @@ public class CompositeTestUserType implements CompositeUserType { public void nullSafeSet( final PreparedStatement st, final Object value, - final int index, final SessionImplementor session) + final int index, final SharedSessionContractImplementor session) throws HibernateException, SQLException { if ( value == null ) { st.setNull( index, StringType.INSTANCE.sqlType() ); @@ -110,19 +110,20 @@ public class CompositeTestUserType implements CompositeUserType { return true; } - public Serializable disassemble(final Object value, final SessionImplementor session) throws HibernateException { + public Serializable disassemble( + final Object value, final SharedSessionContractImplementor session) throws HibernateException { return (Serializable) value; } public Object assemble( - final Serializable cached, final SessionImplementor session, + final Serializable cached, final SharedSessionContractImplementor session, final Object owner) throws HibernateException { return cached; } public Object replace( Object original, Object target, - SessionImplementor session, Object owner) throws HibernateException { + SharedSessionContractImplementor session, Object owner) throws HibernateException { return original; } } \ No newline at end of file diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/customtype/ParametrizedTestUserType.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/customtype/ParametrizedTestUserType.java index aa0afb04be..c088f0d03e 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/customtype/ParametrizedTestUserType.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/customtype/ParametrizedTestUserType.java @@ -14,7 +14,7 @@ import java.sql.Types; import java.util.Properties; import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.type.StringType; import org.hibernate.usertype.ParameterizedType; import org.hibernate.usertype.UserType; @@ -37,12 +37,12 @@ public class ParametrizedTestUserType implements UserType, ParameterizedType { return String.class; } - public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) + public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { return StringType.INSTANCE.nullSafeGet( rs, names[0], session ); } - public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) + public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { if ( value != null ) { String v = (String) value; diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/ids/CustomEnumUserType.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/ids/CustomEnumUserType.java index 10faba5cec..5837e227e6 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/ids/CustomEnumUserType.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/ids/CustomEnumUserType.java @@ -13,7 +13,7 @@ import java.sql.SQLException; import java.sql.Types; import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.usertype.UserType; /** @@ -44,7 +44,7 @@ public class CustomEnumUserType implements UserType { return (x == null) ? 0 : x.hashCode(); } - public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) + public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { String name = rs.getString( names[0] ); if ( rs.wasNull() ) { @@ -53,7 +53,7 @@ public class CustomEnumUserType implements UserType { return CustomEnum.fromYesNo( name ); } - public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) + public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { CustomEnum val = (CustomEnum) value; if ( val == null ) { diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/OutsideTransactionTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/OutsideTransactionTest.java index f6d5d8b145..3b7d6d5c75 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/OutsideTransactionTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/OutsideTransactionTest.java @@ -7,19 +7,17 @@ package org.hibernate.envers.test.integration.basic; import java.util.Map; +import javax.persistence.TransactionRequiredException; import org.hibernate.Session; import org.hibernate.dialect.MySQL5Dialect; import org.hibernate.envers.configuration.EnversSettings; -import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.test.BaseEnversFunctionalTestCase; import org.hibernate.envers.test.entities.StrTestEntity; import org.hibernate.envers.test.integration.collection.norevision.Name; import org.hibernate.envers.test.integration.collection.norevision.Person; - import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.TestForIssue; - import org.junit.Test; /** @@ -41,7 +39,7 @@ public class OutsideTransactionTest extends BaseEnversFunctionalTestCase { settings.put( EnversSettings.REVISION_ON_COLLECTION_CHANGE, "true" ); } - @Test(expected = AuditException.class) + @Test(expected = TransactionRequiredException.class) public void testInsertOutsideActiveTransaction() { Session session = openSession(); @@ -53,7 +51,7 @@ public class OutsideTransactionTest extends BaseEnversFunctionalTestCase { session.close(); } - @Test(expected = AuditException.class) + @Test(expected = TransactionRequiredException.class) public void testUpdateOutsideActiveTransaction() { Session session = openSession(); @@ -71,7 +69,7 @@ public class OutsideTransactionTest extends BaseEnversFunctionalTestCase { session.close(); } - @Test(expected = AuditException.class) + @Test(expected = TransactionRequiredException.class) public void testDeleteOutsideActiveTransaction() { Session session = openSession(); @@ -88,7 +86,7 @@ public class OutsideTransactionTest extends BaseEnversFunctionalTestCase { session.close(); } - @Test(expected = AuditException.class) + @Test(expected = TransactionRequiredException.class) public void testCollectionUpdateOutsideActiveTransaction() { Session session = openSession(); @@ -109,7 +107,7 @@ public class OutsideTransactionTest extends BaseEnversFunctionalTestCase { session.close(); } - @Test(expected = AuditException.class) + @Test(expected = TransactionRequiredException.class) public void testCollectionRemovalOutsideActiveTransaction() { Session session = openSession(); diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/RegisterUserEventListenersTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/RegisterUserEventListenersTest.java index 859894d0e4..4054cd5995 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/RegisterUserEventListenersTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/RegisterUserEventListenersTest.java @@ -10,6 +10,7 @@ import org.hibernate.Session; import org.hibernate.action.spi.AfterTransactionCompletionProcess; import org.hibernate.action.spi.BeforeTransactionCompletionProcess; import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.envers.internal.tools.MutableInteger; import org.hibernate.envers.test.BaseEnversFunctionalTestCase; import org.hibernate.envers.test.entities.StrTestEntity; @@ -18,7 +19,6 @@ import org.hibernate.event.spi.EventType; import org.hibernate.event.spi.PostInsertEvent; import org.hibernate.event.spi.PostInsertEventListener; import org.hibernate.persister.entity.EntityPersister; - import org.hibernate.testing.TestForIssue; import org.junit.Assert; import org.junit.Test; @@ -71,7 +71,7 @@ public class RegisterUserEventListenersTest extends BaseEnversFunctionalTestCase event.getSession().getActionQueue().registerProcess( new AfterTransactionCompletionProcess() { @Override - public void doAfterTransactionCompletion(boolean success, SessionImplementor session) { + public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) { afterCounter.increase(); } } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java index 7bba8f432e..8be870f8dc 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java @@ -11,84 +11,79 @@ import javax.persistence.EntityManager; import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; import org.hibernate.envers.test.entities.IntTestEntity; - +import org.hibernate.internal.SessionImpl; import org.hibernate.testing.TestForIssue; -import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * @author Tomasz Dziurko (tdziurko at gmail dot com) */ public class TransactionRollbackBehaviour extends BaseEnversJPAFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {IntTestEntity.class}; + } + @Test - public void testAuditRecordsRollback() { - // Given - EntityManager em = getEntityManager(); - em.getTransaction().begin(); - IntTestEntity iteToRollback = new IntTestEntity( 30 ); - em.persist( iteToRollback ); - Integer rollbackedIteId = iteToRollback.getId(); - em.getTransaction().rollback(); + public void testAuditRecordsRollbackWithAutoClear() { + testAuditRecordsRollbackBehavior( false, true ); + } - // When - em.getTransaction().begin(); - IntTestEntity ite2 = new IntTestEntity( 50 ); - em.persist( ite2 ); - Integer ite2Id = ite2.getId(); - em.getTransaction().commit(); - - // Then - List revisionsForSavedClass = getAuditReader().getRevisions( IntTestEntity.class, ite2Id ); - Assert.assertEquals( "There should be one revision for inserted entity.", 1, revisionsForSavedClass.size() ); - - List revisionsForRolledbackClass = getAuditReader().getRevisions( - IntTestEntity.class, - rollbackedIteId - ); - Assert.assertEquals( - "There should be no revision for rolled back transaction.", - 0, - revisionsForRolledbackClass.size() - ); + @Test + public void testAuditRecordsRollbackWithNoAutoClear() { + testAuditRecordsRollbackBehavior( false, false ); } @Test @TestForIssue(jiraKey = "HHH-8189") public void testFlushedAuditRecordsRollback() { - // Given - EntityManager em = getEntityManager(); - em.getTransaction().begin(); - IntTestEntity iteToRollback = new IntTestEntity( 30 ); - em.persist( iteToRollback ); - em.flush(); - Integer rollbackedIteId = iteToRollback.getId(); - em.getTransaction().rollback(); - - // When - em.getTransaction().begin(); - IntTestEntity ite2 = new IntTestEntity( 50 ); - em.persist( ite2 ); - em.flush(); - Integer ite2Id = ite2.getId(); - em.getTransaction().commit(); - - // Then - List revisionsForSavedClass = getAuditReader().getRevisions( IntTestEntity.class, ite2Id ); - Assert.assertEquals( "There should be one revision for inserted entity.", 1, revisionsForSavedClass.size() ); - - List revisionsForRolledbackClass = getAuditReader().getRevisions( - IntTestEntity.class, - rollbackedIteId - ); - Assert.assertEquals( - "There should be no revision for rolled back transaction.", - 0, - revisionsForRolledbackClass.size() - ); + // default auto-clear behavior + testAuditRecordsRollbackBehavior( true, null ); } - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] {IntTestEntity.class}; + private void testAuditRecordsRollbackBehavior(boolean flush, Boolean autoClear) { + EntityManager entityManager = getEntityManager(); + try { + if ( autoClear != null ) { + entityManager.unwrap( SessionImpl.class ).setAutoClear( autoClear ); + } + + // persist and rollback + entityManager.getTransaction().begin(); + IntTestEntity rollbackEntity = new IntTestEntity( 30 ); + entityManager.persist( rollbackEntity ); + if ( flush ) { + entityManager.flush(); + } + Integer rollbackId = rollbackEntity.getId(); + entityManager.getTransaction().rollback(); + + // persist and commit + entityManager.getTransaction().begin(); + IntTestEntity commitEntity = new IntTestEntity( 50 ); + entityManager.persist( commitEntity ); + if ( flush ) { + entityManager.flush(); + } + Integer commitId = commitEntity.getId(); + entityManager.getTransaction().commit(); + + List revisionsForSavedClass = getAuditReader().getRevisions( + IntTestEntity.class, + commitId + ); + assertEquals( "There should be one revision for inserted entity.", 1, revisionsForSavedClass.size() ); + + List revisionsForRolledbackClass = getAuditReader().getRevisions( + IntTestEntity.class, + rollbackId + ); + assertEquals( "There should be no revision for rolled back entity.", 0, revisionsForRolledbackClass.size() ); + } + finally { + entityManager.close(); + } } } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/DetachedMultipleCollectionChangeTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/DetachedMultipleCollectionChangeTest.java index d221604308..08b00c1bf2 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/DetachedMultipleCollectionChangeTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/DetachedMultipleCollectionChangeTest.java @@ -15,6 +15,7 @@ import javax.persistence.Query; import javax.transaction.Status; import javax.transaction.TransactionManager; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Oracle8iDialect; import org.hibernate.envers.RevisionType; import org.hibernate.envers.enhanced.SequenceIdRevisionEntity; @@ -66,6 +67,7 @@ public class DetachedMultipleCollectionChangeTest extends BaseEnversJPAFunctiona protected void addConfigOptions(Map options) { super.addConfigOptions( options ); TestingJtaBootstrap.prepare( options ); + options.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" ); tm = TestingJtaPlatformImpl.INSTANCE.getTransactionManager(); } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/AgeType.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/AgeType.java index 677b9f8803..3871c535bf 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/AgeType.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/AgeType.java @@ -12,7 +12,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.type.IntegerType; import org.hibernate.usertype.UserType; @@ -41,13 +41,13 @@ public class AgeType implements UserType { } @Override - public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) + public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { return new Age( rs.getInt( rs.findColumn( names[0] ) ) ); } @Override - public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) + public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { st.setInt( index, ( (Age) value ).getAgeInYears() ); } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/AuditedDynamicComponentsAdvancedCasesTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/AuditedDynamicComponentsAdvancedCasesTest.java index 29380ba065..39c408b09d 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/AuditedDynamicComponentsAdvancedCasesTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/AuditedDynamicComponentsAdvancedCasesTest.java @@ -25,6 +25,8 @@ import org.hibernate.testing.TestForIssue; import org.junit.Test; import junit.framework.Assert; +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; + /** * @author Lukasz Zuchowski (author at zuchos dot com) * More advanced tests for dynamic component. @@ -349,12 +351,9 @@ public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctio //then Assert.fail(); - } - catch ( QueryException e ) { - } catch ( Exception e ) { - Assert.fail(); + assertTyping( IllegalArgumentException.class, e ); } try { @@ -367,16 +366,13 @@ public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctio .getResultList(); Assert.fail(); } - catch ( AuditException e ) { + catch ( Exception e ) { + assertTyping( AuditException.class, e ); Assert.assertEquals( "This type of relation (org.hibernate.envers.test.integration.components.dynamic.AdvancedEntity.dynamicConfiguration_internalMapWithEntities) isn't supported and can't be used in queries.", e.getMessage() ); } - catch ( Exception e ) { - Assert.fail(); - } - } @Test diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/SanityCheckTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/SanityCheckTest.java index 55531cb439..d7fb5c9801 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/SanityCheckTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/components/dynamic/SanityCheckTest.java @@ -9,15 +9,17 @@ package org.hibernate.envers.test.integration.components.dynamic; import java.util.ArrayList; import java.util.List; -import org.hibernate.QueryException; +import junit.framework.Assert; import org.hibernate.Session; import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.query.AuditEntity; import org.hibernate.envers.test.BaseEnversFunctionalTestCase; import org.hibernate.envers.test.Priority; - import org.junit.Test; -import junit.framework.Assert; + +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class SanityCheckTest extends BaseEnversFunctionalTestCase { @@ -141,17 +143,15 @@ public class SanityCheckTest extends BaseEnversFunctionalTestCase { .add( AuditEntity.property( "component_manyToManyList" ).eq( manyToManyEntities ) ) .getResultList(); //then - Assert.fail(); + fail( "This should have generated an AuditException" ); } - catch ( AuditException e ) { - Assert.assertEquals( + catch ( Exception e ) { + assertTyping( AuditException.class, e ); + assertEquals( "This type of relation (org.hibernate.envers.test.integration.components.dynamic.PlainEntity.component_manyToManyList) isn't supported and can't be used in queries.", e.getMessage() ); } - catch ( Exception e ) { - Assert.fail(); - } } @Test @@ -176,12 +176,10 @@ public class SanityCheckTest extends BaseEnversFunctionalTestCase { .getResultList(); //then - Assert.fail(); - } - catch ( QueryException e ) { + fail( "This should have generated an IllegalArgumentException" ); } catch ( Exception e ) { - Assert.fail(); + assertTyping( IllegalArgumentException.class, e ); } } } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/ObjectUserType.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/ObjectUserType.java index 794fae02a0..2595d0db60 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/ObjectUserType.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/ObjectUserType.java @@ -20,7 +20,7 @@ import java.sql.SQLException; import java.sql.Types; import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.usertype.UserType; /** @@ -60,14 +60,14 @@ public class ObjectUserType implements UserType { } @Override - public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) + public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { final String type = rs.getString( names[0] ); // For debug purpose. return convertInputStreamToObject( rs.getBinaryStream( names[1] ) ); } @Override - public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) + public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { if ( value == null ) { st.setNull( index, TYPES[0] ); diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/UnspecifiedEnumTypeTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/UnspecifiedEnumTypeTest.java index fb130165e2..3785d01bbf 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/UnspecifiedEnumTypeTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/UnspecifiedEnumTypeTest.java @@ -6,6 +6,8 @@ */ package org.hibernate.envers.test.integration.customtype; +import java.sql.Connection; +import java.sql.SQLException; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -17,6 +19,7 @@ import org.hibernate.envers.test.BaseEnversFunctionalTestCase; import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.entities.customtype.UnspecifiedEnumTypeEntity; +import org.hibernate.jdbc.Work; import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.TestForIssue; import org.junit.Assert; @@ -58,11 +61,10 @@ public class UnspecifiedEnumTypeTest extends BaseEnversFunctionalTestCase { } public void dropSchema(Session session) { - executeUpdateSafety( session, "alter table ENUM_ENTITY_AUD drop constraint FK_AUD_REV" ); executeUpdateSafety( session, "drop table ENUM_ENTITY if exists" ); executeUpdateSafety( session, "drop table ENUM_ENTITY_AUD if exists" ); executeUpdateSafety( session, "drop table REVINFO if exists" ); - executeUpdateSafety( session, "drop sequence REVISION_GENERATOR" ); + executeUpdateSafety( session, "drop sequence REVISION_GENERATOR if exists" ); } private void createSchema(Session session) { @@ -86,11 +88,14 @@ public class UnspecifiedEnumTypeTest extends BaseEnversFunctionalTestCase { } private void executeUpdateSafety(Session session, String query) { - try { - session.createSQLQuery( query ).executeUpdate(); - } - catch (Exception e) { - } + session.doWork( + new Work() { + @Override + public void execute(Connection connection) throws SQLException { + connection.createStatement().execute( query ); + } + } + ); } @Test diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/jta/JtaTransaction.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/jta/JtaTransaction.java index c5e9f51a2a..e17f81ef8a 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/jta/JtaTransaction.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/jta/JtaTransaction.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import javax.persistence.EntityManager; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.entities.IntTestEntity; @@ -35,6 +36,7 @@ public class JtaTransaction extends BaseEnversJPAFunctionalTestCase { @Override protected void addConfigOptions(Map options) { TestingJtaBootstrap.prepare( options ); + options.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" ); } @Test diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedComponents.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedComponents.java index c8586cf7e5..023ef79dc0 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedComponents.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedComponents.java @@ -9,13 +9,11 @@ package org.hibernate.envers.test.integration.modifiedflags; import java.util.List; import javax.persistence.EntityManager; -import org.hibernate.QueryException; import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.entities.components.Component1; import org.hibernate.envers.test.entities.components.Component2; import org.hibernate.envers.test.entities.components.ComponentTestEntity; import org.hibernate.envers.test.tools.TestTools; - import org.junit.Test; import static junit.framework.Assert.assertEquals; @@ -129,7 +127,7 @@ public class HasChangedComponents extends AbstractModifiedFlagsEntityTest { ); } - @Test(expected = QueryException.class) + @Test(expected = IllegalArgumentException.class) public void testHasChangedNotAudited() throws Exception { queryForPropertyHasChanged( ComponentTestEntity.class, id1, "comp2" ); } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedForDefaultNotUsing.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedForDefaultNotUsing.java index bbf3ca633b..91f992f8f7 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedForDefaultNotUsing.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedForDefaultNotUsing.java @@ -10,14 +10,12 @@ import java.util.Arrays; import java.util.List; import javax.persistence.EntityManager; -import org.hibernate.QueryException; import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.entities.StrTestEntity; import org.hibernate.envers.test.entities.components.Component1; import org.hibernate.envers.test.entities.components.Component2; import org.hibernate.envers.test.integration.modifiedflags.entities.PartialModifiedFlagsEntity; import org.hibernate.envers.test.integration.modifiedflags.entities.WithModifiedFlagReferencingEntity; - import org.junit.Test; import static junit.framework.Assert.assertEquals; @@ -191,7 +189,7 @@ public class HasChangedForDefaultNotUsing extends AbstractModifiedFlagsEntityTes assertEquals( makeList( 3 ), extractRevisionNumbers( list ) ); } - @Test(expected = QueryException.class) + @Test(expected = IllegalArgumentException.class) public void testHasChangedComp2() throws Exception { queryForPropertyHasChanged( PartialModifiedFlagsEntity.class, @@ -209,7 +207,7 @@ public class HasChangedForDefaultNotUsing extends AbstractModifiedFlagsEntityTes assertEquals( makeList( 5, 6 ), extractRevisionNumbers( list ) ); } - @Test(expected = QueryException.class) + @Test(expected = IllegalArgumentException.class) public void testHasChangedReferencing2() throws Exception { queryForPropertyHasChanged( PartialModifiedFlagsEntity.class, diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedUnversionedProperties.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedUnversionedProperties.java index 1cf817118f..c6e180f9c4 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedUnversionedProperties.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedUnversionedProperties.java @@ -9,10 +9,8 @@ package org.hibernate.envers.test.integration.modifiedflags; import java.util.List; import javax.persistence.EntityManager; -import org.hibernate.QueryException; import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.integration.basic.BasicTestEntity2; - import org.junit.Test; import static junit.framework.Assert.assertEquals; @@ -69,7 +67,7 @@ public class HasChangedUnversionedProperties extends AbstractModifiedFlagsEntity assertEquals( makeList( 1, 2 ), extractRevisionNumbers( list ) ); } - @Test(expected = QueryException.class) + @Test(expected = IllegalArgumentException.class) public void testExceptionOnHasChangedQuery() throws Exception { queryForPropertyHasChangedWithDeleted( BasicTestEntity2.class,