HHH-5917 - Split test cases and updated documentation

This commit is contained in:
Lukasz Antoniak 2011-06-27 21:52:40 +02:00
parent 84ee0fb879
commit 1c1e333487
6 changed files with 312 additions and 153 deletions

View File

@ -310,6 +310,14 @@
<literal>@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)</literal>. Then, when reading historic
versions of your entity, the relation will always point to the "current" related entity.
</para>
<para>
If you'd like to audit properties encapsulated by any subset of your entity's mapped superclasses (which are
not explicitly audited), list desired supertypes in <literal>auditParents</literal> attribute of
<interfacename>@Audited</interfacename> annotation. If any <interfacename>@MappedSuperclass</interfacename>
(or any of it's properties) is marked as <interfacename>@Audited</interfacename>, it's behavior is implicitly
inherited by all audited subclasses.
</para>
</section>
<section>

View File

@ -1,153 +0,0 @@
package org.hibernate.envers.test.integration.superclass.auditparents;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.Audited;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrIntTestEntity;
import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.EntityManager;
import java.util.Set;
/**
* Tests several configurations of entity hierarchy that utilizes {@link Audited#auditParents()} property.
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class AuditParentsTest extends AbstractEntityTest {
private long childSingleId = 1L;
private long childMultipleId = 2L;
private long childImpTransId = 3L;
private long childExpTransId = 4L;
private long babyCompleteId = 5L;
private Integer siteSingleId = null;
private Integer siteMultipleId = null;
private Integer siteCompleteId = null;
@Override
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(MappedGrandparentEntity.class);
cfg.addAnnotatedClass(MappedParentEntity.class);
cfg.addAnnotatedClass(ChildSingleParentEntity.class);
cfg.addAnnotatedClass(ChildMultipleParentsEntity.class);
cfg.addAnnotatedClass(TransitiveParentEntity.class);
cfg.addAnnotatedClass(ImplicitTransitiveChildEntity.class);
cfg.addAnnotatedClass(ExplicitTransitiveChildEntity.class);
cfg.addAnnotatedClass(StrIntTestEntity.class);
cfg.addAnnotatedClass(ChildCompleteEntity.class);
cfg.addAnnotatedClass(BabyCompleteEntity.class);
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
StrIntTestEntity siteSingle = new StrIntTestEntity("data 1", 1);
em.persist(siteSingle);
em.persist(new ChildSingleParentEntity(childSingleId, "grandparent 1", "notAudited 1", "parent 1", "child 1", siteSingle));
em.getTransaction().commit();
siteSingleId = siteSingle.getId();
// Revision 2
em.getTransaction().begin();
StrIntTestEntity siteMultiple = new StrIntTestEntity("data 2", 2);
em.persist(siteMultiple);
em.persist(new ChildMultipleParentsEntity(childMultipleId, "grandparent 2", "notAudited 2", "parent 2", "child 2", siteMultiple));
em.getTransaction().commit();
siteMultipleId = siteMultiple.getId();
// Revision 3
em.getTransaction().begin();
em.persist(new ImplicitTransitiveChildEntity(childImpTransId, "grandparent 3", "notAudited 3", "parent 3", "child 3"));
em.getTransaction().commit();
// Revision 4
em.getTransaction().begin();
em.persist(new ExplicitTransitiveChildEntity(childExpTransId, "grandparent 4", "notAudited 4", "parent 4", "child 4"));
em.getTransaction().commit();
// Revision 5
em.getTransaction().begin();
StrIntTestEntity siteComplete = new StrIntTestEntity("data 5", 5);
em.persist(siteComplete);
em.persist(new BabyCompleteEntity(babyCompleteId, "grandparent 5", "notAudited 5", "parent 5", "child 5", siteComplete, "baby 5"));
em.getTransaction().commit();
siteCompleteId = siteComplete.getId();
}
@Test
public void testCreatedAuditTables() {
Table babyCompleteTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.BabyCompleteEntity_AUD").getTable();
checkTableColumns(TestTools.makeSet("baby", "child", "parent", "relation_id", "grandparent", "id"), babyCompleteTable);
Table explicitTransChildTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.ExplicitTransitiveChildEntity_AUD").getTable();
checkTableColumns(TestTools.makeSet("child", "parent", "grandparent", "id"), explicitTransChildTable);
Table implicitTransChildTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.ImplicitTransitiveChildEntity_AUD").getTable();
checkTableColumns(TestTools.makeSet("child", "parent", "grandparent", "id"), implicitTransChildTable);
Table multipleParentChildTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.ChildMultipleParentsEntity_AUD").getTable();
checkTableColumns(TestTools.makeSet("child", "parent", "relation_id", "grandparent", "id"), multipleParentChildTable);
Table singleParentChildTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.ChildSingleParentEntity_AUD").getTable();
checkTableColumns(TestTools.makeSet("child", "grandparent", "id"), singleParentChildTable);
}
@SuppressWarnings("unchecked")
private void checkTableColumns(Set<String> expectedColumns, Table table) {
for (String columnName : expectedColumns) {
// Check whether expected column exists.
Assert.assertNotNull(table.getColumn(new Column(columnName)));
}
}
@Test
public void testSingleAuditParent() {
// expectedSingleChild.parent, expectedSingleChild.relation and expectedSingleChild.notAudited shall be null, because they are not audited.
ChildSingleParentEntity expectedSingleChild = new ChildSingleParentEntity(childSingleId, "grandparent 1", null, null, "child 1", null);
ChildSingleParentEntity child = getAuditReader().find(ChildSingleParentEntity.class, childSingleId, 1);
Assert.assertEquals(expectedSingleChild, child);
Assert.assertNull(child.getRelation());
}
@Test
public void testMultipleAuditParents() {
// expectedMultipleChild.notAudited shall be null, because it is not audited.
ChildMultipleParentsEntity expectedMultipleChild = new ChildMultipleParentsEntity(childMultipleId, "grandparent 2", null, "parent 2", "child 2", new StrIntTestEntity("data 2", 2, siteMultipleId));
ChildMultipleParentsEntity child = getAuditReader().find(ChildMultipleParentsEntity.class, childMultipleId, 2);
Assert.assertEquals(expectedMultipleChild, child);
Assert.assertEquals(expectedMultipleChild.getRelation().getId(), child.getRelation().getId());
}
@Test
public void testImplicitTransitiveAuditParents() {
// expectedChild.notAudited shall be null, because it is not audited.
ImplicitTransitiveChildEntity expectedChild = new ImplicitTransitiveChildEntity(childImpTransId, "grandparent 3", null, "parent 3", "child 3");
ImplicitTransitiveChildEntity child = getAuditReader().find(ImplicitTransitiveChildEntity.class, childImpTransId, 3);
Assert.assertEquals(expectedChild, child);
}
@Test
public void testExplicitTransitiveAuditParents() {
// expectedChild.notAudited shall be null, because it is not audited.
ExplicitTransitiveChildEntity expectedChild = new ExplicitTransitiveChildEntity(childExpTransId, "grandparent 4", null, "parent 4", "child 4");
ExplicitTransitiveChildEntity child = getAuditReader().find(ExplicitTransitiveChildEntity.class, childExpTransId, 4);
Assert.assertEquals(expectedChild, child);
}
@Test
public void testCompleteAuditParents() {
// expectedBaby.notAudited shall be null, because it is not audited.
BabyCompleteEntity expectedBaby = new BabyCompleteEntity(babyCompleteId, "grandparent 5", null, "parent 5", "child 5", new StrIntTestEntity("data 5", 5, siteCompleteId), "baby 5");
BabyCompleteEntity baby = getAuditReader().find(BabyCompleteEntity.class, babyCompleteId, 5);
Assert.assertEquals(expectedBaby, baby);
Assert.assertEquals(expectedBaby.getRelation().getId(), baby.getRelation().getId());
}
}

View File

@ -0,0 +1,73 @@
package org.hibernate.envers.test.integration.superclass.auditparents;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.Audited;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrIntTestEntity;
import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.MappedSuperclass;
import java.util.Set;
/**
* Tests mapping of child entity that declares all of its ancestors as audited with {@link Audited#auditParents()} property.
* All supperclasses are marked with {@link MappedSuperclass} annotation but not {@link Audited}.
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class MultipleAuditParentsTest extends AbstractEntityTest {
private long childMultipleId = 1L;
private Integer siteMultipleId = null;
@Override
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(MappedGrandparentEntity.class);
cfg.addAnnotatedClass(MappedParentEntity.class);
cfg.addAnnotatedClass(ChildMultipleParentsEntity.class);
cfg.addAnnotatedClass(StrIntTestEntity.class);
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
StrIntTestEntity siteMultiple = new StrIntTestEntity("data 1", 1);
em.persist(siteMultiple);
em.persist(new ChildMultipleParentsEntity(childMultipleId, "grandparent 1", "notAudited 1", "parent 1", "child 1", siteMultiple));
em.getTransaction().commit();
siteMultipleId = siteMultiple.getId();
}
@Test
public void testCreatedAuditTable() {
Set<String> expectedColumns = TestTools.makeSet("child", "parent", "relation_id", "grandparent", "id");
Set<String> unexpectedColumns = TestTools.makeSet("notAudited");
Table table = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.ChildMultipleParentsEntity_AUD").getTable();
for (String columnName : expectedColumns) {
// Check whether expected column exists.
Assert.assertNotNull(table.getColumn(new Column(columnName)));
}
for (String columnName : unexpectedColumns) {
// Check whether unexpected column does not exist.
Assert.assertNull(table.getColumn(new Column(columnName)));
}
}
@Test
public void testMultipleAuditParents() {
// expectedMultipleChild.notAudited shall be null, because it is not audited.
ChildMultipleParentsEntity expectedMultipleChild = new ChildMultipleParentsEntity(childMultipleId, "grandparent 1", null, "parent 1", "child 1", new StrIntTestEntity("data 1", 1, siteMultipleId));
ChildMultipleParentsEntity child = getAuditReader().find(ChildMultipleParentsEntity.class, childMultipleId, 1);
Assert.assertEquals(expectedMultipleChild, child);
Assert.assertEquals(expectedMultipleChild.getRelation().getId(), child.getRelation().getId());
}
}

View File

@ -0,0 +1,73 @@
package org.hibernate.envers.test.integration.superclass.auditparents;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.Audited;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrIntTestEntity;
import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.MappedSuperclass;
import java.util.Set;
/**
* Tests mapping of child entity that declares one of its ancestors as audited with {@link Audited#auditParents()} property.
* All supperclasses are marked with {@link MappedSuperclass} annotation but not {@link Audited}.
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class SingleAuditParentsTest extends AbstractEntityTest {
private long childSingleId = 1L;
private Integer siteSingleId = null;
@Override
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(MappedGrandparentEntity.class);
cfg.addAnnotatedClass(MappedParentEntity.class);
cfg.addAnnotatedClass(ChildSingleParentEntity.class);
cfg.addAnnotatedClass(StrIntTestEntity.class);
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
StrIntTestEntity siteSingle = new StrIntTestEntity("data 1", 1);
em.persist(siteSingle);
em.persist(new ChildSingleParentEntity(childSingleId, "grandparent 1", "notAudited 1", "parent 1", "child 1", siteSingle));
em.getTransaction().commit();
siteSingleId = siteSingle.getId();
}
@Test
public void testCreatedAuditTable() {
Set<String> expectedColumns = TestTools.makeSet("child", "grandparent", "id");
Set<String> unexpectedColumns = TestTools.makeSet("parent", "relation_id", "notAudited");
Table table = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.ChildSingleParentEntity_AUD").getTable();
for (String columnName : expectedColumns) {
// Check whether expected column exists.
Assert.assertNotNull(table.getColumn(new Column(columnName)));
}
for (String columnName : unexpectedColumns) {
// Check whether unexpected column does not exist.
Assert.assertNull(table.getColumn(new Column(columnName)));
}
}
@Test
public void testSingleAuditParent() {
// expectedSingleChild.parent, expectedSingleChild.relation and expectedSingleChild.notAudited shall be null, because they are not audited.
ChildSingleParentEntity expectedSingleChild = new ChildSingleParentEntity(childSingleId, "grandparent 1", null, null, "child 1", null);
ChildSingleParentEntity child = getAuditReader().find(ChildSingleParentEntity.class, childSingleId, 1);
Assert.assertEquals(expectedSingleChild, child);
Assert.assertNull(child.getRelation());
}
}

View File

@ -0,0 +1,74 @@
package org.hibernate.envers.test.integration.superclass.auditparents;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.Audited;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrIntTestEntity;
import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.EntityManager;
import java.util.Set;
/**
* Tests mapping of baby entity which declares its parent as audited with {@link Audited#auditParents()} property.
* Moreover, child class (mapped superclass of baby entity) declares grandparent entity as audited. In this case all
* attributes of baby class shall be audited.
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class TotalAuditParentsTest extends AbstractEntityTest {
private long babyCompleteId = 1L;
private Integer siteCompleteId = null;
@Override
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(MappedGrandparentEntity.class);
cfg.addAnnotatedClass(MappedParentEntity.class);
cfg.addAnnotatedClass(StrIntTestEntity.class);
cfg.addAnnotatedClass(ChildCompleteEntity.class);
cfg.addAnnotatedClass(BabyCompleteEntity.class);
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
StrIntTestEntity siteComplete = new StrIntTestEntity("data 1", 1);
em.persist(siteComplete);
em.persist(new BabyCompleteEntity(babyCompleteId, "grandparent 1", "notAudited 1", "parent 1", "child 1", siteComplete, "baby 1"));
em.getTransaction().commit();
siteCompleteId = siteComplete.getId();
}
@Test
public void testCreatedAuditTable() {
Set<String> expectedColumns = TestTools.makeSet("baby", "child", "parent", "relation_id", "grandparent", "id");
Set<String> unexpectedColumns = TestTools.makeSet("notAudited");
Table table = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.BabyCompleteEntity_AUD").getTable();
for (String columnName : expectedColumns) {
// Check whether expected column exists.
Assert.assertNotNull(table.getColumn(new Column(columnName)));
}
for (String columnName : unexpectedColumns) {
// Check whether unexpected column does not exist.
Assert.assertNull(table.getColumn(new Column(columnName)));
}
}
@Test
public void testCompleteAuditParents() {
// expectedBaby.notAudited shall be null, because it is not audited.
BabyCompleteEntity expectedBaby = new BabyCompleteEntity(babyCompleteId, "grandparent 1", null, "parent 1", "child 1", new StrIntTestEntity("data 1", 1, siteCompleteId), "baby 1");
BabyCompleteEntity baby = getAuditReader().find(BabyCompleteEntity.class, babyCompleteId, 1);
Assert.assertEquals(expectedBaby, baby);
Assert.assertEquals(expectedBaby.getRelation().getId(), baby.getRelation().getId());
}
}

View File

@ -0,0 +1,84 @@
package org.hibernate.envers.test.integration.superclass.auditparents;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.Audited;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.EntityManager;
import java.util.Set;
/**
* Tests mapping of child entity which parent declares one of its ancestors as audited with {@link Audited#auditParents()}
* property. Child entity may mark explicitly its parent as audited or not.
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class TransitiveAuditParentsTest extends AbstractEntityTest {
private long childImpTransId = 1L;
private long childExpTransId = 2L;
@Override
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(MappedGrandparentEntity.class);
cfg.addAnnotatedClass(TransitiveParentEntity.class);
cfg.addAnnotatedClass(ImplicitTransitiveChildEntity.class);
cfg.addAnnotatedClass(ExplicitTransitiveChildEntity.class);
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
em.persist(new ImplicitTransitiveChildEntity(childImpTransId, "grandparent 1", "notAudited 1", "parent 1", "child 1"));
em.getTransaction().commit();
// Revision 2
em.getTransaction().begin();
em.persist(new ExplicitTransitiveChildEntity(childExpTransId, "grandparent 2", "notAudited 2", "parent 2", "child 2"));
em.getTransaction().commit();
}
@Test
public void testCreatedAuditTables() {
Table explicitTransChildTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.ExplicitTransitiveChildEntity_AUD").getTable();
checkTableColumns(TestTools.makeSet("child", "parent", "grandparent", "id"), TestTools.makeSet("notAudited"), explicitTransChildTable);
Table implicitTransChildTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditparents.ImplicitTransitiveChildEntity_AUD").getTable();
checkTableColumns(TestTools.makeSet("child", "parent", "grandparent", "id"), TestTools.makeSet("notAudited"), implicitTransChildTable);
}
private void checkTableColumns(Set<String> expectedColumns, Set<String> unexpectedColumns, Table table) {
for (String columnName : expectedColumns) {
// Check whether expected column exists.
Assert.assertNotNull(table.getColumn(new Column(columnName)));
}
for (String columnName : unexpectedColumns) {
// Check whether unexpected column does not exist.
Assert.assertNull(table.getColumn(new Column(columnName)));
}
}
@Test
public void testImplicitTransitiveAuditParents() {
// expectedChild.notAudited shall be null, because it is not audited.
ImplicitTransitiveChildEntity expectedChild = new ImplicitTransitiveChildEntity(childImpTransId, "grandparent 1", null, "parent 1", "child 1");
ImplicitTransitiveChildEntity child = getAuditReader().find(ImplicitTransitiveChildEntity.class, childImpTransId, 1);
Assert.assertEquals(expectedChild, child);
}
@Test
public void testExplicitTransitiveAuditParents() {
// expectedChild.notAudited shall be null, because it is not audited.
ExplicitTransitiveChildEntity expectedChild = new ExplicitTransitiveChildEntity(childExpTransId, "grandparent 2", null, "parent 2", "child 2");
ExplicitTransitiveChildEntity child = getAuditReader().find(ExplicitTransitiveChildEntity.class, childExpTransId, 2);
Assert.assertEquals(expectedChild, child);
}
}