Skip to content

Commit 2df3886

Browse files
committed
Various code cleanup
1 parent 949b9b7 commit 2df3886

File tree

8 files changed

+241
-171
lines changed

8 files changed

+241
-171
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/flush/TestFlushJoinTransaction.java

Lines changed: 55 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,76 @@
44
*/
55
package org.hibernate.orm.test.flush;
66

7-
import java.util.Map;
8-
97
import jakarta.persistence.TransactionRequiredException;
10-
11-
import org.hibernate.Session;
12-
import org.hibernate.cfg.AvailableSettings;
13-
import org.junit.Test;
14-
15-
import org.hibernate.testing.orm.junit.JiraKey;
168
import org.hibernate.testing.jta.TestingJtaBootstrap;
179
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
18-
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
10+
import org.hibernate.testing.orm.junit.DomainModel;
11+
import org.hibernate.testing.orm.junit.JiraKey;
12+
import org.hibernate.testing.orm.junit.ServiceRegistry;
13+
import org.hibernate.testing.orm.junit.SessionFactory;
14+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
15+
import org.hibernate.testing.orm.junit.SettingConfiguration;
16+
import org.junit.jupiter.api.Test;
1917

20-
import static org.junit.Assert.fail;
18+
import static org.junit.jupiter.api.Assertions.fail;
2119

