Author: adamw
Date: 2008-10-13 10:43:11 -0400 (Mon, 13 Oct 2008)
New Revision: 177
Added:
trunk/src/test/org/jboss/envers/test/entities/reventity/CustomPropertyAccessRevEntity.java
trunk/src/test/org/jboss/envers/test/integration/reventity/CustomPropertyAccess.java
Modified:
trunk/src/main/org/jboss/envers/RevisionNumber.java
trunk/src/main/org/jboss/envers/RevisionTimestamp.java
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java
Log:
ENVERS-58: revision name and timestamp annotations on a custom revision entity also
allowed on getter methods + test
Modified: trunk/src/main/org/jboss/envers/RevisionNumber.java
===================================================================
--- trunk/src/main/org/jboss/envers/RevisionNumber.java 2008-10-13 12:27:53 UTC (rev 176)
+++ trunk/src/main/org/jboss/envers/RevisionNumber.java 2008-10-13 14:43:11 UTC (rev 177)
@@ -32,8 +32,9 @@
* of numbers. The value of this property won't be set by Envers. In most cases, this
should be
* an auto-generated database-assigned primary id.
* @author Adam Warski (adam at warski dot org)
+ * @author Sanne Grinovero
*/
@Retention(RetentionPolicy.RUNTIME)
-(a)Target(ElementType.FIELD)
+(a)Target({ElementType.METHOD, ElementType.FIELD})
public @interface RevisionNumber {
}
Modified: trunk/src/main/org/jboss/envers/RevisionTimestamp.java
===================================================================
--- trunk/src/main/org/jboss/envers/RevisionTimestamp.java 2008-10-13 12:27:53 UTC (rev
176)
+++ trunk/src/main/org/jboss/envers/RevisionTimestamp.java 2008-10-13 14:43:11 UTC (rev
177)
@@ -30,8 +30,9 @@
* Marks a property which will hold the timestamp of the revision in a revision entity,
see
* {@link RevisionListener}. The value of this property will be automatically set by
Envers.
* @author Adam Warski (adam at warski dot org)
+ * @author Sanne Grinovero
*/
@Retention(RetentionPolicy.RUNTIME)
-(a)Target(ElementType.FIELD)
+(a)Target({ElementType.METHOD, ElementType.FIELD})
public @interface RevisionTimestamp {
}
Modified: trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-10-13
12:27:53 UTC (rev 176)
+++
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-10-13
14:43:11 UTC (rev 177)
@@ -87,14 +87,10 @@
return rev_rel_mapping;
}
- private void searchForRevisionInfoCfg(YClass clazz, YReflectionManager
reflectionManager,
- MutableBoolean revisionNumberFound,
MutableBoolean revisionTimestampFound) {
- YClass superclazz = clazz.getSuperclass();
- if (!"java.lang.Object".equals(superclazz.getName())) {
- searchForRevisionInfoCfg(superclazz, reflectionManager, revisionNumberFound,
revisionTimestampFound);
- }
-
- for (YProperty property : clazz.getDeclaredProperties("field")) {
+ private void searchForRevisionInfoCfgInProperties(YClass clazz, YReflectionManager
reflectionManager,
+ MutableBoolean revisionNumberFound, MutableBoolean
revisionTimestampFound,
+ String accessType) {
+ for (YProperty property : clazz.getDeclaredProperties(accessType)) {
RevisionNumber revisionNumber =
property.getAnnotation(RevisionNumber.class);
RevisionTimestamp revisionTimestamp =
property.getAnnotation(RevisionTimestamp.class);
@@ -139,6 +135,19 @@
}
}
+ private void searchForRevisionInfoCfg(YClass clazz, YReflectionManager
reflectionManager,
+ MutableBoolean revisionNumberFound,
MutableBoolean revisionTimestampFound) {
+ YClass superclazz = clazz.getSuperclass();
+ if (!"java.lang.Object".equals(superclazz.getName())) {
+ searchForRevisionInfoCfg(superclazz, reflectionManager, revisionNumberFound,
revisionTimestampFound);
+ }
+
+ searchForRevisionInfoCfgInProperties(clazz, reflectionManager,
revisionNumberFound, revisionTimestampFound,
+ "field");
+ searchForRevisionInfoCfgInProperties(clazz, reflectionManager,
revisionNumberFound, revisionTimestampFound,
+ "property");
+ }
+
@SuppressWarnings({"unchecked"})
public RevisionInfoConfigurationResult configure(Configuration cfg,
YReflectionManager reflectionManager) {
Iterator<PersistentClass> classes = (Iterator<PersistentClass>)
cfg.getClassMappings();
Copied:
trunk/src/test/org/jboss/envers/test/entities/reventity/CustomPropertyAccessRevEntity.java
(from rev 173,
trunk/src/test/org/jboss/envers/test/entities/reventity/CustomRevEntity.java)
===================================================================
---
trunk/src/test/org/jboss/envers/test/entities/reventity/CustomPropertyAccessRevEntity.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/entities/reventity/CustomPropertyAccessRevEntity.java 2008-10-13
14:43:11 UTC (rev 177)
@@ -0,0 +1,59 @@
+package org.jboss.envers.test.entities.reventity;
+
+import org.jboss.envers.RevisionNumber;
+import org.jboss.envers.RevisionTimestamp;
+import org.jboss.envers.RevisionEntity;
+
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Entity;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+@RevisionEntity
+public class CustomPropertyAccessRevEntity {
+ private int customId;
+
+ private long customTimestamp;
+
+ @Id
+ @GeneratedValue
+ @RevisionNumber
+ public int getCustomId() {
+ return customId;
+ }
+
+ public void setCustomId(int customId) {
+ this.customId = customId;
+ }
+
+ @RevisionTimestamp
+ public long getCustomTimestamp() {
+ return customTimestamp;
+ }
+
+ public void setCustomTimestamp(long customTimestamp) {
+ this.customTimestamp = customTimestamp;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof CustomPropertyAccessRevEntity)) return false;
+
+ CustomPropertyAccessRevEntity that = (CustomPropertyAccessRevEntity) o;
+
+ if (customId != that.customId) return false;
+ if (customTimestamp != that.customTimestamp) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = customId;
+ result = 31 * result + (int) (customTimestamp ^ (customTimestamp >>>
32));
+ return result;
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/test/org/jboss/envers/test/entities/reventity/CustomPropertyAccessRevEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied:
trunk/src/test/org/jboss/envers/test/integration/reventity/CustomPropertyAccess.java (from
rev 174, trunk/src/test/org/jboss/envers/test/integration/reventity/Custom.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/reventity/CustomPropertyAccess.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/reventity/CustomPropertyAccess.java 2008-10-13
14:43:11 UTC (rev 177)
@@ -0,0 +1,111 @@
+package org.jboss.envers.test.integration.reventity;
+
+import org.jboss.envers.test.AbstractEntityTest;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.jboss.envers.test.entities.reventity.CustomPropertyAccessRevEntity;
+import org.jboss.envers.exception.RevisionDoesNotExistException;
+import org.jboss.envers.VersionsReader;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Date;
+import java.util.Arrays;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class CustomPropertyAccess extends AbstractEntityTest {
+ private Integer id;
+ private long timestamp1;
+ private long timestamp2;
+ private long timestamp3;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ cfg.addAnnotatedClass(CustomPropertyAccessRevEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() throws InterruptedException {
+ timestamp1 = System.currentTimeMillis();
+
+ Thread.sleep(100);
+
+ // Revision 1
+ EntityManager em = getEntityManager();
+ em.getTransaction().begin();
+ StrTestEntity te = new StrTestEntity("x");
+ em.persist(te);
+ id = te.getId();
+ em.getTransaction().commit();
+
+ timestamp2 = System.currentTimeMillis();
+
+ Thread.sleep(100);
+
+ // Revision 2
+ em.getTransaction().begin();
+ te = em.find(StrTestEntity.class, id);
+ te.setStr("y");
+ em.getTransaction().commit();
+
+ timestamp3 = System.currentTimeMillis();
+ }
+
+ @Test(expectedExceptions = RevisionDoesNotExistException.class)
+ public void testTimestamps1() {
+ getVersionsReader().getRevisionNumberForDate(new Date(timestamp1));
+ }
+
+ @Test
+ public void testTimestamps() {
+ assert getVersionsReader().getRevisionNumberForDate(new
Date(timestamp2)).intValue() == 1;
+ assert getVersionsReader().getRevisionNumberForDate(new
Date(timestamp3)).intValue() == 2;
+ }
+
+ @Test
+ public void testDatesForRevisions() {
+ VersionsReader vr = getVersionsReader();
+ assert vr.getRevisionNumberForDate(vr.getRevisionDate(1)).intValue() == 1;
+ assert vr.getRevisionNumberForDate(vr.getRevisionDate(2)).intValue() == 2;
+ }
+
+ @Test
+ public void testRevisionsForDates() {
+ VersionsReader vr = getVersionsReader();
+
+ assert vr.getRevisionDate(vr.getRevisionNumberForDate(new
Date(timestamp2))).getTime() <= timestamp2;
+ assert vr.getRevisionDate(vr.getRevisionNumberForDate(new
Date(timestamp2)).intValue()+1).getTime() > timestamp2;
+
+ assert vr.getRevisionDate(vr.getRevisionNumberForDate(new
Date(timestamp3))).getTime() <= timestamp3;
+ }
+
+ @Test
+ public void testFindRevision() {
+ VersionsReader vr = getVersionsReader();
+
+ long rev1Timestamp = vr.findRevision(CustomPropertyAccessRevEntity.class,
1).getCustomTimestamp();
+ assert rev1Timestamp > timestamp1;
+ assert rev1Timestamp <= timestamp2;
+
+ long rev2Timestamp = vr.findRevision(CustomPropertyAccessRevEntity.class,
2).getCustomTimestamp();
+ assert rev2Timestamp > timestamp2;
+ assert rev2Timestamp <= timestamp3;
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1,
2).equals(getVersionsReader().getRevisions(StrTestEntity.class, id));
+ }
+
+ @Test
+ public void testHistoryOfId1() {
+ StrTestEntity ver1 = new StrTestEntity("x", id);
+ StrTestEntity ver2 = new StrTestEntity("y", id);
+
+ assert getVersionsReader().find(StrTestEntity.class, id, 1).equals(ver1);
+ assert getVersionsReader().find(StrTestEntity.class, id, 2).equals(ver2);
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/test/org/jboss/envers/test/integration/reventity/CustomPropertyAccess.java
___________________________________________________________________
Name: svn:mergeinfo
+