From 420a0b8e92909a6a820aa2f79afe4ea534d1bdc6 Mon Sep 17 00:00:00 2001 From: Adam Warski Date: Wed, 10 Feb 2010 15:38:27 +0000 Subject: [PATCH] =?UTF-8?q?HHH-4731:=20-=20a=20simple=20API=20to=20check?= =?UTF-8?q?=20if=20an=20entity=20is=20audited=20or=20not=20-=20applying=20?= =?UTF-8?q?patch=20by=20Hern=C3=A1n=20Chanfreau=20-=20thanks!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18764 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../org/hibernate/envers/AuditReader.java | 9 ++ .../envers/reader/AuditReaderImpl.java | 9 ++ .../auditReader/AuditReaderAPITest.java | 88 +++++++++++++++++++ .../auditReader/AuditedTestEntity.java | 88 +++++++++++++++++++ .../auditReader/NotAuditedTestEntity.java | 85 ++++++++++++++++++ envers/src/test/resources/testng.xml | 1 + 6 files changed, 280 insertions(+) create mode 100644 envers/src/test/java/org/hibernate/envers/test/integration/auditReader/AuditReaderAPITest.java create mode 100644 envers/src/test/java/org/hibernate/envers/test/integration/auditReader/AuditedTestEntity.java create mode 100644 envers/src/test/java/org/hibernate/envers/test/integration/auditReader/NotAuditedTestEntity.java diff --git a/envers/src/main/java/org/hibernate/envers/AuditReader.java b/envers/src/main/java/org/hibernate/envers/AuditReader.java index 5b353c610b..f8a125cb6e 100644 --- a/envers/src/main/java/org/hibernate/envers/AuditReader.java +++ b/envers/src/main/java/org/hibernate/envers/AuditReader.java @@ -32,6 +32,7 @@ import org.hibernate.envers.query.AuditQueryCreator; /** * @author Adam Warski (adam at warski dot org) + * @author Hernan Chanfreau */ public interface AuditReader { /** @@ -121,4 +122,12 @@ public interface AuditReader { * is closed. */ AuditQueryCreator createQuery(); + + /** + * Checks if the entityClass was configured to be audited. + * + * @param entityClass Class of the entity asking for audit support + * @return true if the entityClass is audited. + */ + boolean isEntityClassAudited(Class entityClass); } diff --git a/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java b/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java index 1165998dea..7b38b1cd99 100644 --- a/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java +++ b/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java @@ -45,6 +45,7 @@ import org.hibernate.engine.SessionImplementor; /** * @author Adam Warski (adam at warski dot org) + * @author Hernan Chanfreau */ public class AuditReaderImpl implements AuditReaderImplementor { private final AuditConfiguration verCfg; @@ -209,4 +210,12 @@ public class AuditReaderImpl implements AuditReaderImplementor { public AuditQueryCreator createQuery() { return new AuditQueryCreator(verCfg, this); } + + public boolean isEntityClassAudited(Class entityClass) { + checkNotNull(entityClass, "Entity class"); + checkSession(); + + String entityName = entityClass.getName(); + return (verCfg.getEntCfg().isVersioned(entityName)); + } } diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/auditReader/AuditReaderAPITest.java b/envers/src/test/java/org/hibernate/envers/test/integration/auditReader/AuditReaderAPITest.java new file mode 100644 index 0000000000..9ccd0316cd --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/auditReader/AuditReaderAPITest.java @@ -0,0 +1,88 @@ +/* + * 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.auditReader; + +import java.util.Arrays; + +import javax.persistence.EntityManager; + +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.envers.exception.NotAuditedException; +import org.hibernate.envers.test.AbstractEntityTest; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * A test which checks the correct behavior of AuditReader.isEntityClassAudited(Class entityClass). + * + * @author Hernan Chanfreau + */ +public class AuditReaderAPITest extends AbstractEntityTest { + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(AuditedTestEntity.class); + cfg.addAnnotatedClass(NotAuditedTestEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + em.getTransaction().begin(); + AuditedTestEntity ent1 = new AuditedTestEntity(1, "str1"); + NotAuditedTestEntity ent2 = new NotAuditedTestEntity(1, "str1"); + + em.persist(ent1); + em.persist(ent2); + em.getTransaction().commit(); + + em.getTransaction().begin(); + + ent1 = em.find(AuditedTestEntity.class, 1); + ent2 = em.find(NotAuditedTestEntity.class, 1); + ent1.setStr1("str2"); + ent2.setStr1("str2"); + em.getTransaction().commit(); + } + + @Test + public void testIsEntityClassAuditedForAuditedEntity() { + assert getAuditReader().isEntityClassAudited(AuditedTestEntity.class); + + assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(AuditedTestEntity.class, 1)); + } + + @Test + public void testIsEntityClassAuditedForNotAuditedEntity() { + + assert !getAuditReader().isEntityClassAudited(NotAuditedTestEntity.class); + + try { + getAuditReader().getRevisions(NotAuditedTestEntity.class, 1); + } catch (NotAuditedException nae) { + // it's ok because the entity is not audited + assert true; + } + } + + +} diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/auditReader/AuditedTestEntity.java b/envers/src/test/java/org/hibernate/envers/test/integration/auditReader/AuditedTestEntity.java new file mode 100644 index 0000000000..67d29c3624 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/auditReader/AuditedTestEntity.java @@ -0,0 +1,88 @@ +/* + * 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.auditReader; + +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.envers.Audited; + +/** + * @author Hernan Chanfreau + */ +@Entity +public class AuditedTestEntity { + @Id + private Integer id; + + @Audited + private String str1; + + public AuditedTestEntity() { + } + + public AuditedTestEntity(String str1) { + this.str1 = str1; + } + + public AuditedTestEntity(Integer id, String str1) { + this.id = id; + this.str1 = str1; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getStr1() { + return str1; + } + + public void setStr1(String str1) { + this.str1 = str1; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof AuditedTestEntity)) return false; + + AuditedTestEntity that = (AuditedTestEntity) 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; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (str1 != null ? str1.hashCode() : 0); + return result; + } +} diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/auditReader/NotAuditedTestEntity.java b/envers/src/test/java/org/hibernate/envers/test/integration/auditReader/NotAuditedTestEntity.java new file mode 100644 index 0000000000..637214732a --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/auditReader/NotAuditedTestEntity.java @@ -0,0 +1,85 @@ +/* + * 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.auditReader; + +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author Adam Warski (adam at warski dot org) + */ +@Entity +public class NotAuditedTestEntity { + @Id + private Integer id; + + private String str1; + + public NotAuditedTestEntity() { + } + + public NotAuditedTestEntity(String str1) { + this.str1 = str1; + } + + public NotAuditedTestEntity(Integer id, String str1) { + this.id = id; + this.str1 = str1; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getStr1() { + return str1; + } + + public void setStr1(String str1) { + this.str1 = str1; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof NotAuditedTestEntity)) return false; + + NotAuditedTestEntity that = (NotAuditedTestEntity) 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; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (str1 != null ? str1.hashCode() : 0); + return result; + } +} diff --git a/envers/src/test/resources/testng.xml b/envers/src/test/resources/testng.xml index ab42b246ca..51bed68b93 100644 --- a/envers/src/test/resources/testng.xml +++ b/envers/src/test/resources/testng.xml @@ -4,6 +4,7 @@ +