* a disabled tests for collecitons of components

* fixing a bug whit auditing collections when the hashcode of the elements in the colleciton changes

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@16055 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Adam Warski 2009-03-02 16:08:45 +00:00
parent dfc657db27
commit cd01125e76
8 changed files with 470 additions and 4 deletions

View File

@ -251,7 +251,7 @@ public class AuditedPropertiesReader {
public Class<? extends Annotation> annotationType() { return this.getClass(); }
};
private class ComponentPropertiesSource implements PersistentPropertiesSource {
private static class ComponentPropertiesSource implements PersistentPropertiesSource {
private final XClass xclass;
private final Component component;

View File

@ -114,13 +114,16 @@ public abstract class AbstractCollectionMapper<T> implements PropertyMapper {
Set<Object> added = new HashSet<Object>();
if (newColl != null) { added.addAll(newCollection); }
if (oldColl != null) { added.removeAll(oldCollection); }
// Re-hashing the old collection as the hash codes of the elements there may have changed, and the
// removeAll in AbstractSet has an implementation that is hashcode-change sensitive (as opposed to addAll).
if (oldColl != null) { added.removeAll(new HashSet(oldCollection)); }
addCollectionChanges(collectionChanges, added, RevisionType.ADD, id);
Set<Object> deleted = new HashSet<Object>();
if (oldColl != null) { deleted.addAll(oldCollection); }
if (newColl != null) { deleted.removeAll(newCollection); }
// The same as above - re-hashing new collection.
if (newColl != null) { deleted.removeAll(new HashSet(newCollection)); }
addCollectionChanges(collectionChanges, deleted, RevisionType.DEL, id);

View File

@ -57,7 +57,6 @@ import org.hibernate.event.PreCollectionRemoveEventListener;
import org.hibernate.event.PreCollectionUpdateEvent;
import org.hibernate.event.PreCollectionUpdateEventListener;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.collection.AbstractCollectionPersister;
import org.hibernate.proxy.HibernateProxy;
/**

View File

@ -0,0 +1,100 @@
/*
* 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.components;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.envers.Audited;
import java.util.Set;
import java.util.HashSet;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
public class ComponentSetTestEntity {
@Id
@GeneratedValue
private Integer id;
@Embedded
@Audited
private Set<Component1> comps = new HashSet<Component1>();
public ComponentSetTestEntity() {
}
public ComponentSetTestEntity(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<Component1> getComps() {
return comps;
}
public void setComps(Set<Component1> comps) {
this.comps = comps;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ComponentSetTestEntity)) return false;
ComponentSetTestEntity that = (ComponentSetTestEntity) o;
if (comps != null ? !comps.equals(that.comps) : that.comps != null) return false;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (comps != null ? comps.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "ComponentSetTestEntity{" +
"id=" + id +
", comps=" + comps +
'}';
}
}

View File

@ -0,0 +1,87 @@
/*
* 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.components.collections;
import java.util.Arrays;
import java.util.Set;
import javax.persistence.EntityManager;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.entities.components.Component1;
import org.hibernate.envers.test.entities.components.ComponentSetTestEntity;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.hibernate.ejb.Ejb3Configuration;
/**
* TODO: enable and implement
* @author Adam Warski (adam at warski dot org)
*/
public class CollectionOfComponents extends AbstractEntityTest {
private Integer id1;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(ComponentSetTestEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
ComponentSetTestEntity cte1 = new ComponentSetTestEntity();
em.persist(cte1);
em.getTransaction().commit();
// Revision 2
em = getEntityManager();
em.getTransaction().begin();
cte1 = em.find(ComponentSetTestEntity.class, cte1.getId());
cte1.getComps().add(new Component1("a", "b"));
em.getTransaction().commit();
id1 = cte1.getId();
}
@Test(enabled = false)
public void testRevisionsCounts() {
assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(ComponentSetTestEntity.class, id1));
}
@Test(enabled = false)
public void testHistoryOfId1() {
assert getAuditReader().find(ComponentSetTestEntity.class, id1, 1).getComps().size() == 0;
Set<Component1> comps1 = getAuditReader().find(ComponentSetTestEntity.class, id1, 2).getComps();
assert comps1.size() == 1;
assert comps1.contains(new Component1("a", "b"));
}
}

View File

@ -0,0 +1,103 @@
/*
* 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.hashcode;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.tools.TestTools;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.hibernate.ejb.Ejb3Configuration;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class ChangingHashcode extends AbstractEntityTest {
private Long pageId;
private Long imageId;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(WikiPage.class);
cfg.addAnnotatedClass(WikiImage.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
WikiPage page = new WikiPage("title", "content");
em.persist(page);
em.getTransaction().commit();
// Revision 2
em = getEntityManager();
em.getTransaction().begin();
WikiImage image = new WikiImage("name1");
em.persist(image);
page = em.find(WikiPage.class, page.getId());
page.getImages().add(image);
em.getTransaction().commit();
// Revision 3
em = getEntityManager();
em.getTransaction().begin();
image = em.find(WikiImage.class, image.getId());
image.setName("name2");
em.getTransaction().commit();
pageId = page.getId();
imageId = image.getId();
}
@Test
public void testRevisionsCounts() {
assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(WikiPage.class, pageId));
assert Arrays.asList(2, 3).equals(getAuditReader().getRevisions(WikiImage.class, imageId));
}
@Test
public void testHistoryOfImage() {
assert getAuditReader().find(WikiImage.class, imageId, 1) == null;
assert getAuditReader().find(WikiImage.class, imageId, 2).equals(new WikiImage("name1"));
assert getAuditReader().find(WikiImage.class, imageId, 3).equals(new WikiImage("name2"));
}
@Test
public void testHistoryOfPage() {
assert getAuditReader().find(WikiPage.class, pageId, 1).getImages().size() == 0;
assert getAuditReader().find(WikiPage.class, pageId, 2).getImages().equals(TestTools.makeSet(new WikiImage("name1")));
assert getAuditReader().find(WikiPage.class, pageId, 3).getImages().equals(TestTools.makeSet(new WikiImage("name2")));
}
}

View File

@ -0,0 +1,65 @@
package org.hibernate.envers.test.integration.hashcode;
import org.hibernate.envers.Audited;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Entity;
import javax.persistence.Basic;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
public class WikiImage {
@Id
@GeneratedValue
private Long id;
@Basic
private String name;
public WikiImage() {
}
public WikiImage(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof WikiImage)) return false;
WikiImage wikiImage = (WikiImage) o;
if (name != null ? !name.equals(wikiImage.name) : wikiImage.name != null) return false;
return true;
}
@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
@Override
public String toString() {
return "WikiImage{" +
"name='" + name + '\'' +
'}';
}
}

View File

@ -0,0 +1,109 @@
package org.hibernate.envers.test.integration.hashcode;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.envers.Audited;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Basic;
import java.util.Set;
import java.util.HashSet;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
public class WikiPage {
@Id
@GeneratedValue
private Long id;
@Basic
private String title;
@Basic
private String content;
@CollectionOfElements
private Set<String> links = new HashSet<String>();
@OneToMany
private Set<WikiImage> images = new HashSet<WikiImage>();
public WikiPage() {
}
public WikiPage(String title, String content) {
this.title = title;
this.content = content;
}
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Set<String> getLinks() {
return links;
}
public void setLinks(Set<String> links) {
this.links = links;
}
public Set<WikiImage> getImages() {
return images;
}
public void setImages(Set<WikiImage> images) {
this.images = images;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof WikiPage)) return false;
WikiPage wikiPage = (WikiPage) o;
if (content != null ? !content.equals(wikiPage.content) : wikiPage.content != null) return false;
if (title != null ? !title.equals(wikiPage.title) : wikiPage.title != null) return false;
return true;
}
@Override
public int hashCode() {
int result = title != null ? title.hashCode() : 0;
result = 31 * result + (content != null ? content.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "WikiPage{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", links=" + links +
", images=" + images +
'}';
}
}