Skip to content

Commit e5c8274

Browse files
committed
HHH-19846 Drop JUnit 4 usage
1 parent 897226e commit e5c8274

File tree

3 files changed

+356
-409
lines changed

3 files changed

+356
-409
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetoone/OptionalOneToOneMapsIdQueryTest.java

Lines changed: 125 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -10,234 +10,222 @@
1010
import jakarta.persistence.MapsId;
1111
import jakarta.persistence.OneToOne;
1212
import jakarta.persistence.Table;
13-
1413
import org.hibernate.annotations.NotFound;
1514
import org.hibernate.annotations.NotFoundAction;
16-
15+
import org.hibernate.testing.orm.junit.DomainModel;
1716
import org.hibernate.testing.orm.junit.JiraKey;
18-
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
19-
import org.junit.After;
20-
import org.junit.Test;
21-
22-
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
23-
import static org.junit.Assert.assertNotNull;
24-
import static org.junit.Assert.assertNull;
25-
26-
@JiraKey( value = "HHH-13875")
27-
public class OptionalOneToOneMapsIdQueryTest extends BaseNonConfigCoreFunctionalTestCase {
17+
import org.hibernate.testing.orm.junit.SessionFactory;
18+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
19+
import org.junit.jupiter.api.AfterEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
@JiraKey(value = "HHH-13875")
25+
@DomainModel(
26+
annotatedClasses = {
27+
OptionalOneToOneMapsIdQueryTest.FooHasBarWithIdNamedId.class,
28+
OptionalOneToOneMapsIdQueryTest.BarWithIdNamedId.class,
29+
OptionalOneToOneMapsIdQueryTest.FooHasBarWithNoIdOrPropNamedId.class,
30+
OptionalOneToOneMapsIdQueryTest.BarWithNoIdOrPropNamedId.class,
31+
OptionalOneToOneMapsIdQueryTest.FooHasBarWithNonIdPropNamedId.class,
32+
OptionalOneToOneMapsIdQueryTest.BarWithNonIdPropNamedId.class
33+
}
34+
)
35+
@SessionFactory
36+
public class OptionalOneToOneMapsIdQueryTest {
2837

2938
@Test
30-
public void testOneToOneWithIdNamedId() {
39+
public void testOneToOneWithIdNamedId(SessionFactoryScope scope) {
3140
// Test with associated entity having ID named "id"
32-
doInHibernate( this::sessionFactory, session -> {
41+
scope.inTransaction( session -> {
3342
BarWithIdNamedId bar = new BarWithIdNamedId();
3443
bar.id = 1L;
3544
bar.longValue = 2L;
3645
FooHasBarWithIdNamedId foo = new FooHasBarWithIdNamedId();
37-
foo.id = 1L;
46+
foo.id = 1L;
3847
foo.bar = bar;
3948
session.persist( bar );
4049
session.persist( foo );
41-
});
50+
} );
4251

43-
doInHibernate( this::sessionFactory, session -> {
52+
scope.inTransaction( session -> {
4453
final FooHasBarWithIdNamedId foo = session.createQuery(
45-
"from FooHasBarWithIdNamedId where bar.id = ?1",
46-
FooHasBarWithIdNamedId.class
47-
).setParameter( 1, 1L )
54+
"from FooHasBarWithIdNamedId where bar.id = ?1",
55+
FooHasBarWithIdNamedId.class
56+
).setParameter( 1, 1L )
4857
.uniqueResult();
49-
assertNotNull( foo );
50-
assertNotNull( foo.bar );
51-
});
58+
assertThat( foo ).isNotNull();
59+
assertThat( foo.bar ).isNotNull();
60+
} );
5261

53-
doInHibernate( this::sessionFactory, session -> {
62+
scope.inTransaction( session -> {
5463
final FooHasBarWithIdNamedId foo = session.get( FooHasBarWithIdNamedId.class, 1L );
5564
session.remove( foo.bar );
5665
foo.bar = null;
57-
});
66+
} );
5867

59-
doInHibernate( this::sessionFactory, session -> {
68+
scope.inTransaction( session -> {
6069
final FooHasBarWithIdNamedId foo = session.createQuery(
61-
"from FooHasBarWithIdNamedId where bar.id = ?1",
62-
FooHasBarWithIdNamedId.class
63-
).setParameter( 1, 1L )
70+
"from FooHasBarWithIdNamedId where bar.id = ?1",
71+
FooHasBarWithIdNamedId.class
72+
).setParameter( 1, 1L )
6473
.uniqueResult();
65-
assertNull( foo );
66-
});
74+
assertThat( foo ).isNull();
75+
} );
6776
}
6877

6978
@Test
70-
public void testOneToOneWithNoIdOrPropNamedId() {
79+
public void testOneToOneWithNoIdOrPropNamedId(SessionFactoryScope scope) {
7180
// Test with associated entity having ID not named "id", and with no property named "id"
72-
doInHibernate( this::sessionFactory, session -> {
81+
scope.inTransaction( session -> {
7382
BarWithNoIdOrPropNamedId bar = new BarWithNoIdOrPropNamedId();
7483
bar.barId = 1L;
7584
bar.longValue = 2L;
7685
FooHasBarWithNoIdOrPropNamedId foo = new FooHasBarWithNoIdOrPropNamedId();
77-
foo.id = 1L;
86+
foo.id = 1L;
7887
foo.bar = bar;
7988
session.persist( bar );
8089
session.persist( foo );
81-
});
90+
} );
8291

83-
doInHibernate( this::sessionFactory, session -> {
92+
scope.inTransaction( session -> {
8493
final FooHasBarWithNoIdOrPropNamedId foo = session.createQuery(
85-
"from FooHasBarWithNoIdOrPropNamedId where bar.barId = ?1",
86-
FooHasBarWithNoIdOrPropNamedId.class
87-
).setParameter( 1, 1L )
94+
"from FooHasBarWithNoIdOrPropNamedId where bar.barId = ?1",
95+
FooHasBarWithNoIdOrPropNamedId.class
96+
).setParameter( 1, 1L )
8897
.uniqueResult();
89-
assertNotNull( foo );
90-
assertNotNull( foo.bar );
91-
});
98+
assertThat( foo ).isNotNull();
99+
assertThat( foo.bar ).isNotNull();
100+
} );
92101

93102
// Querying by the generic "id" should work the same as "barId".
94-
doInHibernate( this::sessionFactory, session -> {
103+
scope.inTransaction( session -> {
95104
final FooHasBarWithNoIdOrPropNamedId foo = session.createQuery(
96-
"from FooHasBarWithNoIdOrPropNamedId where bar.id = ?1",
97-
FooHasBarWithNoIdOrPropNamedId.class
98-
).setParameter( 1, 1L )
105+
"from FooHasBarWithNoIdOrPropNamedId where bar.id = ?1",
106+
FooHasBarWithNoIdOrPropNamedId.class
107+
).setParameter( 1, 1L )
99108
.uniqueResult();
100-
assertNotNull( foo );
101-
assertNotNull( foo.bar );
102-
});
109+
assertThat( foo ).isNotNull();
110+
assertThat( foo.bar ).isNotNull();
111+
} );
103112

104-
doInHibernate( this::sessionFactory, session -> {
113+
scope.inTransaction( session -> {
105114
final FooHasBarWithNoIdOrPropNamedId foo = session.get( FooHasBarWithNoIdOrPropNamedId.class, 1L );
106115
session.remove( foo.bar );
107116
foo.bar = null;
108-
});
117+
} );
109118

110-
doInHibernate( this::sessionFactory, session -> {
119+
scope.inTransaction( session -> {
111120
final FooHasBarWithNoIdOrPropNamedId foo = session.createQuery(
112-
"from FooHasBarWithNoIdOrPropNamedId where bar.barId = ?1",
113-
FooHasBarWithNoIdOrPropNamedId.class
114-
).setParameter( 1, 1L )
121+
"from FooHasBarWithNoIdOrPropNamedId where bar.barId = ?1",
122+
FooHasBarWithNoIdOrPropNamedId.class
123+
).setParameter( 1, 1L )
115124
.uniqueResult();
116-
assertNull( foo );
117-
});
125+
assertThat( foo ).isNull();
126+
} );
118127

