Skip to content

Commit ee76dc2

Browse files
committed
HHH-19846 - Remove JUnit4: org.hibernate.orm.test.dirtiness
Signed-off-by: Jan Schatteman <[email protected]>
1 parent 87caf95 commit ee76dc2

File tree

1 file changed

+82
-90
lines changed

1 file changed

+82
-90
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/dirtiness/CustomDirtinessStrategyTest.java

Lines changed: 82 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -8,137 +8,130 @@
88
import org.hibernate.Interceptor;
99
import org.hibernate.Session;
1010
import org.hibernate.SessionBuilder;
11-
import org.hibernate.SessionFactory;
1211
import org.hibernate.cfg.AvailableSettings;
13-
import org.hibernate.cfg.Configuration;
1412
import org.hibernate.persister.entity.EntityPersister;
13+
import org.hibernate.testing.orm.junit.DomainModel;
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.SettingProvider;
1518
import org.hibernate.type.Type;
1619

17-
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
18-
import org.junit.Test;
20+
import org.junit.jupiter.api.Test;
1921

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertFalse;
22-
import static org.junit.Assert.assertTrue;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
2325

2426
/**
2527
* @author Steve Ebersole
2628
*/
27-
public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
29+
@DomainModel(annotatedClasses = {Thing.class})
30+
@SessionFactory
31+
@ServiceRegistry(
32+
settingProviders =
33+
@SettingProvider(
34+
settingName = AvailableSettings.CUSTOM_ENTITY_DIRTINESS_STRATEGY,
35+
provider = CustomDirtinessStrategyTest.StrategySettingProvider.class
36+
)
37+
)
38+
public class CustomDirtinessStrategyTest {
2839
private static final String INITIAL_NAME = "thing 1";
2940
private static final String SUBSEQUENT_NAME = "thing 2";
3041

31-
@Override
32-
protected void configure(Configuration configuration) {
33-
super.configure( configuration );
34-
configuration.getProperties().put( AvailableSettings.CUSTOM_ENTITY_DIRTINESS_STRATEGY, Strategy.INSTANCE );
35-
}
36-
37-
@Override
38-
protected Class<?>[] getAnnotatedClasses() {
39-
return new Class[] { Thing.class };
42+
public static class StrategySettingProvider implements SettingProvider.Provider<Strategy> {
43+
@Override
44+
public Strategy getSetting() {
45+
return Strategy.INSTANCE;
46+
}
4047
}
4148

4249
@Test
43-
public void testOnlyCustomStrategy() {
44-
Session session = openSession();
45-
session.beginTransaction();
46-
Thing t = new Thing( INITIAL_NAME );
47-
session.persist( t );
48-
Long id = t.getId();
49-
session.getTransaction().commit();
50-
session.close();
50+
public void testOnlyCustomStrategy(SessionFactoryScope scope) {
51+
Long id = scope.fromTransaction( session -> {
52+
Thing t = new Thing( INITIAL_NAME );
53+
session.persist( t );
54+
return t.getId();
55+
} );
5156

5257
Strategy.INSTANCE.resetState();
5358

54-
session = openSession();
55-
session.beginTransaction();
56-
Thing thing = session.get( Thing.class, id );
57-
thing.setName( SUBSEQUENT_NAME );
58-
session.getTransaction().commit();
59-
session.close();
59+
scope.inTransaction( session -> {
60+
Thing thing = session.find( Thing.class, id );
61+
thing.setName( SUBSEQUENT_NAME );
62+
} );
6063

6164
assertEquals( 1, Strategy.INSTANCE.canDirtyCheckCount );
6265
assertEquals( 1, Strategy.INSTANCE.isDirtyCount );
6366
assertEquals( 2, Strategy.INSTANCE.resetDirtyCount );
6467
assertEquals( 1, Strategy.INSTANCE.findDirtyCount );
6568

66-
session = openSession();
67-
session.beginTransaction();
68-
thing = session.get( Thing.class, id );
69-
assertEquals( SUBSEQUENT_NAME, thing.getName() );
70-
session.remove( thing );
71-
session.getTransaction().commit();
72-
session.close();
69+
scope.inTransaction( session -> {
70+
Thing thing = session.find( Thing.class, id );
71+
assertEquals( SUBSEQUENT_NAME, thing.getName() );
72+
session.remove( thing );
73+
} );
7374
}
7475

7576
@Test
76-
public void testCustomStrategyWithFlushInterceptor() {
77-
Session session = openSession();
78-
session.beginTransaction();
79-
Thing t = new Thing( INITIAL_NAME );
80-
session.persist( t );
81-
Long id = t.getId();
82-
session.getTransaction().commit();
83-
session.close();
77+
public void testCustomStrategyWithFlushInterceptor(SessionFactoryScope scope) {
78+
Long id = scope.fromTransaction( session -> {
79+
Thing t = new Thing( INITIAL_NAME );
80+
session.persist( t );
81+
return t.getId();
82+
} );
8483

8584
Strategy.INSTANCE.resetState();
8685

87-
session = sessionWithInterceptor().openSession();
88-
session.beginTransaction();
89-
Thing thing = session.get( Thing.class, id );
90-
thing.setName( SUBSEQUENT_NAME );
91-
session.getTransaction().commit();
92-
session.close();
86+
{
87+
Session session = sessionWithInterceptor(scope).openSession();
88+
session.beginTransaction();
89+
Thing thing = session.find( Thing.class, id );
90+
thing.setName( SUBSEQUENT_NAME );
91+
session.getTransaction().commit();
92+
session.close();
93+
}
9394

9495
// As we used an interceptor, the custom strategy should have been called twice to find dirty properties
9596
assertEquals( 1, Strategy.INSTANCE.canDirtyCheckCount );
9697
assertEquals( 1, Strategy.INSTANCE.isDirtyCount );
9798
assertEquals( 2, Strategy.INSTANCE.resetDirtyCount );
9899
assertEquals( 2, Strategy.INSTANCE.findDirtyCount );
99100

100-
session = openSession();
101-
session.beginTransaction();
102-
thing = session.get( Thing.class, id );
103-
assertEquals( SUBSEQUENT_NAME, thing.getName() );
104-
session.remove( thing );
105-
session.getTransaction().commit();
106-
session.close();
101+
scope.inTransaction( session -> {
102+
Thing thing = session.find( Thing.class, id );
103+
assertEquals( SUBSEQUENT_NAME, thing.getName() );
104+
session.remove( thing );
105+
} );
107106
}
108107

109108
@Test
110-
public void testOnlyCustomStrategyConsultedOnNonDirty() {
111-
Session session = openSession();
112-
session.beginTransaction();
113-
Thing t = new Thing( INITIAL_NAME );
114-
session.persist( t );
115-
Long id = t.getId();
116-
session.getTransaction().commit();
117-
session.close();
118-
119-
session = openSession();
120-
session.beginTransaction();
121-
Thing thing = session.get( Thing.class, id );
122-
// lets change the name
123-
thing.setName( SUBSEQUENT_NAME );
124-
assertTrue( Strategy.INSTANCE.isDirty( thing, null, null ) );
125-
// but fool the dirty map
126-
thing.changedValues.clear();
127-
assertFalse( Strategy.INSTANCE.isDirty( thing, null, null ) );
128-
session.getTransaction().commit();
129-
session.close();
130-
131-
session = openSession();
132-
session.beginTransaction();
133-
thing = session.get( Thing.class, id );
134-
assertEquals( INITIAL_NAME, thing.getName() );
135-
session.createQuery( "delete Thing" ).executeUpdate();
136-
session.getTransaction().commit();
137-
session.close();
109+
public void testOnlyCustomStrategyConsultedOnNonDirty(SessionFactoryScope scope) {
110+
Long id = scope.fromTransaction( session -> {
111+
Thing t = new Thing( INITIAL_NAME );
112+
session.persist( t );
113+
return t.getId();
114+
} );
115+
116+
scope.inTransaction( session -> {
117+
Thing thing = session.find( Thing.class, id );
118+
// let's change the name
119+
thing.setName( SUBSEQUENT_NAME );
120+
assertTrue( Strategy.INSTANCE.isDirty( thing, null, null ) );
121+
// but fool the dirty map
122+
thing.changedValues.clear();
123+
assertFalse( Strategy.INSTANCE.isDirty( thing, null, null ) );
124+
} );
125+
126+
scope.inTransaction( session -> {
127+
Thing thing = session.find( Thing.class, id );
128+
assertEquals( INITIAL_NAME, thing.getName() );
129+
session.createMutationQuery( "delete Thing" ).executeUpdate();
130+
} );
138131
}
139132

140-
private SessionBuilder sessionWithInterceptor() {
141-
return sessionFactory().unwrap( SessionFactory.class )
133+
private SessionBuilder sessionWithInterceptor(SessionFactoryScope scope) {
134+
return scope.getSessionFactory()
142135
.withOptions()
143136
.interceptor( OnFlushDirtyInterceptor.INSTANCE );
144137
}
@@ -192,7 +185,6 @@ void resetState() {
192185
}
193186
}
194187

195-
196188
public static class OnFlushDirtyInterceptor implements Interceptor {
197189
private static final OnFlushDirtyInterceptor INSTANCE = new OnFlushDirtyInterceptor();
198190

0 commit comments

Comments
 (0)