/frontend/lib/src/hibernate-distribution-3.6.7.Final/project/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetomany/detached/IndexedJoinColumnBidirectionalList.java

https://github.com/masteinhauser/connectedkitchen · Java · 250 lines · 165 code · 52 blank · 33 comment · 0 complexity · 9d3436b2ea391f51146de66506c49d0d 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.onetomany.detached;
  25. import org.hibernate.ejb.Ejb3Configuration;
  26. import org.hibernate.envers.test.AbstractEntityTest;
  27. import org.hibernate.envers.test.entities.onetomany.detached.IndexedListJoinColumnBidirectionalRefIngEntity;
  28. import org.hibernate.envers.test.entities.onetomany.detached.IndexedListJoinColumnBidirectionalRefEdEntity;
  29. import static org.testng.Assert.assertEquals;
  30. import static org.testng.Assert.assertTrue;
  31. import org.testng.annotations.Test;
  32. import javax.persistence.EntityManager;
  33. import java.util.Arrays;
  34. /**
  35. * Test for a "fake" bidirectional mapping where one side uses @OneToMany+@JoinColumn (and thus owns the relatin),
  36. * and the other uses a @ManyToOne(insertable=false, updatable=false).
  37. * @author Adam Warski (adam at warski dot org)
  38. */
  39. public class IndexedJoinColumnBidirectionalList extends AbstractEntityTest {
  40. private Integer ed1_id;
  41. private Integer ed2_id;
  42. private Integer ed3_id;
  43. private Integer ing1_id;
  44. private Integer ing2_id;
  45. public void configure(Ejb3Configuration cfg) {
  46. cfg.addAnnotatedClass(IndexedListJoinColumnBidirectionalRefIngEntity.class);
  47. cfg.addAnnotatedClass(IndexedListJoinColumnBidirectionalRefEdEntity.class);
  48. }
  49. @Test(enabled = true)
  50. public void createData() {
  51. EntityManager em = getEntityManager();
  52. IndexedListJoinColumnBidirectionalRefEdEntity ed1 = new IndexedListJoinColumnBidirectionalRefEdEntity("ed1", null);
  53. IndexedListJoinColumnBidirectionalRefEdEntity ed2 = new IndexedListJoinColumnBidirectionalRefEdEntity("ed2", null);
  54. IndexedListJoinColumnBidirectionalRefEdEntity ed3 = new IndexedListJoinColumnBidirectionalRefEdEntity("ed3", null);
  55. IndexedListJoinColumnBidirectionalRefIngEntity ing1 = new IndexedListJoinColumnBidirectionalRefIngEntity("coll1", ed1, ed2, ed3);
  56. IndexedListJoinColumnBidirectionalRefIngEntity ing2 = new IndexedListJoinColumnBidirectionalRefIngEntity("coll1");
  57. // Revision 1 (ing1: ed1, ed2, ed3)
  58. em.getTransaction().begin();
  59. em.persist(ed1);
  60. em.persist(ed2);
  61. em.persist(ed3);
  62. em.persist(ing1);
  63. em.persist(ing2);
  64. em.getTransaction().commit();
  65. // Revision 2 (ing1: ed1, ed3, ing2: ed2)
  66. em.getTransaction().begin();
  67. ing1 = em.find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1.getId());
  68. ing2 = em.find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing2.getId());
  69. ed2 = em.find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2.getId());
  70. ing1.getReferences().remove(ed2);
  71. ing2.getReferences().add(ed2);
  72. em.getTransaction().commit();
  73. em.clear();
  74. // Revision 3 (ing1: ed3, ed1, ing2: ed2)
  75. em.getTransaction().begin();
  76. ing1 = em.find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1.getId());
  77. ing2 = em.find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing2.getId());
  78. ed1 = em.find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed1.getId());
  79. ed2 = em.find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2.getId());
  80. ed3 = em.find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed3.getId());
  81. ing1.getReferences().remove(ed3);
  82. ing1.getReferences().add(0, ed3);
  83. em.getTransaction().commit();
  84. em.clear();
  85. // Revision 4 (ing1: ed2, ed3, ed1)
  86. em.getTransaction().begin();
  87. ing1 = em.find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1.getId());
  88. ing2 = em.find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing2.getId());
  89. ed1 = em.find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed1.getId());
  90. ed2 = em.find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2.getId());
  91. ed3 = em.find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed3.getId());
  92. ing2.getReferences().remove(ed2);
  93. ing1.getReferences().add(0, ed2);
  94. em.getTransaction().commit();
  95. em.clear();
  96. //
  97. ing1_id = ing1.getId();
  98. ing2_id = ing2.getId();
  99. ed1_id = ed1.getId();
  100. ed2_id = ed2.getId();
  101. ed3_id = ed3.getId();
  102. }
  103. @Test(enabled = true, dependsOnMethods = "createData")
  104. public void testRevisionsCounts() {
  105. assertEquals(Arrays.asList(1, 2, 3, 4), getAuditReader().getRevisions(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1_id));
  106. assertEquals(Arrays.asList(1, 2, 4), getAuditReader().getRevisions(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing2_id));
  107. assertEquals(Arrays.asList(1, 3, 4), getAuditReader().getRevisions(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed1_id));
  108. assertEquals(Arrays.asList(1, 2, 4), getAuditReader().getRevisions(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2_id));
  109. assertEquals(Arrays.asList(1, 2, 3, 4), getAuditReader().getRevisions(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed3_id));
  110. }
  111. @Test(enabled = true, dependsOnMethods = "createData")
  112. public void testHistoryOfIng1() {
  113. IndexedListJoinColumnBidirectionalRefEdEntity ed1 = getEntityManager().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed1_id);
  114. IndexedListJoinColumnBidirectionalRefEdEntity ed2 = getEntityManager().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2_id);
  115. IndexedListJoinColumnBidirectionalRefEdEntity ed3 = getEntityManager().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed3_id);
  116. IndexedListJoinColumnBidirectionalRefIngEntity rev1 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1_id, 1);
  117. IndexedListJoinColumnBidirectionalRefIngEntity rev2 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1_id, 2);
  118. IndexedListJoinColumnBidirectionalRefIngEntity rev3 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1_id, 3);
  119. IndexedListJoinColumnBidirectionalRefIngEntity rev4 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1_id, 4);
  120. assertEquals(rev1.getReferences().size(), 3);
  121. assertEquals(rev1.getReferences().get(0), ed1);
  122. assertEquals(rev1.getReferences().get(1), ed2);
  123. assertEquals(rev1.getReferences().get(2), ed3);
  124. assertEquals(rev2.getReferences().size(), 2);
  125. assertEquals(rev2.getReferences().get(0), ed1);
  126. assertEquals(rev2.getReferences().get(1), ed3);
  127. assertEquals(rev3.getReferences().size(), 2);
  128. assertEquals(rev3.getReferences().get(0), ed3);
  129. assertEquals(rev3.getReferences().get(1), ed1);
  130. assertEquals(rev4.getReferences().size(), 3);
  131. assertEquals(rev4.getReferences().get(0), ed2);
  132. assertEquals(rev4.getReferences().get(1), ed3);
  133. assertEquals(rev4.getReferences().get(2), ed1);
  134. }
  135. @Test(enabled = true, dependsOnMethods = "createData")
  136. public void testHistoryOfIng2() {
  137. IndexedListJoinColumnBidirectionalRefEdEntity ed2 = getEntityManager().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2_id);
  138. IndexedListJoinColumnBidirectionalRefIngEntity rev1 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing2_id, 1);
  139. IndexedListJoinColumnBidirectionalRefIngEntity rev2 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing2_id, 2);
  140. IndexedListJoinColumnBidirectionalRefIngEntity rev3 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing2_id, 3);
  141. IndexedListJoinColumnBidirectionalRefIngEntity rev4 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing2_id, 4);
  142. assertEquals(rev1.getReferences().size(), 0);
  143. assertEquals(rev2.getReferences().size(), 1);
  144. assertEquals(rev2.getReferences().get(0), ed2);
  145. assertEquals(rev3.getReferences().size(), 1);
  146. assertEquals(rev3.getReferences().get(0), ed2);
  147. assertEquals(rev4.getReferences().size(), 0);
  148. }
  149. @Test(enabled = true, dependsOnMethods = "createData")
  150. public void testHistoryOfEd1() {
  151. IndexedListJoinColumnBidirectionalRefIngEntity ing1 = getEntityManager().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1_id);
  152. IndexedListJoinColumnBidirectionalRefEdEntity rev1 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed1_id, 1);
  153. IndexedListJoinColumnBidirectionalRefEdEntity rev2 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed1_id, 2);
  154. IndexedListJoinColumnBidirectionalRefEdEntity rev3 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed1_id, 3);
  155. IndexedListJoinColumnBidirectionalRefEdEntity rev4 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed1_id, 4);
  156. assertTrue(rev1.getOwner().equals(ing1));
  157. assertTrue(rev2.getOwner().equals(ing1));
  158. assertTrue(rev3.getOwner().equals(ing1));
  159. assertTrue(rev4.getOwner().equals(ing1));
  160. assertEquals(rev1.getPosition(), new Integer(0));
  161. assertEquals(rev2.getPosition(), new Integer(0));
  162. assertEquals(rev3.getPosition(), new Integer(1));
  163. assertEquals(rev4.getPosition(), new Integer(2));
  164. }
  165. @Test(enabled = true, dependsOnMethods = "createData")
  166. public void testHistoryOfEd2() {
  167. IndexedListJoinColumnBidirectionalRefIngEntity ing1 = getEntityManager().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1_id);
  168. IndexedListJoinColumnBidirectionalRefIngEntity ing2 = getEntityManager().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing2_id);
  169. IndexedListJoinColumnBidirectionalRefEdEntity rev1 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2_id, 1);
  170. IndexedListJoinColumnBidirectionalRefEdEntity rev2 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2_id, 2);
  171. IndexedListJoinColumnBidirectionalRefEdEntity rev3 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2_id, 3);
  172. IndexedListJoinColumnBidirectionalRefEdEntity rev4 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed2_id, 4);
  173. assertTrue(rev1.getOwner().equals(ing1));
  174. assertTrue(rev2.getOwner().equals(ing2));
  175. assertTrue(rev3.getOwner().equals(ing2));
  176. assertTrue(rev4.getOwner().equals(ing1));
  177. assertEquals(rev1.getPosition(), new Integer(1));
  178. assertEquals(rev2.getPosition(), new Integer(0));
  179. assertEquals(rev3.getPosition(), new Integer(0));
  180. assertEquals(rev4.getPosition(), new Integer(0));
  181. }
  182. @Test(enabled = true, dependsOnMethods = "createData")
  183. public void testHistoryOfEd3() {
  184. IndexedListJoinColumnBidirectionalRefIngEntity ing1 = getEntityManager().find(IndexedListJoinColumnBidirectionalRefIngEntity.class, ing1_id);
  185. IndexedListJoinColumnBidirectionalRefEdEntity rev1 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed3_id, 1);
  186. IndexedListJoinColumnBidirectionalRefEdEntity rev2 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed3_id, 2);
  187. IndexedListJoinColumnBidirectionalRefEdEntity rev3 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed3_id, 3);
  188. IndexedListJoinColumnBidirectionalRefEdEntity rev4 = getAuditReader().find(IndexedListJoinColumnBidirectionalRefEdEntity.class, ed3_id, 4);
  189. assertTrue(rev1.getOwner().equals(ing1));
  190. assertTrue(rev2.getOwner().equals(ing1));
  191. assertTrue(rev3.getOwner().equals(ing1));
  192. assertTrue(rev4.getOwner().equals(ing1));
  193. assertEquals(rev1.getPosition(), new Integer(2));
  194. assertEquals(rev2.getPosition(), new Integer(1));
  195. assertEquals(rev3.getPosition(), new Integer(0));
  196. assertEquals(rev4.getPosition(), new Integer(1));
  197. }
  198. }