1
1
/*
2
- * Copyright (c) 2020, 2021 Oracle and/or its affiliates.
2
+ * Copyright (c) 2020, 2022 Oracle and/or its affiliates.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
45
45
import org .junit .jupiter .api .BeforeEach ;
46
46
import org .junit .jupiter .api .Test ;
47
47
48
- import static org .junit .jupiter .api .Assertions .assertEquals ;
49
- import static org .junit .jupiter .api .Assertions .assertFalse ;
50
- import static org .junit .jupiter .api .Assertions .assertNotNull ;
51
- import static org .junit .jupiter .api .Assertions .assertNull ;
52
- import static org .junit .jupiter .api .Assertions .assertSame ;
53
- import static org .junit .jupiter .api .Assertions .assertTrue ;
48
+ import static org .hamcrest .CoreMatchers .is ;
49
+ import static org .hamcrest .CoreMatchers .notNullValue ;
50
+ import static org .hamcrest .CoreMatchers .nullValue ;
51
+ import static org .hamcrest .CoreMatchers .sameInstance ;
52
+ import static org .hamcrest .MatcherAssert .assertThat ;
54
53
import static org .junit .jupiter .api .Assertions .fail ;
55
54
56
55
@ ApplicationScoped
@@ -97,13 +96,13 @@ class TestCascadePersist {
97
96
void startCdiContainerAndRunDDL () throws SQLException {
98
97
final SeContainerInitializer initializer = SeContainerInitializer .newInstance ()
99
98
.addBeanClasses (this .getClass ());
100
- assertNotNull (initializer );
99
+ assertThat (initializer , notNullValue () );
101
100
this .cdiContainer = initializer .initialize ();
102
101
final DataSource ds = this .cdiContainer .select (DataSource .class ).get ();
103
- assertNotNull (ds );
102
+ assertThat (ds , notNullValue () );
104
103
try (final Connection connection = ds .getConnection ();
105
104
final Statement statement = connection .createStatement ()) {
106
- assertNotNull (statement );
105
+ assertThat (statement , notNullValue () );
107
106
statement .executeUpdate ("RUNSCRIPT FROM 'classpath:chirp.ddl'" );
108
107
}
109
108
}
@@ -170,83 +169,83 @@ void testCascadePersist()
170
169
// Get a CDI contextual reference to this test instance. It
171
170
// is important to use "self" in this test instead of "this".
172
171
final TestCascadePersist self = self ();
173
- assertNotNull (self );
172
+ assertThat (self , notNullValue () );
174
173
175
174
// Get the EntityManager that is synchronized with and scoped
176
175
// to a JTA transaction.
177
176
final EntityManager em = self .getEntityManager ();
178
- assertNotNull (em );
179
- assertTrue (em .isOpen ());
177
+ assertThat (em , notNullValue () );
178
+ assertThat (em .isOpen (), is ( true ));
180
179
181
180
// We haven't started any kind of transaction yet and we
182
181
// aren't testing anything using
183
182
// the @javax.transaction.Transactional annotation so there is
184
183
// no transaction in effect so the EntityManager cannot be
185
184
// joined to one.
186
- assertFalse (em .isJoinedToTransaction ());
185
+ assertThat (em .isJoinedToTransaction (), is ( false ));
187
186
188
187
// Get the TransactionManager that normally is behind the
189
188
// scenes and use it to start a Transaction. This simulates
190
189
// entering a method annotated
191
190
// with @Transactional(TxType.REQUIRES_NEW) or similar.
192
191
final TransactionManager tm = self .getTransactionManager ();
193
- assertNotNull (tm );
192
+ assertThat (tm , notNullValue () );
194
193
tm .begin ();
195
- assertEquals ( Status . STATUS_ACTIVE , tm .getStatus ());
194
+ assertThat ( tm .getStatus (), is ( Status . STATUS_ACTIVE ));
196
195
197
196
// Now magically our EntityManager should be joined to it.
198
- assertTrue (em .isJoinedToTransaction ());
197
+ assertThat (em .isJoinedToTransaction (), is ( true ));
199
198
200
199
// Create an author but don't persist him explicitly.
201
200
Author author = new Author ("Abraham Lincoln" );
202
201
203
202
// No trip to the database has happened yet, so the author's
204
203
// identifier isn't set yet.
205
- assertNull (author .getId ());
204
+ assertThat (author .getId (), nullValue ());
206
205
207
206
// Set up a blog for that Author.
208
207
Microblog blog = new Microblog (author , "Gettysburg Address Draft 1" );
209
208
210
209
// Persist the blog. The Author should be persisted too.
211
210
em .persist (blog );
212
- assertTrue (em .contains (blog ));
213
- assertTrue (em .contains (author ));
211
+ assertThat (em .contains (blog ), is ( true ));
212
+ assertThat (em .contains (author ), is ( true ));
214
213
215
214
// Commit the transaction. Because we're relying on the
216
215
// default flush mode, this will cause a flush to the
217
216
// database, which, in turn, will result in identifier
218
217
// generation.
219
218
tm .commit ();
220
- assertEquals ( Status . STATUS_NO_TRANSACTION , tm .getStatus ());
221
- assertEquals ( Integer . valueOf ( 1 ), author . getId ( ));
222
- assertEquals ( Integer . valueOf ( 1 ), blog . getId ( ));
219
+ assertThat ( tm .getStatus (), is ( Status . STATUS_NO_TRANSACTION ));
220
+ assertThat ( author . getId ( ), is ( 1 ));
221
+ assertThat ( blog . getId ( ), is ( 1 ));
223
222
224
223
// We're no longer in a transaction.
225
- assertFalse (em .isJoinedToTransaction ());
224
+ assertThat (em .isJoinedToTransaction (), is ( false ));
226
225
227
226
// The persistence context should be cleared.
228
- assertFalse (em .contains (blog ));
229
- assertFalse (em .contains (author ));
227
+ assertThat (em .contains (blog ), is ( false ));
228
+ assertThat (em .contains (author ), is ( false ));
230
229
231
230
// Let's check the database directly.
232
231
final DataSource ds = this .cdiContainer .select (DataSource .class ).get ();
233
- assertNotNull (ds );
232
+ assertThat (ds , notNullValue () );
234
233
try (final Connection connection = ds .getConnection ();
235
234
final Statement statement = connection .createStatement ()) {
236
- assertNotNull (statement );
235
+ assertThat (statement , notNullValue () );
237
236
ResultSet rs = statement .executeQuery ("SELECT COUNT(1) FROM MICROBLOG" );
238
- assertNotNull (rs );
237
+ assertThat (rs , notNullValue () );
239
238
try {
240
- assertTrue (rs .next ());
241
- assertEquals ( 1 , rs .getInt (1 ));
239
+ assertThat (rs .next (), is ( true ));
240
+ assertThat ( rs .getInt ( 1 ), is (1 ));
242
241
} finally {
243
242
rs .close ();
244
243
}
245
244
rs = statement .executeQuery ("SELECT COUNT(1) FROM AUTHOR" );
246
- assertNotNull (rs );
245
+ assertThat (rs , notNullValue () );
247
246
try {
248
- assertTrue (rs .next ());
249
- assertEquals ( 1 , rs .getInt (1 ));
247
+ assertThat (rs .next (), is ( true ));
248
+ assertThat ( rs .getInt ( 1 ), is (1 ));
250
249
} finally {
251
250
rs .close ();
252
251
}
@@ -255,12 +254,12 @@ void testCascadePersist()
255
254
// Start a new transaction.
256
255
tm .begin ();
257
256
258
- assertFalse (em .contains (blog ));
257
+ assertThat (em .contains (blog ), is ( false ));
259
258
final Microblog newBlog = em .find (Microblog .class , Integer .valueOf (1 ));
260
- assertNotNull (newBlog );
261
- assertTrue (em .contains (newBlog ));
259
+ assertThat (newBlog , notNullValue () );
260
+ assertThat (em .contains (newBlog ), is ( true ));
262
261
263
- assertEquals ( blog .getId (), newBlog .getId ());
262
+ assertThat ( newBlog .getId (), is ( blog .getId () ));
264
263
blog = newBlog ;
265
264
266
265
// Now let's have our author write some stuff.
@@ -272,11 +271,11 @@ void testCascadePersist()
272
271
+ "whether that nation, or any nation so conceived and so "
273
272
+ "dedicated, can long endure." );
274
273
blog .addChirp (chirp1 );
275
- assertSame ( blog , chirp1 .getMicroblog ());
274
+ assertThat ( chirp1 .getMicroblog (), sameInstance ( blog ));
276
275
blog .addChirp (chirp2 );
277
- assertSame ( blog , chirp2 .getMicroblog ());
276
+ assertThat ( chirp2 .getMicroblog (), sameInstance ( blog ));
278
277
blog .addChirp (chirp3 );
279
- assertSame ( blog , chirp3 .getMicroblog ());
278
+ assertThat ( chirp3 .getMicroblog (), sameInstance ( blog ));
280
279
281
280
// Commit the transaction. The changes should be propagated.
282
281
// However, this will fail, because the third chirp above is
@@ -293,12 +292,12 @@ void testCascadePersist()
293
292
// functioned properly. Let's make sure.
294
293
try (final Connection connection = ds .getConnection ();
295
294
final Statement statement = connection .createStatement ()) {
296
- assertNotNull (statement );
295
+ assertThat (statement , notNullValue () );
297
296
ResultSet rs = statement .executeQuery ("SELECT COUNT(1) FROM CHIRP" );
298
- assertNotNull (rs );
297
+ assertThat (rs , notNullValue () );
299
298
try {
300
- assertTrue (rs .next ());
301
- assertEquals ( 0 , rs .getInt (1 ));
299
+ assertThat (rs .next (), is ( true ));
300
+ assertThat ( rs .getInt (1 ), is ( 0 ));
302
301
} finally {
303
302
rs .close ();
304
303
}
0 commit comments