1010import jakarta .persistence .Id ;
1111
1212import org .hibernate .HibernateException ;
13- import org .hibernate .action .spi .AfterTransactionCompletionProcess ;
14- import org .hibernate .engine .spi .SharedSessionContractImplementor ;
1513
14+ import org .hibernate .testing .orm .junit .DomainModel ;
1615import org .hibernate .testing .orm .junit .JiraKey ;
17- import org .hibernate .testing .junit4 .BaseCoreFunctionalTestCase ;
18- import org .junit .Assert ;
19- import org .junit .Test ;
16+ import org .hibernate .testing .orm .junit .SessionFactory ;
17+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
18+ import org .junit .jupiter .api .AfterEach ;
19+ import org .junit .jupiter .api .Assertions ;
20+ import org .junit .jupiter .api .Test ;
2021
2122import static org .hamcrest .CoreMatchers .containsString ;
2223import static org .hamcrest .CoreMatchers .instanceOf ;
23- import static org .junit . Assert . assertEquals ;
24- import static org .junit .Assert . assertThat ;
24+ import static org .hamcrest . MatcherAssert . assertThat ;
25+ import static org .junit .jupiter . api . Assertions . assertEquals ;
2526
26- public class CustomAfterCompletionTest extends BaseCoreFunctionalTestCase {
27+ @ DomainModel (annotatedClasses = {CustomAfterCompletionTest .SimpleEntity .class })
28+ @ SessionFactory
29+ public class CustomAfterCompletionTest {
30+
31+ @ AfterEach
32+ public void cleanup (SessionFactoryScope scope ) {
33+ scope .inTransaction ( session -> session .getSessionFactory ().getSchemaManager ().truncate () );
34+ }
2735
2836 @ Test
2937 @ JiraKey (value = "HHH-13666" )
30- public void success () {
31- inSession ( session -> {
38+ public void success (SessionFactoryScope scope ) {
39+ scope . inSession ( session -> {
3240 AtomicBoolean called = new AtomicBoolean ( false );
33- session .getActionQueue ().registerCallback ( new AfterTransactionCompletionProcess () {
34- @ Override
35- public void doAfterTransactionCompletion (boolean success , SharedSessionContractImplementor session ) {
36- called .set ( true );
37- }
38- } );
39- Assert .assertFalse ( called .get () );
40- inTransaction ( session , theSession -> {
41- theSession .persist ( new SimpleEntity ( "jack" ) );
42- } );
43- Assert .assertTrue ( called .get () );
41+ session .getActionQueue ().registerCallback (
42+ (success , session1 ) -> called .set (true ) );
43+ Assertions .assertFalse ( called .get () );
44+ scope .inTransaction ( session , theSession -> theSession .persist (new SimpleEntity ("jack" )) );
45+ Assertions .assertTrue ( called .get () );
4446 } );
4547
4648 // Check that the transaction was committed
47- inTransaction ( session -> {
48- long count = session .createQuery ( "select count(*) from SimpleEntity" , Long .class )
49- .uniqueResult ();
49+ scope .inTransaction ( session -> {
50+ long count = session .createQuery ( "select count(*) from SimpleEntity" , Long .class ).uniqueResult ();
5051 assertEquals ( 1L , count );
5152 } );
5253 }
5354
5455 @ Test
5556 @ JiraKey (value = "HHH-13666" )
56- public void failure () {
57+ public void failure (SessionFactoryScope scope ) {
5758 try {
58- inSession ( session -> {
59- session .getActionQueue ().registerCallback ( new AfterTransactionCompletionProcess () {
60- @ Override
61- public void doAfterTransactionCompletion (boolean success , SharedSessionContractImplementor session ) {
62- throw new RuntimeException ( "My exception" );
63- }
64- } );
65- inTransaction ( session , theSession -> {
66- theSession .persist ( new SimpleEntity ( "jack" ) );
67- } );
59+ scope .inSession ( session -> {
60+ session .getActionQueue ().registerCallback (
61+ (success , session1 ) -> {throw new RuntimeException ( "My exception" );} );
62+ scope .inTransaction ( session , theSession -> theSession .persist (new SimpleEntity ("jack" )) );
6863 } );
69- Assert .fail ( "Expected exception to be thrown" );
64+ Assertions .fail ( "Expected exception to be thrown" );
7065 }
7166 catch (Exception e ) {
7267 assertThat ( e , instanceOf ( HibernateException .class ) );
@@ -80,23 +75,13 @@ public void doAfterTransactionCompletion(boolean success, SharedSessionContractI
8075 }
8176
8277 // Check that the transaction was committed
83- inTransaction ( session -> {
78+ scope . inTransaction ( session -> {
8479 long count = session .createQuery ( "select count(*) from SimpleEntity" , Long .class )
8580 .uniqueResult ();
8681 assertEquals ( 1L , count );
8782 } );
8883 }
8984
90- @ Override
91- protected Class <?>[] getAnnotatedClasses () {
92- return new Class <?>[] { SimpleEntity .class };
93- }
94-
95- @ Override
96- protected boolean isCleanupTestDataRequired () {
97- return true ;
98- }
99-
10085 @ Entity (name = "SimpleEntity" )
10186 public static class SimpleEntity {
10287 @ Id
0 commit comments