Skip to content

Commit f7ad8fd

Browse files
committed
HHH-19846 - Drop JUnit 4 usage
Not converted... * org.hibernate.orm.test.hql.PostgreSQLFunctionSelectClauseTest - registering custom function * org.hibernate.orm.test.hql.PostgreSQLFunctionWhereClauseTest - aux-db-object * org.hibernate.orm.test.id.usertype - type registrations * org.hibernate.orm.test.idgen.enhanced.HiloOptimizerConcurrencyTest - recreation of SF during tests * org.hibernate.orm.test.type.AbstractJavaTimeTypeTest subtypes - crazy parameterization (see org.hibernate.orm.test.tm.InterceptorTransactionTest) * org.hibernate.orm.test.cdi.general.hibernatesearch.extended.HibernateSearchExtendedCdiSupportTest - not sure yet, all the other tests here pass with conversion - shelved for now
1 parent 78a5305 commit f7ad8fd

File tree

12 files changed

+446
-489
lines changed

12 files changed

+446
-489
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/fetch/runtime/managed/RuntimeFetchFromManagedTest.java

Lines changed: 99 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -4,157 +4,136 @@
44
*/
55
package org.hibernate.orm.test.fetch.runtime.managed;
66

7-
import java.util.Collections;
87
import jakarta.persistence.Entity;
98
import jakarta.persistence.FetchType;
109
import jakarta.persistence.Id;
1110
import jakarta.persistence.JoinColumn;
1211
import jakarta.persistence.ManyToOne;
1312
import jakarta.persistence.Table;
14-
1513
import org.hibernate.Hibernate;
16-
import org.hibernate.boot.MetadataSources;
17-
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
18-
import org.hibernate.cfg.AvailableSettings;
1914
import org.hibernate.graph.spi.RootGraphImplementor;
2015
import org.hibernate.query.Query;
21-
22-
import org.hibernate.testing.FailureExpected;
16+
import org.hibernate.testing.orm.junit.DomainModel;
17+
import org.hibernate.testing.orm.junit.FailureExpected;
2318
import org.hibernate.testing.orm.junit.JiraKey;
24-
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
25-
import org.junit.After;
26-
import org.junit.Before;
27-
import org.junit.Test;
19+
import org.hibernate.testing.orm.junit.ServiceRegistry;
20+
import org.hibernate.testing.orm.junit.SessionFactory;
21+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
22+
import org.hibernate.testing.orm.junit.Setting;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
27+
import java.util.Collections;
2828

2929
import static org.hamcrest.CoreMatchers.is;
3030
import static org.hamcrest.MatcherAssert.assertThat;
31+
import static org.hibernate.cfg.CacheSettings.USE_SECOND_LEVEL_CACHE;
3132

3233
/**
3334
* @author Steve Ebersole
3435
*/
35-
@JiraKey( value = "HHH-13152" )
36-
public class RuntimeFetchFromManagedTest extends BaseNonConfigCoreFunctionalTestCase {
36+
@SuppressWarnings("JUnitMalformedDeclaration")
37+
@JiraKey( "HHH-13152" )
38+
@ServiceRegistry(settings = @Setting(name = USE_SECOND_LEVEL_CACHE, value = "false"))
39+
@DomainModel(annotatedClasses = {RuntimeFetchFromManagedTest.RootEntity.class, RuntimeFetchFromManagedTest.ChildEntity.class})
40+
@SessionFactory
41+
public class RuntimeFetchFromManagedTest {
3742

3843
@Test
39-
public void testFetchingFromManagedEntityHql() {
40-
inTransaction(
41-
session -> {
42-
{
43-
// let's load the root - because the link to child is lazy, this should
44-
// not load it
45-
final RootEntity rootEntity = session.get( RootEntity.class, 2 );
46-
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
47-
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) );
48-
}
49-
50-
{
51-
// now try to perform an HQL join fetch
52-
final RootEntity rootEntity = session.createQuery(
53-
"select r from RootEntity r join fetch r.child",
54-
RootEntity.class
55-
).uniqueResult();
56-
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
57-
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) );
58-
}
59-
}
60-
);
44+
public void testFetchingFromManagedEntityHql(SessionFactoryScope factoryScope) {
45+
factoryScope.inTransaction(session -> {
46+
{
47+
// let's load the root - because the link to child is lazy, this should
48+
// not load it
49+
final RootEntity rootEntity = session.get( RootEntity.class, 2 );
50+
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
51+
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) );
52+
}
53+
54+
{
55+
// now try to perform an HQL join fetch
56+
final RootEntity rootEntity = session.createQuery(
57+
"select r from RootEntity r join fetch r.child",
58+
RootEntity.class
59+
).uniqueResult();
60+
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
61+
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) );
62+
}
63+
} );
6164
}
6265

