Skip to content

Commit a6f7442

Browse files
committed
HHH-19846 Drop JUnit 4 usage
1 parent 029622b commit a6f7442

18 files changed

+408
-496
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/access/jpa/AccessMappingTest.java

Lines changed: 71 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,33 @@
1414
import org.hibernate.property.access.spi.GetterFieldImpl;
1515
import org.hibernate.property.access.spi.GetterMethodImpl;
1616
import org.hibernate.service.ServiceRegistry;
17-
import org.hibernate.type.descriptor.java.spi.JdbcTypeRecommendationException;
18-
1917
import org.hibernate.testing.ServiceRegistryBuilder;
18+
import org.hibernate.testing.orm.junit.BaseUnitTest;
2019
import 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
3435
public 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

hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/embedded/NestedEmbeddableAttributeOverrideTest.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,40 @@
44
*/
55
package org.hibernate.orm.test.bootstrap.binding.annotations.embedded;
66

7-
import org.hibernate.Session;
8-
7+
import org.hibernate.testing.orm.junit.DomainModel;
98
import org.hibernate.testing.orm.junit.JiraKey;
10-
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
11-
import org.junit.Test;
9+
import org.hibernate.testing.orm.junit.SessionFactory;
10+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
11+
import org.junit.jupiter.api.Test;
1212

1313
/**
1414
* @author Brett Meyer
1515
*/
16-
public class NestedEmbeddableAttributeOverrideTest extends BaseCoreFunctionalTestCase {
16+
@DomainModel(
17+
annotatedClasses = {
18+
EntityWithNestedEmbeddables.class
19+
}
20+
)
21+
@SessionFactory
22+
public class NestedEmbeddableAttributeOverrideTest {
1723

1824
@Test
19-
@JiraKey(value="HHH-8021")
20-
public void testAttributeOverride() {
25+
@JiraKey(value = "HHH-8021")
26+
public void testAttributeOverride(SessionFactoryScope scope) {
2127
EmbeddableB embedB = new EmbeddableB();
2228
embedB.setEmbedAttrB( "B" );
2329

2430
EmbeddableA embedA = new EmbeddableA();
25-
embedA.setEmbedAttrA("A");
26-
embedA.setEmbedB(embedB);
31+
embedA.setEmbedAttrA( "A" );
32+
embedA.setEmbedB( embedB );
2733

2834
EntityWithNestedEmbeddables entity = new EntityWithNestedEmbeddables();
29-
entity.setEmbedA(embedA);
30-
31-
Session s = openSession();
32-
s.beginTransaction();
33-
s.persist( entity );
34-
s.getTransaction().commit();
35-
s.close();
36-
}
35+
entity.setEmbedA( embedA );
3736

38-
@Override
39-
protected Class<?>[] getAnnotatedClasses() {
40-
return new Class<?>[] { EntityWithNestedEmbeddables.class };
37+
scope.inTransaction(
38+
session -> {
39+
session.persist( entity );
40+
}
41+
);
4142
}
4243
}

0 commit comments

Comments
 (0)