HHH-7948 - Fix and test

This commit is contained in:
Lukasz Antoniak 2013-01-29 19:03:06 +01:00
parent 3bb0f246fc
commit c05238bad3
3 changed files with 77 additions and 3 deletions

View File

@ -76,6 +76,7 @@ public class CollectionChangeWorkUnit extends AbstractAuditWorkUnit implements A
} }
public AuditWorkUnit merge(ModWorkUnit second) { public AuditWorkUnit merge(ModWorkUnit second) {
mergeCollectionModifiedData(second.getData());
return second; return second;
} }

View File

@ -129,7 +129,7 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest
settings.put( "org.hibernate.envers.audit_strategy", getAuditStrategy() ); settings.put( "org.hibernate.envers.audit_strategy", getAuditStrategy() );
} }
if ( ! isAudit() ) { if ( ! autoRegisterListeners() ) {
settings.put( EnversIntegrator.AUTO_REGISTER, "false" ); settings.put( EnversIntegrator.AUTO_REGISTER, "false" );
} }
@ -214,7 +214,7 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest
return null; return null;
} }
protected boolean isAudit() { protected boolean autoRegisterListeners() {
return true; return true;
} }
@ -224,11 +224,12 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest
entityManagerFactory.close(); entityManagerFactory.close();
} }
} }
@After @After
@SuppressWarnings({ "UnusedDeclaration" }) @SuppressWarnings({ "UnusedDeclaration" })
public void releaseUnclosedEntityManagers() { public void releaseUnclosedEntityManagers() {
releaseUnclosedEntityManager( this.em ); releaseUnclosedEntityManager( this.em );
auditReader =null; auditReader = null;
for ( EntityManager isolatedEm : isolatedEms ) { for ( EntityManager isolatedEm : isolatedEms ) {
releaseUnclosedEntityManager( isolatedEm ); releaseUnclosedEntityManager( isolatedEm );
} }

View File

@ -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<ListRefIngEntity>() ); // 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 ) );
}
}