From d3886e2012826c3a93dc8d77f32ad49558f8f371 Mon Sep 17 00:00:00 2001 From: Adam Warski Date: Wed, 10 Feb 2010 16:00:29 +0000 Subject: [PATCH] HHH-4899: - test git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18765 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../test/entities/ids/DateIdTestEntity.java | 90 +++++++++++++++++++ .../envers/test/integration/ids/DateId.java | 83 +++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 envers/src/test/java/org/hibernate/envers/test/entities/ids/DateIdTestEntity.java create mode 100644 envers/src/test/java/org/hibernate/envers/test/integration/ids/DateId.java diff --git a/envers/src/test/java/org/hibernate/envers/test/entities/ids/DateIdTestEntity.java b/envers/src/test/java/org/hibernate/envers/test/entities/ids/DateIdTestEntity.java new file mode 100644 index 0000000000..0f9801b027 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/entities/ids/DateIdTestEntity.java @@ -0,0 +1,90 @@ +/* + * 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.ids; + +import org.hibernate.envers.Audited; + +import javax.persistence.Entity; +import javax.persistence.Id; +import java.util.Date; + +/** + * @author Adam Warski (adam at warski dot org) + */ +@Entity +public class DateIdTestEntity { + @Id + private Date id; + + @Audited + private String str1; + + public DateIdTestEntity() { + } + + public DateIdTestEntity(String str1) { + this.str1 = str1; + } + + public DateIdTestEntity(Date id, String str1) { + this.id = id; + this.str1 = str1; + } + + public Date getId() { + return id; + } + + public void setId(Date id) { + this.id = id; + } + + public String getStr1() { + return str1; + } + + public void setStr1(String str1) { + this.str1 = str1; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DateIdTestEntity that = (DateIdTestEntity) o; + + if (id != null ? !id.equals(that.id) : that.id != null) return false; + if (str1 != null ? !str1.equals(that.str1) : that.str1 != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + (str1 != null ? str1.hashCode() : 0); + return result; + } +} \ No newline at end of file diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/ids/DateId.java b/envers/src/test/java/org/hibernate/envers/test/integration/ids/DateId.java new file mode 100644 index 0000000000..8d666b9384 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/ids/DateId.java @@ -0,0 +1,83 @@ +/* + * 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.ids; + +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.envers.test.AbstractEntityTest; +import org.hibernate.envers.test.entities.ids.*; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.Date; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class DateId extends AbstractEntityTest { + private Date id1; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(DateIdTestEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + + // Revision 1 + EntityManager em = getEntityManager(); + em.getTransaction().begin(); + + DateIdTestEntity dite = new DateIdTestEntity(new Date(), "x"); + em.persist(dite); + + id1 = dite.getId(); + + em.getTransaction().commit(); + + // Revision 2 + em = getEntityManager(); + em.getTransaction().begin(); + + dite = em.find(DateIdTestEntity.class, id1); + dite.setStr1("y"); + + em.getTransaction().commit(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(DateIdTestEntity.class, id1)); + } + + @Test + public void testHistoryOfId1() { + DateIdTestEntity ver1 = new DateIdTestEntity(id1, "x"); + DateIdTestEntity ver2 = new DateIdTestEntity(id1, "y"); + + assert getAuditReader().find(DateIdTestEntity.class, id1, 1).getStr1().equals("x"); + assert getAuditReader().find(DateIdTestEntity.class, id1, 2).getStr1().equals("y"); + } +} \ No newline at end of file