From c49f7df178aedf312db8b997e811fa797a1050aa Mon Sep 17 00:00:00 2001 From: Michal Skowronek Date: Sun, 15 May 2011 19:40:51 +0200 Subject: [PATCH 1/4] Test cases for the Jira Issue HHH-6176 --- .../entities/StrTestEntityComparator.java | 12 + .../entities/manytomany/SortedSetEntity.java | 114 ++++++++++ ...rtedSetWithCustomComparatorEntityTest.java | 211 ++++++++++++++++++ .../NotAnnotatedSortedSetEntity.java | 101 +++++++++ .../sortedSet/NotAnnotatedStrTestEntity.java | 86 +++++++ .../NotAnnotatedStrTestEntityComparator.java | 12 + ...tedSetWithCustomComparatorSessionTest.java | 186 +++++++++++++++ .../mappings/sortedSet/mappings.hbm.xml | 31 +++ 8 files changed, 753 insertions(+) create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/entities/StrTestEntityComparator.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/entities/manytomany/SortedSetEntity.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedSortedSetEntity.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntity.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntityComparator.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java create mode 100644 hibernate-envers/src/test/resources/mappings/sortedSet/mappings.hbm.xml diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/StrTestEntityComparator.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/StrTestEntityComparator.java new file mode 100644 index 0000000000..5954cc8842 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/StrTestEntityComparator.java @@ -0,0 +1,12 @@ +package org.hibernate.envers.test.entities; + +import java.util.Comparator; + +public class StrTestEntityComparator implements Comparator { + public static final StrTestEntityComparator INSTANCE = new StrTestEntityComparator(); + + @Override + public int compare(StrTestEntity o1, StrTestEntity o2) { + return o1.getStr().compareTo(o2.getStr()); + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/manytomany/SortedSetEntity.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/manytomany/SortedSetEntity.java new file mode 100644 index 0000000000..780da619f8 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/manytomany/SortedSetEntity.java @@ -0,0 +1,114 @@ +/* + * 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; + +import org.hibernate.annotations.Sort; +import org.hibernate.annotations.SortType; +import org.hibernate.envers.Audited; +import org.hibernate.envers.test.entities.StrTestEntity; +import org.hibernate.envers.test.entities.StrTestEntityComparator; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * Entity with custom-ordered SortedSet + * + * @author Michal Skowronek (mskowr at o2 pl) + */ +@Entity +public class SortedSetEntity { + @Id + private Integer id; + + @Audited + private String data; + + @Audited + @ManyToMany + @Sort(type = SortType.COMPARATOR, comparator = StrTestEntityComparator.class) + private SortedSet sortedSet = new TreeSet(StrTestEntityComparator.INSTANCE); + + public SortedSetEntity() { + } + + public SortedSetEntity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public SortedSetEntity(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 SortedSet getSortedSet() { + return sortedSet; + } + + public void setSortedSet(SortedSet sortedSet) { + this.sortedSet = sortedSet; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SortedSetEntity)) return false; + + SortedSetEntity that = (SortedSetEntity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + 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 "SetOwnedEntity(id = " + id + ", data = " + data + ")"; + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java new file mode 100644 index 0000000000..ab5bea4101 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.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; + +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.envers.test.AbstractEntityTest; +import org.hibernate.envers.test.Priority; +import org.hibernate.envers.test.entities.StrTestEntity; +import org.hibernate.envers.test.entities.StrTestEntityComparator; +import org.hibernate.envers.test.entities.manytomany.SortedSetEntity; +import org.hibernate.testing.FailureExpected; +import org.junit.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.Iterator; +import java.util.SortedSet; + +import static org.junit.Assert.assertEquals; + +/** + * @author Michal Skowronek (mskowr at o2 pl) + */ +public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest { + + private Integer id1; + private Integer id2; + private Integer id3; + private Integer id4; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StrTestEntity.class); + cfg.addAnnotatedClass(SortedSetEntity.class); + } + + @Test + @Priority(10) + public void initData() { + EntityManager em = getEntityManager(); + + SortedSetEntity entity1 = new SortedSetEntity(1, "sortedSet1"); + + // Revision 1 + em.getTransaction().begin(); + + em.persist(entity1); + + em.getTransaction().commit(); + + // Revision 2 + + em.getTransaction().begin(); + + entity1 = em.find(SortedSetEntity.class, 1); + final StrTestEntity strTestEntity1 = new StrTestEntity("abc"); + em.persist(strTestEntity1); + id1 = strTestEntity1.getId(); + entity1.getSortedSet().add(strTestEntity1); + + em.getTransaction().commit(); + + // Revision 3 + em.getTransaction().begin(); + + entity1 = em.find(SortedSetEntity.class, 1); + final StrTestEntity strTestEntity2 = new StrTestEntity("aaa"); + em.persist(strTestEntity2); + id2 = strTestEntity2.getId(); + entity1.getSortedSet().add(strTestEntity2); + + em.getTransaction().commit(); + + // Revision 4 + em.getTransaction().begin(); + + entity1 = em.find(SortedSetEntity.class, 1); + final StrTestEntity strTestEntity3 = new StrTestEntity("aba"); + em.persist(strTestEntity3); + id3 = strTestEntity3.getId(); + entity1.getSortedSet().add(strTestEntity3); + + em.getTransaction().commit(); + + // Revision 5 + em.getTransaction().begin(); + + entity1 = em.find(SortedSetEntity.class, 1); + final StrTestEntity strTestEntity4 = new StrTestEntity("aac"); + em.persist(strTestEntity4); + id4 = strTestEntity4.getId(); + entity1.getSortedSet().add(strTestEntity4); + + em.getTransaction().commit(); + } + + @Test + public void testRevisionsCounts() { + assertEquals(Arrays.asList(1, 2, 3, 4, 5), getAuditReader().getRevisions(SortedSetEntity.class, 1)); + assertEquals(Arrays.asList(2), getAuditReader().getRevisions(StrTestEntity.class, id1)); + assertEquals(Arrays.asList(3), getAuditReader().getRevisions(StrTestEntity.class, id2)); + assertEquals(Arrays.asList(4), getAuditReader().getRevisions(StrTestEntity.class, id3)); + assertEquals(Arrays.asList(5), getAuditReader().getRevisions(StrTestEntity.class, id4)); + } + + @Test + public void testCurrentStateOfEntity1() { + final SortedSetEntity entity1 = getEntityManager().find(SortedSetEntity.class, 1); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + final SortedSet sortedSet = entity1.getSortedSet(); + assertEquals(StrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(4, sortedSet.size()); + final Iterator iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id2, "aaa"); + checkStrTestEntity(iterator.next(), id4, "aac"); + checkStrTestEntity(iterator.next(), id3, "aba"); + checkStrTestEntity(iterator.next(), id1, "abc"); + } + + private void checkStrTestEntity(StrTestEntity entity, Integer id, String sortKey) { + assertEquals(id, entity.getId()); + assertEquals(sortKey, entity.getStr()); + } + + @Test + @FailureExpected(message = "Envers doesn't support custom comparators yet", jiraKey = "UPDATE_THAT") + public void testHistoryOfEntity1() throws Exception { + SortedSetEntity entity1 = getAuditReader().find(SortedSetEntity.class, 1, 1); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + SortedSet sortedSet = entity1.getSortedSet(); + assertEquals(StrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(0, sortedSet.size()); + + entity1 = getAuditReader().find(SortedSetEntity.class, 1, 2); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + sortedSet = entity1.getSortedSet(); + assertEquals(StrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(1, sortedSet.size()); + Iterator iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id1, "abc"); + + entity1 = getAuditReader().find(SortedSetEntity.class, 1, 3); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + sortedSet = entity1.getSortedSet(); + assertEquals(StrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(2, sortedSet.size()); + iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id2, "aaa"); + checkStrTestEntity(iterator.next(), id1, "abc"); + + entity1 = getAuditReader().find(SortedSetEntity.class, 1, 4); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + sortedSet = entity1.getSortedSet(); + assertEquals(StrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(3, sortedSet.size()); + iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id2, "aaa"); + checkStrTestEntity(iterator.next(), id3, "aba"); + checkStrTestEntity(iterator.next(), id1, "abc"); + + entity1 = getAuditReader().find(SortedSetEntity.class, 1, 5); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + sortedSet = entity1.getSortedSet(); + assertEquals(StrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(4, sortedSet.size()); + iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id2, "aaa"); + checkStrTestEntity(iterator.next(), id4, "aac"); + checkStrTestEntity(iterator.next(), id3, "aba"); + checkStrTestEntity(iterator.next(), id1, "abc"); + } + +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedSortedSetEntity.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedSortedSetEntity.java new file mode 100644 index 0000000000..b40ce14a49 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedSortedSetEntity.java @@ -0,0 +1,101 @@ +/* + * 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.sortedSet; + +import org.hibernate.envers.Audited; + +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * Entity with custom-ordered SortedSet + * + * @author Michal Skowronek (mskowr at o2 pl) + */ +@Audited +public class NotAnnotatedSortedSetEntity { + private Integer id; + private String data; + private SortedSet sortedSet = new TreeSet( + NotAnnotatedStrTestEntityComparator.INSTANCE); + + public NotAnnotatedSortedSetEntity() { + } + + public NotAnnotatedSortedSetEntity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public NotAnnotatedSortedSetEntity(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 SortedSet getSortedSet() { + return sortedSet; + } + + public void setSortedSet(SortedSet sortedSet) { + this.sortedSet = sortedSet; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof NotAnnotatedSortedSetEntity)) return false; + + NotAnnotatedSortedSetEntity that = (NotAnnotatedSortedSetEntity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + 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 "SetOwnedEntity(id = " + id + ", data = " + data + ")"; + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntity.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntity.java new file mode 100644 index 0000000000..ff7cdb6447 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntity.java @@ -0,0 +1,86 @@ +/* + * 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.sortedSet; + +import org.hibernate.envers.Audited; + +/** + * @author Michal Skowronek (mskowr at o2 pl) + */ +@Audited +public class NotAnnotatedStrTestEntity { + private Integer id; + private String str; + + public NotAnnotatedStrTestEntity() { + } + + public NotAnnotatedStrTestEntity(String str, Integer id) { + this.str = str; + this.id = id; + } + + public NotAnnotatedStrTestEntity(String str) { + this.str = str; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getStr() { + return str; + } + + public void setStr(String str) { + this.str = str; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof NotAnnotatedStrTestEntity)) return false; + + NotAnnotatedStrTestEntity that = (NotAnnotatedStrTestEntity) o; + + if (id != null ? !id.equals(that.id) : that.id != null) return false; + if (str != null ? !str.equals(that.str) : that.str != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (str != null ? str.hashCode() : 0); + return result; + } + + public String toString() { + return "STE(id = " + id + ", str = " + str + ")"; + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntityComparator.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntityComparator.java new file mode 100644 index 0000000000..55c9af0650 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntityComparator.java @@ -0,0 +1,12 @@ +package org.hibernate.envers.test.integration.sortedSet; + +import java.util.Comparator; + +public class NotAnnotatedStrTestEntityComparator implements Comparator { + public static final NotAnnotatedStrTestEntityComparator INSTANCE = new NotAnnotatedStrTestEntityComparator(); + + @Override + public int compare(NotAnnotatedStrTestEntity o1, NotAnnotatedStrTestEntity o2) { + return o1.getStr().compareTo(o2.getStr()); + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java new file mode 100644 index 0000000000..85b6274b8a --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java @@ -0,0 +1,186 @@ +package org.hibernate.envers.test.integration.sortedSet; + +import org.hibernate.MappingException; +import org.hibernate.envers.test.AbstractOneSessionTest; +import org.hibernate.envers.test.Priority; +import org.hibernate.testing.FailureExpected; +import org.junit.Test; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Arrays; +import java.util.Iterator; +import java.util.SortedSet; + +import static org.junit.Assert.assertEquals; + +/** + * @author Michal Skowronek (mskowr at o2 pl) + */ +public class SortedSetWithCustomComparatorSessionTest extends AbstractOneSessionTest { + + private Integer id1; + private Integer id2; + private Integer id3; + private Integer id4; + + protected void initMappings() throws MappingException, URISyntaxException { + URL url = Thread.currentThread().getContextClassLoader().getResource("mappings/sortedSet/mappings.hbm.xml"); + config.addFile(new File(url.toURI())); + } + + @Test + @Priority(10) + public void initData() { + NotAnnotatedSortedSetEntity entity1 = new NotAnnotatedSortedSetEntity(1, "sortedSet1"); + + // Revision 1 + getSession().getTransaction().begin(); + + getSession().persist(entity1); + + getSession().getTransaction().commit(); + + // Revision 2 + + getSession().getTransaction().begin(); + + entity1 = (NotAnnotatedSortedSetEntity) getSession().get(NotAnnotatedSortedSetEntity.class, 1); + final NotAnnotatedStrTestEntity strTestEntity1 = new NotAnnotatedStrTestEntity("abc"); + getSession().persist(strTestEntity1); + id1 = strTestEntity1.getId(); + entity1.getSortedSet().add(strTestEntity1); + + getSession().getTransaction().commit(); + + // Revision 3 + getSession().getTransaction().begin(); + + entity1 = (NotAnnotatedSortedSetEntity) getSession().get(NotAnnotatedSortedSetEntity.class, 1); + final NotAnnotatedStrTestEntity strTestEntity2 = new NotAnnotatedStrTestEntity("aaa"); + getSession().persist(strTestEntity2); + id2 = strTestEntity2.getId(); + entity1.getSortedSet().add(strTestEntity2); + + getSession().getTransaction().commit(); + + // Revision 4 + getSession().getTransaction().begin(); + + entity1 = (NotAnnotatedSortedSetEntity) getSession().get(NotAnnotatedSortedSetEntity.class, 1); + final NotAnnotatedStrTestEntity strTestEntity3 = new NotAnnotatedStrTestEntity("aba"); + getSession().persist(strTestEntity3); + id3 = strTestEntity3.getId(); + entity1.getSortedSet().add(strTestEntity3); + + getSession().getTransaction().commit(); + + // Revision 5 + getSession().getTransaction().begin(); + + entity1 = (NotAnnotatedSortedSetEntity) getSession().get(NotAnnotatedSortedSetEntity.class, 1); + final NotAnnotatedStrTestEntity strTestEntity4 = new NotAnnotatedStrTestEntity("aac"); + getSession().persist(strTestEntity4); + id4 = strTestEntity4.getId(); + entity1.getSortedSet().add(strTestEntity4); + + getSession().getTransaction().commit(); + } + + @Test + public void testRevisionsCounts() { + assertEquals(Arrays.asList(1, 2, 3, 4, 5), getAuditReader().getRevisions(NotAnnotatedSortedSetEntity.class, 1)); + assertEquals(Arrays.asList(2), getAuditReader().getRevisions(NotAnnotatedStrTestEntity.class, id1)); + assertEquals(Arrays.asList(3), getAuditReader().getRevisions(NotAnnotatedStrTestEntity.class, id2)); + assertEquals(Arrays.asList(4), getAuditReader().getRevisions(NotAnnotatedStrTestEntity.class, id3)); + assertEquals(Arrays.asList(5), getAuditReader().getRevisions(NotAnnotatedStrTestEntity.class, id4)); + } + + @Test + public void testCurrentStateOfEntity1() { + final NotAnnotatedSortedSetEntity entity1 = (NotAnnotatedSortedSetEntity) getSession().get( + NotAnnotatedSortedSetEntity.class, 1); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + final SortedSet sortedSet = entity1.getSortedSet(); + assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(4, sortedSet.size()); + final Iterator iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id2, "aaa"); + checkStrTestEntity(iterator.next(), id4, "aac"); + checkStrTestEntity(iterator.next(), id3, "aba"); + checkStrTestEntity(iterator.next(), id1, "abc"); + } + + private void checkStrTestEntity(NotAnnotatedStrTestEntity entity, Integer id, String sortKey) { + assertEquals(id, entity.getId()); + assertEquals(sortKey, entity.getStr()); + } + + @Test + @FailureExpected(message = "Envers doesn't support custom comparators yet", jiraKey = "UPDATE_THAT") + public void testHistoryOfEntity1() throws Exception { + NotAnnotatedSortedSetEntity entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 1); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + SortedSet sortedSet = entity1.getSortedSet(); + assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(0, sortedSet.size()); + + entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 2); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + sortedSet = entity1.getSortedSet(); + assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(1, sortedSet.size()); + Iterator iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id1, "abc"); + + entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 3); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + sortedSet = entity1.getSortedSet(); + assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(2, sortedSet.size()); + iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id2, "aaa"); + checkStrTestEntity(iterator.next(), id1, "abc"); + + entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 4); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + sortedSet = entity1.getSortedSet(); + assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(3, sortedSet.size()); + iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id2, "aaa"); + checkStrTestEntity(iterator.next(), id3, "aba"); + checkStrTestEntity(iterator.next(), id1, "abc"); + + entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 5); + + assertEquals("sortedSet1", entity1.getData()); + assertEquals(Integer.valueOf(1), entity1.getId()); + + sortedSet = entity1.getSortedSet(); + assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); + assertEquals(4, sortedSet.size()); + iterator = sortedSet.iterator(); + checkStrTestEntity(iterator.next(), id2, "aaa"); + checkStrTestEntity(iterator.next(), id4, "aac"); + checkStrTestEntity(iterator.next(), id3, "aba"); + checkStrTestEntity(iterator.next(), id1, "abc"); + } + +} diff --git a/hibernate-envers/src/test/resources/mappings/sortedSet/mappings.hbm.xml b/hibernate-envers/src/test/resources/mappings/sortedSet/mappings.hbm.xml new file mode 100644 index 0000000000..f1788ca123 --- /dev/null +++ b/hibernate-envers/src/test/resources/mappings/sortedSet/mappings.hbm.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From c380da695092d6a6c35b919e009f4aeaaeabc31c Mon Sep 17 00:00:00 2001 From: Michal Skowronek Date: Sun, 15 May 2011 19:52:36 +0200 Subject: [PATCH 2/4] Updated JIRA Keys --- .../manytomany/SortedSetWithCustomComparatorEntityTest.java | 2 +- .../sortedSet/SortedSetWithCustomComparatorSessionTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java index ab5bea4101..5ee4a107e8 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java @@ -146,7 +146,7 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest } @Test - @FailureExpected(message = "Envers doesn't support custom comparators yet", jiraKey = "UPDATE_THAT") + @FailureExpected(message = "Envers doesn't support custom comparators yet", jiraKey = "HHH-6176") public void testHistoryOfEntity1() throws Exception { SortedSetEntity entity1 = getAuditReader().find(SortedSetEntity.class, 1, 1); diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java index 85b6274b8a..f27573347d 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java @@ -121,7 +121,7 @@ public class SortedSetWithCustomComparatorSessionTest extends AbstractOneSession } @Test - @FailureExpected(message = "Envers doesn't support custom comparators yet", jiraKey = "UPDATE_THAT") + @FailureExpected(message = "Envers doesn't support custom comparators yet", jiraKey = "HHH-6176") public void testHistoryOfEntity1() throws Exception { NotAnnotatedSortedSetEntity entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 1); From fc7bb2162c0a19357188dd5d809a79a6401c0255 Mon Sep 17 00:00:00 2001 From: Michal Skowronek Date: Tue, 17 May 2011 00:06:46 +0200 Subject: [PATCH 3/4] Removed xml configuration-based test cases --- .../NotAnnotatedSortedSetEntity.java | 101 ---------- .../sortedSet/NotAnnotatedStrTestEntity.java | 86 -------- .../NotAnnotatedStrTestEntityComparator.java | 12 -- ...tedSetWithCustomComparatorSessionTest.java | 186 ------------------ .../mappings/sortedSet/mappings.hbm.xml | 31 --- 5 files changed, 416 deletions(-) delete mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedSortedSetEntity.java delete mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntity.java delete mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntityComparator.java delete mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java delete mode 100644 hibernate-envers/src/test/resources/mappings/sortedSet/mappings.hbm.xml diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedSortedSetEntity.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedSortedSetEntity.java deleted file mode 100644 index b40ce14a49..0000000000 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedSortedSetEntity.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * 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.sortedSet; - -import org.hibernate.envers.Audited; - -import java.util.SortedSet; -import java.util.TreeSet; - -/** - * Entity with custom-ordered SortedSet - * - * @author Michal Skowronek (mskowr at o2 pl) - */ -@Audited -public class NotAnnotatedSortedSetEntity { - private Integer id; - private String data; - private SortedSet sortedSet = new TreeSet( - NotAnnotatedStrTestEntityComparator.INSTANCE); - - public NotAnnotatedSortedSetEntity() { - } - - public NotAnnotatedSortedSetEntity(Integer id, String data) { - this.id = id; - this.data = data; - } - - public NotAnnotatedSortedSetEntity(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 SortedSet getSortedSet() { - return sortedSet; - } - - public void setSortedSet(SortedSet sortedSet) { - this.sortedSet = sortedSet; - } - - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof NotAnnotatedSortedSetEntity)) return false; - - NotAnnotatedSortedSetEntity that = (NotAnnotatedSortedSetEntity) o; - - if (data != null ? !data.equals(that.data) : that.data != null) return false; - 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 "SetOwnedEntity(id = " + id + ", data = " + data + ")"; - } -} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntity.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntity.java deleted file mode 100644 index ff7cdb6447..0000000000 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntity.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.sortedSet; - -import org.hibernate.envers.Audited; - -/** - * @author Michal Skowronek (mskowr at o2 pl) - */ -@Audited -public class NotAnnotatedStrTestEntity { - private Integer id; - private String str; - - public NotAnnotatedStrTestEntity() { - } - - public NotAnnotatedStrTestEntity(String str, Integer id) { - this.str = str; - this.id = id; - } - - public NotAnnotatedStrTestEntity(String str) { - this.str = str; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getStr() { - return str; - } - - public void setStr(String str) { - this.str = str; - } - - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof NotAnnotatedStrTestEntity)) return false; - - NotAnnotatedStrTestEntity that = (NotAnnotatedStrTestEntity) o; - - if (id != null ? !id.equals(that.id) : that.id != null) return false; - if (str != null ? !str.equals(that.str) : that.str != null) return false; - - return true; - } - - public int hashCode() { - int result; - result = (id != null ? id.hashCode() : 0); - result = 31 * result + (str != null ? str.hashCode() : 0); - return result; - } - - public String toString() { - return "STE(id = " + id + ", str = " + str + ")"; - } -} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntityComparator.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntityComparator.java deleted file mode 100644 index 55c9af0650..0000000000 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/NotAnnotatedStrTestEntityComparator.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.hibernate.envers.test.integration.sortedSet; - -import java.util.Comparator; - -public class NotAnnotatedStrTestEntityComparator implements Comparator { - public static final NotAnnotatedStrTestEntityComparator INSTANCE = new NotAnnotatedStrTestEntityComparator(); - - @Override - public int compare(NotAnnotatedStrTestEntity o1, NotAnnotatedStrTestEntity o2) { - return o1.getStr().compareTo(o2.getStr()); - } -} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java deleted file mode 100644 index f27573347d..0000000000 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/sortedSet/SortedSetWithCustomComparatorSessionTest.java +++ /dev/null @@ -1,186 +0,0 @@ -package org.hibernate.envers.test.integration.sortedSet; - -import org.hibernate.MappingException; -import org.hibernate.envers.test.AbstractOneSessionTest; -import org.hibernate.envers.test.Priority; -import org.hibernate.testing.FailureExpected; -import org.junit.Test; - -import java.io.File; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.Arrays; -import java.util.Iterator; -import java.util.SortedSet; - -import static org.junit.Assert.assertEquals; - -/** - * @author Michal Skowronek (mskowr at o2 pl) - */ -public class SortedSetWithCustomComparatorSessionTest extends AbstractOneSessionTest { - - private Integer id1; - private Integer id2; - private Integer id3; - private Integer id4; - - protected void initMappings() throws MappingException, URISyntaxException { - URL url = Thread.currentThread().getContextClassLoader().getResource("mappings/sortedSet/mappings.hbm.xml"); - config.addFile(new File(url.toURI())); - } - - @Test - @Priority(10) - public void initData() { - NotAnnotatedSortedSetEntity entity1 = new NotAnnotatedSortedSetEntity(1, "sortedSet1"); - - // Revision 1 - getSession().getTransaction().begin(); - - getSession().persist(entity1); - - getSession().getTransaction().commit(); - - // Revision 2 - - getSession().getTransaction().begin(); - - entity1 = (NotAnnotatedSortedSetEntity) getSession().get(NotAnnotatedSortedSetEntity.class, 1); - final NotAnnotatedStrTestEntity strTestEntity1 = new NotAnnotatedStrTestEntity("abc"); - getSession().persist(strTestEntity1); - id1 = strTestEntity1.getId(); - entity1.getSortedSet().add(strTestEntity1); - - getSession().getTransaction().commit(); - - // Revision 3 - getSession().getTransaction().begin(); - - entity1 = (NotAnnotatedSortedSetEntity) getSession().get(NotAnnotatedSortedSetEntity.class, 1); - final NotAnnotatedStrTestEntity strTestEntity2 = new NotAnnotatedStrTestEntity("aaa"); - getSession().persist(strTestEntity2); - id2 = strTestEntity2.getId(); - entity1.getSortedSet().add(strTestEntity2); - - getSession().getTransaction().commit(); - - // Revision 4 - getSession().getTransaction().begin(); - - entity1 = (NotAnnotatedSortedSetEntity) getSession().get(NotAnnotatedSortedSetEntity.class, 1); - final NotAnnotatedStrTestEntity strTestEntity3 = new NotAnnotatedStrTestEntity("aba"); - getSession().persist(strTestEntity3); - id3 = strTestEntity3.getId(); - entity1.getSortedSet().add(strTestEntity3); - - getSession().getTransaction().commit(); - - // Revision 5 - getSession().getTransaction().begin(); - - entity1 = (NotAnnotatedSortedSetEntity) getSession().get(NotAnnotatedSortedSetEntity.class, 1); - final NotAnnotatedStrTestEntity strTestEntity4 = new NotAnnotatedStrTestEntity("aac"); - getSession().persist(strTestEntity4); - id4 = strTestEntity4.getId(); - entity1.getSortedSet().add(strTestEntity4); - - getSession().getTransaction().commit(); - } - - @Test - public void testRevisionsCounts() { - assertEquals(Arrays.asList(1, 2, 3, 4, 5), getAuditReader().getRevisions(NotAnnotatedSortedSetEntity.class, 1)); - assertEquals(Arrays.asList(2), getAuditReader().getRevisions(NotAnnotatedStrTestEntity.class, id1)); - assertEquals(Arrays.asList(3), getAuditReader().getRevisions(NotAnnotatedStrTestEntity.class, id2)); - assertEquals(Arrays.asList(4), getAuditReader().getRevisions(NotAnnotatedStrTestEntity.class, id3)); - assertEquals(Arrays.asList(5), getAuditReader().getRevisions(NotAnnotatedStrTestEntity.class, id4)); - } - - @Test - public void testCurrentStateOfEntity1() { - final NotAnnotatedSortedSetEntity entity1 = (NotAnnotatedSortedSetEntity) getSession().get( - NotAnnotatedSortedSetEntity.class, 1); - - assertEquals("sortedSet1", entity1.getData()); - assertEquals(Integer.valueOf(1), entity1.getId()); - - final SortedSet sortedSet = entity1.getSortedSet(); - assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); - assertEquals(4, sortedSet.size()); - final Iterator iterator = sortedSet.iterator(); - checkStrTestEntity(iterator.next(), id2, "aaa"); - checkStrTestEntity(iterator.next(), id4, "aac"); - checkStrTestEntity(iterator.next(), id3, "aba"); - checkStrTestEntity(iterator.next(), id1, "abc"); - } - - private void checkStrTestEntity(NotAnnotatedStrTestEntity entity, Integer id, String sortKey) { - assertEquals(id, entity.getId()); - assertEquals(sortKey, entity.getStr()); - } - - @Test - @FailureExpected(message = "Envers doesn't support custom comparators yet", jiraKey = "HHH-6176") - public void testHistoryOfEntity1() throws Exception { - NotAnnotatedSortedSetEntity entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 1); - - assertEquals("sortedSet1", entity1.getData()); - assertEquals(Integer.valueOf(1), entity1.getId()); - - SortedSet sortedSet = entity1.getSortedSet(); - assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); - assertEquals(0, sortedSet.size()); - - entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 2); - - assertEquals("sortedSet1", entity1.getData()); - assertEquals(Integer.valueOf(1), entity1.getId()); - - sortedSet = entity1.getSortedSet(); - assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); - assertEquals(1, sortedSet.size()); - Iterator iterator = sortedSet.iterator(); - checkStrTestEntity(iterator.next(), id1, "abc"); - - entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 3); - - assertEquals("sortedSet1", entity1.getData()); - assertEquals(Integer.valueOf(1), entity1.getId()); - - sortedSet = entity1.getSortedSet(); - assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); - assertEquals(2, sortedSet.size()); - iterator = sortedSet.iterator(); - checkStrTestEntity(iterator.next(), id2, "aaa"); - checkStrTestEntity(iterator.next(), id1, "abc"); - - entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 4); - - assertEquals("sortedSet1", entity1.getData()); - assertEquals(Integer.valueOf(1), entity1.getId()); - - sortedSet = entity1.getSortedSet(); - assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); - assertEquals(3, sortedSet.size()); - iterator = sortedSet.iterator(); - checkStrTestEntity(iterator.next(), id2, "aaa"); - checkStrTestEntity(iterator.next(), id3, "aba"); - checkStrTestEntity(iterator.next(), id1, "abc"); - - entity1 = getAuditReader().find(NotAnnotatedSortedSetEntity.class, 1, 5); - - assertEquals("sortedSet1", entity1.getData()); - assertEquals(Integer.valueOf(1), entity1.getId()); - - sortedSet = entity1.getSortedSet(); - assertEquals(NotAnnotatedStrTestEntityComparator.class, sortedSet.comparator().getClass()); - assertEquals(4, sortedSet.size()); - iterator = sortedSet.iterator(); - checkStrTestEntity(iterator.next(), id2, "aaa"); - checkStrTestEntity(iterator.next(), id4, "aac"); - checkStrTestEntity(iterator.next(), id3, "aba"); - checkStrTestEntity(iterator.next(), id1, "abc"); - } - -} diff --git a/hibernate-envers/src/test/resources/mappings/sortedSet/mappings.hbm.xml b/hibernate-envers/src/test/resources/mappings/sortedSet/mappings.hbm.xml deleted file mode 100644 index f1788ca123..0000000000 --- a/hibernate-envers/src/test/resources/mappings/sortedSet/mappings.hbm.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From f07044a82ff5884cbaa3513274e56c45396972e7 Mon Sep 17 00:00:00 2001 From: Michal Skowronek Date: Sat, 21 May 2011 22:11:28 +0200 Subject: [PATCH 4/4] Added test cases for SortedMap Fixed issue with custom comparators --- .../metadata/CollectionMetadataGenerator.java | 72 +++++--------- .../relation/BasicCollectionMapper.java | 12 ++- .../mapper/relation/MapCollectionMapper.java | 14 +-- .../relation/SortedMapCollectionMapper.java | 53 +++++++++++ .../relation/SortedSetCollectionMapper.java | 53 +++++++++++ .../BasicCollectionInitializor.java | 10 +- .../initializor/MapCollectionInitializor.java | 8 +- .../SortedMapCollectionInitializor.java | 72 ++++++++++++++ .../SortedSetCollectionInitializor.java | 66 +++++++++++++ .../entities/manytomany/SortedSetEntity.java | 30 ++++-- ...t.java => CustomComparatorEntityTest.java} | 93 ++++++++++++++++--- 11 files changed, 391 insertions(+), 92 deletions(-) create mode 100644 hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/SortedMapCollectionMapper.java create mode 100644 hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/SortedSetCollectionMapper.java create mode 100644 hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/SortedMapCollectionInitializor.java create mode 100644 hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/SortedSetCollectionInitializor.java rename hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/{SortedSetWithCustomComparatorEntityTest.java => CustomComparatorEntityTest.java} (64%) diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/CollectionMetadataGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/CollectionMetadataGenerator.java index 8d69b06f13..2e66f592b0 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/CollectionMetadataGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/CollectionMetadataGenerator.java @@ -22,19 +22,9 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.envers.configuration.metadata; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; -import javax.persistence.JoinColumn; + import org.dom4j.Element; import org.hibernate.MappingException; -import org.hibernate.envers.internal.EnversMessageLogger; import org.hibernate.envers.ModificationStore; import org.hibernate.envers.RelationTargetAuditMode; import org.hibernate.envers.configuration.metadata.reader.PropertyAuditingData; @@ -45,46 +35,26 @@ import org.hibernate.envers.entities.mapper.CompositeMapperBuilder; import org.hibernate.envers.entities.mapper.PropertyMapper; import org.hibernate.envers.entities.mapper.SinglePropertyMapper; import org.hibernate.envers.entities.mapper.id.IdMapper; -import org.hibernate.envers.entities.mapper.relation.BasicCollectionMapper; -import org.hibernate.envers.entities.mapper.relation.CommonCollectionMapperData; -import org.hibernate.envers.entities.mapper.relation.ListCollectionMapper; -import org.hibernate.envers.entities.mapper.relation.MapCollectionMapper; -import org.hibernate.envers.entities.mapper.relation.MiddleComponentData; -import org.hibernate.envers.entities.mapper.relation.MiddleIdData; -import org.hibernate.envers.entities.mapper.relation.ToOneIdMapper; -import org.hibernate.envers.entities.mapper.relation.component.MiddleDummyComponentMapper; -import org.hibernate.envers.entities.mapper.relation.component.MiddleMapKeyIdComponentMapper; -import org.hibernate.envers.entities.mapper.relation.component.MiddleMapKeyPropertyComponentMapper; -import org.hibernate.envers.entities.mapper.relation.component.MiddleRelatedComponentMapper; -import org.hibernate.envers.entities.mapper.relation.component.MiddleSimpleComponentMapper; -import org.hibernate.envers.entities.mapper.relation.component.MiddleStraightComponentMapper; -import org.hibernate.envers.entities.mapper.relation.lazy.proxy.ListProxy; -import org.hibernate.envers.entities.mapper.relation.lazy.proxy.MapProxy; -import org.hibernate.envers.entities.mapper.relation.lazy.proxy.SetProxy; -import org.hibernate.envers.entities.mapper.relation.lazy.proxy.SortedMapProxy; -import org.hibernate.envers.entities.mapper.relation.lazy.proxy.SortedSetProxy; +import org.hibernate.envers.entities.mapper.relation.*; +import org.hibernate.envers.entities.mapper.relation.component.*; +import org.hibernate.envers.entities.mapper.relation.lazy.proxy.*; import org.hibernate.envers.entities.mapper.relation.query.OneAuditEntityQueryGenerator; import org.hibernate.envers.entities.mapper.relation.query.RelationQueryGenerator; +import org.hibernate.envers.internal.EnversMessageLogger; import org.hibernate.envers.tools.MappingTools; import org.hibernate.envers.tools.StringTools; import org.hibernate.envers.tools.Tools; import org.hibernate.mapping.Collection; -import org.hibernate.mapping.IndexedCollection; -import org.hibernate.mapping.OneToMany; -import org.hibernate.mapping.PersistentClass; -import org.hibernate.mapping.Property; -import org.hibernate.mapping.Table; -import org.hibernate.mapping.Value; -import org.hibernate.type.BagType; -import org.hibernate.type.ListType; -import org.hibernate.type.ManyToOneType; -import org.hibernate.type.MapType; -import org.hibernate.type.SetType; -import org.hibernate.type.SortedMapType; -import org.hibernate.type.SortedSetType; -import org.hibernate.type.Type; +import org.hibernate.mapping.*; +import org.hibernate.type.*; import org.jboss.logging.Logger; +import javax.persistence.JoinColumn; +import java.util.*; +import java.util.List; +import java.util.Map; +import java.util.Set; + /** * Generates metadata for a collection-valued property. * @author Adam Warski (adam at warski dot org) @@ -476,18 +446,18 @@ public final class CollectionMetadataGenerator { MiddleComponentData indexComponentData) { Type type = propertyValue.getType(); if (type instanceof SortedSetType) { - currentMapper.addComposite(propertyAuditingData.getPropertyData(), - new BasicCollectionMapper(commonCollectionMapperData, - TreeSet.class, SortedSetProxy.class, elementComponentData)); - } else if (type instanceof SetType) { - currentMapper.addComposite(propertyAuditingData.getPropertyData(), + currentMapper.addComposite(propertyAuditingData.getPropertyData(), + new SortedSetCollectionMapper(commonCollectionMapperData, + TreeSet.class, SortedSetProxy.class, elementComponentData, propertyValue.getComparator())); + } else if (type instanceof SetType) { + currentMapper.addComposite(propertyAuditingData.getPropertyData(), new BasicCollectionMapper(commonCollectionMapperData, HashSet.class, SetProxy.class, elementComponentData)); } else if (type instanceof SortedMapType) { // Indexed collection, so indexComponentData is not null. - currentMapper.addComposite(propertyAuditingData.getPropertyData(), - new MapCollectionMapper(commonCollectionMapperData, - TreeMap.class, SortedMapProxy.class, elementComponentData, indexComponentData)); + currentMapper.addComposite(propertyAuditingData.getPropertyData(), + new SortedMapCollectionMapper(commonCollectionMapperData, + TreeMap.class, SortedMapProxy.class, elementComponentData, indexComponentData, propertyValue.getComparator())); } else if (type instanceof MapType) { // Indexed collection, so indexComponentData is not null. currentMapper.addComposite(propertyAuditingData.getPropertyData(), diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/BasicCollectionMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/BasicCollectionMapper.java index 8145c87495..f87fd42a75 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/BasicCollectionMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/BasicCollectionMapper.java @@ -22,9 +22,7 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.envers.entities.mapper.relation; -import java.io.Serializable; -import java.util.Collection; -import java.util.Map; + import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.entities.mapper.PropertyMapper; @@ -32,11 +30,15 @@ import org.hibernate.envers.entities.mapper.relation.lazy.initializor.BasicColle import org.hibernate.envers.entities.mapper.relation.lazy.initializor.Initializor; import org.hibernate.envers.reader.AuditReaderImplementor; +import java.io.Serializable; +import java.util.Collection; +import java.util.Map; + /** * @author Adam Warski (adam at warski dot org) */ -public final class BasicCollectionMapper extends AbstractCollectionMapper implements PropertyMapper { - private final MiddleComponentData elementComponentData; +public class BasicCollectionMapper extends AbstractCollectionMapper implements PropertyMapper { + protected final MiddleComponentData elementComponentData; public BasicCollectionMapper(CommonCollectionMapperData commonCollectionMapperData, Class collectionClass, Class proxyClass, diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/MapCollectionMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/MapCollectionMapper.java index 6d2f29bb40..7b6a9899fe 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/MapCollectionMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/MapCollectionMapper.java @@ -22,9 +22,7 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.envers.entities.mapper.relation; -import java.io.Serializable; -import java.util.Collection; -import java.util.Map; + import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.entities.mapper.PropertyMapper; @@ -32,12 +30,16 @@ import org.hibernate.envers.entities.mapper.relation.lazy.initializor.Initializo import org.hibernate.envers.entities.mapper.relation.lazy.initializor.MapCollectionInitializor; import org.hibernate.envers.reader.AuditReaderImplementor; +import java.io.Serializable; +import java.util.Collection; +import java.util.Map; + /** * @author Adam Warski (adam at warski dot org) */ -public final class MapCollectionMapper extends AbstractCollectionMapper implements PropertyMapper { - private final MiddleComponentData elementComponentData; - private final MiddleComponentData indexComponentData; +public class MapCollectionMapper extends AbstractCollectionMapper implements PropertyMapper { + protected final MiddleComponentData elementComponentData; + protected final MiddleComponentData indexComponentData; public MapCollectionMapper(CommonCollectionMapperData commonCollectionMapperData, Class collectionClass, Class proxyClass, diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/SortedMapCollectionMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/SortedMapCollectionMapper.java new file mode 100644 index 0000000000..b40cdbd03c --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/SortedMapCollectionMapper.java @@ -0,0 +1,53 @@ +/* + * 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.entities.mapper.relation; + +import org.hibernate.envers.configuration.AuditConfiguration; +import org.hibernate.envers.entities.mapper.relation.lazy.initializor.Initializor; +import org.hibernate.envers.entities.mapper.relation.lazy.initializor.SortedMapCollectionInitializor; +import org.hibernate.envers.reader.AuditReaderImplementor; + +import java.util.Comparator; +import java.util.SortedMap; + +/** + * @author Michal Skowronek (mskowr at o2 dot pl) + */ +public final class SortedMapCollectionMapper extends MapCollectionMapper { + private final Comparator comparator; + + public SortedMapCollectionMapper(CommonCollectionMapperData commonCollectionMapperData, + Class collectionClass, Class proxyClass, + MiddleComponentData elementComponentData, MiddleComponentData indexComponentData, Comparator comparator) { + super(commonCollectionMapperData, collectionClass, proxyClass, elementComponentData, indexComponentData); + this.comparator = comparator; + } + + protected Initializor getInitializor(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, + Object primaryKey, Number revision) { + return new SortedMapCollectionInitializor(verCfg, versionsReader, commonCollectionMapperData.getQueryGenerator(), + primaryKey, revision, collectionClass, elementComponentData, indexComponentData, comparator); + } + +} \ No newline at end of file diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/SortedSetCollectionMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/SortedSetCollectionMapper.java new file mode 100644 index 0000000000..2b29559334 --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/SortedSetCollectionMapper.java @@ -0,0 +1,53 @@ +/* + * 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.entities.mapper.relation; + +import org.hibernate.envers.configuration.AuditConfiguration; +import org.hibernate.envers.entities.mapper.relation.lazy.initializor.Initializor; +import org.hibernate.envers.entities.mapper.relation.lazy.initializor.SortedSetCollectionInitializor; +import org.hibernate.envers.reader.AuditReaderImplementor; + +import java.util.Comparator; +import java.util.SortedSet; + +/** + * @author Michal Skowronek (mskowr at o2 dot pl) + */ +public final class SortedSetCollectionMapper extends BasicCollectionMapper { + private final Comparator comparator; + + public SortedSetCollectionMapper(CommonCollectionMapperData commonCollectionMapperData, + Class collectionClass, Class proxyClass, + MiddleComponentData elementComponentData, Comparator comparator) { + super(commonCollectionMapperData, collectionClass, proxyClass, elementComponentData); + this.comparator = comparator; + } + + protected Initializor getInitializor(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, + Object primaryKey, Number revision) { + return new SortedSetCollectionInitializor(verCfg, versionsReader, commonCollectionMapperData.getQueryGenerator(), + primaryKey, revision, collectionClass, elementComponentData, comparator); + } + +} diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java index 9fc31c1c82..b975433a4a 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java @@ -22,21 +22,23 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.envers.entities.mapper.relation.lazy.initializor; -import java.util.Collection; -import java.util.List; -import java.util.Map; + import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.entities.mapper.relation.MiddleComponentData; import org.hibernate.envers.entities.mapper.relation.query.RelationQueryGenerator; import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.reader.AuditReaderImplementor; +import java.util.Collection; +import java.util.List; +import java.util.Map; + /** * Initializes a non-indexed java collection (set or list, eventually sorted). * @author Adam Warski (adam at warski dot org) */ public class BasicCollectionInitializor extends AbstractCollectionInitializor { - private final Class collectionClass; + protected final Class collectionClass; private final MiddleComponentData elementComponentData; public BasicCollectionInitializor(AuditConfiguration verCfg, diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java index f49810fdd3..dce3cdb0af 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java @@ -22,20 +22,22 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.envers.entities.mapper.relation.lazy.initializor; -import java.util.List; -import java.util.Map; + import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.entities.mapper.relation.MiddleComponentData; import org.hibernate.envers.entities.mapper.relation.query.RelationQueryGenerator; import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.reader.AuditReaderImplementor; +import java.util.List; +import java.util.Map; + /** * Initializes a map. * @author Adam Warski (adam at warski dot org) */ public class MapCollectionInitializor extends AbstractCollectionInitializor { - private final Class collectionClass; + protected final Class collectionClass; private final MiddleComponentData elementComponentData; private final MiddleComponentData indexComponentData; diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/SortedMapCollectionInitializor.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/SortedMapCollectionInitializor.java new file mode 100644 index 0000000000..8b11aef4cf --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/SortedMapCollectionInitializor.java @@ -0,0 +1,72 @@ +/* + * 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.entities.mapper.relation.lazy.initializor; + +import org.hibernate.envers.configuration.AuditConfiguration; +import org.hibernate.envers.entities.mapper.relation.MiddleComponentData; +import org.hibernate.envers.entities.mapper.relation.query.RelationQueryGenerator; +import org.hibernate.envers.exception.AuditException; +import org.hibernate.envers.reader.AuditReaderImplementor; + +import java.lang.reflect.InvocationTargetException; +import java.util.Comparator; +import java.util.SortedMap; + +/** + * Initializes SortedMap collection with proper Comparator + * + * @author Michal Skowronek (mskowr at o2 dot pl) + */ +public class SortedMapCollectionInitializor extends MapCollectionInitializor { + private final Comparator comparator; + + public SortedMapCollectionInitializor(AuditConfiguration verCfg, + AuditReaderImplementor versionsReader, + RelationQueryGenerator queryGenerator, + Object primaryKey, Number revision, + Class collectionClass, + MiddleComponentData elementComponentData, + MiddleComponentData indexComponentData, Comparator comparator) { + super(verCfg, versionsReader, queryGenerator, primaryKey, revision, collectionClass, elementComponentData, indexComponentData); + this.comparator = comparator; + } + + protected SortedMap initializeCollection(int size) { + if (comparator == null) { + return super.initializeCollection(size); + } + try { + return collectionClass.getConstructor(Comparator.class).newInstance(comparator); + } catch (InstantiationException e) { + throw new AuditException(e); + } catch (IllegalAccessException e) { + throw new AuditException(e); + } catch (NoSuchMethodException e) { + throw new AuditException(e); + } catch (InvocationTargetException e) { + throw new AuditException(e); + } + } + +} diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/SortedSetCollectionInitializor.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/SortedSetCollectionInitializor.java new file mode 100644 index 0000000000..0ec0865c61 --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/initializor/SortedSetCollectionInitializor.java @@ -0,0 +1,66 @@ +/* + * 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.entities.mapper.relation.lazy.initializor; + +import org.hibernate.envers.configuration.AuditConfiguration; +import org.hibernate.envers.entities.mapper.relation.MiddleComponentData; +import org.hibernate.envers.entities.mapper.relation.query.RelationQueryGenerator; +import org.hibernate.envers.exception.AuditException; +import org.hibernate.envers.reader.AuditReaderImplementor; + +import java.lang.reflect.InvocationTargetException; +import java.util.Comparator; +import java.util.SortedSet; + +/** + * Initializes SortedSet collection with proper Comparator + * + * @author Michal Skowronek (mskowr at o2 dot pl) + */ +public class SortedSetCollectionInitializor extends BasicCollectionInitializor { + private final Comparator comparator; + + public SortedSetCollectionInitializor(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, RelationQueryGenerator queryGenerator, Object primaryKey, Number revision, Class collectionClass, MiddleComponentData elementComponentData, Comparator comparator) { + super(verCfg, versionsReader, queryGenerator, primaryKey, revision, collectionClass, elementComponentData); + this.comparator = comparator; + } + + @Override + protected SortedSet initializeCollection(int size) { + if (comparator == null) { + return super.initializeCollection(size); + } + try { + return collectionClass.getConstructor(Comparator.class).newInstance(comparator); + } catch (InstantiationException e) { + throw new AuditException(e); + } catch (IllegalAccessException e) { + throw new AuditException(e); + } catch (NoSuchMethodException e) { + throw new AuditException(e); + } catch (InvocationTargetException e) { + throw new AuditException(e); + } + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/manytomany/SortedSetEntity.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/manytomany/SortedSetEntity.java index 780da619f8..47d82e16fb 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/manytomany/SortedSetEntity.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/manytomany/SortedSetEntity.java @@ -29,14 +29,14 @@ import org.hibernate.envers.Audited; import org.hibernate.envers.test.entities.StrTestEntity; import org.hibernate.envers.test.entities.StrTestEntityComparator; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.ManyToMany; +import javax.persistence.*; +import java.util.SortedMap; import java.util.SortedSet; +import java.util.TreeMap; import java.util.TreeSet; /** - * Entity with custom-ordered SortedSet + * Entity with custom-ordered SortedSet and SortedMap * * @author Michal Skowronek (mskowr at o2 pl) */ @@ -52,6 +52,11 @@ public class SortedSetEntity { @ManyToMany @Sort(type = SortType.COMPARATOR, comparator = StrTestEntityComparator.class) private SortedSet sortedSet = new TreeSet(StrTestEntityComparator.INSTANCE); + @Audited + @ElementCollection + @MapKeyJoinColumn + @Sort(type = SortType.COMPARATOR, comparator = StrTestEntityComparator.class) + private SortedMap sortedMap = new TreeMap(StrTestEntityComparator.INSTANCE); public SortedSetEntity() { } @@ -89,17 +94,22 @@ public class SortedSetEntity { this.sortedSet = sortedSet; } - public boolean equals(Object o) { + public SortedMap getSortedMap() { + return sortedMap; + } + + public void setSortedMap(SortedMap sortedMap) { + this.sortedMap = sortedMap; + } + + public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof SortedSetEntity)) return false; SortedSetEntity that = (SortedSetEntity) o; - if (data != null ? !data.equals(that.data) : that.data != null) return false; - if (id != null ? !id.equals(that.id) : that.id != null) return false; - - return true; - } + return !(data != null ? !data.equals(that.data) : that.data != null) && !(id != null ? !id.equals(that.id) : that.id != null); + } public int hashCode() { int result; diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/CustomComparatorEntityTest.java similarity index 64% rename from hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java rename to hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/CustomComparatorEntityTest.java index 5ee4a107e8..6e55a844e4 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/SortedSetWithCustomComparatorEntityTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytomany/CustomComparatorEntityTest.java @@ -29,20 +29,17 @@ import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.entities.StrTestEntity; import org.hibernate.envers.test.entities.StrTestEntityComparator; import org.hibernate.envers.test.entities.manytomany.SortedSetEntity; -import org.hibernate.testing.FailureExpected; import org.junit.Test; import javax.persistence.EntityManager; -import java.util.Arrays; -import java.util.Iterator; -import java.util.SortedSet; +import java.util.*; import static org.junit.Assert.assertEquals; /** * @author Michal Skowronek (mskowr at o2 pl) */ -public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest { +public class CustomComparatorEntityTest extends AbstractEntityTest { private Integer id1; private Integer id2; @@ -59,7 +56,7 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest public void initData() { EntityManager em = getEntityManager(); - SortedSetEntity entity1 = new SortedSetEntity(1, "sortedSet1"); + SortedSetEntity entity1 = new SortedSetEntity(1, "sortedEntity1"); // Revision 1 em.getTransaction().begin(); @@ -77,6 +74,7 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest em.persist(strTestEntity1); id1 = strTestEntity1.getId(); entity1.getSortedSet().add(strTestEntity1); + entity1.getSortedMap().put(strTestEntity1, "abc"); em.getTransaction().commit(); @@ -88,6 +86,7 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest em.persist(strTestEntity2); id2 = strTestEntity2.getId(); entity1.getSortedSet().add(strTestEntity2); + entity1.getSortedMap().put(strTestEntity2, "aaa"); em.getTransaction().commit(); @@ -99,6 +98,7 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest em.persist(strTestEntity3); id3 = strTestEntity3.getId(); entity1.getSortedSet().add(strTestEntity3); + entity1.getSortedMap().put(strTestEntity3, "aba"); em.getTransaction().commit(); @@ -110,6 +110,7 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest em.persist(strTestEntity4); id4 = strTestEntity4.getId(); entity1.getSortedSet().add(strTestEntity4); + entity1.getSortedMap().put(strTestEntity4, "aac"); em.getTransaction().commit(); } @@ -127,7 +128,7 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest public void testCurrentStateOfEntity1() { final SortedSetEntity entity1 = getEntityManager().find(SortedSetEntity.class, 1); - assertEquals("sortedSet1", entity1.getData()); + assertEquals("sortedEntity1", entity1.getData()); assertEquals(Integer.valueOf(1), entity1.getId()); final SortedSet sortedSet = entity1.getSortedSet(); @@ -138,6 +139,21 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest checkStrTestEntity(iterator.next(), id4, "aac"); checkStrTestEntity(iterator.next(), id3, "aba"); checkStrTestEntity(iterator.next(), id1, "abc"); + + final SortedMap sortedMap = entity1.getSortedMap(); + assertEquals(StrTestEntityComparator.class, sortedMap.comparator().getClass()); + assertEquals(4, sortedMap.size()); + Iterator> mapIterator = sortedMap.entrySet().iterator(); + checkStrTestEntity(mapIterator.next().getKey(), id2, "aaa"); + checkStrTestEntity(mapIterator.next().getKey(), id4, "aac"); + checkStrTestEntity(mapIterator.next().getKey(), id3, "aba"); + checkStrTestEntity(mapIterator.next().getKey(), id1, "abc"); + + mapIterator = sortedMap.entrySet().iterator(); + assertEquals(mapIterator.next().getValue(), "aaa"); + assertEquals(mapIterator.next().getValue(), "aac"); + assertEquals(mapIterator.next().getValue(), "aba"); + assertEquals(mapIterator.next().getValue(), "abc"); } private void checkStrTestEntity(StrTestEntity entity, Integer id, String sortKey) { @@ -146,20 +162,23 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest } @Test - @FailureExpected(message = "Envers doesn't support custom comparators yet", jiraKey = "HHH-6176") public void testHistoryOfEntity1() throws Exception { SortedSetEntity entity1 = getAuditReader().find(SortedSetEntity.class, 1, 1); - assertEquals("sortedSet1", entity1.getData()); + assertEquals("sortedEntity1", entity1.getData()); assertEquals(Integer.valueOf(1), entity1.getId()); SortedSet sortedSet = entity1.getSortedSet(); assertEquals(StrTestEntityComparator.class, sortedSet.comparator().getClass()); assertEquals(0, sortedSet.size()); + SortedMap sortedMap = entity1.getSortedMap(); + assertEquals(StrTestEntityComparator.class, sortedMap.comparator().getClass()); + assertEquals(0, sortedMap.size()); + entity1 = getAuditReader().find(SortedSetEntity.class, 1, 2); - assertEquals("sortedSet1", entity1.getData()); + assertEquals("sortedEntity1", entity1.getData()); assertEquals(Integer.valueOf(1), entity1.getId()); sortedSet = entity1.getSortedSet(); @@ -168,9 +187,18 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest Iterator iterator = sortedSet.iterator(); checkStrTestEntity(iterator.next(), id1, "abc"); + sortedMap = entity1.getSortedMap(); + assertEquals(StrTestEntityComparator.class, sortedMap.comparator().getClass()); + assertEquals(1, sortedMap.size()); + Iterator> mapIterator = sortedMap.entrySet().iterator(); + checkStrTestEntity(mapIterator.next().getKey(), id1, "abc"); + + mapIterator = sortedMap.entrySet().iterator(); + assertEquals(mapIterator.next().getValue(), "abc"); + entity1 = getAuditReader().find(SortedSetEntity.class, 1, 3); - assertEquals("sortedSet1", entity1.getData()); + assertEquals("sortedEntity1", entity1.getData()); assertEquals(Integer.valueOf(1), entity1.getId()); sortedSet = entity1.getSortedSet(); @@ -180,9 +208,20 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest checkStrTestEntity(iterator.next(), id2, "aaa"); checkStrTestEntity(iterator.next(), id1, "abc"); + sortedMap = entity1.getSortedMap(); + assertEquals(StrTestEntityComparator.class, sortedMap.comparator().getClass()); + assertEquals(2, sortedMap.size()); + mapIterator = sortedMap.entrySet().iterator(); + checkStrTestEntity(mapIterator.next().getKey(), id2, "aaa"); + checkStrTestEntity(mapIterator.next().getKey(), id1, "abc"); + + mapIterator = sortedMap.entrySet().iterator(); + assertEquals(mapIterator.next().getValue(), "aaa"); + assertEquals(mapIterator.next().getValue(), "abc"); + entity1 = getAuditReader().find(SortedSetEntity.class, 1, 4); - assertEquals("sortedSet1", entity1.getData()); + assertEquals("sortedEntity1", entity1.getData()); assertEquals(Integer.valueOf(1), entity1.getId()); sortedSet = entity1.getSortedSet(); @@ -193,9 +232,22 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest checkStrTestEntity(iterator.next(), id3, "aba"); checkStrTestEntity(iterator.next(), id1, "abc"); + sortedMap = entity1.getSortedMap(); + assertEquals(StrTestEntityComparator.class, sortedMap.comparator().getClass()); + assertEquals(3, sortedMap.size()); + mapIterator = sortedMap.entrySet().iterator(); + checkStrTestEntity(mapIterator.next().getKey(), id2, "aaa"); + checkStrTestEntity(mapIterator.next().getKey(), id3, "aba"); + checkStrTestEntity(mapIterator.next().getKey(), id1, "abc"); + + mapIterator = sortedMap.entrySet().iterator(); + assertEquals(mapIterator.next().getValue(), "aaa"); + assertEquals(mapIterator.next().getValue(), "aba"); + assertEquals(mapIterator.next().getValue(), "abc"); + entity1 = getAuditReader().find(SortedSetEntity.class, 1, 5); - assertEquals("sortedSet1", entity1.getData()); + assertEquals("sortedEntity1", entity1.getData()); assertEquals(Integer.valueOf(1), entity1.getId()); sortedSet = entity1.getSortedSet(); @@ -206,6 +258,21 @@ public class SortedSetWithCustomComparatorEntityTest extends AbstractEntityTest checkStrTestEntity(iterator.next(), id4, "aac"); checkStrTestEntity(iterator.next(), id3, "aba"); checkStrTestEntity(iterator.next(), id1, "abc"); + + sortedMap = entity1.getSortedMap(); + assertEquals(StrTestEntityComparator.class, sortedMap.comparator().getClass()); + assertEquals(4, sortedMap.size()); + mapIterator = sortedMap.entrySet().iterator(); + checkStrTestEntity(mapIterator.next().getKey(), id2, "aaa"); + checkStrTestEntity(mapIterator.next().getKey(), id4, "aac"); + checkStrTestEntity(mapIterator.next().getKey(), id3, "aba"); + checkStrTestEntity(mapIterator.next().getKey(), id1, "abc"); + + mapIterator = sortedMap.entrySet().iterator(); + assertEquals(mapIterator.next().getValue(), "aaa"); + assertEquals(mapIterator.next().getValue(), "aac"); + assertEquals(mapIterator.next().getValue(), "aba"); + assertEquals(mapIterator.next().getValue(), "abc"); } }