From e831e29435845c4511e10acb9b9bff22f98604d7 Mon Sep 17 00:00:00 2001 From: Svyatoslav Reyentenko Date: Thu, 2 Dec 2010 03:34:10 +0300 Subject: [PATCH] Ignore collection changes if no revision on collection change is set --- .../envers/event/AuditEventListener.java | 4 ++- .../CollectionChangRevisionTest.java | 27 ++++++++++++++++ .../CollectionChangeNoRevisionTest.java | 32 +++++++++++++++---- .../mappings/norevision/mappings.hbm.xml | 2 +- .../src/test/resources/testng.xml | 1 + 5 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangRevisionTest.java diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java b/hibernate-envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java index 3cbfc70d4a..9beeb9e1e5 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java @@ -257,7 +257,9 @@ public class AuditEventListener implements PostInsertEventListener, PostUpdateEv private void onCollectionAction(AbstractCollectionEvent event, PersistentCollection newColl, Serializable oldColl, CollectionEntry collectionEntry) { String entityName = event.getAffectedOwnerEntityName(); - + if (! verCfg.getGlobalCfg().isGenerateRevisionsForCollections()) { + return; + } if (verCfg.getEntCfg().isVersioned(entityName)) { AuditProcess auditProcess = verCfg.getSyncManager().get(event.getSession()); diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangRevisionTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangRevisionTest.java new file mode 100644 index 0000000000..fb9bd8a7e5 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangRevisionTest.java @@ -0,0 +1,27 @@ +package org.hibernate.envers.test.integration.collection.norevision; + +import org.hibernate.MappingException; +import org.testng.annotations.BeforeMethod; + +import java.net.URISyntaxException; + +public class CollectionChangRevisionTest extends CollectionChangeNoRevisionTest { + + protected static final int PERSON_COUNT_NEW_REVISION_ON_COLLECTION = 2; + protected static final String NEW_REVISION_ON_COLLECTION = "true"; + + @Override + protected void initMappings() throws MappingException, URISyntaxException { + super.initMappings(); + } + + @Override + protected int getExpectedPersonRevisionCount() { + return PERSON_COUNT_NEW_REVISION_ON_COLLECTION; + } + + @Override + protected String getCollectionChangeValue() { + return NEW_REVISION_ON_COLLECTION; + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeNoRevisionTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeNoRevisionTest.java index 329119761d..3786856b8d 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeNoRevisionTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeNoRevisionTest.java @@ -13,16 +13,26 @@ import org.testng.annotations.Test; import java.io.File; import java.net.URISyntaxException; import java.net.URL; +import java.util.ArrayList; +import java.util.List; public class CollectionChangeNoRevisionTest extends AbstractSessionTest { - private Integer personId; + protected static final int EXPECTED_PERSON_REVISION_COUNT = 1; + protected static final int EXPECTED_NAME_REVISION_COUNT = 1; + protected static final String CREATE_REVISION_ON_COLLECTION_CHANGE = "false"; + protected Integer personId; + protected List namesId = new ArrayList(); @Override protected void initMappings() throws MappingException, URISyntaxException { URL url = Thread.currentThread().getContextClassLoader().getResource("mappings/norevision/mappings.hbm.xml"); config.addFile(new File(url.toURI())); - config.setProperty("org.hibernate.envers.revision_on_collection_change", "false"); + config.setProperty("org.hibernate.envers.revision_on_collection_change", getCollectionChangeValue()); + } + + protected String getCollectionChangeValue() { + return CREATE_REVISION_ON_COLLECTION_CHANGE; } @BeforeMethod(firstTimeOnly = true) @@ -36,7 +46,7 @@ public class CollectionChangeNoRevisionTest extends AbstractSessionTest { getSession().saveOrUpdate(p); transaction.commit(); personId = p.getId(); - System.err.print(p); + namesId.add(n.getId()); } @@ -49,10 +59,18 @@ public class CollectionChangeNoRevisionTest extends AbstractSessionTest { Transaction transaction = getSession().beginTransaction(); getSession().saveOrUpdate(p); transaction.commit(); - int size = getAuditReader().getRevisions(Person.class, personId).size(); - System.out.println(size); - Assert.assertEquals(config.getProperty("org.hibernate.envers.revision_on_collection_change"), "false"); - Assert.assertEquals(size, 1); + namesId.add(n2.getId()); + int sizePerson = getAuditReader().getRevisions(Person.class, personId).size(); + Assert.assertEquals(config.getProperty("org.hibernate.envers.revision_on_collection_change"), getCollectionChangeValue()); + Assert.assertEquals(sizePerson, getExpectedPersonRevisionCount()); + for (Integer id : namesId) { + int sizeName = getAuditReader().getRevisions(Name.class, id).size(); + Assert.assertEquals(sizeName, EXPECTED_NAME_REVISION_COUNT); + } + } + + protected int getExpectedPersonRevisionCount() { + return EXPECTED_PERSON_REVISION_COUNT; } diff --git a/hibernate-envers/src/test/resources/mappings/norevision/mappings.hbm.xml b/hibernate-envers/src/test/resources/mappings/norevision/mappings.hbm.xml index db1463c71c..dc3dd6b005 100644 --- a/hibernate-envers/src/test/resources/mappings/norevision/mappings.hbm.xml +++ b/hibernate-envers/src/test/resources/mappings/norevision/mappings.hbm.xml @@ -16,6 +16,6 @@ + column="Person_Id" cascade="save-update" update="false" insert="false"/> \ No newline at end of file diff --git a/hibernate-envers/src/test/resources/testng.xml b/hibernate-envers/src/test/resources/testng.xml index da3f6768f6..905743a18a 100644 --- a/hibernate-envers/src/test/resources/testng.xml +++ b/hibernate-envers/src/test/resources/testng.xml @@ -6,6 +6,7 @@ +