Skip to content

Commit 690ac80

Browse files
committed
HHH-19921 Drop last JUnit 4 usages in Hibernate Envers
1 parent 866144b commit 690ac80

File tree

12 files changed

+613
-593
lines changed

12 files changed

+613
-593
lines changed

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditDefaultTrackingTest.java

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,27 @@
44
*/
55
package org.hibernate.orm.test.envers;
66

7-
import java.util.Arrays;
8-
import java.util.Date;
9-
import java.util.Map;
107
import jakarta.persistence.Column;
118
import jakarta.persistence.Entity;
129
import jakarta.persistence.EntityManagerFactory;
1310
import jakarta.persistence.Id;
1411
import jakarta.persistence.Table;
1512
import jakarta.persistence.Temporal;
1613
import jakarta.persistence.TemporalType;
17-
1814
import org.hibernate.annotations.CreationTimestamp;
1915
import org.hibernate.cfg.AvailableSettings;
20-
import org.hibernate.engine.spi.SessionFactoryImplementor;
2116
import org.hibernate.envers.Audited;
22-
import org.hibernate.envers.TrackingModifiedEntitiesRevisionMapping;
2317
import org.hibernate.envers.RevisionEntity;
18+
import org.hibernate.envers.TrackingModifiedEntitiesRevisionMapping;
2419
import org.hibernate.jpa.boot.spi.Bootstrap;
25-
2620
import org.hibernate.testing.orm.junit.EntityManagerFactoryBasedFunctionalTest;
2721
import org.hibernate.testing.transaction.TransactionUtil;
28-
import org.junit.Test;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.Arrays;
25+
import java.util.Date;
26+
import java.util.Map;
2927

30-
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
3128

3229
/**
3330
* @author Vlad Mihalcea
@@ -37,64 +34,51 @@ public class EntityTypeChangeAuditDefaultTrackingTest extends EntityManagerFacto
3734
@Override
3835
protected Class<?>[] getAnnotatedClasses() {
3936
return new Class<?>[] {
40-
Customer.class,
41-
CustomTrackingRevisionEntity.class
37+
Customer.class,
38+
CustomTrackingRevisionEntity.class
4239
};
4340
}
4441

4542
@Test
4643
public void testLifecycle() {
47-
final EntityManagerFactory testEmf = produceEntityManagerFactory();
48-
49-
TransactionUtil.doInJPA( () -> testEmf, entityManager -> {
50-
Customer customer = new Customer();
51-
customer.setId(1L);
52-
customer.setFirstName("John");
53-
customer.setLastName("Doe");
54-
55-
entityManager.persist(customer);
56-
});
57-
58-
EntityManagerFactory entityManagerFactory = null;
59-
try {
60-
Map settings = buildSettings();
61-
settings.put(
62-
AvailableSettings.LOADED_CLASSES,
63-
Arrays.asList(
64-
ApplicationCustomer.class,
65-
CustomTrackingRevisionEntity.class
66-
)
67-
);
68-
settings.put(
69-
AvailableSettings.HBM2DDL_AUTO,
70-
"update"
71-
);
72-
entityManagerFactory = Bootstrap
73-
.getEntityManagerFactoryBuilder(
74-
new TestingPersistenceUnitDescriptorImpl(getClass().getSimpleName()),
75-
settings)
76-
.build()
77-
.unwrap(SessionFactoryImplementor.class);
78-
79-
final EntityManagerFactory emf = entityManagerFactory;
80-
81-
doInJPA(() -> emf, entityManager -> {
82-
ApplicationCustomer customer = new ApplicationCustomer();
83-
customer.setId(2L);
84-
customer.setFirstName("John");
85-
customer.setLastName("Doe Jr.");
86-
87-
entityManager.persist(customer);
88-
});
89-
}
90-
finally {
91-
if ( entityManagerFactory != null ) {
92-
entityManagerFactory.close();
44+
try (final EntityManagerFactory testEmf = produceEntityManagerFactory()) {
45+
46+
TransactionUtil.doInJPA( () -> testEmf, entityManager -> {
47+
Customer customer = new Customer();
48+
customer.setId( 1L );
49+
customer.setFirstName( "John" );
50+
customer.setLastName( "Doe" );
51+
52+
entityManager.persist( customer );
53+
} );
54+
55+
try (EntityManagerFactory entityManagerFactory = buildEntityManagerFactory()) {
56+
entityManagerFactory.runInTransaction( entityManager -> {
57+
ApplicationCustomer customer = new ApplicationCustomer();
58+
customer.setId( 2L );
59+
customer.setFirstName( "John" );
60+
customer.setLastName( "Doe Jr." );
61+
62+
entityManager.persist( customer );
63+
entityManager.getTransaction().commit();
64+
} );
9365
}
94-
testEmf.close();
9566
}
9667
}
9768

69+
private EntityManagerFactory buildEntityManagerFactory() {
70+
Map<Object, Object> settings = buildSettings();
71+
settings.put(
72+
AvailableSettings.LOADED_CLASSES,
73+
Arrays.asList( ApplicationCustomer.class, CustomTrackingRevisionEntity.class )
74+
);
75+
settings.put( AvailableSettings.HBM2DDL_AUTO, "update" );
76+
return Bootstrap.getEntityManagerFactoryBuilder(
77+
new TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ),
78+
settings )
79+
.build();
80+
}
81+
9882
@Audited
9983
@Entity(name = "Customer")
10084
public static class Customer {
@@ -198,7 +182,7 @@ public void setCreatedOn(Date createdOn) {
198182
@Table(name = "TRACKING_REV_INFO")
199183
@RevisionEntity
200184
public static class CustomTrackingRevisionEntity
201-
extends TrackingModifiedEntitiesRevisionMapping {
185+
extends TrackingModifiedEntitiesRevisionMapping {
202186

203187
}
204188
//end::envers-tracking-modified-entities-revchanges-example[]

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditTest.java

Lines changed: 79 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
*/
55
package org.hibernate.orm.test.envers;
66