6366
@Test
64-
@FailureExpected( jiraKey = "", message = "The entity is returned directly from the PC" )
65-
public void testFetchingFromManagedEntityEntityGraphLoad() {
66-
inTransaction(
67-
session -> {
68-
{
69-
// let's load the root - because the link to child is lazy, this should
70-
// not load it
71-
final RootEntity rootEntity = session.get( RootEntity.class, 2 );
72-
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
73-
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) );
74-
}
75-
76-
{
77-
// now try to load the root entity again using an EntityGraph that specifies to fetch child
78-
final RootGraphImplementor<RootEntity> entityGraph = session.createEntityGraph( RootEntity.class );
79-
entityGraph.addAttributeNode( "child" );
80-
81-
final RootEntity rootEntity = session.find(
82-
RootEntity.class,
83-
2,
84-
Collections.singletonMap( "javax.persistence.loadgraph", entityGraph )
85-
);
86-
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
87-
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) );
88-
}
89-
}
90-
);
67+
@FailureExpected(reason = "The entity is returned directly from the PC" )
68+
public void testFetchingFromManagedEntityEntityGraphLoad(SessionFactoryScope factoryScope) {
69+
factoryScope.inTransaction(session -> {
70+
{
71+
// let's load the root - because the link to child is lazy, this should
72+
// not load it
73+
final RootEntity rootEntity = session.get( RootEntity.class, 2 );
74+
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
75+
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) );
76+
}
77+
78+
{
79+
// now try to load the root entity again using an EntityGraph that specifies to fetch child
80+
final RootGraphImplementor<RootEntity> entityGraph = session.createEntityGraph( RootEntity.class );
81+
entityGraph.addAttributeNode( "child" );
82+
83+
final RootEntity rootEntity = session.find(
84+
RootEntity.class,
85+
2,
86+
Collections.singletonMap( "javax.persistence.loadgraph", entityGraph )
87+
);
88+
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
89+
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) );
90+
}
91+
} );
9192
}
9293

9394
@Test
94-
public void testFetchingFromManagedEntityEntityGraphHql() {
95-
inTransaction(
96-
session -> {
97-
{
98-
// let's load the root - because the link to child is lazy, this should
99-
// not load it
100-
final RootEntity rootEntity = session.get( RootEntity.class, 2 );
101-
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
102-
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) );
103-
}
104-
105-
{
106-
// now try to query the root entity again using an EntityGraph that specifies to fetch child
107-
final RootGraphImplementor<RootEntity> entityGraph = session.createEntityGraph( RootEntity.class );
108-
entityGraph.addAttributeNode( "child" );
109-
110-
final Query<RootEntity> query = session.createQuery(
111-
"select r from RootEntity r",
112-
RootEntity.class
113-
);
114-
115-
final RootEntity rootEntity = query.setHint( "javax.persistence.loadgraph", entityGraph ).uniqueResult();
116-
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
117-
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) );
118-
}
119-
}
120-
);
95+
public void testFetchingFromManagedEntityEntityGraphHql(SessionFactoryScope factoryScope) {
96+
factoryScope.inTransaction(session -> {
97+
{
98+
// let's load the root - because the link to child is lazy, this should
99+
// not load it
100+
final RootEntity rootEntity = session.get( RootEntity.class, 2 );
101+
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
102+
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) );
103+
}
104+
105+
{
106+
// now try to query the root entity again using an EntityGraph that specifies to fetch child
107+
final RootGraphImplementor<RootEntity> entityGraph = session.createEntityGraph( RootEntity.class );
108+
entityGraph.addAttributeNode( "child" );
109+
110+
final Query<RootEntity> query = session.createQuery(
111+
"select r from RootEntity r",
112+
RootEntity.class
113+
);
114+
115+
final RootEntity rootEntity = query.setHint( "javax.persistence.loadgraph", entityGraph ).uniqueResult();
116+
assertThat( Hibernate.isInitialized( rootEntity ), is( true ) );
117+
assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) );
118+
}
119+
} );
121120
}
122121