119128
// Querying by the generic "id" should work the same as "barId".
120-
doInHibernate( this::sessionFactory, session -> {
129+
scope.inTransaction( session -> {
121130
final FooHasBarWithNoIdOrPropNamedId foo = session.createQuery(
122-
"from FooHasBarWithNoIdOrPropNamedId where bar.id = ?1",
123-
FooHasBarWithNoIdOrPropNamedId.class
124-
).setParameter( 1, 1L )
131+
"from FooHasBarWithNoIdOrPropNamedId where bar.id = ?1",
132+
FooHasBarWithNoIdOrPropNamedId.class
133+
).setParameter( 1, 1L )
125134
.uniqueResult();
126-
assertNull( foo );
127-
});
135+
assertThat( foo ).isNull();
136+
} );
128137
}
129138

130139
@Test
131-
public void testOneToOneWithNonIdPropNamedId() {
140+
public void testOneToOneWithNonIdPropNamedId(SessionFactoryScope scope) {
132141
// Test with associated entity having a non-ID property named "id"
133-
doInHibernate( this::sessionFactory, session -> {
142+
scope.inTransaction( session -> {
134143
BarWithNonIdPropNamedId bar = new BarWithNonIdPropNamedId();
135144
bar.barId = 1L;
136145
bar.id = 2L;
137146
FooHasBarWithNonIdPropNamedId foo = new FooHasBarWithNonIdPropNamedId();
138-
foo.id = 1L;
147+
foo.id = 1L;
139148
foo.bar = bar;
140149
session.persist( bar );
141150
session.persist( foo );
142-
});
151+
} );
143152

144-
doInHibernate( this::sessionFactory, session -> {
153+
scope.inTransaction( session -> {
145154
final FooHasBarWithNonIdPropNamedId foo = session.createQuery(
146-
"from FooHasBarWithNonIdPropNamedId where bar.barId = ?1",
147-
FooHasBarWithNonIdPropNamedId.class
148-
).setParameter( 1, 1L )
155+
"from FooHasBarWithNonIdPropNamedId where bar.barId = ?1",
156+
FooHasBarWithNonIdPropNamedId.class
157+
).setParameter( 1, 1L )
149158
.uniqueResult();
150-
assertNotNull( foo );
151-
assertNotNull( foo.bar );
152-
});
159+
assertThat( foo ).isNotNull();
160+
assertThat( foo.bar ).isNotNull();
161+
} );
153162

154163
// bar.id is a non-ID property.
155-
doInHibernate( this::sessionFactory, session -> {
164+
scope.inTransaction( session -> {
156165
final FooHasBarWithNonIdPropNamedId foo = session.createQuery(
157-
"from FooHasBarWithNonIdPropNamedId where bar.id = ?1",
158-
FooHasBarWithNonIdPropNamedId.class
159-
).setParameter( 1, 2L )
166+
"from FooHasBarWithNonIdPropNamedId where bar.id = ?1",
167+
FooHasBarWithNonIdPropNamedId.class
168+
).setParameter( 1, 2L )
160169
.uniqueResult();
161-
assertNotNull( foo );
162-
assertNotNull( foo.bar );
163-
});
170+
assertThat( foo ).isNotNull();
171+
assertThat( foo.bar ).isNotNull();
172+
} );
164173

