Skip to content

Commit 14ccbd3

Browse files
committed
HHH-19871 --wip-- Envers migration to JUnit 6
- `org.hibernate.orm.test.envers.integration.jta`: added settings configuration to `@Jpa`
1 parent 6c36fdb commit 14ccbd3

12 files changed

+355
-325
lines changed

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/jta/DeleteCollectionJtaSessionClosedBeforeCommitTest.java

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,51 @@
77
import java.util.ArrayList;
88
import java.util.Arrays;
99
import java.util.List;
10-
import java.util.Map;
11-
import jakarta.persistence.Entity;
12-
import jakarta.persistence.EntityManager;
13-
import jakarta.persistence.Id;
14-
import jakarta.persistence.JoinColumn;
15-
import jakarta.persistence.JoinTable;
16-
import jakarta.persistence.OneToMany;
17-
import jakarta.persistence.Table;
1810

1911
import org.hibernate.cfg.AvailableSettings;
12+
import org.hibernate.envers.AuditReaderFactory;
2013
import org.hibernate.envers.Audited;
21-
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
22-
import org.hibernate.orm.test.envers.Priority;
2314

24-
import org.hibernate.testing.orm.junit.JiraKey;
15+
import org.hibernate.testing.envers.junit.EnversTest;
2516
import org.hibernate.testing.jta.TestingJtaBootstrap;
2617
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
27-
import org.junit.Test;
18+
import org.hibernate.testing.orm.junit.BeforeClassTemplate;
19+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
20+
import org.hibernate.testing.orm.junit.JiraKey;
21+
import org.hibernate.testing.orm.junit.Jpa;
22+
import org.hibernate.testing.orm.junit.Setting;
23+
import org.hibernate.testing.orm.junit.SettingConfiguration;
24+
import org.junit.jupiter.api.Test;
2825

29-
import static org.junit.Assert.assertEquals;
26+
import jakarta.persistence.Entity;
27+
import jakarta.persistence.Id;
28+
import jakarta.persistence.JoinColumn;
29+
import jakarta.persistence.JoinTable;
30+
import jakarta.persistence.OneToMany;
31+
import jakarta.persistence.Table;
32+
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
3034

