Skip to content

Commit d627207

Browse files
committed
HHH-19846 Drop JUnit 4 usage
1 parent 7f4c320 commit d627207

File tree

4 files changed

+103
-118
lines changed

4 files changed

+103
-118
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/compositeelement/Child.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ public int getBioLength() {
7070
return bioLength;
7171
}
7272
public void setBioLength(Integer bioLength) {
73-
this.bioLength = bioLength==null ? 0 : bioLength.intValue();
73+
this.bioLength = bioLength==null ? 0 : bioLength;
7474
}
7575
}

hibernate-core/src/test/java/org/hibernate/orm/test/compositeelement/CompositeElementTest.java

Lines changed: 73 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,77 @@
44
*/
55
package org.hibernate.orm.test.compositeelement;
66

7-
import org.hibernate.Session;
8-
import org.hibernate.Transaction;
9-
import org.hibernate.boot.Metadata;
10-
import org.hibernate.mapping.Collection;
11-
import org.hibernate.mapping.Component;
12-
import org.hibernate.mapping.Formula;
13-
14-
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
15-
import org.junit.Test;
16-
177
import jakarta.persistence.criteria.CriteriaBuilder;
188
import jakarta.persistence.criteria.CriteriaQuery;
199
import jakarta.persistence.criteria.Root;
10+
import org.hibernate.testing.orm.junit.DomainModel;
11+
import org.hibernate.testing.orm.junit.SessionFactory;
12+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.Test;
2015

21-
import static org.junit.Assert.assertEquals;
16+
import static org.assertj.core.api.Assertions.assertThat;
2217

