PageRenderTime 167ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/reventity/CustomPropertyAccess.java

https://github.com/CodingFabian/hibernate-core
Java | 135 lines | 84 code | 23 blank | 28 comment | 4 complexity | c4388b4042efec3ae5b01b92904b38e2 MD5 | raw file
  1. /*
  2. * Hibernate, Relational Persistence for Idiomatic Java
  3. *
  4. * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
  5. * indicated by the @author tags or express copyright attribution
  6. * statements applied by the authors. All third-party contributions are
  7. * distributed under license by Red Hat Middleware LLC.
  8. *
  9. * This copyrighted material is made available to anyone wishing to use, modify,
  10. * copy, or redistribute it subject to the terms and conditions of the GNU
  11. * Lesser General Public License, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this distribution; if not, write to:
  20. * Free Software Foundation, Inc.
  21. * 51 Franklin Street, Fifth Floor
  22. * Boston, MA 02110-1301 USA
  23. */
  24. package org.hibernate.envers.test.integration.reventity;
  25. import org.hibernate.ejb.Ejb3Configuration;
  26. import org.hibernate.envers.AuditReader;
  27. import org.hibernate.envers.exception.RevisionDoesNotExistException;
  28. import org.hibernate.envers.test.AbstractEntityTest;
  29. import org.hibernate.envers.test.Priority;
  30. import org.hibernate.envers.test.entities.StrTestEntity;
  31. import org.hibernate.envers.test.entities.reventity.CustomPropertyAccessRevEntity;
  32. import org.junit.Test;
  33. import javax.persistence.EntityManager;
  34. import java.util.Arrays;
  35. import java.util.Date;
  36. /**
  37. * @author Adam Warski (adam at warski dot org)
  38. */
  39. public class CustomPropertyAccess extends AbstractEntityTest {
  40. private Integer id;
  41. private long timestamp1;
  42. private long timestamp2;
  43. private long timestamp3;
  44. public void configure(Ejb3Configuration cfg) {
  45. cfg.addAnnotatedClass(StrTestEntity.class);
  46. cfg.addAnnotatedClass(CustomPropertyAccessRevEntity.class);
  47. }
  48. @Test
  49. @Priority(10)
  50. public void initData() throws InterruptedException {
  51. timestamp1 = System.currentTimeMillis();
  52. Thread.sleep(100);
  53. // Revision 1
  54. EntityManager em = getEntityManager();
  55. em.getTransaction().begin();
  56. StrTestEntity te = new StrTestEntity("x");
  57. em.persist(te);
  58. id = te.getId();
  59. em.getTransaction().commit();
  60. timestamp2 = System.currentTimeMillis();
  61. Thread.sleep(100);
  62. // Revision 2
  63. em.getTransaction().begin();
  64. te = em.find(StrTestEntity.class, id);
  65. te.setStr("y");
  66. em.getTransaction().commit();
  67. timestamp3 = System.currentTimeMillis();
  68. }
  69. @Test(expected = RevisionDoesNotExistException.class)
  70. public void testTimestamps1() {
  71. getAuditReader().getRevisionNumberForDate(new Date(timestamp1));
  72. }
  73. @Test
  74. public void testTimestamps() {
  75. assert getAuditReader().getRevisionNumberForDate(new Date(timestamp2)).intValue() == 1;
  76. assert getAuditReader().getRevisionNumberForDate(new Date(timestamp3)).intValue() == 2;
  77. }
  78. @Test
  79. public void testDatesForRevisions() {
  80. AuditReader vr = getAuditReader();
  81. assert vr.getRevisionNumberForDate(vr.getRevisionDate(1)).intValue() == 1;
  82. assert vr.getRevisionNumberForDate(vr.getRevisionDate(2)).intValue() == 2;
  83. }
  84. @Test
  85. public void testRevisionsForDates() {
  86. AuditReader vr = getAuditReader();
  87. assert vr.getRevisionDate(vr.getRevisionNumberForDate(new Date(timestamp2))).getTime() <= timestamp2;
  88. assert vr.getRevisionDate(vr.getRevisionNumberForDate(new Date(timestamp2)).intValue()+1).getTime() > timestamp2;
  89. assert vr.getRevisionDate(vr.getRevisionNumberForDate(new Date(timestamp3))).getTime() <= timestamp3;
  90. }
  91. @Test
  92. public void testFindRevision() {
  93. AuditReader vr = getAuditReader();
  94. long rev1Timestamp = vr.findRevision(CustomPropertyAccessRevEntity.class, 1).getCustomTimestamp();
  95. assert rev1Timestamp > timestamp1;
  96. assert rev1Timestamp <= timestamp2;
  97. long rev2Timestamp = vr.findRevision(CustomPropertyAccessRevEntity.class, 2).getCustomTimestamp();
  98. assert rev2Timestamp > timestamp2;
  99. assert rev2Timestamp <= timestamp3;
  100. }
  101. @Test
  102. public void testRevisionsCounts() {
  103. assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(StrTestEntity.class, id));
  104. }
  105. @Test
  106. public void testHistoryOfId1() {
  107. StrTestEntity ver1 = new StrTestEntity("x", id);
  108. StrTestEntity ver2 = new StrTestEntity("y", id);
  109. assert getAuditReader().find(StrTestEntity.class, id, 1).equals(ver1);
  110. assert getAuditReader().find(StrTestEntity.class, id, 2).equals(ver2);
  111. }
  112. }