7-
import java.util.Arrays;
8-
import java.util.Date;
9-
import java.util.HashSet;
10-
import java.util.Map;
11-
import java.util.Set;
127
import jakarta.persistence.Column;
138
import jakarta.persistence.ElementCollection;
149
import jakarta.persistence.Entity;
@@ -19,22 +14,25 @@
1914
import jakarta.persistence.Table;
2015
import jakarta.persistence.Temporal;
2116
import jakarta.persistence.TemporalType;
22-
2317
import org.hibernate.annotations.CreationTimestamp;
2418
import org.hibernate.cfg.AvailableSettings;
25-
import org.hibernate.engine.spi.SessionFactoryImplementor;
2619
import org.hibernate.envers.AuditReaderFactory;
2720
import org.hibernate.envers.Audited;
28-
import org.hibernate.envers.RevisionMapping;
2921
import org.hibernate.envers.ModifiedEntityNames;
3022
import org.hibernate.envers.RevisionEntity;
23+
import org.hibernate.envers.RevisionMapping;
3124
import org.hibernate.jpa.boot.spi.Bootstrap;
32-
3325
import org.hibernate.testing.orm.junit.EntityManagerFactoryBasedFunctionalTest;
34-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.util.Arrays;
29+
import java.util.Date;
30+
import java.util.HashSet;
31+
import java.util.Map;
32+
import java.util.Set;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
3535

36-
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
37-
import static org.junit.Assert.assertEquals;
3836

