Skip to content

Commit 6b27991

Browse files
committed
HHH-19846 Drop JUnit 4 usage
1 parent bb88bba commit 6b27991

11 files changed

+280
-266
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/bulkid/AbstractMutationStrategyGeneratedIdWithOptimizerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import jakarta.persistence.Id;
1111
import jakarta.persistence.Inheritance;
1212
import jakarta.persistence.InheritanceType;
13+
import jakarta.persistence.SequenceGenerator;
1314
import org.hibernate.testing.orm.junit.DomainModel;
1415
import org.hibernate.testing.orm.junit.SessionFactory;
1516
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -112,7 +113,8 @@ public void testInsertSelectGenerated(SessionFactoryScope scope) {
112113
public static class Person {
113114

114115
@Id
115-
@GeneratedValue(strategy = GenerationType.SEQUENCE)
116+
@GeneratedValue( generator = "a_sequence")
117+
@SequenceGenerator(name = "a_sequence", sequenceName = "person_sequence")
116118
private Integer id;
117119

118120
private String name;

hibernate-core/src/test/java/org/hibernate/orm/test/serialization/GetterSetterSerializationTest.java

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44
*/
55
package org.hibernate.orm.test.serialization;
66

7-
import java.io.ByteArrayInputStream;
8-
import java.io.ByteArrayOutputStream;
9-
import java.io.ObjectInputStream;
10-
import java.io.ObjectOutputStream;
11-
12-
import org.junit.Test;
13-
147
import org.hibernate.internal.util.ReflectHelper;
158
import org.hibernate.orm.test.serialization.entity.AnEntity;
169
import org.hibernate.orm.test.serialization.entity.PK;
@@ -20,10 +13,15 @@
2013
import org.hibernate.property.access.spi.Setter;
2114
import org.hibernate.property.access.spi.SetterFieldImpl;
2215
import org.hibernate.property.access.spi.SetterMethodImpl;
23-
2416
import org.hibernate.testing.orm.junit.JiraKey;
17+
import org.junit.jupiter.api.Test;
2518

26-
import static org.junit.Assert.assertSame;
19+
import java.io.ByteArrayInputStream;
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.ObjectInputStream;
22+
import java.io.ObjectOutputStream;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
2725

2826
/**
2927
* Tests that the can access inaccessible private fields and
@@ -34,59 +32,68 @@
3432
public class GetterSetterSerializationTest {
3533

3634
@Test
37-
@JiraKey( value = "HHH-11202")
35+
@JiraKey(value = "HHH-11202")
3836
public void testPrivateFieldGetter() throws Exception {
3937
final AnEntity entity = new AnEntity( new PK( 1L ) );
4038

4139
final String propertyName = "pk";
4240
final Getter getter = new GetterFieldImpl(
4341
AnEntity.class,
4442
propertyName,
45-
ReflectHelper.findField( AnEntity.class, propertyName)
43+
ReflectHelper.findField( AnEntity.class, propertyName )
4644
);
47-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
48-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
49-
oos.writeObject( getter );
5045

51-
final ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( baos.toByteArray() ) );
46+
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
47+
try (final ObjectOutputStream oos = new ObjectOutputStream( baos )) {
48+
oos.writeObject( getter );
5249

53-
final Getter getterClone = (Getter) ois.readObject();
50+
try (final ObjectInputStream ois = new ObjectInputStream(
51+
new ByteArrayInputStream( baos.toByteArray() ) )) {
5452

55-
assertSame( getter.get( entity ), getterClone.get( entity ) );
53+
final Getter getterClone = (Getter) ois.readObject();
54+
55+
assertThat( getterClone.get( entity ) ).isSameAs( getter.get( entity ) );
56+
}
57+
}
58+
}
5659
}
5760

5861
@Test
59-
@JiraKey( value = "HHH-11202")
62+
@JiraKey(value = "HHH-11202")
6063
public void testPrivateFieldSetter() throws Exception {
6164
AnEntity entity = new AnEntity( new PK( 1L ) );
6265

6366
final String propertyName = "pk";
6467
final Getter getter = new GetterFieldImpl(
6568
AnEntity.class,
6669
propertyName,
67-
ReflectHelper.findField( AnEntity.class, propertyName)
70+
ReflectHelper.findField( AnEntity.class, propertyName )
6871
);
6972
final Setter setter = new SetterFieldImpl(
7073
AnEntity.class,
7174
propertyName,
72-
ReflectHelper.findField( AnEntity.class, propertyName)
75+
ReflectHelper.findField( AnEntity.class, propertyName )
7376
);
7477

75-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
76-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
77-
oos.writeObject( setter );
78+
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
79+
try (final ObjectOutputStream oos = new ObjectOutputStream( baos )) {
80+
oos.writeObject( setter );
7881

79-
final ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( baos.toByteArray() ) );
82+
try (final ObjectInputStream ois = new ObjectInputStream(
83+
new ByteArrayInputStream( baos.toByteArray() ) )) {
8084

81-
final Setter setterClone = (Setter) ois.readObject();
82-
final PK pkNew = new PK( 2L );
83-
setterClone.set( entity, pkNew );
85+
final Setter setterClone = (Setter) ois.readObject();
86+
final PK pkNew = new PK( 2L );
87+
setterClone.set( entity, pkNew );
8488

85-
assertSame( pkNew, getter.get( entity ) );
89+
assertThat( pkNew ).isSameAs( getter.get( entity ) );
90+
}
91+
}
92+
}
8693
}
8794

8895
@Test
89-
@JiraKey( value = "HHH-11202")
96+
@JiraKey(value = "HHH-11202")
9097
public void testProtectedMethodGetter() throws Exception {
9198
final AnEntity entity = new AnEntity( new PK( 1L ) );
9299

@@ -95,19 +102,24 @@ public void testProtectedMethodGetter() throws Exception {
95102
"pk",
96103
ReflectHelper.findGetterMethod( AnEntity.class, "pk" )
97104
);
98-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
99-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
100-
oos.writeObject( getter );
101105

102-
final ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( baos.toByteArray() ) );
106+
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
107+
try (final ObjectOutputStream oos = new ObjectOutputStream( baos )) {
108+
oos.writeObject( getter );
109+
110+
try (final ObjectInputStream ois = new ObjectInputStream(
111+
new ByteArrayInputStream( baos.toByteArray() ) )) {
103112

104-
final Getter getterClone = (Getter) ois.readObject();
113+
final Getter getterClone = (Getter) ois.readObject();
105114

106-
assertSame( getter.get( entity ), getterClone.get( entity ) );
115+
assertThat( getterClone.get( entity ) ).isSameAs( getter.get( entity ) );
116+
}
117+
}
118+
}
107119
}
108120

109121
@Test
110-
@JiraKey( value = "HHH-11202")
122+
@JiraKey(value = "HHH-11202")
111123
public void testProtectedMethodSetter() throws Exception {
112124
final AnEntity entity = new AnEntity( new PK( 1L ) );
113125

@@ -122,16 +134,19 @@ public void testProtectedMethodSetter() throws Exception {
122134
ReflectHelper.findSetterMethod( AnEntity.class, "pk", PK.class )
123135
);
124136

125-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
126-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
127-
oos.writeObject( setter );
128-
129-
final ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( baos.toByteArray() ) );
137+
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
138+
try (final ObjectOutputStream oos = new ObjectOutputStream( baos )) {
139+
oos.writeObject( setter );
130140

131-
final Setter setterClone = (Setter) ois.readObject();
132-
final PK pkNew = new PK( 2L );
133-
setterClone.set( entity, pkNew );
141+
try (final ObjectInputStream ois = new ObjectInputStream(
142+
new ByteArrayInputStream( baos.toByteArray() ) )) {
143+
final Setter setterClone = (Setter) ois.readObject();
144+
final PK pkNew = new PK( 2L );
145+
setterClone.set( entity, pkNew );
134146

135-
assertSame( pkNew, getter.get( entity ) );
147+
assertThat( pkNew ).isSameAs( getter.get( entity ) );
148+
}
149+
}
150+
}
136151
}
137152
}

hibernate-core/src/test/java/org/hibernate/orm/test/serialization/ProxySerializationNoSessionFactoryTest.java

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,34 @@
44
*/
55
package org.hibernate.orm.test.serialization;
66

7-
import java.io.Serializable;
8-
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.FetchType;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.JoinColumn;
11+
import jakarta.persistence.ManyToOne;
912
import org.hibernate.Hibernate;
10-
import org.hibernate.SessionFactory;
1113
import org.hibernate.cfg.AvailableSettings;
1214
import org.hibernate.cfg.Configuration;
15+
import org.hibernate.engine.spi.SessionFactoryImplementor;
1316
import org.hibernate.internal.SessionFactoryRegistry;
1417
import org.hibernate.internal.util.SerializationHelper;
1518
import org.hibernate.proxy.HibernateProxy;
16-
17-
import org.hibernate.testing.junit4.BaseUnitTestCase;
19+
import org.hibernate.testing.orm.junit.BaseUnitTest;
1820
import org.hibernate.testing.util.ServiceRegistryUtil;
1921
import org.hibernate.tool.schema.Action;
20-
import org.junit.Test;
22+
import org.junit.jupiter.api.Test;
2123

22-
import jakarta.persistence.Entity;
23-
import jakarta.persistence.FetchType;
24-
import jakarta.persistence.Id;
25-
import jakarta.persistence.JoinColumn;
26-
import jakarta.persistence.ManyToOne;
24+
import java.io.Serializable;
2725

28-
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
29-
import static org.junit.Assert.assertEquals;
30-
import static org.junit.Assert.assertFalse;
31-
import static org.junit.Assert.assertNotNull;
32-
import static org.junit.Assert.assertTrue;
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.hibernate.testing.orm.transaction.TransactionUtil.fromTransaction;
28+
import static org.hibernate.testing.orm.transaction.TransactionUtil.inTransaction;
3329

3430
/**
3531
* @author Marco Belladelli
3632
*/
37-
public class ProxySerializationNoSessionFactoryTest extends BaseUnitTestCase {
33+
@BaseUnitTest
34+
public class ProxySerializationNoSessionFactoryTest {
3835
@Test
3936
public void testUninitializedProxy() {
4037
executeTest( false );
@@ -52,45 +49,46 @@ private void executeTest(boolean initializeProxy) {
5249
.addAnnotatedClass( ChildEntity.class );
5350
ServiceRegistryUtil.applySettings( cfg.getStandardServiceRegistryBuilder() );
5451
final SimpleEntity parent;
55-
try (final SessionFactory factory = cfg.buildSessionFactory()) {
56-
doInHibernate( () -> factory, session -> {
57-
final SimpleEntity entity = new SimpleEntity();
58-
entity.setId( 1L );
59-
entity.setName( "TheParent" );
60-
session.persist( entity );
61-
62-
final ChildEntity child = new ChildEntity();
63-
child.setId( 1L );
64-
child.setParent( entity );
65-
session.persist( child );
66-
} );
67-
68-
parent = doInHibernate( () -> factory, session -> {
69-
final ChildEntity childEntity = session.find( ChildEntity.class, 1L );
70-
final SimpleEntity entity = childEntity.getParent();
71-
if ( initializeProxy ) {
72-
assertEquals( "TheParent",entity.getName() );
73-
}
74-
return entity;
75-
} );
52+
try (final SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory()) {
53+
inTransaction( factory, session -> {
54+
final SimpleEntity entity = new SimpleEntity();
55+
entity.setId( 1L );
56+
entity.setName( "TheParent" );
57+
session.persist( entity );
58+
59+
final ChildEntity child = new ChildEntity();
60+
child.setId( 1L );
61+
child.setParent( entity );
62+
session.persist( child );
63+
}
64+
);
65+
parent = fromTransaction( factory, session -> {
66+
final ChildEntity childEntity = session.find( ChildEntity.class, 1L );
67+
final SimpleEntity entity = childEntity.getParent();
68+
if ( initializeProxy ) {
69+
assertThat( entity.getName() ).isEqualTo( "TheParent" );
70+
}
71+
return entity;
72+
}
73+
);
7674
}
7775

7876
// The session factory is not available anymore
79-
assertFalse( SessionFactoryRegistry.INSTANCE.hasRegistrations() );
77+
assertThat( SessionFactoryRegistry.INSTANCE.hasRegistrations() ).isFalse();
8078

81-
assertTrue( parent instanceof HibernateProxy );
82-
assertEquals( initializeProxy, Hibernate.isInitialized( parent ) );
79+
assertThat( parent instanceof HibernateProxy ).isTrue();
80+
assertThat( Hibernate.isInitialized( parent ) ).isEqualTo( initializeProxy );
8381

8482
// Serialization and deserialization should still work
8583
final SimpleEntity clone = (SimpleEntity) SerializationHelper.clone( parent );
86-
assertNotNull( clone );
87-
assertEquals( parent.getId(), clone.getId() );
84+
assertThat( clone ).isNotNull();
85+
assertThat( clone.getId() ).isEqualTo( parent.getId() );
8886
if ( initializeProxy ) {
89-
assertEquals( parent.getName(), clone.getName() );
87+
assertThat( clone.getName() ).isEqualTo( parent.getName() );
9088
}
9189
}
9290

93-
@Entity( name = "SimpleEntity" )
91+
@Entity(name = "SimpleEntity")
9492
static class SimpleEntity implements Serializable {
9593
@Id
9694
private Long id;
@@ -114,12 +112,12 @@ public void setName(final String name) {
114112
}
115113
}
116114

117-
@Entity( name = "ChildEntity" )
115+
@Entity(name = "ChildEntity")
118116
static class ChildEntity {
119117
@Id
120118
private Long id;
121119

122-
@ManyToOne( fetch = FetchType.LAZY )
120+
@ManyToOne(fetch = FetchType.LAZY)
123121
@JoinColumn
124122
private SimpleEntity parent;
125123

0 commit comments

Comments
 (0)