2220
/**
2321
* @author Michiel Hendriks
2422
*/
23+
@SuppressWarnings("JUnitMalformedDeclaration")
2524
@JiraKey(value = "HHH-13936")
26-
public class TestFlushJoinTransaction extends BaseNonConfigCoreFunctionalTestCase {
27-
28-
@Override
29-
protected void addSettings(Map<String,Object> settings) {
30-
super.addSettings( settings );
31-
TestingJtaBootstrap.prepare( settings );
32-
settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" );
33-
}
34-
25+
@ServiceRegistry(
26+
settingConfigurations = @SettingConfiguration( configurer = TestingJtaBootstrap.class )
27+
)
28+
@DomainModel
29+
@SessionFactory
30+
public class TestFlushJoinTransaction {
3531
@Test
36-
public void testFlush() throws Exception {
37-
Session session = openSession();
38-
try {
39-
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
40-
session.flush();
41-
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
42-
}
43-
catch (TransactionRequiredException e) {
44-
fail("No TransactionRequiredException expected.");
45-
}
46-
finally {
47-
session.close();
48-
}
32+
public void testFlush(SessionFactoryScope sessions) {
33+
sessions.inSession( (session) -> {
34+
try {
35+
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
36+
session.flush();
37+
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
38+
}
39+
catch (TransactionRequiredException e) {
40+
fail("No TransactionRequiredException expected.");
41+
}
42+
catch (Exception e) {
43+
fail("Unexpected JTA exception", e);
44+
}
45+
} );
4946
}
5047

5148
@Test
52-
public void testIsConnectedFlush() throws Exception {
53-
Session session = openSession();
54-
try {
55-
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
56-
session.isConnected();
57-
session.flush();
58-
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
59-
}
60-
catch (TransactionRequiredException e) {
61-
fail("No TransactionRequiredException expected.");
62-
}
63-
finally {
64-
session.close();
65-
}
49+
public void testIsConnectedFlush(SessionFactoryScope sessions) {
50+
sessions.inSession( (session) -> {
51+
try {
52+
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
53+
session.isConnected();
54+
session.flush();
55+
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
56+
}
57+
catch (TransactionRequiredException e) {
58+
fail("No TransactionRequiredException expected.");
59+
}
60+
catch (Exception e) {
61+
fail("Unexpected JTA exception", e);
62+
}
63+
} );
6664
}
6765

6866
@Test
69-
public void testIsConnectedFlushShouldThrowExceptionIfNoTransaction() {
70-
Session session = openSession();
71-
try {
72-
session.isConnected();
73-
session.flush();
74-
fail("A TransactionRequiredException should be thrown");
75-
}
76-
catch (TransactionRequiredException e) {
77-
//expected
78-
}
79-
finally {
80-
session.close();
81-
}
67+
public void testIsConnectedFlushShouldThrowExceptionIfNoTransaction(SessionFactoryScope sessions) {
68+
sessions.inSession( (session) -> {
69+
try {
70+
session.isConnected();
71+
session.flush();
72+
fail("A TransactionRequiredException should be thrown");
73+
}
74+
catch (TransactionRequiredException e) {
75+
//expected
76+
}
77+
} );
8278
}
8379
}

hibernate-core/src/test/java/org/hibernate/orm/test/resource/transaction/jta/JpaComplianceAlreadyStartedTransactionTest.java

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,70 @@
44
*/
55
package org.hibernate.orm.test.resource.transaction.jta;
66

7-
import java.util.Map;
8-
import jakarta.transaction.Status;
97
import jakarta.transaction.TransactionManager;
10-
11-
import org.hibernate.Session;
128
import org.hibernate.Transaction;
13-
import org.hibernate.cfg.AvailableSettings;
14-
15-
import org.hibernate.testing.orm.junit.JiraKey;
169
import org.hibernate.testing.jta.TestingJtaBootstrap;
17-
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
18-
import org.junit.Before;
19-
import org.junit.Test;
10+
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
11+
import org.hibernate.testing.orm.junit.DomainModel;
12+
import org.hibernate.testing.orm.junit.JiraKey;
13+
import org.hibernate.testing.orm.junit.ServiceRegistry;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.hibernate.testing.orm.junit.Setting;
17+
import org.hibernate.testing.orm.junit.SettingConfiguration;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.hibernate.cfg.JpaComplianceSettings.JPA_TRANSACTION_COMPLIANCE;
22+
import static org.junit.jupiter.api.Assertions.fail;
2023

2124
/**
25+
* Same as {@linkplain NonJpaComplianceAlreadyStartedTransactionTest}, but here with JPA compliance enabled
26+
*
2227
* @author Andrea Boriero
2328
*/
2429
@JiraKey(value = "HHH-13076")
25-
public class JpaComplianceAlreadyStartedTransactionTest extends BaseNonConfigCoreFunctionalTestCase {
30+
@ServiceRegistry(
31+
settingConfigurations = @SettingConfiguration( configurer = TestingJtaBootstrap.class ),
32+
settings = {
33+
@Setting( name = JPA_TRANSACTION_COMPLIANCE, value = "true" )
34+
}
35+
)
36+
@DomainModel
37+
@SessionFactory
38+
@SuppressWarnings("JUnitMalformedDeclaration")
39+
public class JpaComplianceAlreadyStartedTransactionTest {
2640
private TransactionManager tm;
2741

28-
@Override
29-
protected void addSettings(Map<String,Object> settings) {
30-
super.addSettings( settings );
31-
TestingJtaBootstrap.prepare( settings );
32-
settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" );
33-
settings.put( AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true" );
34-
}
35-
36-
@Before
42+
@BeforeEach
3743
public void setUp() {
3844
tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
3945
}
4046

41-
@Test(expected = IllegalStateException.class)
42-
public void anIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlreadyActiveTX() throws Exception {
43-
try (Session s = openSession()) {
44-
tm.begin();
45-
Transaction tx = null;
46-
try {
47-
// A call to begin() with an active Tx should cause an IllegalStateException
48-
tx = s.getTransaction();
49-
tx.begin();
50-
}
51-
catch (Exception e) {
52-
if ( tx != null && tx.isActive() ) {
53-
tx.rollback();
47+
@Test
48+
public void testBeginWithinActiveTransaction(SessionFactoryScope sessions) throws Exception {
49+
TestingJtaPlatformImpl.inNoopJtaTransaction( tm, () -> {
50+
sessions.inSession( (session) -> {
51+
Transaction tx = null;
52+
try {
53+
// A call to begin() with an active Tx should cause an IllegalStateException
54+
tx = session.getTransaction();
55+
tx.begin();
56+
fail( "Expecting an IllegalStateException" );
5457
}
55-
throw e;
56-
}
57-
}
58-
catch (Exception e) {
59-
if ( tm.getStatus() == Status.STATUS_ACTIVE ) {
60-
tm.rollback();
61-
}
62-
throw e;
63-
}
58+
catch (IllegalStateException expected) {
59+
}
60+
finally {
61+
if ( tx != null ) {
62+
try {
63+
tx.rollback();
64+
}
65+
catch (Exception ignore) {
66+
}
67+
}
68+
}
69+
} );
70+
} );
6471
}
72+
6573
}

hibernate-core/src/test/java/org/hibernate/orm/test/resource/transaction/jta/NonJpaComplianceAlreadyStartedTransactionTest.java

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,65 @@
44
*/
55
package org.hibernate.orm.test.resource.transaction.jta;
66

7-
import java.util.Map;
87

9-
import org.hibernate.Session;
8+
import jakarta.transaction.TransactionManager;
109
import org.hibernate.Transaction;
11-
import org.hibernate.cfg.AvailableSettings;
12-
1310
import org.hibernate.testing.jta.TestingJtaBootstrap;
14-
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
11+
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
12+
import org.hibernate.testing.orm.junit.DomainModel;
1513
import org.hibernate.testing.orm.junit.JiraKey;
16-
import org.junit.Before;
17-
import org.junit.Test;
14+
import org.hibernate.testing.orm.junit.ServiceRegistry;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.hibernate.testing.orm.junit.SettingConfiguration;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
1820

19-
import jakarta.persistence.Entity;
20-
import jakarta.persistence.GeneratedValue;
21-
import jakarta.persistence.Id;
22-
import jakarta.transaction.Status;
23-
import jakarta.transaction.TransactionManager;
21+
import static org.junit.jupiter.api.Assertions.fail;
2422

2523
/**
24+
* Same as {@linkplain JpaComplianceAlreadyStartedTransactionTest}, but here with JPA compliance disabled
25+
*
2626
* @author Andrea Boriero
2727
*/
2828
@JiraKey("HHH-13076")
29-
public class NonJpaComplianceAlreadyStartedTransactionTest extends BaseNonConfigCoreFunctionalTestCase {
29+
@ServiceRegistry(
30+
settingConfigurations = @SettingConfiguration( configurer = TestingJtaBootstrap.class )
31+
)
32+
@DomainModel
33+
@SessionFactory
34+
@SuppressWarnings("JUnitMalformedDeclaration")
35+
public class NonJpaComplianceAlreadyStartedTransactionTest {
3036
private TransactionManager tm;
3137

32-
@Override
33-
protected void addSettings(Map<String,Object> settings) {
34-
super.addSettings( settings );
35-
TestingJtaBootstrap.prepare( settings );
36-
settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" );
37-
}
38-
39-
@Override
40-
protected Class[] getAnnotatedClasses() {
41-
return new Class[] { TestEntity.class };
42-
}
43-
44-
@Before
38+
@BeforeEach
4539
public void setUp() {
4640
tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
4741
}
4842

4943
@Test
50-
public void noIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlreadyActiveTx() throws Exception {
51-
tm.begin();
52-
try (Session s = openSession()) {
53-
Transaction tx = s.getTransaction();
54-
tx.begin();
55-
try {
56-
s.persist( new TestEntity( "ABC" ) );
57-
tx.commit();
58-
}
59-
catch (Exception e) {
60-
if ( tx.isActive() ) {
61-
tx.rollback();
44+
public void testBeginWithinActiveTransaction(SessionFactoryScope sessions) throws Exception {
45+
TestingJtaPlatformImpl.inNoopJtaTransaction( tm, () -> {
46+
sessions.inSession( (session) -> {
47+
Transaction tx = null;
48+
try {
49+
// A call to begin() with an active Tx should cause an IllegalStateException
50+
tx = session.getTransaction();
51+
tx.begin();
6252
}
63-
throw e;
64-
}
65-
}
66-
try {
67-
tm.commit();
68-
}
69-
catch (Exception e) {
70-
if ( tm.getStatus() == Status.STATUS_ACTIVE ) {
71-
tm.rollback();
72-
}
73-
throw e;
74-
}
75-
}
76-
77-
78-
@Entity(name = "TestEntity")
79-
public static class TestEntity {
80-
@Id
81-
@GeneratedValue
82-
private Long id;
83-
84-
private String stringAttribute;
85-
86-
public TestEntity() {
87-
}
88-
89-
public TestEntity(String stringAttribute) {
90-
this.stringAttribute = stringAttribute;
91-
}
53+
catch (IllegalStateException unexpected) {
54+
fail( "Unexpected failure", unexpected );
55+
}
56+
finally {
57+
if ( tx != null ) {
58+
try {
59+
tx.rollback();
60+
}
61+
catch (Exception ignore) {
62+
}
63+
}
64+
}
65+
} );
66+
} );
9267
}
9368
}

0 commit comments

Comments
 (0)