2318
/**
2419
* @author Gavin King
2520
*/
26-
public class CompositeElementTest extends BaseNonConfigCoreFunctionalTestCase {
27-
28-
@Override
29-
protected String getBaseForMappings() {
30-
return "";
31-
}
32-
33-
@Override
34-
public String[] getMappings() {
35-
return new String[] { "org/hibernate/orm/test/compositeelement/Parent.hbm.xml" };
36-
}
37-
38-
@Override
39-
protected void afterMetadataBuilt(Metadata metadata) {
40-
Collection children = metadata.getCollectionBinding( Parent.class.getName() + ".children" );
41-
Component childComponents = ( Component ) children.getElement();
42-
Formula f = ( Formula ) childComponents.getProperty( "bioLength" ).getValue().getSelectables().get( 0 );
43-
44-
// SQLFunction lengthFunction = metadata.getDatabase().getJdbcEnvironment().getDialect().getFunctions().get( "length" );
45-
// if ( lengthFunction != null ) {
46-
// ArrayList args = new ArrayList();
47-
// args.add( "bio" );
48-
// f.setFormula( lengthFunction.render( StandardBasicTypes.INTEGER, args, null ) );
49-
// }
21+
@DomainModel(
22+
xmlMappings = "org/hibernate/orm/test/compositeelement/Parent.hbm.xml"
23+
)
24+
@SessionFactory
25+
public class CompositeElementTest {
26+
27+
@AfterEach
28+
public void tearDown(SessionFactoryScope scope) {
29+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
5030
}
5131

5232
@Test
53-
public void testHandSQL() {
54-
Session s = openSession();
55-
Transaction t = s.beginTransaction();
56-
Child c = new Child( "Child One" );
57-
Parent p = new Parent( "Parent" );
58-
p.getChildren().add( c );
59-
c.setParent( p );
60-
s.persist( p );
61-
s.flush();
62-
63-
p.getChildren().remove( c );
64-
c.setParent( null );
65-
s.flush();
66-
67-
p.getChildren().add( c );
68-
c.setParent( p );
69-
t.commit();
70-
s.close();
71-
72-
s = openSession();
73-
t = s.beginTransaction();
74-
s.createQuery( "select distinct p from Parent p join p.children c where c.name like 'Child%'" ).uniqueResult();
75-
s.clear();
76-
s.createQuery( "select new Child(c.name) from Parent p left outer join p.children c where c.name like 'Child%'" )
77-
.uniqueResult();
78-
s.clear();
79-
//s.createQuery("select c from Parent p left outer join p.children c where c.name like 'Child%'").uniqueResult(); //we really need to be able to do this!
80-
s.clear();
81-
p = ( Parent ) s.createQuery( "from Parent p left join fetch p.children" ).uniqueResult();
82-
t.commit();
83-
s.close();
84-
85-
s = openSession();
86-
t = s.beginTransaction();
87-
s.remove( p );
88-
t.commit();
89-
s.close();
33+
public void testHandSQL(SessionFactoryScope scope) {
34+
scope.inTransaction(
35+
session -> {
36+
Child c = new Child( "Child One" );
37+
Parent p = new Parent( "Parent" );
38+
p.getChildren().add( c );
39+
c.setParent( p );
40+
session.persist( p );
41+
session.flush();
42+
43+
p.getChildren().remove( c );
44+
c.setParent( null );
45+
session.flush();
46+
47+
p.getChildren().add( c );
48+
c.setParent( p );
49+
}
50+
);
51+
52+
Parent parent = scope.fromTransaction( session -> {
53+
session.createQuery( "select distinct p from Parent p join p.children c where c.name like 'Child%'",
54+
Parent.class )
55+
.uniqueResult();
56+
session.clear();
57+
session.createQuery(
58+
"select new Child(c.name) from Parent p left outer join p.children c where c.name like 'Child%'",
59+
Child.class )
60+
.uniqueResult();
61+
session.clear();
62+
//s.createQuery("select c from Parent p left outer join p.children c where c.name like 'Child%'").uniqueResult(); //we really need to be able to do this!
63+
session.clear();
64+
return session.createQuery( "from Parent p left join fetch p.children", Parent.class )
65+
.uniqueResult();
66+
}
67+
);
68+
69+
scope.inTransaction(
70+
session ->
71+
session.remove( parent )
72+
);
9073
}
9174

9275
@Test
93-
public void testCustomColumnReadAndWrite() {
94-
inTransaction( s -> {
76+
public void testCustomColumnReadAndWrite(SessionFactoryScope scope) {
77+
scope.inTransaction( s -> {
9578
Child c = new Child( "Child One" );
9679
c.setPosition( 1 );
9780
Parent p = new Parent( "Parent" );
@@ -102,38 +85,38 @@ public void testCustomColumnReadAndWrite() {
10285

10386
// Oracle returns BigDecimaal while other dialects return Integer;
10487
// casting to Number so it works on all dialects
105-
Number sqlValue = ( (Number) s.createNativeQuery(
106-
"select child_position from ParentChild c where c.name='Child One'" )
107-
.uniqueResult() );
108-
assertEquals( 0, sqlValue.intValue() );
88+
Number sqlValue = ((Number) s.createNativeQuery(
89+
"select child_position from ParentChild c where c.name='Child One'" )
90+
.uniqueResult());
91+
assertThat( sqlValue.intValue() ).isEqualTo( 0 );
10992

110-
Integer hqlValue = (Integer) s.createQuery(
111-
"select c.position from Parent p join p.children c where p.name='Parent'" )
93+
Integer hqlValue = s.createQuery(
94+
"select c.position from Parent p join p.children c where p.name='Parent'", Integer.class )
11295
.uniqueResult();
113-
assertEquals( 1, hqlValue.intValue() );
96+
assertThat( hqlValue ).isEqualTo( 1 );
11497

11598
// p = (Parent) s.createCriteria( Parent.class ).add( Restrictions.eq( "name", "Parent" ) ).uniqueResult();
11699

117100
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
118101
CriteriaQuery<Parent> criteria = criteriaBuilder.createQuery( Parent.class );
119102
Root<Parent> root = criteria.from( Parent.class );
120-
criteria.where( criteriaBuilder.equal( root.get( "name" ),"Parent" ) );
103+
criteria.where( criteriaBuilder.equal( root.get( "name" ), "Parent" ) );
121104

122105
p = s.createQuery( criteria ).uniqueResult();
123106

124-
c = (Child) p.getChildren().iterator().next();
125-
assertEquals( 1, c.getPosition() );
107+
c = p.getChildren().iterator().next();
108+
assertThat( c.getPosition() ).isEqualTo( 1 );
126109

127110
p = s.createQuery( "from Parent p join p.children c where c.position = 1", Parent.class ).uniqueResult();
128-
c = (Child) p.getChildren().iterator().next();
129-
assertEquals( 1, c.getPosition() );
111+
c = p.getChildren().iterator().next();
112+
assertThat( c.getPosition() ).isEqualTo( 1 );
130113

131114
c.setPosition( 2 );
132115
s.flush();
133-
sqlValue = ( (Number) s.createNativeQuery(
134-
"select child_position from ParentChild c where c.name='Child One'" )
135-
.uniqueResult() );
136-
assertEquals( 1, sqlValue.intValue() );
116+
sqlValue = ((Number) s.createNativeQuery(
117+
"select child_position from ParentChild c where c.name='Child One'" )
118+
.uniqueResult());
119+
assertThat( sqlValue.intValue() ).isEqualTo( 1 );
137120
s.remove( p );
138121
} );
139122
}

hibernate-core/src/test/java/org/hibernate/orm/test/compositeelement/Parent.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
55
package org.hibernate.orm.test.compositeelement;
6+
67
import java.util.Collection;
78
import java.util.HashSet;
89

@@ -12,21 +13,21 @@
1213
public class Parent {
1314
private Long id;
1415
private String name;
15-
private Collection children = new HashSet();
16+
private Collection<Child> children = new HashSet<>();
1617
Parent() {}
1718
public Parent(String name) {
1819
this.name = name;
1920
}
2021
/**
2122
* @return Returns the children.
2223
*/
23-
public Collection getChildren() {
24+
public Collection<Child> getChildren() {
2425
return children;
2526
}
2627
/**
2728
* @param children The children to set.
2829
*/
29-
public void setChildren(Collection children) {
30+
public void setChildren(Collection<Child> children) {
3031
this.children = children;
3132
}
3233
/**

hibernate-core/src/test/java/org/hibernate/orm/test/connection/ConnectionCreatorTest.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
*/
55
package org.hibernate.orm.test.connection;
66

7-
import java.sql.Connection;
8-
import java.sql.Driver;
9-
import java.util.Collections;
10-
import java.util.HashMap;
11-
import java.util.List;
12-
import java.util.Map;
13-
import java.util.Properties;
14-
157
import org.hibernate.boot.registry.BootstrapServiceRegistry;
168
import org.hibernate.boot.registry.StandardServiceInitiator;
179
import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl;
@@ -25,19 +17,28 @@
2517
import org.hibernate.exception.JDBCConnectionException;
2618
import org.hibernate.service.Service;
2719
import org.hibernate.service.internal.ProvidedService;
28-
import org.hibernate.testing.junit4.BaseUnitTestCase;
20+
import org.hibernate.testing.orm.junit.BaseUnitTest;
2921
import org.hibernate.testing.orm.junit.JiraKey;
22+
import org.junit.jupiter.api.Test;
3023

31-
import org.junit.Test;
24+
import java.sql.Connection;
25+
import java.sql.Driver;
26+
import java.util.Collections;
27+
import java.util.HashMap;
28+
import java.util.List;
29+
import java.util.Map;
30+
import java.util.Properties;
3231

33-
import static org.junit.Assert.fail;
32+
import static org.junit.jupiter.api.Assertions.assertThrows;
3433

3534
/**
3635
* @author Steve Ebersole
3736
*/
38-
public class ConnectionCreatorTest extends BaseUnitTestCase {
37+
@BaseUnitTest
38+
public class ConnectionCreatorTest {
39+
3940
@Test
40-
@JiraKey(value = "HHH-8621" )
41+
@JiraKey(value = "HHH-8621")
4142
public void testBadUrl() throws Exception {
4243
DriverConnectionCreator connectionCreator = new DriverConnectionCreator(
4344
(Driver) Class.forName( "org.h2.Driver" ).newInstance(),
@@ -55,13 +56,11 @@ public void testBadUrl() throws Exception {
5556
null
5657
);
5758

58-
try {
59-
Connection conn = connectionCreator.createConnection();
60-
conn.close();
61-
fail( "Expecting the bad Connection URL to cause an exception" );
62-
}
63-
catch (JDBCConnectionException expected) {
64-
}
59+
assertThrows( JDBCConnectionException.class, () -> {
60+
Connection conn = connectionCreator.createConnection();
61+
conn.close();
62+
},
63+
"Expecting the bad Connection URL to cause an exception" );
6564
}
6665

6766
private final static class CCTStandardServiceRegistryImpl extends StandardServiceRegistryImpl {
@@ -72,16 +71,17 @@ private CCTStandardServiceRegistryImpl(
7271
Map<String, Object> configurationValues) {
7372
super( autoCloseRegistry, bootstrapServiceRegistry, configurationValues );
7473
}
74+
7575
@Override
7676
@SuppressWarnings("unchecked")
7777
public <R extends Service> R getService(Class<R> serviceRole) {
7878
if ( JdbcServices.class.equals( serviceRole ) ) {
7979
// return a new, not fully initialized JdbcServicesImpl
80-
JdbcServicesImpl jdbcServices = new JdbcServicesImpl(this);
80+
JdbcServicesImpl jdbcServices = new JdbcServicesImpl( this );
8181
jdbcServices.configure( new HashMap<>() );
8282
return (R) jdbcServices;
8383
}
84-
if( JdbcEnvironment.class.equals( serviceRole ) ){
84+
if ( JdbcEnvironment.class.equals( serviceRole ) ) {
8585
return (R) new JdbcEnvironmentImpl( this, new H2Dialect() );
8686
}
8787
return super.getService( serviceRole );
@@ -92,9 +92,10 @@ public static CCTStandardServiceRegistryImpl create(
9292
BootstrapServiceRegistry bootstrapServiceRegistry,
9393
List<StandardServiceInitiator<?>> serviceInitiators,
9494
List<ProvidedService<?>> providedServices,
95-
Map<String,Object> configurationValues) {
95+
Map<String, Object> configurationValues) {
9696

97-
CCTStandardServiceRegistryImpl instance = new CCTStandardServiceRegistryImpl( autoCloseRegistry, bootstrapServiceRegistry, configurationValues );
97+
CCTStandardServiceRegistryImpl instance = new CCTStandardServiceRegistryImpl( autoCloseRegistry,
98+
bootstrapServiceRegistry, configurationValues );
9899
instance.initialize();
99100
instance.applyServiceRegistrations( serviceInitiators, providedServices );
100101

0 commit comments

Comments
 (0)