|
8 | 8 | import org.hibernate.Interceptor; |
9 | 9 | import org.hibernate.Session; |
10 | 10 | import org.hibernate.SessionBuilder; |
11 | | -import org.hibernate.SessionFactory; |
12 | 11 | import org.hibernate.cfg.AvailableSettings; |
13 | | -import org.hibernate.cfg.Configuration; |
14 | 12 | 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; |
15 | 18 | import org.hibernate.type.Type; |
16 | 19 |
|
17 | | -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
18 | | -import org.junit.Test; |
| 20 | +import org.junit.jupiter.api.Test; |
19 | 21 |
|
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; |
23 | 25 |
|
24 | 26 | /** |
25 | 27 | * @author Steve Ebersole |
26 | 28 | */ |
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 { |
28 | 39 | private static final String INITIAL_NAME = "thing 1"; |
29 | 40 | private static final String SUBSEQUENT_NAME = "thing 2"; |
30 | 41 |
|
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 | + } |
40 | 47 | } |
41 | 48 |
|
42 | 49 | @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 | + } ); |
51 | 56 |
|
52 | 57 | Strategy.INSTANCE.resetState(); |
53 | 58 |
|
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 | + } ); |
60 | 63 |
|
61 | 64 | assertEquals( 1, Strategy.INSTANCE.canDirtyCheckCount ); |
62 | 65 | assertEquals( 1, Strategy.INSTANCE.isDirtyCount ); |
63 | 66 | assertEquals( 2, Strategy.INSTANCE.resetDirtyCount ); |
64 | 67 | assertEquals( 1, Strategy.INSTANCE.findDirtyCount ); |
65 | 68 |
|
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 | + } ); |
73 | 74 | } |
74 | 75 |
|
75 | 76 | @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 | + } ); |
84 | 83 |
|
85 | 84 | Strategy.INSTANCE.resetState(); |
86 | 85 |
|
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 | + } |
93 | 94 |
|
94 | 95 | // As we used an interceptor, the custom strategy should have been called twice to find dirty properties |
95 | 96 | assertEquals( 1, Strategy.INSTANCE.canDirtyCheckCount ); |
96 | 97 | assertEquals( 1, Strategy.INSTANCE.isDirtyCount ); |
97 | 98 | assertEquals( 2, Strategy.INSTANCE.resetDirtyCount ); |
98 | 99 | assertEquals( 2, Strategy.INSTANCE.findDirtyCount ); |
99 | 100 |
|
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 | + } ); |
107 | 106 | } |
108 | 107 |
|
109 | 108 | @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 | + } ); |
138 | 131 | } |
139 | 132 |
|
140 | | - private SessionBuilder sessionWithInterceptor() { |
141 | | - return sessionFactory().unwrap( SessionFactory.class ) |
| 133 | + private SessionBuilder sessionWithInterceptor(SessionFactoryScope scope) { |
| 134 | + return scope.getSessionFactory() |
142 | 135 | .withOptions() |
143 | 136 | .interceptor( OnFlushDirtyInterceptor.INSTANCE ); |
144 | 137 | } |
@@ -192,7 +185,6 @@ void resetState() { |
192 | 185 | } |
193 | 186 | } |
194 | 187 |
|
195 | | - |
196 | 188 | public static class OnFlushDirtyInterceptor implements Interceptor { |
197 | 189 | private static final OnFlushDirtyInterceptor INSTANCE = new OnFlushDirtyInterceptor(); |
198 | 190 |
|
|
0 commit comments