1414import org .hibernate .property .access .spi .GetterFieldImpl ;
1515import org .hibernate .property .access .spi .GetterMethodImpl ;
1616import org .hibernate .service .ServiceRegistry ;
17- import org .hibernate .type .descriptor .java .spi .JdbcTypeRecommendationException ;
18-
1917import org .hibernate .testing .ServiceRegistryBuilder ;
18+ import org .hibernate .testing .orm .junit .BaseUnitTest ;
2019import org .hibernate .testing .orm .junit .JiraKey ;
21- import org .junit .After ;
22- import org .junit .Before ;
23- import org .junit .Test ;
20+ import org .hibernate .type .descriptor .java .spi .JdbcTypeRecommendationException ;
21+ import org .junit .jupiter .api .AfterEach ;
22+ import org .junit .jupiter .api .BeforeEach ;
23+ import org .junit .jupiter .api .Test ;
2424
25- import static org .junit . Assert . assertTrue ;
26- import static org .junit .Assert .fail ;
25+ import static org .assertj . core . api . Assertions . assertThat ;
26+ import static org .junit .jupiter . api . Assertions .fail ;
2727
2828
2929/**
3030 * Tests verifying the correct behaviour for the usage of {@code @jakarta.persistence.Access}.
3131 *
3232 * @author Hardy Ferentschik
3333 */
34+ @ BaseUnitTest
3435public class AccessMappingTest {
3536 private ServiceRegistry serviceRegistry ;
3637
37- @ Before
38+ @ BeforeEach
3839 public void setUp () {
3940 serviceRegistry = ServiceRegistryBuilder .buildServiceRegistry ( Environment .getProperties () );
4041 }
4142
42- @ After
43+ @ AfterEach
4344 public void tearDown () {
4445 if ( serviceRegistry != null ) {
4546 ServiceRegistryBuilder .destroy ( serviceRegistry );
@@ -51,19 +52,12 @@ public void testInconsistentAnnotationPlacement() {
5152 Configuration cfg = new Configuration ();
5253 cfg .addAnnotatedClass ( Course1 .class );
5354 cfg .addAnnotatedClass ( Student .class );
54- SessionFactory sf = null ;
55- try {
56- sf = cfg .buildSessionFactory ( serviceRegistry );
55+ try (SessionFactory sf = cfg .buildSessionFactory ( serviceRegistry )) {
5756 fail ( "@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail." );
5857 }
5958 catch (MappingException | JdbcTypeRecommendationException e ) {
6059 // success
6160 }
62- finally {
63- if ( sf != null ) {
64- sf .close ();
65- }
66- }
6761 }
6862
6963 @ Test
@@ -72,20 +66,16 @@ public void testFieldAnnotationPlacement() {
7266 Class <?> classUnderTest = Course6 .class ;
7367 cfg .addAnnotatedClass ( classUnderTest );
7468 cfg .addAnnotatedClass ( Student .class );
75- SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg . buildSessionFactory ( serviceRegistry );
76- try {
69+ try ( SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg
70+ . buildSessionFactory ( serviceRegistry )) {
7771 final EntityPersister entityPersister = factory .getRuntimeMetamodels ()
7872 .getMappingMetamodel ()
79- .getEntityDescriptor (classUnderTest .getName ());
73+ .getEntityDescriptor ( classUnderTest .getName () );
8074 final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl ) entityPersister .getIdentifierMapping ();
8175
82- assertTrue (
83- "Field access should be used." ,
84- identifierMapping .getPropertyAccess ().getGetter () instanceof GetterFieldImpl
85- );
86- }
87- finally {
88- factory .close ();
76+ assertThat ( identifierMapping .getPropertyAccess ().getGetter () )
77+ .describedAs ( "Field access should be used." )
78+ .isInstanceOf ( GetterFieldImpl .class );
8979 }
9080 }
9181
@@ -95,43 +85,36 @@ public void testPropertyAnnotationPlacement() {
9585 Class <?> classUnderTest = Course7 .class ;
9686 cfg .addAnnotatedClass ( classUnderTest );
9787 cfg .addAnnotatedClass ( Student .class );
98- SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg .buildSessionFactory ( serviceRegistry );
99- try {
88+
89+ try (SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg
90+ .buildSessionFactory ( serviceRegistry )) {
10091 final EntityPersister entityPersister = factory .getRuntimeMetamodels ()
10192 .getMappingMetamodel ()
102- .getEntityDescriptor (classUnderTest .getName ());
93+ .getEntityDescriptor ( classUnderTest .getName () );
10394 final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl ) entityPersister .getIdentifierMapping ();
10495
105- assertTrue (
106- "Property access should be used." ,
107- identifierMapping .getPropertyAccess ().getGetter () instanceof GetterMethodImpl
108- );
109- }
110- finally {
111- factory .close ();
96+ assertThat ( identifierMapping .getPropertyAccess ().getGetter () )
97+ .describedAs ( "Property access should be used." )
98+ .isInstanceOf ( GetterMethodImpl .class );
11299 }
113100 }
114101
115102 @ Test
116- public void testExplicitPropertyAccessAnnotationsOnProperty () throws Exception {
103+ public void testExplicitPropertyAccessAnnotationsOnProperty () {
117104 Configuration cfg = new Configuration ();
118105 Class <?> classUnderTest = Course2 .class ;
119106 cfg .addAnnotatedClass ( classUnderTest );
120107 cfg .addAnnotatedClass ( Student .class );
121- SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg . buildSessionFactory ( serviceRegistry );
122- try {
108+ try ( SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg
109+ . buildSessionFactory ( serviceRegistry )) {
123110 final EntityPersister entityPersister = factory .getRuntimeMetamodels ()
124111 .getMappingMetamodel ()
125- .getEntityDescriptor (classUnderTest .getName ());
112+ .getEntityDescriptor ( classUnderTest .getName () );
126113 final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl ) entityPersister .getIdentifierMapping ();
127114
128- assertTrue (
129- "Property access should be used." ,
130- identifierMapping .getPropertyAccess ().getGetter () instanceof GetterMethodImpl
131- );
132- }
133- finally {
134- factory .close ();
115+ assertThat ( identifierMapping .getPropertyAccess ().getGetter () )
116+ .describedAs ( "Property access should be used." )
117+ .isInstanceOf ( GetterMethodImpl .class );
135118 }
136119 }
137120
@@ -140,19 +123,12 @@ public void testExplicitPropertyAccessAnnotationsOnField() {
140123 Configuration cfg = new Configuration ();
141124 cfg .addAnnotatedClass ( Course4 .class );
142125 cfg .addAnnotatedClass ( Student .class );
143- SessionFactory sf = null ;
144- try {
145- sf = cfg .buildSessionFactory ( serviceRegistry );
126+ try (SessionFactory sf = cfg .buildSessionFactory ( serviceRegistry )) {
146127 fail ( "@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail." );
147128 }
148129 catch (MappingException e ) {
149130 // success
150131 }
151- finally {
152- if ( sf != null ) {
153- sf .close ();
154- }
155- }
156132 }
157133
158134 @ Test
@@ -161,25 +137,20 @@ public void testExplicitPropertyAccessAnnotationsWithHibernateStyleOverride() {
161137 Class <?> classUnderTest = Course3 .class ;
162138 cfg .addAnnotatedClass ( classUnderTest );
163139 cfg .addAnnotatedClass ( Student .class );
164- SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg . buildSessionFactory ( serviceRegistry );
165- try {
140+ try ( SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg
141+ . buildSessionFactory ( serviceRegistry )) {
166142 final EntityPersister entityPersister = factory .getRuntimeMetamodels ()
167143 .getMappingMetamodel ()
168- .getEntityDescriptor (classUnderTest .getName ());
144+ .getEntityDescriptor ( classUnderTest .getName () );
169145 final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl ) entityPersister .getIdentifierMapping ();
170146
171- assertTrue (
172- "Field access should be used." ,
173- identifierMapping .getPropertyAccess ().getGetter () instanceof GetterFieldImpl
174- );
147+ assertThat ( identifierMapping .getPropertyAccess ().getGetter () )
148+ .describedAs ( "Field access should be used." )
149+ .isInstanceOf ( GetterFieldImpl .class );
175150
176- assertTrue (
177- "Property access should be used." ,
178- entityPersister .getAttributeMapping ( 0 ).getPropertyAccess ().getGetter () instanceof GetterMethodImpl
179- );
180- }
181- finally {
182- factory .close ();
151+ assertThat ( entityPersister .getAttributeMapping ( 0 ).getPropertyAccess ().getGetter () )
152+ .describedAs ( "Property access should be used." )
153+ .isInstanceOf ( GetterMethodImpl .class );
183154 }
184155 }
185156
@@ -189,25 +160,23 @@ public void testExplicitPropertyAccessAnnotationsWithJpaStyleOverride() {
189160 Class <?> classUnderTest = Course5 .class ;
190161 cfg .addAnnotatedClass ( classUnderTest );
191162 cfg .addAnnotatedClass ( Student .class );
192- SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg . buildSessionFactory ( serviceRegistry );
193- try {
163+ try ( SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg
164+ . buildSessionFactory ( serviceRegistry )) {
194165 final EntityPersister entityPersister = factory .getRuntimeMetamodels ()
195166 .getMappingMetamodel ()
196- .getEntityDescriptor (classUnderTest .getName ());
167+ .getEntityDescriptor ( classUnderTest .getName () );
197168 final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl ) entityPersister .getIdentifierMapping ();
198169
199- assertTrue (
200- "Field access should be used." ,
201- identifierMapping .getPropertyAccess ().getGetter () instanceof GetterFieldImpl
202- );
170+ assertThat (
203171
204- assertTrue (
205- "Property access should be used." ,
206- entityPersister .getAttributeMapping ( 0 ).getPropertyAccess ().getGetter () instanceof GetterMethodImpl
207- );
208- }
209- finally {
210- factory .close ();
172+ identifierMapping .getPropertyAccess ().getGetter ()
173+ )
174+ .describedAs ( "Field access should be used." )
175+ .isInstanceOf ( GetterFieldImpl .class );
176+
177+ assertThat ( entityPersister .getAttributeMapping ( 0 ).getPropertyAccess ().getGetter () )
178+ .describedAs ( "Property access should be used." )
179+ .isInstanceOf ( GetterMethodImpl .class );
211180 }
212181 }
213182
@@ -218,20 +187,18 @@ public void testDefaultFieldAccessIsInherited() {
218187 cfg .addAnnotatedClass ( classUnderTest );
219188 cfg .addAnnotatedClass ( Person .class );
220189 cfg .addAnnotatedClass ( Being .class );
221- SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg . buildSessionFactory ( serviceRegistry );
222- try {
190+ try ( SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg
191+ . buildSessionFactory ( serviceRegistry )) {
223192 final EntityPersister entityPersister = factory .getRuntimeMetamodels ()
224193 .getMappingMetamodel ()
225- .getEntityDescriptor (classUnderTest .getName ());
194+ .getEntityDescriptor ( classUnderTest .getName () );
226195 final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl ) entityPersister .getIdentifierMapping ();
227196
228- assertTrue (
229- "Field access should be used since the default access mode gets inherited" ,
230- identifierMapping .getPropertyAccess ().getGetter () instanceof GetterFieldImpl
231- );
232- }
233- finally {
234- factory .close ();
197+ assertThat (
198+ identifierMapping .getPropertyAccess ().getGetter () )
199+ .describedAs ( "Field access should be used since the default access mode gets inherited" )
200+ .isInstanceOf ( GetterFieldImpl .class );
201+ ;
235202 }
236203 }
237204
@@ -241,29 +208,24 @@ public void testDefaultPropertyAccessIsInherited() {
241208 cfg .addAnnotatedClass ( Horse .class );
242209 cfg .addAnnotatedClass ( Animal .class );
243210
244- SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg . buildSessionFactory ( serviceRegistry );
245- try {
211+ try ( SessionFactoryImplementor factory = (SessionFactoryImplementor ) cfg
212+ . buildSessionFactory ( serviceRegistry )) {
246213 EntityPersister entityPersister = factory .getRuntimeMetamodels ()
247214 .getMappingMetamodel ()
248- .getEntityDescriptor (Animal .class .getName ());
215+ .getEntityDescriptor ( Animal .class .getName () );
249216 final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl ) entityPersister .getIdentifierMapping ();
250217
251- assertTrue (
252- "Property access should be used since explicity configured via @Access" ,
253- identifierMapping .getPropertyAccess ().getGetter () instanceof GetterMethodImpl
254- );
218+ assertThat ( identifierMapping .getPropertyAccess ().getGetter () )
219+ .describedAs ( "Property access should be used since explicity configured via @Access" )
220+ .isInstanceOf ( GetterMethodImpl .class );
255221
256222 entityPersister = factory .getRuntimeMetamodels ()
257223 .getMappingMetamodel ()
258- .getEntityDescriptor (Horse .class .getName ());
224+ .getEntityDescriptor ( Horse .class .getName () );
259225
260- assertTrue (
261- "Field access should be used since the default access mode gets inherited" ,
262- entityPersister .getAttributeMapping ( 0 ).getPropertyAccess ().getGetter () instanceof GetterFieldImpl
263- );
264- }
265- finally {
266- factory .close ();
226+ assertThat ( entityPersister .getAttributeMapping ( 0 ).getPropertyAccess ().getGetter () )
227+ .describedAs ( "Field access should be used since the default access mode gets inherited" )
228+ .isInstanceOf ( GetterFieldImpl .class );
267229 }
268230 }
269231
0 commit comments