|
5 | 5 | package org.hibernate.orm.test.envers.integration.hashcode; |
6 | 6 |
|
7 | 7 | import java.util.Arrays; |
8 | | -import jakarta.persistence.EntityManager; |
9 | 8 |
|
10 | | -import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; |
11 | | -import org.hibernate.orm.test.envers.Priority; |
| 9 | +import org.hibernate.envers.AuditReaderFactory; |
12 | 10 | import org.hibernate.orm.test.envers.tools.TestTools; |
| 11 | +import org.hibernate.testing.envers.junit.EnversTest; |
| 12 | +import org.hibernate.testing.orm.junit.BeforeClassTemplate; |
| 13 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 14 | +import org.hibernate.testing.orm.junit.Jpa; |
13 | 15 |
|
14 | | -import org.junit.Test; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNull; |
15 | 20 |
|
16 | 21 | /** |
17 | 22 | * @author Adam Warski (adam at warski dot org) |
18 | 23 | */ |
19 | | -public class ChangingHashcode extends BaseEnversJPAFunctionalTestCase { |
| 24 | +@EnversTest |
| 25 | +@Jpa(annotatedClasses = {WikiPage.class, WikiImage.class}) |
| 26 | +public class ChangingHashcode { |
20 | 27 | private Long pageId; |
21 | 28 | private Long imageId; |
22 | 29 |
|
23 | | - @Override |
24 | | - protected Class<?>[] getAnnotatedClasses() { |
25 | | - return new Class[] {WikiPage.class, WikiImage.class}; |
26 | | - } |
27 | | - |
28 | | - @Test |
29 | | - @Priority(10) |
30 | | - public void initData() { |
| 30 | + @BeforeClassTemplate |
| 31 | + public void initData(EntityManagerFactoryScope scope) { |
31 | 32 | // Revision 1 |
32 | | - EntityManager em = getEntityManager(); |
33 | | - em.getTransaction().begin(); |
34 | | - |
35 | | - WikiPage page = new WikiPage( "title", "content" ); |
36 | | - em.persist( page ); |
37 | | - |
38 | | - em.getTransaction().commit(); |
| 33 | + scope.inTransaction( em -> { |
| 34 | + WikiPage page = new WikiPage( "title", "content" ); |
| 35 | + em.persist( page ); |
| 36 | + pageId = page.getId(); |
| 37 | + } ); |
39 | 38 |
|
40 | 39 | // Revision 2 |
41 | | - em = getEntityManager(); |
42 | | - em.getTransaction().begin(); |
43 | | - |
44 | | - WikiImage image = new WikiImage( "name1" ); |
45 | | - em.persist( image ); |
| 40 | + scope.inTransaction( em -> { |
| 41 | + WikiImage image = new WikiImage( "name1" ); |
| 42 | + em.persist( image ); |
46 | 43 |
|
47 | | - page = em.find( WikiPage.class, page.getId() ); |
48 | | - page.getImages().add( image ); |
| 44 | + WikiPage page = em.find( WikiPage.class, pageId ); |
| 45 | + page.getImages().add( image ); |
49 | 46 |
|
50 | | - em.getTransaction().commit(); |
| 47 | + imageId = image.getId(); |
| 48 | + } ); |
51 | 49 |
|
52 | 50 | // Revision 3 |
53 | | - em = getEntityManager(); |
54 | | - em.getTransaction().begin(); |
55 | | - |
56 | | - image = em.find( WikiImage.class, image.getId() ); |
57 | | - image.setName( "name2" ); |
58 | | - |
59 | | - em.getTransaction().commit(); |
60 | | - |
61 | | - pageId = page.getId(); |
62 | | - imageId = image.getId(); |
| 51 | + scope.inTransaction( em -> { |
| 52 | + WikiImage image = em.find( WikiImage.class, imageId ); |
| 53 | + image.setName( "name2" ); |
| 54 | + } ); |
63 | 55 | } |
64 | 56 |
|
65 | 57 | @Test |
66 | | - public void testRevisionsCounts() { |
67 | | - assert Arrays.asList( 1, 2 ).equals( getAuditReader().getRevisions( WikiPage.class, pageId ) ); |
68 | | - assert Arrays.asList( 2, 3 ).equals( getAuditReader().getRevisions( WikiImage.class, imageId ) ); |
| 58 | + public void testRevisionsCounts(EntityManagerFactoryScope scope) { |
| 59 | + scope.inEntityManager( em -> { |
| 60 | + final var auditReader = AuditReaderFactory.get( em ); |
| 61 | + assertEquals( Arrays.asList( 1, 2 ), auditReader.getRevisions( WikiPage.class, pageId ) ); |
| 62 | + assertEquals( Arrays.asList( 2, 3 ), auditReader.getRevisions( WikiImage.class, imageId ) ); |
| 63 | + } ); |
69 | 64 | } |
70 | 65 |
|
71 | 66 | @Test |
72 | | - public void testHistoryOfImage() { |
73 | | - assert getAuditReader().find( WikiImage.class, imageId, 1 ) == null; |
74 | | - assert getAuditReader().find( WikiImage.class, imageId, 2 ).equals( new WikiImage( "name1" ) ); |
75 | | - assert getAuditReader().find( WikiImage.class, imageId, 3 ).equals( new WikiImage( "name2" ) ); |
| 67 | + public void testHistoryOfImage(EntityManagerFactoryScope scope) { |
| 68 | + scope.inEntityManager( em -> { |
| 69 | + final var auditReader = AuditReaderFactory.get( em ); |
| 70 | + assertNull( auditReader.find( WikiImage.class, imageId, 1 ) ); |
| 71 | + assertEquals( new WikiImage( "name1" ), auditReader.find( WikiImage.class, imageId, 2 ) ); |
| 72 | + assertEquals( new WikiImage( "name2" ), auditReader.find( WikiImage.class, imageId, 3 ) ); |
| 73 | + } ); |
76 | 74 | } |
77 | 75 |
|
78 | 76 | @Test |
79 | | - public void testHistoryOfPage() { |
80 | | - assert getAuditReader().find( WikiPage.class, pageId, 1 ).getImages().size() == 0; |
81 | | - assert getAuditReader().find( WikiPage.class, pageId, 2 ).getImages().equals( |
82 | | - TestTools.makeSet( |
83 | | - new WikiImage( |
84 | | - "name1" |
85 | | - ) |
86 | | - ) |
87 | | - ); |
88 | | - assert getAuditReader().find( WikiPage.class, pageId, 3 ).getImages().equals( |
89 | | - TestTools.makeSet( |
90 | | - new WikiImage( |
91 | | - "name2" |
92 | | - ) |
93 | | - ) |
94 | | - ); |
| 77 | + public void testHistoryOfPage(EntityManagerFactoryScope scope) { |
| 78 | + scope.inEntityManager( em -> { |
| 79 | + final var auditReader = AuditReaderFactory.get( em ); |
| 80 | + assertEquals( 0, auditReader.find( WikiPage.class, pageId, 1 ).getImages().size() ); |
| 81 | + assertEquals( |
| 82 | + TestTools.makeSet( new WikiImage( "name1" ) ), |
| 83 | + auditReader.find( WikiPage.class, pageId, 2 ).getImages() |
| 84 | + ); |
| 85 | + assertEquals( |
| 86 | + TestTools.makeSet( new WikiImage( "name2" ) ), |
| 87 | + auditReader.find( WikiPage.class, pageId, 3 ).getImages() |
| 88 | + ); |
| 89 | + } ); |
95 | 90 | } |
96 | 91 | } |
0 commit comments