165174
// bar.id is a non-ID property.
166-
doInHibernate( this::sessionFactory, session -> {
175+
scope.inTransaction( session -> {
167176
final FooHasBarWithNonIdPropNamedId foo = session.createQuery(
168-
"from FooHasBarWithNonIdPropNamedId where bar.id = ?1",
169-
FooHasBarWithNonIdPropNamedId.class
170-
).setParameter( 1, 1L )
177+
"from FooHasBarWithNonIdPropNamedId where bar.id = ?1",
178+
FooHasBarWithNonIdPropNamedId.class
179+
).setParameter( 1, 1L )
171180
.uniqueResult();
172-
assertNull( foo );
173-
});
181+
assertThat( foo ).isNull();
182+
} );
174183

175-
doInHibernate( this::sessionFactory, session -> {
184+
scope.inTransaction( session -> {
176185
final FooHasBarWithNonIdPropNamedId foo = session.get( FooHasBarWithNonIdPropNamedId.class, 1L );
177186
session.remove( foo.bar );
178187
foo.bar = null;
179-
});
188+
} );
180189

181-
doInHibernate( this::sessionFactory, session -> {
190+
scope.inTransaction( session -> {
182191
final FooHasBarWithNonIdPropNamedId foo = session.createQuery(
183-
"from FooHasBarWithNonIdPropNamedId where bar.barId = ?1",
184-
FooHasBarWithNonIdPropNamedId.class
185-
).setParameter( 1, 1L )
192+
"from FooHasBarWithNonIdPropNamedId where bar.barId = ?1",
193+
FooHasBarWithNonIdPropNamedId.class
194+
).setParameter( 1, 1L )
186195
.uniqueResult();
187-
assertNull( foo );
188-
});
196+
assertThat( foo ).isNull();
197+
} );
189198

