Skip to content

Commit 47faaa1

Browse files
blafondDavideD
authored andcommitted
[#1582] Convert BaseReactiveTest to JUnit5 and vertx-junit5
1 parent 1ab2aea commit 47faaa1

File tree

1 file changed

+43
-60
lines changed

1 file changed

+43
-60
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/BaseReactiveTest.java

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,20 @@
3030
import org.hibernate.reactive.testing.SessionFactoryManager;
3131
import org.hibernate.tool.schema.spi.SchemaManagementTool;
3232

33-
import org.junit.After;
34-
import org.junit.AfterClass;
35-
import org.junit.Before;
36-
import org.junit.ClassRule;
37-
import org.junit.runner.RunWith;
33+
import org.junit.jupiter.api.AfterAll;
34+
import org.junit.jupiter.api.AfterEach;
35+
import org.junit.jupiter.api.BeforeEach;
36+
import org.junit.jupiter.api.TestInstance;
37+
import org.junit.jupiter.api.extension.ExtendWith;
38+
import org.junit.jupiter.api.extension.RegisterExtension;
3839

3940
import io.smallrye.mutiny.Uni;
4041
import io.vertx.core.Promise;
41-
import io.vertx.core.Vertx;
4242
import io.vertx.core.VertxOptions;
43-
import io.vertx.ext.unit.Async;
44-
import io.vertx.ext.unit.TestContext;
45-
import io.vertx.ext.unit.junit.RunTestOnContext;
46-
import io.vertx.ext.unit.junit.Timeout;
47-
import io.vertx.ext.unit.junit.VertxUnitRunner;
48-
import jakarta.persistence.Table;
43+
import io.vertx.junit5.RunTestOnContext;
44+
import io.vertx.junit5.Timeout;
45+
import io.vertx.junit5.VertxExtension;
46+
import io.vertx.junit5.VertxTestContext;
4947
import jakarta.persistence.criteria.CriteriaQuery;
5048

5149
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
@@ -65,8 +63,21 @@
6563
* using Vert.x unit.
6664
* </p>
6765
*/
68-
@RunWith(VertxUnitRunner.class)
66+
@ExtendWith(VertxExtension.class)
67+
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
68+
@Timeout(value = 600, timeUnit = TimeUnit.SECONDS)
6969
public abstract class BaseReactiveTest {
70+
/**
71+
* Configure Vertx JUnit5 test context
72+
*/
73+
@RegisterExtension
74+
static RunTestOnContext testOnContext = new RunTestOnContext( vertxOptions() );
75+
76+
private static VertxOptions vertxOptions() {
77+
return new VertxOptions()
78+
.setBlockedThreadCheckInterval( 5 )
79+
.setBlockedThreadCheckIntervalUnit( TimeUnit.MINUTES );
80+
}
7081

7182
/**
7283
* Configure properties defined in {@link Settings}.
@@ -92,28 +103,13 @@ public static void setDefaultProperties(Configuration configuration) {
92103
configuration.setProperty( Settings.HIGHLIGHT_SQL, System.getProperty( Settings.HIGHLIGHT_SQL, "true" ) );
93104
}
94105

95-
public static SessionFactoryManager factoryManager = new SessionFactoryManager();
96-
97-
@ClassRule
98-
public static Timeout rule = Timeout.seconds( 10 * 60 );
99-
100-
@ClassRule
101-
public static RunTestOnContext vertxContextRule = new RunTestOnContext( () -> {
102-
VertxOptions options = new VertxOptions();
103-
options.setBlockedThreadCheckInterval( 5 );
104-
options.setBlockedThreadCheckIntervalUnit( TimeUnit.MINUTES );
105-
return Vertx.vertx( options );
106-
} );
106+
public static final SessionFactoryManager factoryManager = new SessionFactoryManager();
107107

108108
private Object session;
109109
private Object statelessSession;
110110

111111
private ReactiveConnection connection;
112112

113-
protected static void test(TestContext context, CompletionStage<?> work) {
114-
test( context.async(), context, work );
115-
}
116-
117113
/**
118114
* These entities will be added to the configuration of the factory and
119115
* the rows in the mapping tables deleted after each test.
@@ -132,29 +128,19 @@ protected Collection<String> mappings() {
132128
return List.of();
133129
}
134130

135-
/**
136-
* For when we need to create the {@link Async} in advance
137-
*/
138-
protected static void test(Async async, TestContext context, CompletionStage<?> work) {
131+
public static void test(VertxTestContext context, CompletionStage<?> work) {
139132
work.whenComplete( (res, err) -> {
140133
if ( err != null ) {
141-
context.fail( err );
134+
context.failNow( err );
142135
}
143136
else {
144-
async.complete();
137+
context.completeNow();
145138
}
146139
} );
147140
}
148141

149-
protected static void test(TestContext context, Uni<?> uni) {
150-
test( context.async(), context, uni );
151-
}
152-
153-
/**
154-
* For when we need to create the {@link Async} in advance
155-
*/
156-
public static void test(Async async, TestContext context, Uni<?> uni) {
157-
uni.subscribe().with( res -> async.complete(), context::fail );
142+
public static void test(VertxTestContext context, Uni<?> uni) {
143+
uni.subscribe().with( res -> context.completeNow(), context::failNow );
158144
}
159145

160146
private static boolean doneTablespace;
@@ -167,10 +153,10 @@ protected Configuration constructConfiguration() {
167153
}
168154

169155
protected void addEntities(Configuration configuration) {
170-
for ( Class<?> entity: annotatedEntities() ) {
156+
for ( Class<?> entity : annotatedEntities() ) {
171157
configuration.addAnnotatedClass( entity );
172158
}
173-
for ( String mapping: mappings() ) {
159+
for ( String mapping : mappings() ) {
174160
configuration.addResource( mapping );
175161
}
176162
}
@@ -192,13 +178,8 @@ private <T> CriteriaQuery<T> queryForDelete(Class<T> entityClass) {
192178
return query;
193179
}
194180

195-
protected String entityTable(Class<?> entityClass) {
196-
Table annotation = entityClass.getAnnotation( Table.class );
197-
return annotation == null ? entityClass.getSimpleName() : annotation.name();
198-
}
199-
200-
@Before
201-
public void before(TestContext context) {
181+
@BeforeEach
182+
public void before(VertxTestContext context) {
202183
test( context, setupSessionFactory( this::constructConfiguration ) );
203184
}
204185

@@ -213,11 +194,12 @@ protected CompletionStage<Void> setupSessionFactory(Configuration configuration)
213194
* Set up the session factory but create the configuration only if necessary.
214195
*
215196
* @param confSupplier supplies the configuration for the factory
197+
*
216198
* @return a {@link CompletionStage} void that succeeds when the factory is ready.
217199
*/
218200
protected CompletionStage<Void> setupSessionFactory(Supplier<Configuration> confSupplier) {
219201
CompletableFuture<Void> future = new CompletableFuture<>();
220-
vertxContextRule.vertx()
202+
testOnContext.vertx()
221203
.executeBlocking(
222204
// schema generation is a blocking operation and so it causes an
223205
// exception when run on the Vert.x event loop. So call it using
@@ -235,7 +217,7 @@ protected CompletionStage<Void> setupSessionFactory(Supplier<Configuration> conf
235217
return future;
236218
}
237219

238-
private void startFactoryManager(Promise<Object> p, Supplier<Configuration> confSupplier ) {
220+
private void startFactoryManager(Promise<Object> p, Supplier<Configuration> confSupplier) {
239221
try {
240222
factoryManager.start( () -> createHibernateSessionFactory( confSupplier.get() ) );
241223
p.complete();
@@ -254,7 +236,8 @@ private SessionFactory createHibernateSessionFactory(Configuration configuration
254236
return configuration.buildSessionFactory( registry );
255237
}
256238

257-
protected void addServices(StandardServiceRegistryBuilder builder) {}
239+
protected void addServices(StandardServiceRegistryBuilder builder) {
240+
}
258241

259242
/*
260243
* MySQL doesn't implement 'drop table cascade constraints'.
@@ -283,8 +266,8 @@ public void release() {
283266
}
284267
}
285268

286-
@After
287-
public void after(TestContext context) {
269+
@AfterEach
270+
public void after(VertxTestContext context) {
288271
test( context, closeSession( session )
289272
.thenAccept( v -> session = null )
290273
.thenCompose( v -> closeSession( statelessSession ) )
@@ -345,8 +328,8 @@ protected static CompletionStage<Void> closeSession(Object closable) {
345328
return voidFuture();
346329
}
347330

348-
@AfterClass
349-
public static void closeFactory(TestContext context) {
331+
@AfterAll
332+
public static void closeFactory(VertxTestContext context) {
350333
test( context, factoryManager.stop() );
351334
}
352335

0 commit comments

Comments
 (0)