3135
/**
3236
* @author Andrea Boriero
3337
*/
3438
@JiraKey(value = "HHH-11580")
35-
public class DeleteCollectionJtaSessionClosedBeforeCommitTest extends BaseEnversJPAFunctionalTestCase {
39+
@EnversTest
40+
@Jpa(annotatedClasses = {
41+
DeleteCollectionJtaSessionClosedBeforeCommitTest.TestEntity.class,
42+
DeleteCollectionJtaSessionClosedBeforeCommitTest.OtherTestEntity.class
43+
},
44+
integrationSettings = @Setting(name = AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, value = "true"),
45+
settingConfigurations = @SettingConfiguration(configurer = TestingJtaBootstrap.class)
46+
)
47+
public class DeleteCollectionJtaSessionClosedBeforeCommitTest {
3648
private static final int ENTITY_ID = 1;
3749
private static final int OTHER_ENTITY_ID = 2;
3850

39-
@Override
40-
protected Class<?>[] getAnnotatedClasses() {
41-
return new Class<?>[] {TestEntity.class, OtherTestEntity.class};
42-
}
43-
44-
@Override
45-
protected void addConfigOptions(Map options) {
46-
TestingJtaBootstrap.prepare( options );
47-
options.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" );
48-
}
49-
50-
@Test
51-
@Priority(10)
52-
public void initData() throws Exception {
51+
@BeforeClassTemplate
52+
public void initData(EntityManagerFactoryScope scope) throws Exception {
5353
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
54-
EntityManager entityManager = getEntityManager();
54+
var entityManager = scope.getEntityManagerFactory().createEntityManager();
5555
try {
5656
TestEntity entity = new TestEntity( ENTITY_ID, "Fab" );
5757
entityManager.persist( entity );
@@ -61,40 +61,40 @@ public void initData() throws Exception {
6161
entity.addOther( other );
6262
entityManager.persist( entity );
6363
entityManager.persist( other );
64-
64+
entityManager.flush();
6565
}
6666
finally {
6767
entityManager.close();
6868
TestingJtaPlatformImpl.tryCommit();
6969
}
7070
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
71-
entityManager = getEntityManager();
71+
entityManager = scope.getEntityManagerFactory().createEntityManager();
7272
try {
7373
TestEntity entity = entityManager.find( TestEntity.class, ENTITY_ID );
7474
OtherTestEntity other = entityManager.find( OtherTestEntity.class, OTHER_ENTITY_ID );
7575
entityManager.remove( entity );
7676
entityManager.remove( other );
7777
}
7878
finally {
79-
entityManager.close();
8079
TestingJtaPlatformImpl.tryCommit();
80+
entityManager.close();
8181
}
8282
}
8383

8484
@Test
85-
public void testRevisionCounts() {
86-
assertEquals(
85+
public void testRevisionCounts(EntityManagerFactoryScope scope) {
86+
scope.inEntityManager( entityManager -> assertEquals(
8787
Arrays.asList( 1, 2 ),
88-
getAuditReader().getRevisions( TestEntity.class, ENTITY_ID )
89-
);
88+
AuditReaderFactory.get( entityManager ).getRevisions( TestEntity.class, ENTITY_ID )
89+
) );
9090
}
9191

9292
@Test
93-
public void testRevisionHistory() {
94-
assertEquals(
93+
public void testRevisionHistory(EntityManagerFactoryScope scope) {
94+
scope.inEntityManager( entityManager -> assertEquals(
9595
new TestEntity( 1, "Fab" ),
96-
getAuditReader().find( TestEntity.class, ENTITY_ID, 1 )
97-
);
96+
AuditReaderFactory.get( entityManager ).find( TestEntity.class, ENTITY_ID, 1 )
97+
) );
9898
}
9999

100100
@Audited

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/jta/IdentifierProxyJtaSessionClosedBeforeCommitTest.java

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,27 @@
77
import java.util.ArrayList;
88
import java.util.Arrays;
99
import java.util.List;
10-
import java.util.Map;
10+
11+
import org.hibernate.cfg.AvailableSettings;
12+
import org.hibernate.envers.AuditJoinTable;
13+
import org.hibernate.envers.AuditReaderFactory;
14+
import org.hibernate.envers.Audited;
15+
16+
import org.hibernate.testing.envers.junit.EnversTest;
17+
import org.hibernate.testing.jta.TestingJtaBootstrap;
18+
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
19+
import org.hibernate.testing.orm.junit.BeforeClassTemplate;
20+
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
21+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
22+
import org.hibernate.testing.orm.junit.JiraKey;
23+
import org.hibernate.testing.orm.junit.Jpa;
24+
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
25+
import org.hibernate.testing.orm.junit.Setting;
26+
import org.hibernate.testing.orm.junit.SettingConfiguration;
27+
import org.junit.jupiter.api.Test;
1128

1229
import jakarta.persistence.CascadeType;
1330
import jakarta.persistence.Entity;
14-
import jakarta.persistence.EntityManager;
1531
import jakarta.persistence.FetchType;
1632
import jakarta.persistence.GeneratedValue;
1733
import jakarta.persistence.GenerationType;
@@ -20,51 +36,35 @@
2036
import jakarta.persistence.ManyToOne;
2137
import jakarta.persistence.OneToMany;
2238

23-
import org.hibernate.cfg.AvailableSettings;
24-
import org.hibernate.envers.AuditJoinTable;
25-
import org.hibernate.envers.Audited;
26-
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
27-
import org.hibernate.orm.test.envers.Priority;
28-
import org.junit.Test;
29-
30-
import org.hibernate.testing.DialectChecks;
31-
import org.hibernate.testing.RequiresDialectFeature;
32-
import org.hibernate.testing.orm.junit.JiraKey;
33-
import org.hibernate.testing.jta.TestingJtaBootstrap;
34-
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
35-
36-
import static org.junit.Assert.assertEquals;
39+
import static org.junit.jupiter.api.Assertions.assertEquals;
3740

3841
/**
3942
* @author Chris Cranford
4043
*/
41-
@JiraKey(value="HHH-13191")
42-
@RequiresDialectFeature({ DialectChecks.SupportsNoColumnInsert.class, DialectChecks.SupportsIdentityColumns.class })
43-
public class IdentifierProxyJtaSessionClosedBeforeCommitTest extends BaseEnversJPAFunctionalTestCase {
44-
@Override
45-
protected Class<?>[] getAnnotatedClasses() {
46-
return new Class<?>[] { AuthUser.class, AuthClient.class };
47-
}
48-
49-
@Override
50-
protected void addConfigOptions(Map options) {
51-
TestingJtaBootstrap.prepare( options );
52-
options.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" );
53-
54-
// NOTE: This option is critical in order for the problem to be reproducable.
55-
// If this option is not set to 'true', then the failure condition does not happen.
56-
options.put( AvailableSettings.JPA_PROXY_COMPLIANCE, "true" );
57-
}
58-
44+
@JiraKey(value = "HHH-13191")
45+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsNoColumnInsert.class)
46+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsIdentityColumns.class)
47+
@EnversTest
48+
@Jpa(
49+
annotatedClasses = {
50+
IdentifierProxyJtaSessionClosedBeforeCommitTest.AuthUser.class,
51+
IdentifierProxyJtaSessionClosedBeforeCommitTest.AuthClient.class
52+
},
53+
integrationSettings = {
54+
@Setting(name = AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, value = "true"),
55+
@Setting(name = AvailableSettings.JPA_PROXY_COMPLIANCE, value = "true")
56+
},
57+
settingConfigurations = @SettingConfiguration(configurer = TestingJtaBootstrap.class)
58+
)
59+
public class IdentifierProxyJtaSessionClosedBeforeCommitTest {
5960
private Integer authUserId;
6061
private Integer authClientId;
6162

62-
@Test
63-
@Priority(10)
64-
public void initData() throws Exception {
63+
@BeforeClassTemplate
64+
public void initData(EntityManagerFactoryScope scope) throws Exception {
6565
// Revision 1
6666
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
67-
EntityManager entityManager = getEntityManager();
67+
var entityManager = scope.getEntityManagerFactory().createEntityManager();
6868
try {
6969
final AuthUser authUser = new AuthUser();
7070
final AuthClient authClient = new AuthClient();
@@ -84,7 +84,7 @@ public void initData() throws Exception {
8484

8585
// Revision 2
8686
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
87-
entityManager = getEntityManager();
87+
entityManager = scope.getEntityManagerFactory().createEntityManager();
8888
try {
8989
final AuthUser authUser = entityManager.find( AuthUser.class, authUserId );
9090
authUser.setSomeValue( "test" );
@@ -97,8 +97,11 @@ public void initData() throws Exception {
9797
}
9898

9999
@Test
100-
public void testRevisionCounts() {
101-
assertEquals( Arrays.asList( 1, 2 ), getAuditReader().getRevisions( AuthUser.class, authUserId ) );
100+
public void testRevisionCounts(EntityManagerFactoryScope scope) {
101+
scope.inEntityManager( entityManager -> assertEquals(
102+
Arrays.asList( 1, 2 ),
103+
AuditReaderFactory.get( entityManager ).getRevisions( AuthUser.class, authUserId )
104+
) );
102105
}
103106

104107
@Entity(name = "AuthUser")
@@ -111,7 +114,7 @@ public static class AuthUser {
111114
private String someValue;
112115

113116
@ManyToOne(fetch = FetchType.LAZY)
114-
@JoinColumn(name="idclient", insertable=false, updatable = false)
117+
@JoinColumn(name = "idclient", insertable = false, updatable = false)
115118
private AuthClient authClient;
116119

117120
public AuthUser() {

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/jta/JtaExceptionListener.java

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,66 @@
44
*/
55
package org.hibernate.orm.test.envers.integration.jta;
66

7-
import java.util.Map;
8-
import jakarta.persistence.EntityManager;
9-
import jakarta.transaction.RollbackException;
10-
11-
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
12-
import org.hibernate.orm.test.envers.Priority;
137
import org.hibernate.orm.test.envers.entities.StrTestEntity;
148
import org.hibernate.orm.test.envers.integration.reventity.ExceptionListenerRevEntity;
159

10+
import org.hibernate.testing.envers.junit.EnversTest;
1611
import org.hibernate.testing.jta.TestingJtaBootstrap;
1712
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
18-
import org.junit.Assert;
19-
import org.junit.Test;
13+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
14+
import org.hibernate.testing.orm.junit.Jpa;
15+
import org.hibernate.testing.orm.junit.SettingConfiguration;
16+
import org.junit.jupiter.api.Test;
17+
18+
import jakarta.transaction.RollbackException;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2022

2123
/**
2224
* Same as {@link org.hibernate.orm.test.envers.integration.reventity.ExceptionListener}, but in a JTA environment.
2325
*
2426
* @author Adam Warski (adam at warski dot org)
2527
*/
26-
public class JtaExceptionListener extends BaseEnversJPAFunctionalTestCase {
27-
@Override
28-
protected Class<?>[] getAnnotatedClasses() {
29-
return new Class[] {StrTestEntity.class, ExceptionListenerRevEntity.class};
30-
}
31-
32-
@Override
33-
protected void addConfigOptions(Map options) {
34-
TestingJtaBootstrap.prepare( options );
35-
}
36-
37-
@Test(expected = RollbackException.class)
38-
@Priority(5) // must run before testDataNotPersisted()
39-
public void testTransactionRollback() throws Exception {
40-
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
41-
42-
try {
43-
EntityManager em = getEntityManager();
28+
@EnversTest
29+
@Jpa(annotatedClasses = { StrTestEntity.class, ExceptionListenerRevEntity.class },
30+
settingConfigurations = @SettingConfiguration(configurer = TestingJtaBootstrap.class))
31+
public class JtaExceptionListener {
32+
@Test
33+
public void testTransactionRollback(EntityManagerFactoryScope scope) {
34+
final var emf = scope.getEntityManagerFactory();
35+
assertThrows(
36+
RollbackException.class, () -> {
37+
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
4438

45-
// Trying to persist an entity - however the listener should throw an exception, so the entity
46-
// shouldn't be persisted
47-
StrTestEntity te = new StrTestEntity( "x" );
48-
em.persist( te );
49-
}
50-
finally {
51-
TestingJtaPlatformImpl.tryCommit();
52-
}
39+
var entityManager = emf.createEntityManager();
40+
try {
41+
// Trying to persist an entity - however the listener should throw an exception, so the entity
42+
// shouldn't be persisted
43+
StrTestEntity te = new StrTestEntity( "x" );
44+
entityManager.persist( te );
45+
}
46+
finally {
47+
entityManager.close();
48+
TestingJtaPlatformImpl.tryCommit();
49+
}
50+
}
51+
);
5352
}
5453

5554
@Test
56-
public void testDataNotPersisted() throws Exception {
55+
public void testDataNotPersisted(EntityManagerFactoryScope scope) throws Exception {
56+
final var emf = scope.getEntityManagerFactory();
5757
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
58-
58+
// Checking if the entity became persisted
59+
var entityManager = emf.createEntityManager();
5960
try {
60-
// Checking if the entity became persisted
61-
EntityManager em = getEntityManager();
62-
long count = em.createQuery( "from StrTestEntity s where s.str = 'x'" ).getResultList().size();
63-
Assert.assertEquals( 0, count );
61+
long count = entityManager.createQuery( "from StrTestEntity s where s.str = 'x'", StrTestEntity.class )
62+
.getResultList().size();
63+
assertEquals( 0, count );
6464
}
6565
finally {
66+
entityManager.close();
6667
TestingJtaPlatformImpl.tryCommit();
6768
}
6869
}

0 commit comments

Comments
 (0)