190-
doInHibernate( this::sessionFactory, session -> {
199+
scope.inTransaction( session -> {
191200
final FooHasBarWithNonIdPropNamedId foo = session.createQuery(
192-
"from FooHasBarWithNonIdPropNamedId where bar.id = ?1",
193-
FooHasBarWithNonIdPropNamedId.class
194-
).setParameter( 1, 1L )
201+
"from FooHasBarWithNonIdPropNamedId where bar.id = ?1",
202+
FooHasBarWithNonIdPropNamedId.class
203+
).setParameter( 1, 1L )
195204
.uniqueResult();
196-
assertNull( foo );
197-
});
205+
assertThat( foo ).isNull();
206+
} );
198207

199-
doInHibernate( this::sessionFactory, session -> {
208+
scope.inTransaction( session -> {
200209
final FooHasBarWithNonIdPropNamedId foo = session.createQuery(
201-
"from FooHasBarWithNonIdPropNamedId where bar.id = ?1",
202-
FooHasBarWithNonIdPropNamedId.class
203-
).setParameter( 1, 2L )
210+
"from FooHasBarWithNonIdPropNamedId where bar.id = ?1",
211+
FooHasBarWithNonIdPropNamedId.class
212+
).setParameter( 1, 2L )
204213
.uniqueResult();
205-
assertNull( foo );
206-
});
207-
}
208-
209-
@After
210-
public void cleanupData() {
211-
doInHibernate( this::sessionFactory, session -> {
212-
session.createQuery( "delete from FooHasBarWithIdNamedId" ).executeUpdate();
213-
session.createQuery( "delete from FooHasBarWithNoIdOrPropNamedId" ).executeUpdate();
214-
session.createQuery( "delete from FooHasBarWithNonIdPropNamedId" ).executeUpdate();
215-
session.createQuery( "delete from BarWithIdNamedId" ).executeUpdate();
216-
session.createQuery( "delete from BarWithNoIdOrPropNamedId" ).executeUpdate();
217-
session.createQuery( "delete from BarWithNoIdOrPropNamedId" ).executeUpdate();
218-
});
214+
assertThat( foo ).isNull();
215+
} );
219216
}
220217

221-
@Override
222-
protected Class[] getAnnotatedClasses()
223-
{
224-
return new Class[] {
225-
FooHasBarWithIdNamedId.class,
226-
BarWithIdNamedId.class,
227-
FooHasBarWithNoIdOrPropNamedId.class,
228-
BarWithNoIdOrPropNamedId.class,
229-
FooHasBarWithNonIdPropNamedId.class,
230-
BarWithNonIdPropNamedId.class
231-
};
218+
@AfterEach
219+
public void cleanupData(SessionFactoryScope scope) {
220+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
232221
}
233222

234223
@Entity(name = "FooHasBarWithIdNamedId")
235-
public static class FooHasBarWithIdNamedId
236-
{
224+
public static class FooHasBarWithIdNamedId {
237225
@Id
238226
private Long id;
239227

240-
@OneToOne(optional = true)
228+
@OneToOne
241229
@MapsId
242230
@JoinColumn(name = "id")
243231
@NotFound(action = NotFoundAction.IGNORE)
@@ -253,12 +241,11 @@ public static class BarWithIdNamedId {
253241

254242
@Entity(name = "FooHasBarWithNoIdOrPropNamedId")
255243
@Table(name = "FooHasBarNoIdOrPropNamedId")
256-
public static class FooHasBarWithNoIdOrPropNamedId
257-
{
244+
public static class FooHasBarWithNoIdOrPropNamedId {
258245
@Id
259246
private Long id;
260247

261-
@OneToOne(optional = true)
248+
@OneToOne
262249
@MapsId
263250
@JoinColumn(name = "id")
264251
@NotFound(action = NotFoundAction.IGNORE)
@@ -274,12 +261,11 @@ public static class BarWithNoIdOrPropNamedId {
274261

275262
@Entity(name = "FooHasBarWithNonIdPropNamedId")
276263
@Table(name = "FooHasBarNonIdPropNamedId")
277-
public static class FooHasBarWithNonIdPropNamedId
278-
{
264+
public static class FooHasBarWithNonIdPropNamedId {
279265
@Id
280266
private Long id;
281267

282-
@OneToOne(optional = true)
268+
@OneToOne
283269
@MapsId
284270
@JoinColumn(name = "id")
285271
@NotFound(action = NotFoundAction.IGNORE)

0 commit comments

Comments
 (0)