From 7b67cfc999b956b1c27720dcc2542c61aa6d80c5 Mon Sep 17 00:00:00 2001 From: Tomasz Dziurko Date: Thu, 11 Oct 2012 08:48:58 +0200 Subject: [PATCH 1/3] Added failing tests showing bug HHH-7683 --- .../envers/test/integration/basic/Simple.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/Simple.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/Simple.java index 13f16a206a..3c976852b8 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/Simple.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/Simple.java @@ -24,6 +24,7 @@ package org.hibernate.envers.test.integration.basic; import java.util.Arrays; +import java.util.List; import javax.persistence.EntityManager; import org.hibernate.envers.test.AbstractEntityTest; @@ -33,6 +34,8 @@ import org.testng.annotations.Test; import org.hibernate.ejb.Ejb3Configuration; +import static org.testng.Assert.assertEquals; + /** * @author Adam Warski (adam at warski dot org) */ @@ -71,4 +74,29 @@ public class Simple extends AbstractEntityTest { assert getAuditReader().find(IntTestEntity.class, id1, 1).equals(ver1); assert getAuditReader().find(IntTestEntity.class, id1, 2).equals(ver2); } + + @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(); + + // 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); + assertEquals(revisionsForSavedClass.size(), 1, "There should be one revision for inserted entity"); + + List revisionsForRolledbackClass = getAuditReader().getRevisions(IntTestEntity.class, rollbackedIteId); + assertEquals(revisionsForRolledbackClass.size(), 0, "There should be no revisions for insert that was rolled back"); + } } \ No newline at end of file From 02c071718f29797bf0d76f3a6f2e6a5438ccaab1 Mon Sep 17 00:00:00 2001 From: Tomasz Dziurko Date: Thu, 11 Oct 2012 09:01:02 +0200 Subject: [PATCH 2/3] Added fix in AuditProcessManager to make test green --- .../envers/synchronization/AuditProcessManager.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/AuditProcessManager.java b/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/AuditProcessManager.java index 1f0eadc450..508fba81d6 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/AuditProcessManager.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/AuditProcessManager.java @@ -27,6 +27,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.hibernate.action.AfterTransactionCompletionProcess; +import org.hibernate.action.BeforeTransactionCompletionProcess; import org.hibernate.engine.SessionImplementor; import org.hibernate.envers.revisioninfo.RevisionInfoGenerator; @@ -55,7 +56,13 @@ public class AuditProcessManager { auditProcess = new AuditProcess(revisionInfoGenerator, session); auditProcesses.put(transaction, auditProcess); - session.getActionQueue().registerProcess(auditProcess); + session.getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() { + public void doBeforeTransactionCompletion(SessionImplementor session) { + if(auditProcesses.get(transaction) != null) { + auditProcesses.get(transaction).doBeforeTransactionCompletion(session); + } + } + }); session.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() { public void doAfterTransactionCompletion(boolean success, SessionImplementor session) { From df1e818b347c5c247a2cbae4176eeb75c9be9d90 Mon Sep 17 00:00:00 2001 From: Tomasz Dziurko Date: Thu, 11 Oct 2012 12:17:38 +0200 Subject: [PATCH 3/3] Moved test case to a separate class --- .../envers/test/integration/basic/Simple.java | 28 ----------- .../basic/TransactionRollbackBehaviour.java | 46 +++++++++++++++++++ 2 files changed, 46 insertions(+), 28 deletions(-) create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/Simple.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/Simple.java index 3c976852b8..13f16a206a 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/Simple.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/Simple.java @@ -24,7 +24,6 @@ package org.hibernate.envers.test.integration.basic; import java.util.Arrays; -import java.util.List; import javax.persistence.EntityManager; import org.hibernate.envers.test.AbstractEntityTest; @@ -34,8 +33,6 @@ import org.testng.annotations.Test; import org.hibernate.ejb.Ejb3Configuration; -import static org.testng.Assert.assertEquals; - /** * @author Adam Warski (adam at warski dot org) */ @@ -74,29 +71,4 @@ public class Simple extends AbstractEntityTest { assert getAuditReader().find(IntTestEntity.class, id1, 1).equals(ver1); assert getAuditReader().find(IntTestEntity.class, id1, 2).equals(ver2); } - - @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(); - - // 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); - assertEquals(revisionsForSavedClass.size(), 1, "There should be one revision for inserted entity"); - - List revisionsForRolledbackClass = getAuditReader().getRevisions(IntTestEntity.class, rollbackedIteId); - assertEquals(revisionsForRolledbackClass.size(), 0, "There should be no revisions for insert that was rolled back"); - } } \ No newline at end of file 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 new file mode 100644 index 0000000000..fd0931d319 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java @@ -0,0 +1,46 @@ +package org.hibernate.envers.test.integration.basic; + +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.envers.test.AbstractEntityTest; +import org.hibernate.envers.test.entities.IntTestEntity; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.List; + +import static org.testng.Assert.assertEquals; + +/** + * @author Tomasz Dziurko (tdziurko at gmail dot com) + */ +public class TransactionRollbackBehaviour extends AbstractEntityTest { + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(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(); + + // 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); + assertEquals(revisionsForSavedClass.size(), 1, "There should be one revision for inserted entity"); + + List revisionsForRolledbackClass = getAuditReader().getRevisions(IntTestEntity.class, rollbackedIteId); + assertEquals(revisionsForRolledbackClass.size(), 0, "There should be no revisions for insert that was rolled back"); + } +}