30
30
import org .hibernate .reactive .testing .SessionFactoryManager ;
31
31
import org .hibernate .tool .schema .spi .SchemaManagementTool ;
32
32
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 ;
38
39
39
40
import io .smallrye .mutiny .Uni ;
40
41
import io .vertx .core .Promise ;
41
- import io .vertx .core .Vertx ;
42
42
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 ;
49
47
import jakarta .persistence .criteria .CriteriaQuery ;
50
48
51
49
import static org .hibernate .reactive .containers .DatabaseConfiguration .dbType ;
65
63
* using Vert.x unit.
66
64
* </p>
67
65
*/
68
- @ RunWith (VertxUnitRunner .class )
66
+ @ ExtendWith (VertxExtension .class )
67
+ @ TestInstance (TestInstance .Lifecycle .PER_METHOD )
68
+ @ Timeout (value = 600 , timeUnit = TimeUnit .SECONDS )
69
69
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
+ }
70
81
71
82
/**
72
83
* Configure properties defined in {@link Settings}.
@@ -92,28 +103,13 @@ public static void setDefaultProperties(Configuration configuration) {
92
103
configuration .setProperty ( Settings .HIGHLIGHT_SQL , System .getProperty ( Settings .HIGHLIGHT_SQL , "true" ) );
93
104
}
94
105
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 ();
107
107
108
108
private Object session ;
109
109
private Object statelessSession ;
110
110
111
111
private ReactiveConnection connection ;
112
112
113
- protected static void test (TestContext context , CompletionStage <?> work ) {
114
- test ( context .async (), context , work );
115
- }
116
-
117
113
/**
118
114
* These entities will be added to the configuration of the factory and
119
115
* the rows in the mapping tables deleted after each test.
@@ -132,29 +128,19 @@ protected Collection<String> mappings() {
132
128
return List .of ();
133
129
}
134
130
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 ) {
139
132
work .whenComplete ( (res , err ) -> {
140
133
if ( err != null ) {
141
- context .fail ( err );
134
+ context .failNow ( err );
142
135
}
143
136
else {
144
- async . complete ();
137
+ context . completeNow ();
145
138
}
146
139
} );
147
140
}
148
141
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 );
158
144
}
159
145
160
146
private static boolean doneTablespace ;
@@ -167,10 +153,10 @@ protected Configuration constructConfiguration() {
167
153
}
168
154
169
155
protected void addEntities (Configuration configuration ) {
170
- for ( Class <?> entity : annotatedEntities () ) {
156
+ for ( Class <?> entity : annotatedEntities () ) {
171
157
configuration .addAnnotatedClass ( entity );
172
158
}
173
- for ( String mapping : mappings () ) {
159
+ for ( String mapping : mappings () ) {
174
160
configuration .addResource ( mapping );
175
161
}
176
162
}
@@ -192,13 +178,8 @@ private <T> CriteriaQuery<T> queryForDelete(Class<T> entityClass) {
192
178
return query ;
193
179
}
194
180
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 ) {
202
183
test ( context , setupSessionFactory ( this ::constructConfiguration ) );
203
184
}
204
185
@@ -213,11 +194,12 @@ protected CompletionStage<Void> setupSessionFactory(Configuration configuration)
213
194
* Set up the session factory but create the configuration only if necessary.
214
195
*
215
196
* @param confSupplier supplies the configuration for the factory
197
+ *
216
198
* @return a {@link CompletionStage} void that succeeds when the factory is ready.
217
199
*/
218
200
protected CompletionStage <Void > setupSessionFactory (Supplier <Configuration > confSupplier ) {
219
201
CompletableFuture <Void > future = new CompletableFuture <>();
220
- vertxContextRule .vertx ()
202
+ testOnContext .vertx ()
221
203
.executeBlocking (
222
204
// schema generation is a blocking operation and so it causes an
223
205
// exception when run on the Vert.x event loop. So call it using
@@ -235,7 +217,7 @@ protected CompletionStage<Void> setupSessionFactory(Supplier<Configuration> conf
235
217
return future ;
236
218
}
237
219
238
- private void startFactoryManager (Promise <Object > p , Supplier <Configuration > confSupplier ) {
220
+ private void startFactoryManager (Promise <Object > p , Supplier <Configuration > confSupplier ) {
239
221
try {
240
222
factoryManager .start ( () -> createHibernateSessionFactory ( confSupplier .get () ) );
241
223
p .complete ();
@@ -254,7 +236,8 @@ private SessionFactory createHibernateSessionFactory(Configuration configuration
254
236
return configuration .buildSessionFactory ( registry );
255
237
}
256
238
257
- protected void addServices (StandardServiceRegistryBuilder builder ) {}
239
+ protected void addServices (StandardServiceRegistryBuilder builder ) {
240
+ }
258
241
259
242
/*
260
243
* MySQL doesn't implement 'drop table cascade constraints'.
@@ -283,8 +266,8 @@ public void release() {
283
266
}
284
267
}
285
268
286
- @ After
287
- public void after (TestContext context ) {
269
+ @ AfterEach
270
+ public void after (VertxTestContext context ) {
288
271
test ( context , closeSession ( session )
289
272
.thenAccept ( v -> session = null )
290
273
.thenCompose ( v -> closeSession ( statelessSession ) )
@@ -345,8 +328,8 @@ protected static CompletionStage<Void> closeSession(Object closable) {
345
328
return voidFuture ();
346
329
}
347
330
348
- @ AfterClass
349
- public static void closeFactory (TestContext context ) {
331
+ @ AfterAll
332
+ public static void closeFactory (VertxTestContext context ) {
350
333
test ( context , factoryManager .stop () );
351
334
}
352
335
0 commit comments