Skip to content

Commit 6c36fdb

Browse files
committed
HHH-19871 --wip-- Envers migration to JUnit 6
- `org.hibernate.orm.test.envers.integration.inheritance`
1 parent d3a5d0b commit 6c36fdb

27 files changed

+1336
-1211
lines changed

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/ChildAuditing.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,73 @@
55
package org.hibernate.orm.test.envers.integration.inheritance.joined;
66

77
import java.util.Arrays;
8-
import jakarta.persistence.EntityManager;
98

10-
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
11-
import org.hibernate.orm.test.envers.Priority;
9+
import org.hibernate.envers.AuditReaderFactory;
10+
import org.hibernate.testing.envers.junit.EnversTest;
11+
import org.hibernate.testing.orm.junit.BeforeClassTemplate;
12+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
13+
import org.hibernate.testing.orm.junit.Jpa;
1214

13-
import org.junit.Test;
15+
import org.junit.jupiter.api.Test;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
1418

1519
/**
1620
* @author Adam Warski (adam at warski dot org)
1721
*/
18-
public class ChildAuditing extends BaseEnversJPAFunctionalTestCase {
22+
@EnversTest
23+
@Jpa(annotatedClasses = {ChildEntity.class, ParentEntity.class})
24+
public class ChildAuditing {
1925
private Integer id1;
2026

21-
@Override
22-
protected Class<?>[] getAnnotatedClasses() {
23-
return new Class[] {ChildEntity.class, ParentEntity.class};
24-
}
25-
26-
@Test
27-
@Priority(10)
28-
public void initData() {
29-
EntityManager em = getEntityManager();
30-
27+
@BeforeClassTemplate
28+
public void initData(EntityManagerFactoryScope scope) {
3129
id1 = 1;
3230

3331
// Rev 1
34-
em.getTransaction().begin();
35-
ChildEntity ce = new ChildEntity( id1, "x", 1l );
36-
em.persist( ce );
37-
em.getTransaction().commit();
32+
scope.inTransaction( em -> {
33+
ChildEntity ce = new ChildEntity( id1, "x", 1l );
34+
em.persist( ce );
35+
} );
3836

3937
// Rev 2
40-
em.getTransaction().begin();
41-
ce = em.find( ChildEntity.class, id1 );
42-
ce.setData( "y" );
43-
ce.setNumVal( 2l );
44-
em.getTransaction().commit();
38+
scope.inTransaction( em -> {
39+
ChildEntity ce = em.find( ChildEntity.class, id1 );
40+
ce.setData( "y" );
41+
ce.setNumVal( 2l );
42+
} );
4543
}
4644

4745
@Test
48-
public void testRevisionsCounts() {
49-
assert Arrays.asList( 1, 2 ).equals( getAuditReader().getRevisions( ChildEntity.class, id1 ) );
46+
public void testRevisionsCounts(EntityManagerFactoryScope scope) {
47+
scope.inEntityManager( em -> {
48+
assertEquals( Arrays.asList( 1, 2 ), AuditReaderFactory.get( em ).getRevisions( ChildEntity.class, id1 ) );
49+
} );
5050
}
5151

5252
@Test
53-
public void testHistoryOfChildId1() {
53+
public void testHistoryOfChildId1(EntityManagerFactoryScope scope) {
5454
ChildEntity ver1 = new ChildEntity( id1, "x", 1l );
5555
ChildEntity ver2 = new ChildEntity( id1, "y", 2l );
5656

57-
assert getAuditReader().find( ChildEntity.class, id1, 1 ).equals( ver1 );
58-
assert getAuditReader().find( ChildEntity.class, id1, 2 ).equals( ver2 );
57+
scope.inEntityManager( em -> {
58+
var auditReader = AuditReaderFactory.get( em );
59+
assertEquals( ver1, auditReader.find( ChildEntity.class, id1, 1 ) );
60+
assertEquals( ver2, auditReader.find( ChildEntity.class, id1, 2 ) );
5961

60-
assert getAuditReader().find( ParentEntity.class, id1, 1 ).equals( ver1 );
61-
assert getAuditReader().find( ParentEntity.class, id1, 2 ).equals( ver2 );
62+
assertEquals( ver1, auditReader.find( ParentEntity.class, id1, 1 ) );
63+
assertEquals( ver2, auditReader.find( ParentEntity.class, id1, 2 ) );
64+
} );
6265
}
6366

6467
@Test
65-
public void testPolymorphicQuery() {
68+
public void testPolymorphicQuery(EntityManagerFactoryScope scope) {
6669
ChildEntity childVer1 = new ChildEntity( id1, "x", 1l );
6770

68-
assert getAuditReader().createQuery().forEntitiesAtRevision( ChildEntity.class, 1 ).getSingleResult()
69-
.equals( childVer1 );
70-
71-
assert getAuditReader().createQuery().forEntitiesAtRevision( ParentEntity.class, 1 ).getSingleResult()
72-
.equals( childVer1 );
71+
scope.inEntityManager( em -> {
72+
var auditReader = AuditReaderFactory.get( em );
73+
assertEquals( childVer1, auditReader.createQuery().forEntitiesAtRevision( ChildEntity.class, 1 ).getSingleResult() );
74+
assertEquals( childVer1, auditReader.createQuery().forEntitiesAtRevision( ParentEntity.class, 1 ).getSingleResult() );
75+
} );
7376
}
7477
}

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/ChildNullAuditing.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,73 @@
55
package org.hibernate.orm.test.envers.integration.inheritance.joined;
66

77
import java.util.Arrays;
8-
import jakarta.persistence.EntityManager;
98

10-
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
11-
import org.hibernate.orm.test.envers.Priority;
9+
import org.hibernate.envers.AuditReaderFactory;
10+
import org.hibernate.testing.envers.junit.EnversTest;
11+
import org.hibernate.testing.orm.junit.BeforeClassTemplate;
12+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
13+
import org.hibernate.testing.orm.junit.Jpa;
1214

13-
import org.junit.Test;
15+
import org.junit.jupiter.api.Test;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
1418

1519
/**
1620
* @author Adam Warski (adam at warski dot org)
1721
*/
18-
public class ChildNullAuditing extends BaseEnversJPAFunctionalTestCase {
22+
@EnversTest
23+
@Jpa(annotatedClasses = {ChildEntity.class, ParentEntity.class})
24+
public class ChildNullAuditing {
1925
private Integer id1;
2026

21-
@Override
22-
protected Class<?>[] getAnnotatedClasses() {
23-
return new Class[] {ChildEntity.class, ParentEntity.class};
24-
}
25-
26-
@Test
27-
@Priority(10)
28-
public void initData() {
29-
EntityManager em = getEntityManager();
30-
27+
@BeforeClassTemplate
28+
public void initData(EntityManagerFactoryScope scope) {
3129
id1 = 1;
3230

3331
// Rev 1
34-
em.getTransaction().begin();
35-
ChildEntity ce = new ChildEntity( id1, "x", null );
36-
em.persist( ce );
37-
em.getTransaction().commit();
32+
scope.inTransaction( em -> {
33+
ChildEntity ce = new ChildEntity( id1, "x", null );
34+
em.persist( ce );
35+
} );
3836

3937
// Rev 2
40-
em.getTransaction().begin();
41-
ce = em.find( ChildEntity.class, id1 );
42-
ce.setData( null );
43-
ce.setNumVal( 2l );
44-
em.getTransaction().commit();
38+
scope.inTransaction( em -> {
39+
ChildEntity ce = em.find( ChildEntity.class, id1 );
40+
ce.setData( null );
41+
ce.setNumVal( 2l );
42+
} );
4543
}
4644

4745
@Test
48-
public void testRevisionsCounts() {
49-
assert Arrays.asList( 1, 2 ).equals( getAuditReader().getRevisions( ChildEntity.class, id1 ) );
46+
public void testRevisionsCounts(EntityManagerFactoryScope scope) {
47+
scope.inEntityManager( em -> {
48+
assertEquals( Arrays.asList( 1, 2 ), AuditReaderFactory.get( em ).getRevisions( ChildEntity.class, id1 ) );
49+
} );
5050
}
5151

5252
@Test
53-
public void testHistoryOfChildId1() {
53+
public void testHistoryOfChildId1(EntityManagerFactoryScope scope) {
5454
ChildEntity ver1 = new ChildEntity( id1, "x", null );
5555
ChildEntity ver2 = new ChildEntity( id1, null, 2l );
5656

57-
assert getAuditReader().find( ChildEntity.class, id1, 1 ).equals( ver1 );
58-
assert getAuditReader().find( ChildEntity.class, id1, 2 ).equals( ver2 );
57+
scope.inEntityManager( em -> {
58+
var auditReader = AuditReaderFactory.get( em );
59+
assertEquals( ver1, auditReader.find( ChildEntity.class, id1, 1 ) );
60+
assertEquals( ver2, auditReader.find( ChildEntity.class, id1, 2 ) );
5961

60-
assert getAuditReader().find( ParentEntity.class, id1, 1 ).equals( ver1 );
61-
assert getAuditReader().find( ParentEntity.class, id1, 2 ).equals( ver2 );
62+
assertEquals( ver1, auditReader.find( ParentEntity.class, id1, 1 ) );
63+
assertEquals( ver2, auditReader.find( ParentEntity.class, id1, 2 ) );
64+
} );
6265
}
6366

6467
@Test
65-
public void testPolymorphicQuery() {
68+
public void testPolymorphicQuery(EntityManagerFactoryScope scope) {
6669
ChildEntity childVer1 = new ChildEntity( id1, "x", null );
6770

68-
assert getAuditReader().createQuery().forEntitiesAtRevision( ChildEntity.class, 1 ).getSingleResult()
69-
.equals( childVer1 );
70-
71-
assert getAuditReader().createQuery().forEntitiesAtRevision( ParentEntity.class, 1 ).getSingleResult()
72-
.equals( childVer1 );
71+
scope.inEntityManager( em -> {
72+
var auditReader = AuditReaderFactory.get( em );
73+
assertEquals( childVer1, auditReader.createQuery().forEntitiesAtRevision( ChildEntity.class, 1 ).getSingleResult() );
74+
assertEquals( childVer1, auditReader.createQuery().forEntitiesAtRevision( ParentEntity.class, 1 ).getSingleResult() );
75+
} );
7376
}
7477
}

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/DiscriminatorJoinedInheritanceTest.java

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,75 @@
1111
import jakarta.persistence.DiscriminatorColumn;
1212
import jakarta.persistence.DiscriminatorValue;
1313
import jakarta.persistence.Entity;
14-
import jakarta.persistence.EntityManager;
1514
import jakarta.persistence.Id;
1615
import jakarta.persistence.Inheritance;
1716
import jakarta.persistence.InheritanceType;
1817
import jakarta.persistence.OneToMany;
1918
import jakarta.persistence.Table;
2019

2120
import org.hibernate.envers.Audited;
22-
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
23-
import org.hibernate.orm.test.envers.Priority;
21+
import org.hibernate.envers.AuditReaderFactory;
22+
import org.hibernate.testing.envers.junit.EnversTest;
23+
import org.hibernate.testing.orm.junit.BeforeClassTemplate;
24+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
2425
import org.hibernate.testing.orm.junit.JiraKey;
25-
import org.junit.Test;
26+
import org.hibernate.testing.orm.junit.Jpa;
27+
import org.junit.jupiter.api.Test;
2628

27-
import static org.junit.Assert.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
2830

2931
/**
3032
* @author Chris Cranford
3133
*/
3234
@JiraKey(value = "HHH-11133")
33-
public class DiscriminatorJoinedInheritanceTest extends BaseEnversJPAFunctionalTestCase {
34-
35-
@Override
36-
protected Class<?>[] getAnnotatedClasses() {
37-
return new Class<?>[] { ParentEntity.class, ChildEntity.class, ChildListHolder.class };
38-
}
39-
40-
@Test
41-
@Priority(10)
42-
public void initData() {
43-
EntityManager entityManager = getEntityManager();
44-
try {
35+
@EnversTest
36+
@Jpa(annotatedClasses = {
37+
DiscriminatorJoinedInheritanceTest.ParentEntity.class,
38+
DiscriminatorJoinedInheritanceTest.ChildEntity.class,
39+
DiscriminatorJoinedInheritanceTest.ChildListHolder.class
40+
})
41+
public class DiscriminatorJoinedInheritanceTest {
42+
43+
@BeforeClassTemplate
44+
public void initData(EntityManagerFactoryScope scope) {
45+
scope.inTransaction( em -> {
4546
ChildEntity childEntity = new ChildEntity( 1, "Child" );
46-
entityManager.getTransaction().begin();
47-
entityManager.persist( childEntity );
48-
entityManager.getTransaction().commit();
47+
em.persist( childEntity );
48+
} );
4949

50+
scope.inTransaction( em -> {
51+
ChildEntity childEntity = em.find( ChildEntity.class, 1 );
5052
ChildListHolder holder = new ChildListHolder();
5153
holder.setId( 1 );
5254
holder.setChildren( Arrays.asList( childEntity ) );
53-
entityManager.getTransaction().begin();
54-
entityManager.persist( holder );
55-
entityManager.getTransaction().commit();
56-
57-
}
58-
catch ( Exception e ) {
59-
if ( entityManager.getTransaction().isActive() ) {
60-
entityManager.getTransaction().rollback();
61-
}
62-
throw e;
63-
}
64-
finally {
65-
entityManager.close();
66-
}
55+
em.persist( holder );
56+
} );
6757
}
6858

6959
@Test
70-
public void testRevisionCounts() {
71-
assertEquals( Arrays.asList( 1 ), getAuditReader().getRevisions( ChildEntity.class, 1 ) );
72-
assertEquals( Arrays.asList( 2 ), getAuditReader().getRevisions( ChildListHolder.class, 1 ) );
60+
public void testRevisionCounts(EntityManagerFactoryScope scope) {
61+
scope.inEntityManager( em -> {
62+
var auditReader = AuditReaderFactory.get( em );
63+
assertEquals( Arrays.asList( 1 ), auditReader.getRevisions( ChildEntity.class, 1 ) );
64+
assertEquals( Arrays.asList( 2 ), auditReader.getRevisions( ChildListHolder.class, 1 ) );
65+
} );
7366
}
7467

7568
@Test
76-
public void testConfiguredDiscriminatorValue() {
77-
ChildEntity entity = getAuditReader().find( ChildEntity.class, 1, 1 );
78-
assertEquals( "ce", entity.getType() );
69+
public void testConfiguredDiscriminatorValue(EntityManagerFactoryScope scope) {
70+
scope.inEntityManager( em -> {
71+
ChildEntity entity = AuditReaderFactory.get( em ).find( ChildEntity.class, 1, 1 );
72+
assertEquals( "ce", entity.getType() );
73+
} );
7974
}
8075

8176
@Test
82-
public void testDiscriminatorValuesViaRelatedEntityQuery() {
83-
ChildListHolder holder = getAuditReader().find( ChildListHolder.class, 1, 2 );
84-
assertEquals( 1, holder.getChildren().size() );
85-
assertEquals( "ce", holder.getChildren().get( 0 ).getType() );
77+
public void testDiscriminatorValuesViaRelatedEntityQuery(EntityManagerFactoryScope scope) {
78+
scope.inEntityManager( em -> {
79+
ChildListHolder holder = AuditReaderFactory.get( em ).find( ChildListHolder.class, 1, 2 );
80+
assertEquals( 1, holder.getChildren().size() );
81+
assertEquals( "ce", holder.getChildren().get( 0 ).getType() );
82+
} );
8683
}
8784

8885
@Entity(name = "ParentEntity")

0 commit comments

Comments
 (0)