From 07ec77023e542292ce02f3791414e048e83f55b8 Mon Sep 17 00:00:00 2001 From: Adam Warski Date: Wed, 2 Dec 2009 12:09:56 +0000 Subject: [PATCH] HHH-4634: - testcase git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18113 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../biowned/ListBiowning1Entity.java | 110 +++++++++ .../biowned/ListBiowning2Entity.java | 110 +++++++++ .../manytomany/biowned/BasicBiowned.java | 211 ++++++++++++++++++ envers/src/test/resources/testng.xml | 1 + 4 files changed, 432 insertions(+) create mode 100644 envers/src/test/java/org/hibernate/envers/test/entities/manytomany/biowned/ListBiowning1Entity.java create mode 100644 envers/src/test/java/org/hibernate/envers/test/entities/manytomany/biowned/ListBiowning2Entity.java create mode 100644 envers/src/test/java/org/hibernate/envers/test/integration/manytomany/biowned/BasicBiowned.java diff --git a/envers/src/test/java/org/hibernate/envers/test/entities/manytomany/biowned/ListBiowning1Entity.java b/envers/src/test/java/org/hibernate/envers/test/entities/manytomany/biowned/ListBiowning1Entity.java new file mode 100644 index 0000000000..4bc691f40e --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/entities/manytomany/biowned/ListBiowning1Entity.java @@ -0,0 +1,110 @@ +/* + * 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.entities.manytomany.biowned; + +import java.util.List; +import java.util.ArrayList; +import javax.persistence.*; + +import org.hibernate.envers.Audited; + +/** + * Entity owning a many-to-many relation, where the other entity also owns the relation. + * @author Adam Warski (adam at warski dot org) + */ +@Entity +@Audited +public class ListBiowning1Entity { + @Id + private Integer id; + + private String data; + + @ManyToMany + @JoinTable( + name = "biowning", + joinColumns = @JoinColumn(name = "biowning1_id"), + inverseJoinColumns = @JoinColumn(name = "biowning2_id", insertable = false, updatable = false) + ) + private List references = new ArrayList(); + + public ListBiowning1Entity() { } + + public ListBiowning1Entity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public ListBiowning1Entity(String data) { + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public List getReferences() { + return references; + } + + public void setReferences(List references) { + this.references = references; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ListBiowning1Entity)) return false; + + ListBiowning1Entity that = (ListBiowning1Entity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + //noinspection RedundantIfStatement + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + public String toString() { + return "ListBiowning1Entity(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file diff --git a/envers/src/test/java/org/hibernate/envers/test/entities/manytomany/biowned/ListBiowning2Entity.java b/envers/src/test/java/org/hibernate/envers/test/entities/manytomany/biowned/ListBiowning2Entity.java new file mode 100644 index 0000000000..c4c87ceccd --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/entities/manytomany/biowned/ListBiowning2Entity.java @@ -0,0 +1,110 @@ +/* + * 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.entities.manytomany.biowned; + +import java.util.List; +import java.util.ArrayList; +import javax.persistence.*; + +import org.hibernate.envers.Audited; + +/** + * Entity owning a many-to-many relation, where the other entity also owns the relation. + * @author Adam Warski (adam at warski dot org) + */ +@Entity +@Audited +public class ListBiowning2Entity { + @Id + private Integer id; + + private String data; + + @ManyToMany + @JoinTable( + name = "biowning", + joinColumns = @JoinColumn(name = "biowning2_id"), + inverseJoinColumns = @JoinColumn(name = "biowning1_id", insertable = false, updatable = false) + ) + private List references = new ArrayList(); + + public ListBiowning2Entity() { } + + public ListBiowning2Entity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public ListBiowning2Entity(String data) { + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public List getReferences() { + return references; + } + + public void setReferences(List references) { + this.references = references; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ListBiowning2Entity)) return false; + + ListBiowning2Entity that = (ListBiowning2Entity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + //noinspection RedundantIfStatement + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + public String toString() { + return "ListBiowning2Entity(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/manytomany/biowned/BasicBiowned.java b/envers/src/test/java/org/hibernate/envers/test/integration/manytomany/biowned/BasicBiowned.java new file mode 100644 index 0000000000..8405eae634 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/manytomany/biowned/BasicBiowned.java @@ -0,0 +1,211 @@ +/* + * 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.biowned; + +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.envers.test.AbstractEntityTest; +import org.hibernate.envers.test.entities.manytomany.biowned.ListBiowning1Entity; +import org.hibernate.envers.test.entities.manytomany.biowned.ListBiowning2Entity; +import org.hibernate.envers.test.tools.TestTools; +import org.testng.annotations.Test; +import org.testng.annotations.BeforeClass; + +import javax.persistence.EntityManager; +import java.util.Arrays; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class BasicBiowned extends AbstractEntityTest { + private Integer o1_1_id; + private Integer o1_2_id; + private Integer o2_1_id; + private Integer o2_2_id; + + public void configure(Ejb3Configuration cfg) { + //cfg.addAnnotatedClass(ListBiowning1Entity.class); + //cfg.addAnnotatedClass(ListBiowning2Entity.class); + } + + //@BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + ListBiowning1Entity o1_1 = new ListBiowning1Entity("o1_1"); + ListBiowning1Entity o1_2 = new ListBiowning1Entity("o1_2"); + ListBiowning2Entity o2_1 = new ListBiowning2Entity("o2_1"); + ListBiowning2Entity o2_2 = new ListBiowning2Entity("o2_2"); + + // Revision 1 + em.getTransaction().begin(); + + em.persist(o1_1); + em.persist(o1_2); + em.persist(o2_1); + em.persist(o2_2); + + em.getTransaction().commit(); + + // Revision 2 (1_1 <-> 2_1; 1_2 <-> 2_2) + + em.getTransaction().begin(); + + o1_1 = em.find(ListBiowning1Entity.class, o1_1.getId()); + o1_2 = em.find(ListBiowning1Entity.class, o1_2.getId()); + o2_1 = em.find(ListBiowning2Entity.class, o2_1.getId()); + o2_2 = em.find(ListBiowning2Entity.class, o2_2.getId()); + + o1_1.getReferences().add(o2_1); + o1_2.getReferences().add(o2_2); + + em.getTransaction().commit(); + + // Revision 3 (1_1 <-> 2_1, 2_2; 1_2 <-> 2_2) + em.getTransaction().begin(); + + o1_1 = em.find(ListBiowning1Entity.class, o1_1.getId()); + o2_2 = em.find(ListBiowning2Entity.class, o2_2.getId()); + + o1_1.getReferences().add(o2_2); + + em.getTransaction().commit(); + + // Revision 4 (1_1 <-> 2_1; 1_2 <-> 2_1) + em.getTransaction().begin(); + + o1_1 = em.find(ListBiowning1Entity.class, o1_1.getId()); + o1_2 = em.find(ListBiowning1Entity.class, o1_2.getId()); + o2_1 = em.find(ListBiowning2Entity.class, o2_1.getId()); + o2_2 = em.find(ListBiowning2Entity.class, o2_2.getId()); + + o2_2.getReferences().remove(o1_1); + o2_1.getReferences().remove(o1_1); + o1_2.getReferences().add(o2_1); + + em.getTransaction().commit(); + + // Revision 5 (1_2 <-> 2_1, 2_2) + em.getTransaction().begin(); + + o1_1 = em.find(ListBiowning1Entity.class, o1_1.getId()); + o1_2 = em.find(ListBiowning1Entity.class, o1_2.getId()); + o2_1 = em.find(ListBiowning2Entity.class, o2_1.getId()); + o2_2 = em.find(ListBiowning2Entity.class, o2_2.getId()); + + o2_1.getReferences().remove(o1_1); + o1_1.getReferences().remove(o2_1); + o1_2.getReferences().add(o2_2); + o2_2.getReferences().add(o1_2); + + em.getTransaction().commit(); + + // + + o1_1_id = o1_1.getId(); + o1_2_id = o1_2.getId(); + o2_1_id = o2_1.getId(); + o2_2_id = o2_2.getId(); + } + + @Test(enabled = false) + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3, 4, 5).equals(getAuditReader().getRevisions(ListBiowning1Entity.class, o1_1_id)); + assert Arrays.asList(1, 2, 4, 5).equals(getAuditReader().getRevisions(ListBiowning1Entity.class, o1_2_id)); + + assert Arrays.asList(1, 2, 4, 5).equals(getAuditReader().getRevisions(ListBiowning2Entity.class, o2_1_id)); + assert Arrays.asList(1, 2, 3, 4, 5).equals(getAuditReader().getRevisions(ListBiowning2Entity.class, o2_2_id)); + } + + @Test(enabled = false) + public void testHistoryOfO1_1() { + ListBiowning2Entity o2_1 = getEntityManager().find(ListBiowning2Entity.class, o2_1_id); + ListBiowning2Entity o2_2 = getEntityManager().find(ListBiowning2Entity.class, o2_2_id); + + ListBiowning1Entity rev1 = getAuditReader().find(ListBiowning1Entity.class, o1_1_id, 1); + ListBiowning1Entity rev2 = getAuditReader().find(ListBiowning1Entity.class, o1_1_id, 2); + ListBiowning1Entity rev3 = getAuditReader().find(ListBiowning1Entity.class, o1_1_id, 3); + ListBiowning1Entity rev4 = getAuditReader().find(ListBiowning1Entity.class, o1_1_id, 4); + ListBiowning1Entity rev5 = getAuditReader().find(ListBiowning1Entity.class, o1_1_id, 5); + + assert TestTools.checkList(rev1.getReferences()); + assert TestTools.checkList(rev2.getReferences(), o2_1); + assert TestTools.checkList(rev3.getReferences(), o2_1, o2_2); + assert TestTools.checkList(rev4.getReferences(), o2_1); + assert TestTools.checkList(rev5.getReferences()); + } + + @Test(enabled = false) + public void testHistoryOfO1_2() { + ListBiowning2Entity o2_1 = getEntityManager().find(ListBiowning2Entity.class, o2_1_id); + ListBiowning2Entity o2_2 = getEntityManager().find(ListBiowning2Entity.class, o2_2_id); + + ListBiowning1Entity rev1 = getAuditReader().find(ListBiowning1Entity.class, o1_2_id, 1); + ListBiowning1Entity rev2 = getAuditReader().find(ListBiowning1Entity.class, o1_2_id, 2); + ListBiowning1Entity rev3 = getAuditReader().find(ListBiowning1Entity.class, o1_2_id, 3); + ListBiowning1Entity rev4 = getAuditReader().find(ListBiowning1Entity.class, o1_2_id, 4); + ListBiowning1Entity rev5 = getAuditReader().find(ListBiowning1Entity.class, o1_2_id, 5); + + assert TestTools.checkList(rev1.getReferences()); + assert TestTools.checkList(rev2.getReferences(), o2_2); + assert TestTools.checkList(rev3.getReferences(), o2_2); + assert TestTools.checkList(rev4.getReferences(), o2_1); + assert TestTools.checkList(rev5.getReferences(), o2_1, o2_2); + } + + @Test(enabled = false) + public void testHistoryOfO2_1() { + ListBiowning1Entity o1_1 = getEntityManager().find(ListBiowning1Entity.class, o1_1_id); + ListBiowning1Entity o1_2 = getEntityManager().find(ListBiowning1Entity.class, o1_2_id); + + ListBiowning2Entity rev1 = getAuditReader().find(ListBiowning2Entity.class, o1_1_id, 1); + ListBiowning2Entity rev2 = getAuditReader().find(ListBiowning2Entity.class, o1_1_id, 2); + ListBiowning2Entity rev3 = getAuditReader().find(ListBiowning2Entity.class, o1_1_id, 3); + ListBiowning2Entity rev4 = getAuditReader().find(ListBiowning2Entity.class, o1_1_id, 4); + ListBiowning2Entity rev5 = getAuditReader().find(ListBiowning2Entity.class, o1_1_id, 5); + + assert TestTools.checkList(rev1.getReferences()); + assert TestTools.checkList(rev2.getReferences(), o1_1); + assert TestTools.checkList(rev3.getReferences(), o1_1); + assert TestTools.checkList(rev4.getReferences(), o1_1, o1_2); + assert TestTools.checkList(rev5.getReferences(), o1_2); + } + + @Test(enabled = false) + public void testHistoryOfO2_2() { + ListBiowning1Entity o1_1 = getEntityManager().find(ListBiowning1Entity.class, o1_1_id); + ListBiowning1Entity o1_2 = getEntityManager().find(ListBiowning1Entity.class, o1_2_id); + + ListBiowning2Entity rev1 = getAuditReader().find(ListBiowning2Entity.class, o1_2_id, 1); + ListBiowning2Entity rev2 = getAuditReader().find(ListBiowning2Entity.class, o1_2_id, 2); + ListBiowning2Entity rev3 = getAuditReader().find(ListBiowning2Entity.class, o1_2_id, 3); + ListBiowning2Entity rev4 = getAuditReader().find(ListBiowning2Entity.class, o1_2_id, 4); + ListBiowning2Entity rev5 = getAuditReader().find(ListBiowning2Entity.class, o1_2_id, 5); + + assert TestTools.checkList(rev1.getReferences()); + assert TestTools.checkList(rev2.getReferences(), o1_2); + assert TestTools.checkList(rev3.getReferences(), o1_1, o1_2); + assert TestTools.checkList(rev4.getReferences()); + assert TestTools.checkList(rev5.getReferences(), o1_2); + } +} \ No newline at end of file diff --git a/envers/src/test/resources/testng.xml b/envers/src/test/resources/testng.xml index ad6f88eb15..2dd5d5db0f 100644 --- a/envers/src/test/resources/testng.xml +++ b/envers/src/test/resources/testng.xml @@ -31,6 +31,7 @@ +