|
10 | 10 | import jakarta.persistence.Entity; |
11 | 11 | import jakarta.persistence.Id; |
12 | 12 |
|
13 | | -import org.hibernate.Session; |
| 13 | +import org.hibernate.envers.AuditReaderFactory; |
14 | 14 | import org.hibernate.envers.Audited; |
15 | 15 | import org.hibernate.envers.query.AuditEntity; |
16 | | -import org.hibernate.orm.test.envers.BaseEnversFunctionalTestCase; |
17 | | -import org.hibernate.orm.test.envers.Priority; |
18 | | -import org.junit.Test; |
19 | | - |
| 16 | +import org.hibernate.testing.envers.junit.EnversTest; |
| 17 | +import org.hibernate.testing.orm.junit.BeforeClassTemplate; |
| 18 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
20 | 19 | import org.hibernate.testing.orm.junit.JiraKey; |
| 20 | +import org.hibernate.testing.orm.junit.Jpa; |
| 21 | +import org.junit.jupiter.api.Test; |
21 | 22 |
|
22 | | -import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
23 | 24 |
|
24 | 25 | /** |
25 | 26 | * Test that an updated detached entity will still properly track {@code withModifiedFlag} |
|
29 | 30 | * @author Chris Cranford |
30 | 31 | */ |
31 | 32 | @JiraKey("HHH-8973") |
32 | | -public class DetachedEntityTest extends BaseEnversFunctionalTestCase { |
33 | | - @Override |
34 | | - protected Class[] getAnnotatedClasses() { |
35 | | - return new Class<?>[] { Project.class }; |
36 | | - } |
| 33 | +@EnversTest |
| 34 | +@Jpa(annotatedClasses = { DetachedEntityTest.Project.class }) |
| 35 | +public class DetachedEntityTest { |
37 | 36 |
|
38 | | - @Test |
39 | | - @Priority(10) |
40 | | - public void initData() { |
41 | | - final Session s = openSession(); |
42 | | - try { |
| 37 | + @BeforeClassTemplate |
| 38 | + public void initData(EntityManagerFactoryScope scope) { |
| 39 | + scope.inTransaction( em -> { |
43 | 40 | // revision 1 - persist the project entity |
44 | | - s.getTransaction().begin(); |
45 | 41 | final Project project = new Project( 1, "fooName" ); |
46 | | - s.persist( project ); |
47 | | - s.getTransaction().commit(); |
48 | | - |
49 | | - // detach the project entity |
50 | | - s.clear(); |
51 | | - |
52 | | - // revision 2 to 6 - update the detached project entity. |
53 | | - for( int i = 0; i < 5; ++i ) { |
54 | | - s.getTransaction().begin(); |
55 | | - project.setName( "fooName" + ( i + 2 ) ); |
56 | | - s.merge( project ); |
57 | | - s.getTransaction().commit(); |
58 | | - s.clear(); |
59 | | - } |
60 | | - } |
61 | | - catch ( Throwable t ) { |
62 | | - if ( s.getTransaction().isActive() ) { |
63 | | - s.getTransaction().rollback(); |
64 | | - } |
65 | | - throw t; |
66 | | - } |
67 | | - finally { |
68 | | - s.close(); |
| 42 | + em.persist( project ); |
| 43 | + } ); |
| 44 | + |
| 45 | + // revision 2 to 6 - update the detached project entity. |
| 46 | + for ( int i = 0; i < 5; ++i ) { |
| 47 | + final int index = i; |
| 48 | + scope.inTransaction( em -> { |
| 49 | + final Project project = em.find( Project.class, 1 ); |
| 50 | + em.detach( project ); |
| 51 | + project.setName( "fooName" + ( index + 2 ) ); |
| 52 | + em.merge( project ); |
| 53 | + } ); |
69 | 54 | } |
70 | 55 | } |
71 | 56 |
|
72 | 57 | @Test |
73 | | - public void testRevisionCounts() { |
74 | | - assertEquals( Arrays.asList( 1, 2, 3, 4, 5, 6 ), getAuditReader().getRevisions( Project.class, 1 ) ); |
| 58 | + public void testRevisionCounts(EntityManagerFactoryScope scope) { |
| 59 | + scope.inEntityManager( em -> { |
| 60 | + final var auditReader = AuditReaderFactory.get( em ); |
| 61 | + assertEquals( Arrays.asList( 1, 2, 3, 4, 5, 6 ), auditReader.getRevisions( Project.class, 1 ) ); |
| 62 | + } ); |
75 | 63 | } |
76 | 64 |
|
77 | 65 | @Test |
78 | | - public void testRevisionHistory() { |
79 | | - for ( Integer revision : Arrays.asList( 1, 2, 3, 4, 5, 6 ) ) { |
80 | | - final Project project = getAuditReader().find( Project.class, 1, revision ); |
81 | | - if ( revision == 1 ) { |
82 | | - assertEquals( new Project( 1, "fooName" ), project ); |
| 66 | + public void testRevisionHistory(EntityManagerFactoryScope scope) { |
| 67 | + scope.inEntityManager( em -> { |
| 68 | + final var auditReader = AuditReaderFactory.get( em ); |
| 69 | + for ( Integer revision : Arrays.asList( 1, 2, 3, 4, 5, 6 ) ) { |
| 70 | + final Project project = auditReader.find( Project.class, 1, revision ); |
| 71 | + if ( revision == 1 ) { |
| 72 | + assertEquals( new Project( 1, "fooName" ), project ); |
| 73 | + } |
| 74 | + else { |
| 75 | + assertEquals( new Project( 1, "fooName" + revision ), project ); |
| 76 | + } |
83 | 77 | } |
84 | | - else { |
85 | | - assertEquals( new Project( 1, "fooName" + revision ), project ); |
86 | | - } |
87 | | - } |
| 78 | + } ); |
88 | 79 | } |
89 | 80 |
|
90 | 81 | @Test |
91 | | - public void testModifiedFlagChangesForProjectType() { |
92 | | - final List results = getAuditReader().createQuery() |
93 | | - .forRevisionsOfEntity( Project.class, false, true ) |
94 | | - .add( AuditEntity.property( "type" ).hasChanged() ) |
95 | | - .addProjection( AuditEntity.revisionNumber() ) |
96 | | - .addOrder( AuditEntity.revisionNumber().asc() ) |
97 | | - .getResultList(); |
98 | | - assertEquals( Arrays.asList( 1 ), results ); |
| 82 | + public void testModifiedFlagChangesForProjectType(EntityManagerFactoryScope scope) { |
| 83 | + scope.inEntityManager( em -> { |
| 84 | + final var auditReader = AuditReaderFactory.get( em ); |
| 85 | + final List results = auditReader.createQuery() |
| 86 | + .forRevisionsOfEntity( Project.class, false, true ) |
| 87 | + .add( AuditEntity.property( "type" ).hasChanged() ) |
| 88 | + .addProjection( AuditEntity.revisionNumber() ) |
| 89 | + .addOrder( AuditEntity.revisionNumber().asc() ) |
| 90 | + .getResultList(); |
| 91 | + assertEquals( Arrays.asList( 1 ), results ); |
| 92 | + } ); |
99 | 93 | } |
100 | 94 |
|
101 | 95 | @Test |
102 | | - public void testModifiedFlagChangesForProjectName() { |
103 | | - final List results = getAuditReader().createQuery() |
104 | | - .forRevisionsOfEntity( Project.class, false, true ) |
105 | | - .add( AuditEntity.property( "name" ).hasChanged() ) |
106 | | - .addProjection( AuditEntity.revisionNumber() ) |
107 | | - .addOrder( AuditEntity.revisionNumber().asc() ) |
108 | | - .getResultList(); |
109 | | - assertEquals( Arrays.asList( 1, 2, 3, 4, 5, 6 ), results ); |
| 96 | + public void testModifiedFlagChangesForProjectName(EntityManagerFactoryScope scope) { |
| 97 | + scope.inEntityManager( em -> { |
| 98 | + final var auditReader = AuditReaderFactory.get( em ); |
| 99 | + final List results = auditReader.createQuery() |
| 100 | + .forRevisionsOfEntity( Project.class, false, true ) |
| 101 | + .add( AuditEntity.property( "name" ).hasChanged() ) |
| 102 | + .addProjection( AuditEntity.revisionNumber() ) |
| 103 | + .addOrder( AuditEntity.revisionNumber().asc() ) |
| 104 | + .getResultList(); |
| 105 | + assertEquals( Arrays.asList( 1, 2, 3, 4, 5, 6 ), results ); |
| 106 | + } ); |
110 | 107 | } |
111 | 108 |
|
112 | 109 | @Entity(name = "Project") |
|
0 commit comments