123-
@Before
124-
public void createTestData() {
122+
@BeforeEach
123+
public void createTestData(SessionFactoryScope factoryScope) {
125124
// create some test
126-
inTransaction(
127-
session -> {
128-
final ChildEntity child = new ChildEntity( 1, "child" );
129-
session.persist( child );
130-
131-
final RootEntity root = new RootEntity( 2, "root", child );
132-
session.persist( root );
133-
}
134-
);
135-
}
136-
137-
@After
138-
public void cleanUpTestData() {
139-
inTransaction(
140-
session -> {
141-
session.createQuery( "delete from RootEntity" ).executeUpdate();
142-
session.createQuery( "delete from ChildEntity" ).executeUpdate();
143-
}
144-
);
145-
}
125+
factoryScope.inTransaction(session -> {
126+
final ChildEntity child = new ChildEntity( 1, "child" );
127+
session.persist( child );
146128

147-
@Override
148-
protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) {
149-
super.configureStandardServiceRegistryBuilder( ssrb );
150-
ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" );
129+
final RootEntity root = new RootEntity( 2, "root", child );
130+
session.persist( root );
131+
} );
151132
}
152133

153-
@Override
154-
protected void applyMetadataSources(MetadataSources sources) {
155-
super.applyMetadataSources( sources );
156-
sources.addAnnotatedClass( RootEntity.class );
157-
sources.addAnnotatedClass( ChildEntity.class );
134+
@AfterEach
135+
public void cleanUpTestData(SessionFactoryScope factoryScope) {
136+
factoryScope.dropData();
158137
}
159138

160139
@Entity( name = "RootEntity" )

hibernate-core/src/test/java/org/hibernate/orm/test/fetching/BatchFetchingTest.java

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,43 @@
44
*/
55
package org.hibernate.orm.test.fetching;
66

7-
import java.util.ArrayList;
8-
import java.util.List;
97
import jakarta.persistence.Entity;
108
import jakarta.persistence.FetchType;
119
import jakarta.persistence.Id;
1210
import jakarta.persistence.ManyToOne;
1311
import jakarta.persistence.OneToMany;
14-
1512
import org.hibernate.annotations.BatchSize;
1613
import org.hibernate.annotations.NaturalId;
17-
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.jboss.logging.Logger;
18+
import org.junit.jupiter.api.AfterEach;
19+
import org.junit.jupiter.api.Test;
1820

19-
import org.junit.Test;
20-
21-
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
21+
import java.util.ArrayList;
22+
import java.util.List;
2223

2324
/**
2425
* @author Vlad Mihalcea
2526
*/
26-
public class BatchFetchingTest extends BaseEntityManagerFunctionalTestCase {
27-
28-
@Override
29-
protected Class<?>[] getAnnotatedClasses() {
30-
return new Class<?>[] {
31-
Department.class,
32-
Employee.class
33-
};
27+
@SuppressWarnings("JUnitMalformedDeclaration")
28+
@DomainModel(annotatedClasses = {
29+
BatchFetchingTest.Department.class,
30+
BatchFetchingTest.Employee.class
31+
})
32+
@SessionFactory
33+
public class BatchFetchingTest {
34+
private final Logger log = Logger.getLogger( BatchFetchingTest.class );
35+
36+
@AfterEach
37+
void tearDown(SessionFactoryScope factoryScope) {
38+
factoryScope.dropData();
3439
}
3540

3641
@Test
37-
public void test() {
38-
doInJPA(this::entityManagerFactory, entityManager -> {
42+
public void test(SessionFactoryScope factoryScope) {
43+
factoryScope.inTransaction( (entityManager) -> {
3944
for (long i = 0; i < 10; i++) {
4045
Department department = new Department();
4146
department.id = i;
@@ -49,28 +54,27 @@ public void test() {
4954
entityManager.persist(employee);
5055
department.employees.add(employee);
5156
}
52-
entityManager.flush();
5357
}
54-
});
58+
} );
5559

56-
doInJPA(this::entityManagerFactory, entityManager -> {
60+
factoryScope.inTransaction( (entityManager) -> {
5761
//tag::fetching-batch-fetching-example[]
5862
List<Department> departments = entityManager.createQuery(
59-
"select d " +
60-
"from Department d " +
61-
"inner join d.employees e " +
62-
"where e.name like 'John%'", Department.class)
63-
.getResultList();
63+
"select d " +
64+
"from Department d " +
65+
"inner join d.employees e " +
66+
"where e.name like 'John%'", Department.class)
67+
.getResultList();
6468

6569
for (Department department : departments) {
6670
log.infof(
67-
"Department %d has {} employees",
68-
department.getId(),
69-
department.getEmployees().size()
71+
"Department %d has {} employees",
72+
department.getId(),
73+
department.getEmployees().size()
7074
);
7175
}
7276
//end::fetching-batch-fetching-example[]
73-
});
77+
} );
7478
}
7579

7680
//tag::fetching-batch-mapping-example[]

0 commit comments

Comments
 (0)