Skip to content

Commit 58d61ab

Browse files
committed
HHH-19846 Drop JUnit 4 usage
1 parent 48473d9 commit 58d61ab

File tree

11 files changed

+360
-306
lines changed

11 files changed

+360
-306
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idmanytoone/Customers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Customers implements Serializable {
2424
private int customerID;
2525

2626
@OneToMany(mappedBy="owner", cascade= CascadeType.ALL, targetEntity=ShoppingBaskets.class)
27-
private java.util.Set shoppingBasketses = new java.util.HashSet();
27+
private java.util.Set<ShoppingBaskets> shoppingBasketses = new java.util.HashSet<>();
2828

2929
public void setCustomerID(int value) {
3030
this.customerID = value;
@@ -38,11 +38,11 @@ public int getORMID() {
3838
return getCustomerID();
3939
}
4040

41-
public void setShoppingBasketses(java.util.Set value) {
41+
public void setShoppingBasketses(java.util.Set<ShoppingBaskets> value) {
4242
this.shoppingBasketses = value;
4343
}
4444

45-
public java.util.Set getShoppingBasketses() {
45+
public java.util.Set<ShoppingBaskets> getShoppingBasketses() {
4646
return shoppingBasketses;
4747
}
4848

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idmanytoone/ShoppingBaskets.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ShoppingBaskets implements Serializable {
3333
private java.util.Date basketDatetime;
3434

3535
@OneToMany(mappedBy="shoppingBaskets", cascade=CascadeType.ALL, targetEntity=BasketItems.class)
36-
private java.util.Set items = new java.util.HashSet();
36+
private java.util.Set<BasketItems> items = new java.util.HashSet<>();
3737

3838
public void setBasketDatetime(java.util.Date value) {
3939
this.basketDatetime = value;
@@ -51,11 +51,11 @@ public Customers getOwner() {
5151
return owner;
5252
}
5353

54-
public void setItems(java.util.Set value) {
54+
public void setItems(java.util.Set<BasketItems> value) {
5555
this.items = value;
5656
}
5757

58-
public java.util.Set getItems() {
58+
public java.util.Set<BasketItems> getItems() {
5959
return items;
6060
}
6161

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idmanytoone/StoreCustomerPK.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
55
package org.hibernate.orm.test.annotations.idmanytoone;
6-
import java.io.Serializable;
6+
77
import jakarta.persistence.Id;
88
import jakarta.persistence.JoinColumn;
99
import jakarta.persistence.ManyToOne;
1010

11+
import java.io.Serializable;
12+
1113
/**
1214
* @author Emmanuel Bernard
1315
*/
1416
public class StoreCustomerPK implements Serializable {
15-
StoreCustomerPK() {}
17+
StoreCustomerPK() {
18+
}
19+
1620
@Id
1721
@ManyToOne(optional = false)
1822
@JoinColumn(name = "idA")
@@ -25,8 +29,8 @@ public class StoreCustomerPK implements Serializable {
2529

2630

2731
public StoreCustomerPK(Store store, Customer customer) {
28-
this.store = store;
29-
this.customer = customer;
32+
this.store = store;
33+
this.customer = customer;
3034
}
3135

3236

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/immutable/Caption.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import org.hibernate.annotations.Immutable;
88

9+
import java.util.Objects;
10+
911
/**
1012
* Created by soldier on 12.04.16.
1113
*/
@@ -35,7 +37,7 @@ public boolean equals(Object o) {
3537
return false;
3638
}
3739
Caption caption = (Caption) o;
38-
return text != null ? text.equals( caption.text ) : caption.text == null;
40+
return Objects.equals( text, caption.text );
3941

4042
}
4143

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/immutable/ImmutableEntityUpdateQueryHandlingModeExceptionTest.java

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,129 +4,122 @@
44
*/
55
package org.hibernate.orm.test.annotations.immutable;
66

7-
import java.util.Map;
8-
97
import jakarta.persistence.Entity;
108
import jakarta.persistence.GeneratedValue;
119
import jakarta.persistence.Id;
1210
import jakarta.persistence.OneToOne;
1311
import jakarta.persistence.PersistenceException;
14-
1512
import org.hibernate.HibernateException;
1613
import org.hibernate.annotations.Immutable;
17-
1814
import org.hibernate.query.Query;
15+
import org.hibernate.testing.orm.junit.DomainModel;
1916
import org.hibernate.testing.orm.junit.JiraKey;
20-
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
21-
import org.junit.Test;
17+
import org.hibernate.testing.orm.junit.SessionFactory;
18+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
19+
import org.junit.jupiter.api.Test;
2220

23-
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
24-
import static org.junit.Assert.assertEquals;
25-
import static org.junit.Assert.assertTrue;
26-
import static org.junit.Assert.fail;
21+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
22+
import static org.assertj.core.api.Fail.fail;
2723

2824
/**
2925
* @author Vlad Mihalcea
3026
*/
31-
@JiraKey( value = "HHH-12387" )
32-
public class ImmutableEntityUpdateQueryHandlingModeExceptionTest extends BaseNonConfigCoreFunctionalTestCase {
33-
34-
@Override
35-
protected Class[] getAnnotatedClasses() {
36-
return new Class[] { Country.class, State.class, Photo.class,
37-
ImmutableEntity.class, MutableEntity.class};
38-
}
39-
40-
@Override
41-
protected void addSettings(Map<String,Object> settings) {
42-
// settings.put( AvailableSettings.IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE, "exception" );
43-
}
27+
@JiraKey(value = "HHH-12387")
28+
@DomainModel(
29+
annotatedClasses = {
30+
Country.class,
31+
State.class,
32+
Photo.class,
33+
ImmutableEntityUpdateQueryHandlingModeExceptionTest.ImmutableEntity.class,
34+
ImmutableEntityUpdateQueryHandlingModeExceptionTest.MutableEntity.class
35+
}
36+
)
37+
@SessionFactory
38+
public class ImmutableEntityUpdateQueryHandlingModeExceptionTest {
4439

4540
@Test
46-
public void testBulkUpdate(){
47-
Country _country = doInHibernate( this::sessionFactory, session -> {
41+
public void testBulkUpdate(SessionFactoryScope scope) {
42+
Country _country = scope.fromTransaction( session -> {
4843
Country country = new Country();
49-
country.setName("Germany");
50-
session.persist(country);
51-
44+
country.setName( "Germany" );
45+
session.persist( country );
5246
return country;
5347
} );
5448

5549
try {
56-
doInHibernate( this::sessionFactory, session -> {
57-
session.createQuery("update Country set name = :name" );
58-
} );
59-
fail("Should throw PersistenceException");
50+
scope.inTransaction( session ->
51+
session.createQuery( "update Country set name = :name" )
52+
);
53+
fail( "Should throw PersistenceException" );
6054
}
6155
catch (PersistenceException e) {
62-
assertTrue( e instanceof HibernateException );
63-
assertEquals(
64-
"Error interpreting query [The query attempts to update an immutable entity: [Country] (set 'hibernate.query.immutable_entity_update_query_handling_mode' to suppress)] [update Country set name = :name]",
65-
e.getMessage()
66-
);
56+
assertThat( e ).isInstanceOf( HibernateException.class );
57+
assertThat( e.getMessage() )
58+
.isEqualTo(
59+
"Error interpreting query [The query attempts to update an immutable entity: [Country] (set 'hibernate.query.immutable_entity_update_query_handling_mode' to suppress)] [update Country set name = :name]" );
6760
}
6861

69-
doInHibernate( this::sessionFactory, session -> {
70-
Country country = session.find(Country.class, _country.getId());
71-
assertEquals( "Germany", country.getName() );
62+
scope.inTransaction( session -> {
63+
Country country = session.find( Country.class, _country.getId() );
64+
assertThat( country.getName() ).isEqualTo( "Germany" );
7265
} );
7366
}
7467

7568
@Test
76-
@JiraKey( value = "HHH-12927" )
77-
public void testUpdateMutableWithImmutableSubSelect() {
78-
doInHibernate(this::sessionFactory, session -> {
69+
@JiraKey(value = "HHH-12927")
70+
public void testUpdateMutableWithImmutableSubSelect(SessionFactoryScope scope) {
71+
scope.inTransaction( session -> {
7972
String selector = "foo";
80-
ImmutableEntity trouble = new ImmutableEntity(selector);
81-
session.persist(trouble);
73+
ImmutableEntity trouble = new ImmutableEntity( selector );
74+
session.persist( trouble );
8275

83-
MutableEntity entity = new MutableEntity(trouble, "start");
84-
session.persist(entity);
76+
MutableEntity entity = new MutableEntity( trouble, "start" );
77+
session.persist( entity );
8578

8679
// Change a mutable value via selection based on an immutable property
8780
String statement = "Update MutableEntity e set e.changeable = :changeable where e.trouble.id in " +
88-
"(select i.id from ImmutableEntity i where i.selector = :selector)";
81+
"(select i.id from ImmutableEntity i where i.selector = :selector)";
8982

90-
Query query = session.createQuery(statement);
91-
query.setParameter("changeable", "end");
92-
query.setParameter("selector", "foo");
83+
Query query = session.createQuery( statement );
84+
query.setParameter( "changeable", "end" );
85+
query.setParameter( "selector", "foo" );
9386
query.executeUpdate();
9487

95-
session.refresh(entity);
88+
session.refresh( entity );
9689

9790
// Assert that the value was changed. If HHH-12927 has not been fixed an exception will be thrown
9891
// before we get here.
99-
assertEquals("end", entity.getChangeable());
100-
});
92+
assertThat( entity.getChangeable() ).isEqualTo( "end" );
93+
} );
10194
}
10295

10396
@Test
104-
@JiraKey( value = "HHH-12927" )
105-
public void testUpdateImmutableWithMutableSubSelect() {
106-
doInHibernate(this::sessionFactory, session -> {
97+
@JiraKey(value = "HHH-12927")
98+
public void testUpdateImmutableWithMutableSubSelect(SessionFactoryScope scope) {
99+
scope.inTransaction( session -> {
107100
String selector = "foo";
108-
ImmutableEntity trouble = new ImmutableEntity(selector);
109-
session.persist(trouble);
101+
ImmutableEntity trouble = new ImmutableEntity( selector );
102+
session.persist( trouble );
110103

111-
MutableEntity entity = new MutableEntity(trouble, "start");
112-
session.persist(entity);
104+
MutableEntity entity = new MutableEntity( trouble, "start" );
105+
session.persist( entity );
113106

114107
// Change a mutable value via selection based on an immutable property
115108
String statement = "Update ImmutableEntity e set e.selector = :changeable where e.id in " +
116-
"(select i.id from MutableEntity i where i.changeable = :selector)";
109+
"(select i.id from MutableEntity i where i.changeable = :selector)";
117110

118111
try {
119-
session.createQuery(statement);
112+
session.createQuery( statement );
120113

121-
fail("Should have throw exception");
114+
fail( "Should have throw exception" );
122115
}
123116
catch (Exception expected) {
124117
}
125-
});
118+
} );
126119
}
127120

128121
@Entity(name = "MutableEntity")
129-
public static class MutableEntity{
122+
public static class MutableEntity {
130123

131124
@Id
132125
@GeneratedValue

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/immutable/ImmutableEntityUpdateQueryHandlingModeWarningTest.java

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,69 @@
55
package org.hibernate.orm.test.annotations.immutable;
66

77

8-
import org.hibernate.testing.orm.junit.JiraKey;
9-
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
10-
import org.hibernate.testing.logger.LoggerInspectionRule;
118
import org.hibernate.testing.logger.Triggerable;
12-
import org.junit.Rule;
13-
import org.junit.Test;
14-
15-
16-
import java.util.Map;
9+
import org.hibernate.testing.orm.junit.DomainModel;
10+
import org.hibernate.testing.orm.junit.JiraKey;
11+
import org.hibernate.testing.orm.junit.ServiceRegistry;
12+
import org.hibernate.testing.orm.junit.SessionFactory;
13+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
14+
import org.hibernate.testing.orm.junit.Setting;
15+
import org.hibernate.testing.orm.logger.LoggerInspectionExtension;
16+
import org.junit.jupiter.api.Test;
17+
import org.junit.jupiter.api.extension.RegisterExtension;
1718

19+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
1820
import static org.hibernate.cfg.QuerySettings.IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE;
1921
import static org.hibernate.internal.CoreMessageLogger.CORE_LOGGER;
20-
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
21-
import static org.junit.Assert.assertEquals;
2222

2323
/**
2424
* @author Vlad Mihalcea
2525
*/
26-
@JiraKey( value = "HHH-12387" )
27-
public class ImmutableEntityUpdateQueryHandlingModeWarningTest extends BaseNonConfigCoreFunctionalTestCase {
26+
@JiraKey(value = "HHH-12387")
27+
@DomainModel(
28+
annotatedClasses = {
29+
Country.class,
30+
State.class,
31+
Photo.class
32+
}
33+
)
34+
@SessionFactory
35+
@ServiceRegistry(
36+
settings = @Setting(name = IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE, value = "warning")
37+
)
38+
public class ImmutableEntityUpdateQueryHandlingModeWarningTest {
2839

29-
@Rule
30-
public LoggerInspectionRule logInspection = new LoggerInspectionRule( CORE_LOGGER );
31-
32-
@Override
33-
protected Class[] getAnnotatedClasses() {
34-
return new Class[] { Country.class, State.class, Photo.class };
35-
}
36-
37-
@Override
38-
protected void addSettings(Map<String, Object> settings) {
39-
settings.put( IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE, "warning" );
40-
}
40+
@RegisterExtension
41+
public LoggerInspectionExtension logInspection =
42+
LoggerInspectionExtension.builder().setLogger( CORE_LOGGER ).build();
4143

4244
@Test
43-
public void testBulkUpdate(){
44-
Country _country = doInHibernate( this::sessionFactory, session -> {
45+
public void testBulkUpdate(SessionFactoryScope scope) {
46+
Country _country = scope.fromTransaction( session -> {
4547
Country country = new Country();
46-
country.setName("Germany");
47-
session.persist(country);
48+
country.setName( "Germany" );
49+
session.persist( country );
4850
return country;
4951
} );
5052

5153
Triggerable triggerable = logInspection.watchForLogMessages( "HHH000487" );
5254
triggerable.reset();
5355

54-
doInHibernate( this::sessionFactory, session -> {
55-
session.createQuery(
56-
"update Country " +
57-
"set name = :name" )
58-
.setParameter( "name", "N/A" )
59-
.executeUpdate();
56+
scope.inTransaction( session -> {
57+
session.createMutationQuery(
58+
"update Country " +
59+
"set name = :name" )
60+
.setParameter( "name", "N/A" )
61+
.executeUpdate();
6062
} );
6163

62-
assertEquals( "HHH000487: The query [update Country set name = :name] updates an immutable entity: [Country]", triggerable.triggerMessage() );
64+
assertThat( triggerable.triggerMessage() )
65+
.isEqualTo(
66+
"HHH000487: The query [update Country set name = :name] updates an immutable entity: [Country]" );
6367

64-
doInHibernate( this::sessionFactory, session -> {
65-
Country country = session.find(Country.class, _country.getId());
66-
assertEquals( "N/A", country.getName() );
68+
scope.inTransaction( session -> {
69+
Country country = session.find( Country.class, _country.getId() );
70+
assertThat( country.getName() ).isEqualTo( "N/A" );
6771
} );
6872
}
6973
}

0 commit comments

Comments
 (0)