From c05238bad3cda475e1052d1097d62dc61a8844cd Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Tue, 29 Jan 2013 19:03:06 +0100 Subject: [PATCH] HHH-7948 - Fix and test --- .../work/CollectionChangeWorkUnit.java | 1 + .../test/BaseEnversJPAFunctionalTestCase.java | 7 +- .../modifiedflags/HasChangedMergeTest.java | 72 +++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedMergeTest.java diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/work/CollectionChangeWorkUnit.java b/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/work/CollectionChangeWorkUnit.java index f28270f74b..f4f73786f3 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/work/CollectionChangeWorkUnit.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/work/CollectionChangeWorkUnit.java @@ -76,6 +76,7 @@ public class CollectionChangeWorkUnit extends AbstractAuditWorkUnit implements A } public AuditWorkUnit merge(ModWorkUnit second) { + mergeCollectionModifiedData(second.getData()); return second; } 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 1856dba36d..242761b26c 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 @@ -129,7 +129,7 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest settings.put( "org.hibernate.envers.audit_strategy", getAuditStrategy() ); } - if ( ! isAudit() ) { + if ( ! autoRegisterListeners() ) { settings.put( EnversIntegrator.AUTO_REGISTER, "false" ); } @@ -214,7 +214,7 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest return null; } - protected boolean isAudit() { + protected boolean autoRegisterListeners() { return true; } @@ -224,11 +224,12 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest entityManagerFactory.close(); } } + @After @SuppressWarnings({ "UnusedDeclaration" }) public void releaseUnclosedEntityManagers() { releaseUnclosedEntityManager( this.em ); - auditReader =null; + auditReader = null; for ( EntityManager isolatedEm : isolatedEms ) { releaseUnclosedEntityManager( isolatedEm ); } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedMergeTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedMergeTest.java new file mode 100644 index 0000000000..1f1f608f96 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedMergeTest.java @@ -0,0 +1,72 @@ +package org.hibernate.envers.test.integration.modifiedflags; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.EntityManager; + +import org.junit.Test; + +import org.hibernate.envers.test.Priority; +import org.hibernate.envers.test.entities.onetomany.ListRefEdEntity; +import org.hibernate.envers.test.entities.onetomany.ListRefIngEntity; +import org.hibernate.testing.TestForIssue; + +import static org.hibernate.envers.test.tools.TestTools.extractRevisionNumbers; +import static org.hibernate.envers.test.tools.TestTools.makeList; +import static org.junit.Assert.assertEquals; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class HasChangedMergeTest extends AbstractModifiedFlagsEntityTest { + private Integer parentId = null; + private Integer childId = null; + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { ListRefEdEntity.class, ListRefIngEntity.class }; + } + + @Test + @Priority(10) + public void initData() { + EntityManager em = getEntityManager(); + + // Revision 1 - data preparation + em.getTransaction().begin(); + ListRefEdEntity parent = new ListRefEdEntity( 1, "initial data" ); + parent.setReffering( new ArrayList() ); // Empty collection is not the same as null reference. + em.persist( parent ); + em.getTransaction().commit(); + + // Revision 2 - inserting new child entity and updating parent + em.getTransaction().begin(); + parent = em.find( ListRefEdEntity.class, parent.getId() ); + ListRefIngEntity child = new ListRefIngEntity( 1, "initial data", parent ); + em.persist( child ); + parent.setData( "updated data" ); + parent = em.merge( parent ); + em.getTransaction().commit(); + + parentId = parent.getId(); + childId = child.getId(); + + em.close(); + } + + @Test + @TestForIssue(jiraKey = "HHH-7948") + public void testOneToManyInsertChildUpdateParent() { + List list = queryForPropertyHasChanged( ListRefEdEntity.class, parentId, "data" ); + assertEquals( 2, list.size() ); + assertEquals( makeList( 1, 2 ), extractRevisionNumbers( list ) ); + + list = queryForPropertyHasChanged( ListRefEdEntity.class, parentId, "reffering" ); + assertEquals( 2, list.size() ); + assertEquals( makeList( 1, 2 ), extractRevisionNumbers( list ) ); + + list = queryForPropertyHasChanged( ListRefIngEntity.class, childId, "reference" ); + assertEquals( 1, list.size() ); + assertEquals( makeList( 2 ), extractRevisionNumbers( list ) ); + } +}