|
| 1 | +package org.hibernate.envers.test.integration.multiplerelations; |
| 2 | + |
| 3 | +import org.hibernate.ejb.Ejb3Configuration; |
| 4 | +import org.hibernate.envers.test.AbstractEntityTest; |
| 5 | +import org.hibernate.envers.test.Priority; |
| 6 | +import org.hibernate.envers.test.tools.TestTools; |
| 7 | +import org.hibernate.testing.TestForIssue; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import javax.persistence.EntityManager; |
| 12 | +import java.util.Arrays; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) |
| 16 | + */ |
| 17 | +@TestForIssue(jiraKey = "HHH-7073") |
| 18 | +public class MultipleAssociationsTest extends AbstractEntityTest { |
| 19 | + private long lukaszId = 0; |
| 20 | + private long kingaId = 0; |
| 21 | + private long warsawId = 0; |
| 22 | + private long cracowId = 0; |
| 23 | + |
| 24 | + @Override |
| 25 | + public void configure(Ejb3Configuration cfg) { |
| 26 | + cfg.addAnnotatedClass(Person.class); |
| 27 | + cfg.addAnnotatedClass(Address.class); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + @Priority(10) |
| 32 | + public void initData() { |
| 33 | + EntityManager em = getEntityManager(); |
| 34 | + |
| 35 | + // Revision 1 |
| 36 | + em.getTransaction().begin(); |
| 37 | + Person lukasz = new Person("Lukasz"); |
| 38 | + Person kinga = new Person("Kinga"); |
| 39 | + Address warsaw = new Address("Warsaw"); |
| 40 | + warsaw.getTenants().add(lukasz); |
| 41 | + warsaw.setLandlord(lukasz); |
| 42 | + warsaw.getTenants().add(kinga); |
| 43 | + lukasz.getAddresses().add(warsaw); |
| 44 | + lukasz.getOwnedAddresses().add(warsaw); |
| 45 | + kinga.getAddresses().add(warsaw); |
| 46 | + em.persist(lukasz); |
| 47 | + em.persist(kinga); |
| 48 | + em.persist(warsaw); |
| 49 | + em.getTransaction().commit(); |
| 50 | + |
| 51 | + lukaszId = lukasz.getId(); |
| 52 | + kingaId = kinga.getId(); |
| 53 | + warsawId = warsaw.getId(); |
| 54 | + |
| 55 | + // Revision 2 |
| 56 | + em.getTransaction().begin(); |
| 57 | + kinga = em.find(Person.class, kinga.getId()); |
| 58 | + Address cracow = new Address("Cracow"); |
| 59 | + kinga.getAddresses().add(cracow); |
| 60 | + cracow.getTenants().add(kinga); |
| 61 | + cracow.setLandlord(kinga); |
| 62 | + em.persist(cracow); |
| 63 | + em.getTransaction().commit(); |
| 64 | + |
| 65 | + cracowId = cracow.getId(); |
| 66 | + |
| 67 | + // Revision 3 |
| 68 | + em.getTransaction().begin(); |
| 69 | + cracow = em.find(Address.class, cracow.getId()); |
| 70 | + cracow.setCity("Krakow"); |
| 71 | + em.merge(cracow); |
| 72 | + em.getTransaction().commit(); |
| 73 | + |
| 74 | + // Revision 4 |
| 75 | + em.getTransaction().begin(); |
| 76 | + lukasz = em.find(Person.class, lukasz.getId()); |
| 77 | + lukasz.setName("Lucas"); |
| 78 | + em.merge(lukasz); |
| 79 | + em.getTransaction().commit(); |
| 80 | + |
| 81 | + // Revision 5 |
| 82 | + em.getTransaction().begin(); |
| 83 | + warsaw = em.find(Address.class, warsaw.getId()); |
| 84 | + lukasz = em.find(Person.class, lukasz.getId()); |
| 85 | + kinga = em.find(Person.class, kinga.getId()); |
| 86 | + warsaw.setLandlord(kinga); |
| 87 | + kinga.getOwnedAddresses().add(warsaw); |
| 88 | + lukasz.getOwnedAddresses().remove(warsaw); |
| 89 | + em.merge(warsaw); |
| 90 | + em.merge(lukasz); |
| 91 | + em.merge(kinga); |
| 92 | + em.getTransaction().commit(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testRevisionsCounts() { |
| 97 | + Assert.assertEquals(Arrays.asList(1, 4, 5), getAuditReader().getRevisions(Person.class, lukaszId)); |
| 98 | + Assert.assertEquals(Arrays.asList(1, 2, 5), getAuditReader().getRevisions(Person.class, kingaId)); |
| 99 | + Assert.assertEquals(Arrays.asList(1, 5), getAuditReader().getRevisions(Address.class, warsawId)); |
| 100 | + Assert.assertEquals(Arrays.asList(2, 3), getAuditReader().getRevisions(Address.class, cracowId)); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testHistoryOfLukasz() { |
| 105 | + Person lukasz = new Person("Lukasz", lukaszId); |
| 106 | + Address warsaw = new Address("Warsaw", warsawId); |
| 107 | + lukasz.getAddresses().add(warsaw); |
| 108 | + lukasz.getOwnedAddresses().add(warsaw); |
| 109 | + |
| 110 | + Person ver1 = getAuditReader().find(Person.class, lukaszId, 1); |
| 111 | + Assert.assertEquals(lukasz, ver1); |
| 112 | + Assert.assertEquals(lukasz.getAddresses(), ver1.getAddresses()); |
| 113 | + Assert.assertEquals(lukasz.getOwnedAddresses(), ver1.getOwnedAddresses()); |
| 114 | + |
| 115 | + lukasz.setName("Lucas"); |
| 116 | + |
| 117 | + Person ver4 = getAuditReader().find(Person.class, lukaszId, 4); |
| 118 | + Assert.assertEquals(lukasz, ver4); |
| 119 | + |
| 120 | + lukasz.getOwnedAddresses().remove(warsaw); |
| 121 | + |
| 122 | + Person ver5 = getAuditReader().find(Person.class, lukaszId, 5); |
| 123 | + Assert.assertEquals(lukasz.getOwnedAddresses(), ver5.getOwnedAddresses()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testHistoryOfKinga() { |
| 128 | + Person kinga = new Person("Kinga", kingaId); |
| 129 | + Address warsaw = new Address("Warsaw", warsawId); |
| 130 | + kinga.getAddresses().add(warsaw); |
| 131 | + |
| 132 | + Person ver1 = getAuditReader().find(Person.class, kingaId, 1); |
| 133 | + Assert.assertEquals(kinga, ver1); |
| 134 | + Assert.assertEquals(kinga.getAddresses(), ver1.getAddresses()); |
| 135 | + Assert.assertEquals(kinga.getOwnedAddresses(), ver1.getOwnedAddresses()); |
| 136 | + |
| 137 | + Address cracow = new Address("Cracow", cracowId); |
| 138 | + kinga.getOwnedAddresses().add(cracow); |
| 139 | + kinga.getAddresses().add(cracow); |
| 140 | + |
| 141 | + Person ver2 = getAuditReader().find(Person.class, kingaId, 2); |
| 142 | + Assert.assertEquals(kinga, ver2); |
| 143 | + Assert.assertEquals(kinga.getAddresses(), ver2.getAddresses()); |
| 144 | + Assert.assertEquals(kinga.getOwnedAddresses(), ver2.getOwnedAddresses()); |
| 145 | + |
| 146 | + kinga.getOwnedAddresses().add(warsaw); |
| 147 | + cracow.setCity("Krakow"); |
| 148 | + |
| 149 | + Person ver5 = getAuditReader().find(Person.class, kingaId, 5); |
| 150 | + Assert.assertEquals(TestTools.makeSet(kinga.getAddresses()), TestTools.makeSet(ver5.getAddresses())); |
| 151 | + Assert.assertEquals(TestTools.makeSet(kinga.getOwnedAddresses()), TestTools.makeSet(ver5.getOwnedAddresses())); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testHistoryOfCracow() { |
| 156 | + Address cracow = new Address("Cracow", cracowId); |
| 157 | + Person kinga = new Person("Kinga", kingaId); |
| 158 | + cracow.getTenants().add(kinga); |
| 159 | + cracow.setLandlord(kinga); |
| 160 | + |
| 161 | + Address ver2 = getAuditReader().find(Address.class, cracowId, 2); |
| 162 | + Assert.assertEquals(cracow, ver2); |
| 163 | + Assert.assertEquals(cracow.getTenants(), ver2.getTenants()); |
| 164 | + Assert.assertEquals(cracow.getLandlord().getId(), ver2.getLandlord().getId()); |
| 165 | + |
| 166 | + cracow.setCity("Krakow"); |
| 167 | + |
| 168 | + Address ver3 = getAuditReader().find(Address.class, cracowId, 3); |
| 169 | + Assert.assertEquals(cracow, ver3); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void testHistoryOfWarsaw() { |
| 174 | + Address warsaw = new Address("Warsaw", warsawId); |
| 175 | + Person kinga = new Person("Kinga", kingaId); |
| 176 | + Person lukasz = new Person("Lukasz", lukaszId); |
| 177 | + warsaw.getTenants().add(lukasz); |
| 178 | + warsaw.getTenants().add(kinga); |
| 179 | + warsaw.setLandlord(lukasz); |
| 180 | + |
| 181 | + Address ver1 = getAuditReader().find(Address.class, warsawId, 1); |
| 182 | + Assert.assertEquals(warsaw, ver1); |
| 183 | + Assert.assertEquals(warsaw.getTenants(), ver1.getTenants()); |
| 184 | + Assert.assertEquals(warsaw.getLandlord().getId(), ver1.getLandlord().getId()); |
| 185 | + |
| 186 | + warsaw.setLandlord(kinga); |
| 187 | + |
| 188 | + Address ver5 = getAuditReader().find(Address.class, warsawId, 5); |
| 189 | + Assert.assertEquals(warsaw.getLandlord().getId(), ver5.getLandlord().getId()); |
| 190 | + } |
| 191 | +} |
0 commit comments