HHH-4633:

- testcase

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18112 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Adam Warski 2009-12-02 11:38:32 +00:00
parent 5916bf0161
commit b46895ff17
5 changed files with 559 additions and 0 deletions

View File

@ -0,0 +1,87 @@
package org.hibernate.envers.test.entities.manytomany.sametable;
import org.hibernate.annotations.WhereJoinTable;
import org.hibernate.envers.Audited;
import javax.persistence.*;
import java.util.List;
import java.util.ArrayList;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
public class Child1Entity {
@Id
private Integer id;
private String child1Data;
public Child1Entity() {
}
public Child1Entity(String child1Data) {
this.child1Data = child1Data;
}
public Child1Entity(String child1Data, Integer id) {
this.child1Data = child1Data;
this.id = id;
}
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "children",
joinColumns = @JoinColumn(name = "child1_id"),
inverseJoinColumns = @JoinColumn(name = "parent_id", insertable = false, updatable = false)
)
@WhereJoinTable(clause = "child1_id is not null")
private List<ParentEntity> parents = new ArrayList<ParentEntity>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getChild1Data() {
return child1Data;
}
public void setChild1Data(String child1Data) {
this.child1Data = child1Data;
}
public List<ParentEntity> getParents() {
return parents;
}
public void setParents(List<ParentEntity> parents) {
this.parents = parents;
}
@SuppressWarnings({"RedundantIfStatement"})
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Child1Entity that = (Child1Entity) o;
if (child1Data != null ? !child1Data.equals(that.child1Data) : that.child1Data != null) return false;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (child1Data != null ? child1Data.hashCode() : 0);
result = 31 * result + (parents != null ? parents.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,87 @@
package org.hibernate.envers.test.entities.manytomany.sametable;
import org.hibernate.annotations.WhereJoinTable;
import org.hibernate.envers.Audited;
import javax.persistence.*;
import java.util.List;
import java.util.ArrayList;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
public class Child2Entity {
@Id
private Integer id;
private String child2Data;
public Child2Entity() {
}
public Child2Entity(String child2Data) {
this.child2Data = child2Data;
}
public Child2Entity(Integer id, String child2Data) {
this.id = id;
this.child2Data = child2Data;
}
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "children",
joinColumns = @JoinColumn(name = "child2_id"),
inverseJoinColumns = @JoinColumn(name = "parent_id", insertable = false, updatable = false)
)
@WhereJoinTable(clause = "child2_id is not null")
private List<ParentEntity> parents = new ArrayList<ParentEntity>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getChild2Data() {
return child2Data;
}
public void setChild2Data(String child2Data) {
this.child2Data = child2Data;
}
public List<ParentEntity> getParents() {
return parents;
}
public void setParents(List<ParentEntity> parents) {
this.parents = parents;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Child2Entity that = (Child2Entity) o;
if (child2Data != null ? !child2Data.equals(that.child2Data) : that.child2Data != null) return false;
//noinspection RedundantIfStatement
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (child2Data != null ? child2Data.hashCode() : 0);
result = 31 * result + (parents != null ? parents.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,105 @@
package org.hibernate.envers.test.entities.manytomany.sametable;
import org.hibernate.envers.Audited;
import org.hibernate.annotations.WhereJoinTable;
import javax.persistence.*;
import java.util.List;
import java.util.ArrayList;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
public class ParentEntity {
@Id
private Integer id;
private String parentData;
public ParentEntity() {
}
public ParentEntity(String parentData) {
this.parentData = parentData;
}
public ParentEntity(Integer id, String parentData) {
this.id = id;
this.parentData = parentData;
}
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "children",
joinColumns = @JoinColumn(name = "parent_id"),
inverseJoinColumns = @JoinColumn(name = "child1_id", insertable = false, updatable = false)
)
@WhereJoinTable(clause = "child1_id is not null")
private List<Child1Entity> children1 = new ArrayList<Child1Entity>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "children",
joinColumns = @JoinColumn(name = "parent_id"),
inverseJoinColumns = @JoinColumn(name = "child2_id", insertable = false, updatable = false)
)
@WhereJoinTable(clause = "child2_id is not null")
private List<Child2Entity> children2 = new ArrayList<Child2Entity>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getParentData() {
return parentData;
}
public void setParentData(String parentData) {
this.parentData = parentData;
}
public List<Child1Entity> getChildren1() {
return children1;
}
public void setChildren1(List<Child1Entity> children1) {
this.children1 = children1;
}
public List<Child2Entity> getChildren2() {
return children2;
}
public void setChildren2(List<Child2Entity> children2) {
this.children2 = children2;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ParentEntity that = (ParentEntity) o;
if (parentData != null ? !parentData.equals(that.parentData) : that.parentData != null) return false;
//noinspection RedundantIfStatement
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (parentData != null ? parentData.hashCode() : 0);
result = 31 * result + (children1 != null ? children1.hashCode() : 0);
result = 31 * result + (children2 != null ? children2.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,279 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.manytomany.sametable;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity;
import org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity;
import org.hibernate.envers.test.entities.manytomany.sametable.Child2Entity;
import org.hibernate.envers.test.tools.TestTools;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.hibernate.ejb.Ejb3Configuration;
/**
* Test which checks that auditing entities which contain multiple mappings to same tables work.
* @author Adam Warski (adam at warski dot org)
*/
public class BasicSametable extends AbstractEntityTest {
private Integer p1_id;
private Integer p2_id;
private Integer c1_1_id;
private Integer c1_2_id;
private Integer c2_1_id;
private Integer c2_2_id;
public void configure(Ejb3Configuration cfg) {
//cfg.addAnnotatedClass(ParentEntity.class);
//cfg.addAnnotatedClass(Child1Entity.class);
//cfg.addAnnotatedClass(Child2Entity.class);
}
//@BeforeClass(enabled = false, dependsOnMethods = "init")
public void initData() {
EntityManager em = getEntityManager();
ParentEntity p1 = new ParentEntity("parent_1");
ParentEntity p2 = new ParentEntity("parent_2");
Child1Entity c1_1 = new Child1Entity("child1_1");
Child1Entity c1_2 = new Child1Entity("child1_2");
Child2Entity c2_1 = new Child2Entity("child2_1");
Child2Entity c2_2 = new Child2Entity("child2_2");
// Revision 1
em.getTransaction().begin();
em.persist(p1);
em.persist(p2);
em.persist(c1_1);
em.persist(c1_2);
em.persist(c2_1);
em.persist(c2_2);
em.getTransaction().commit();
// Revision 2 - (p1: c1_1, p2: c2_1)
em.getTransaction().begin();
p1 = em.find(ParentEntity.class, p1.getId());
p2 = em.find(ParentEntity.class, p2.getId());
c1_1 = em.find(Child1Entity.class, c1_1.getId());
c2_1 = em.find(Child2Entity.class, c2_1.getId());
p1.getChildren1().add(c1_1);
p2.getChildren2().add(c2_1);
em.getTransaction().commit();
// Revision 3 - (p1: c1_1, c1_2, c2_2, p2: c1_1, c2_1)
em.getTransaction().begin();
p1 = em.find(ParentEntity.class, p1.getId());
p2 = em.find(ParentEntity.class, p2.getId());
c1_1 = em.find(Child1Entity.class, c1_1.getId());
c1_2 = em.find(Child1Entity.class, c1_2.getId());
c2_2 = em.find(Child2Entity.class, c2_2.getId());
p1.getChildren1().add(c1_2);
p1.getChildren2().add(c2_2);
p2.getChildren1().add(c1_1);
em.getTransaction().commit();
// Revision 4 - (p1: c1_2, c2_2, p2: c1_1, c2_1, c2_2)
em.getTransaction().begin();
p1 = em.find(ParentEntity.class, p1.getId());
p2 = em.find(ParentEntity.class, p2.getId());
c1_1 = em.find(Child1Entity.class, c1_1.getId());
c2_2 = em.find(Child2Entity.class, c2_2.getId());
p1.getChildren1().remove(c1_1);
p2.getChildren2().add(c2_2);
em.getTransaction().commit();
// Revision 5 - (p1: c2_2, p2: c1_1, c2_1)
em.getTransaction().begin();
p1 = em.find(ParentEntity.class, p1.getId());
p2 = em.find(ParentEntity.class, p2.getId());
c1_2 = em.find(Child1Entity.class, c1_2.getId());
c2_2 = em.find(Child2Entity.class, c2_2.getId());
c2_2.getParents().remove(p2);
c1_2.getParents().remove(p1);
em.getTransaction().commit();
//
p1_id = p1.getId();
p2_id = p2.getId();
c1_1_id = c1_1.getId();
c1_2_id = c1_2.getId();
c2_1_id = c2_1.getId();
c2_2_id = c2_2.getId();
}
@Test(enabled = false)
public void testRevisionsCounts() {
assert Arrays.asList(1, 2, 3, 4, 5).equals(getAuditReader().getRevisions(ParentEntity.class, p1_id));
assert Arrays.asList(1, 2, 3, 4, 5).equals(getAuditReader().getRevisions(ParentEntity.class, p2_id));
assert Arrays.asList(1, 2, 3, 4).equals(getAuditReader().getRevisions(Child1Entity.class, c1_1_id));
assert Arrays.asList(1, 3, 5).equals(getAuditReader().getRevisions(Child1Entity.class, c1_2_id));
assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(Child1Entity.class, c2_1_id));
assert Arrays.asList(1, 3, 4, 5).equals(getAuditReader().getRevisions(Child1Entity.class, c2_2_id));
}
@Test(enabled = false)
public void testHistoryOfParent1() {
Child1Entity c1_1 = getEntityManager().find(Child1Entity.class, c1_1_id);
Child2Entity c2_2 = getEntityManager().find(Child2Entity.class, c2_2_id);
ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p1_id, 1);
ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p1_id, 2);
ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p1_id, 3);
ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p1_id, 4);
ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p1_id, 5);
assert TestTools.checkList(rev1.getChildren1());
assert TestTools.checkList(rev2.getChildren1());
assert TestTools.checkList(rev3.getChildren1(), c1_1);
assert TestTools.checkList(rev4.getChildren1(), c1_1);
assert TestTools.checkList(rev5.getChildren1(), c1_1);
assert TestTools.checkList(rev1.getChildren2());
assert TestTools.checkList(rev2.getChildren2());
assert TestTools.checkList(rev3.getChildren2(), c2_2);
assert TestTools.checkList(rev4.getChildren2(), c2_2);
assert TestTools.checkList(rev5.getChildren2(), c2_2);
}
@Test(enabled = false)
public void testHistoryOfParent2() {
Child1Entity c1_1 = getEntityManager().find(Child1Entity.class, c1_1_id);
Child1Entity c1_2 = getEntityManager().find(Child1Entity.class, c1_2_id);
Child2Entity c2_1 = getEntityManager().find(Child2Entity.class, c2_1_id);
Child2Entity c2_2 = getEntityManager().find(Child2Entity.class, c2_2_id);
ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p2_id, 1);
ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p2_id, 2);
ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p2_id, 3);
ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p2_id, 4);
ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p2_id, 5);
assert TestTools.checkList(rev1.getChildren1());
assert TestTools.checkList(rev2.getChildren1(), c1_1);
assert TestTools.checkList(rev3.getChildren1(), c1_1, c1_2);
assert TestTools.checkList(rev4.getChildren1(), c1_2);
assert TestTools.checkList(rev5.getChildren1());
assert TestTools.checkList(rev1.getChildren2());
assert TestTools.checkList(rev2.getChildren2(), c2_1);
assert TestTools.checkList(rev3.getChildren2(), c2_1);
assert TestTools.checkList(rev4.getChildren2(), c2_1, c2_2);
assert TestTools.checkList(rev5.getChildren2(), c2_1);
}
@Test(enabled = false)
public void testHistoryOfChild1_1() {
ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_1_id, 1);
Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_1_id, 2);
Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_1_id, 3);
Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_1_id, 4);
Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_1_id, 5);
assert TestTools.checkList(rev1.getParents());
assert TestTools.checkList(rev2.getParents(), p1);
assert TestTools.checkList(rev3.getParents(), p1, p2);
assert TestTools.checkList(rev4.getParents(), p2);
assert TestTools.checkList(rev5.getParents(), p2);
}
@Test(enabled = false)
public void testHistoryOfChild1_2() {
ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_2_id, 1);
Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_2_id, 2);
Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_2_id, 3);
Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_2_id, 4);
Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_2_id, 5);
assert TestTools.checkList(rev1.getParents());
assert TestTools.checkList(rev2.getParents());
assert TestTools.checkList(rev3.getParents(), p1);
assert TestTools.checkList(rev4.getParents(), p1);
assert TestTools.checkList(rev5.getParents());
}
@Test(enabled = false)
public void testHistoryOfChild2_1() {
ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_1_id, 1);
Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_1_id, 2);
Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_1_id, 3);
Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_1_id, 4);
Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_1_id, 5);
assert TestTools.checkList(rev1.getParents());
assert TestTools.checkList(rev2.getParents(), p2);
assert TestTools.checkList(rev3.getParents(), p2);
assert TestTools.checkList(rev4.getParents(), p2);
assert TestTools.checkList(rev5.getParents(), p2);
}
@Test(enabled = false)
public void testHistoryOfChild2_2() {
ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_2_id, 1);
Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_2_id, 2);
Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_2_id, 3);
Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_2_id, 4);
Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_2_id, 5);
assert TestTools.checkList(rev1.getParents());
assert TestTools.checkList(rev2.getParents());
assert TestTools.checkList(rev3.getParents(), p1);
assert TestTools.checkList(rev4.getParents(), p1, p2);
assert TestTools.checkList(rev5.getParents(), p1);
}
}

View File

@ -31,6 +31,7 @@
<package name="org.hibernate.envers.test.integration.interfaces.components" />
<package name="org.hibernate.envers.test.integration.interfaces.relation" />
<package name="org.hibernate.envers.test.integration.manytomany" />
<package name="org.hibernate.envers.test.integration.manytomany.sametable" />
<package name="org.hibernate.envers.test.integration.manytomany.ternary" />
<package name="org.hibernate.envers.test.integration.manytomany.unidirectional" />
<package name="org.hibernate.envers.test.integration.manytoone.unidirectional" />