3937
/**
4038
* @author Vlad Mihalcea
@@ -44,86 +42,78 @@ public class EntityTypeChangeAuditTest extends EntityManagerFactoryBasedFunction
4442
@Override
4543
protected Class<?>[] getAnnotatedClasses() {
4644
return new Class<?>[] {
47-
Customer.class,
48-
CustomTrackingRevisionEntity.class
45+
Customer.class,
46+
CustomTrackingRevisionEntity.class
4947
};
5048
}
5149

5250
@Test
5351
public void test() {
54-
final EntityManagerFactory testEmf = produceEntityManagerFactory();
55-
56-
doInJPA( () -> testEmf, entityManager -> {
57-
Customer customer = new Customer();
58-
customer.setId(1L);
59-
customer.setFirstName("John");
60-
customer.setLastName("Doe");
61-
62-
entityManager.persist(customer);
63-
});
64-
65-
doInJPA( () -> testEmf, entityManager -> {
66-
//tag::envers-tracking-modified-entities-queries-example1[]
67-
assertEquals(
68-
"org.hibernate.orm.test.envers.EntityTypeChangeAuditTest$Customer",
69-
AuditReaderFactory
70-
.get(entityManager)
71-
.getCrossTypeRevisionChangesReader()
72-
.findEntityTypes(1)
73-
.iterator().next()
74-
.getFirst()
75-
);
76-
//end::envers-tracking-modified-entities-queries-example1[]
77-
});
78-
79-
EntityManagerFactory entityManagerFactory = null;
80-
try {
81-
Map settings = buildSettings();
82-
settings.put(
52+
try (final EntityManagerFactory testEmf = produceEntityManagerFactory()) {
53+
54+
testEmf.runInTransaction( entityManager -> {
55+
Customer customer = new Customer();
56+
customer.setId( 1L );
57+
customer.setFirstName( "John" );
58+
customer.setLastName( "Doe" );
59+
60+
entityManager.persist( customer );
61+
} );
62+
63+
testEmf.runInTransaction( entityManager -> {
64+
//tag::envers-tracking-modified-entities-queries-example1[]
65+
assertThat(
66+
AuditReaderFactory
67+
.get( entityManager )
68+
.getCrossTypeRevisionChangesReader()
69+
.findEntityTypes( 1 )
70+
.iterator().next()
71+
.getFirst()
72+
).isEqualTo( "org.hibernate.orm.test.envers.EntityTypeChangeAuditTest$Customer" );
73+
//end::envers-tracking-modified-entities-queries-example1[]
74+
} );
75+
76+
try (EntityManagerFactory entityManagerFactory = buildEntityManagerFactory()) {
77+
final EntityManagerFactory emf = entityManagerFactory;
78+
79+
entityManagerFactory.runInTransaction( entityManager -> {
80+
ApplicationCustomer customer = entityManager.find( ApplicationCustomer.class, 1L );
81+
customer.setLastName( "Doe Jr." );
82+
} );
83+
84+
entityManagerFactory.runInTransaction( entityManager -> {
85+
//tag::envers-tracking-modified-entities-queries-example2[]
86+
assertThat(
87+
AuditReaderFactory
88+
.get( entityManager )
89+
.getCrossTypeRevisionChangesReader()
90+
.findEntityTypes( 2 )
91+
.iterator().next()
92+
.getFirst()
93+
).isEqualTo( "org.hibernate.orm.test.envers.EntityTypeChangeAuditTest$ApplicationCustomer" );
94+
//end::envers-tracking-modified-entities-queries-example2[]
95+
} );
96+
}
97+
}
98+
}
99+
100+
private EntityManagerFactory buildEntityManagerFactory() {
101+
Map<Object, Object> settings = buildSettings();
102+
settings.put(
83103
AvailableSettings.LOADED_CLASSES,
84104
Arrays.asList(
85-
ApplicationCustomer.class,
86-
CustomTrackingRevisionEntity.class
87-
)
88-
);
89-
settings.put(
90-
AvailableSettings.HBM2DDL_AUTO,
91-
"update"
105+
ApplicationCustomer.class,
106+
CustomTrackingRevisionEntity.class
107+
)
92108
);
93-
entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(
94-
new TestingPersistenceUnitDescriptorImpl(getClass().getSimpleName()),
95-
settings
96-
).build().unwrap(SessionFactoryImplementor.class);
97-
98-
final EntityManagerFactory emf = entityManagerFactory;
99-
100-
doInJPA(() -> emf, entityManager -> {
101-
ApplicationCustomer customer = entityManager.find(ApplicationCustomer.class, 1L);
102-
customer.setLastName("Doe Jr.");
103-
});
104-
105-
doInJPA(() -> emf, entityManager -> {
106-
//tag::envers-tracking-modified-entities-queries-example2[]
107-
assertEquals(
108-
"org.hibernate.orm.test.envers.EntityTypeChangeAuditTest$ApplicationCustomer",
109-
AuditReaderFactory
110-
.get(entityManager)
111-
.getCrossTypeRevisionChangesReader()
112-
.findEntityTypes(2)
113-
.iterator().next()
114-
.getFirst()
115-
);
116-
//end::envers-tracking-modified-entities-queries-example2[]
117-
});
118-
}
119-
finally {
120-
if (entityManagerFactory != null) {
121-
entityManagerFactory.close();
122-
}
123-
testEmf.close();
124-
}
109+
settings.put( AvailableSettings.HBM2DDL_AUTO, "update" );
110+
return Bootstrap.getEntityManagerFactoryBuilder(
111+
new TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ),
112+
settings )
113+
.build();
125114
}
126115

116+
127117
//tag::envers-tracking-modified-entities-revchanges-before-rename-example[]
128118
@Audited
129119
@Entity(name = "Customer")
@@ -142,7 +132,7 @@ public static class Customer {
142132
private Date createdOn;
143133

144134
//Getters and setters are omitted for brevity
145-
//end::envers-tracking-modified-entities-revchanges-before-rename-example[]
135+
//end::envers-tracking-modified-entities-revchanges-before-rename-example[]
146136

147137
public Long getId() {
148138
return id;
@@ -175,7 +165,7 @@ public Date getCreatedOn() {
175165
public void setCreatedOn(Date createdOn) {
176166
this.createdOn = createdOn;
177167
}
178-
//tag::envers-tracking-modified-entities-revchanges-before-rename-example[]
168+
//tag::envers-tracking-modified-entities-revchanges-before-rename-example[]
179169
}
180170
//end::envers-tracking-modified-entities-revchanges-before-rename-example[]
181171

@@ -197,7 +187,7 @@ public static class ApplicationCustomer {
197187
private Date createdOn;
198188

199189
//Getters and setters are omitted for brevity
200-
//end::envers-tracking-modified-entities-revchanges-after-rename-example[]
190+
//end::envers-tracking-modified-entities-revchanges-after-rename-example[]
201191

202192
public Long getId() {
203193
return id;
@@ -230,7 +220,7 @@ public Date getCreatedOn() {
230220
public void setCreatedOn(Date createdOn) {
231221
this.createdOn = createdOn;
232222
}
233-
//tag::envers-tracking-modified-entities-revchanges-after-rename-example[]
223+
//tag::envers-tracking-modified-entities-revchanges-after-rename-example[]
234224
}
235225
//end::envers-tracking-modified-entities-revchanges-after-rename-example[]
236226

@@ -242,9 +232,9 @@ public static class CustomTrackingRevisionEntity extends RevisionMapping {
242232

243233
@ElementCollection
244234
@JoinTable(
245-
name = "REVCHANGES",
246-
joinColumns = @JoinColumn(name = "REV")
247-
)
235+
name = "REVCHANGES",
236+
joinColumns = @JoinColumn(name = "REV")
237+
)
248238
@Column(name = "ENTITYNAME")
249239
@ModifiedEntityNames
250240
private Set<String> modifiedEntityNames = new HashSet<>();

0 commit comments

Comments
 (0)