Skip to content

Commit 5207206

Browse files
Captain1653romain-grecourt
authored andcommitted
Use Hamcrest assertions instead of JUnit in integrations/cdi/jpa-cdi - backport 2.x (helidon-io#1749)
1 parent 8c46ff7 commit 5207206

10 files changed

+306
-311
lines changed

integrations/cdi/jpa-cdi/src/test/java/io/helidon/integrations/cdi/jpa/TestAnnotationRewriting.java

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,9 +42,10 @@
4242
import org.junit.jupiter.api.BeforeEach;
4343
import org.junit.jupiter.api.Test;
4444

45-
import static org.junit.jupiter.api.Assertions.assertFalse;
46-
import static org.junit.jupiter.api.Assertions.assertNotNull;
47-
import static org.junit.jupiter.api.Assertions.assertTrue;
45+
import static org.hamcrest.CoreMatchers.instanceOf;
46+
import static org.hamcrest.CoreMatchers.is;
47+
import static org.hamcrest.CoreMatchers.notNullValue;
48+
import static org.hamcrest.MatcherAssert.assertThat;
4849
import static org.junit.jupiter.api.Assertions.fail;
4950

5051
@ApplicationScoped
@@ -79,7 +80,7 @@ void startCdiContainer() {
7980
System.setProperty("jpaAnnotationRewritingEnabled", "true");
8081
final SeContainerInitializer initializer = SeContainerInitializer.newInstance()
8182
.addBeanClasses(this.getClass());
82-
assertNotNull(initializer);
83+
assertThat(initializer, notNullValue());
8384
this.cdiContainer = initializer.initialize();
8485
}
8586

@@ -110,24 +111,24 @@ private void onShutdown(@Observes @BeforeDestroyed(ApplicationScoped.class) fina
110111
@PersistenceContext(unitName = "test")
111112
private void observerMethod(@Observes final TestIsRunning event,
112113
final EntityManager emParameter) {
113-
assertNotNull(event);
114+
assertThat(event, notNullValue());
114115

115-
assertNotNull(emParameter);
116-
assertTrue(emParameter.isOpen());
117-
assertFalse(emParameter.isJoinedToTransaction());
116+
assertThat(emParameter, notNullValue());
117+
assertThat(emParameter.isOpen(), is(true));
118+
assertThat(emParameter.isJoinedToTransaction(), is(false));
118119

119-
assertNotNull(this.em);
120-
assertTrue(this.em.isOpen());
121-
assertFalse(this.em.isJoinedToTransaction());
120+
assertThat(this.em, notNullValue());
121+
assertThat(this.em.isOpen(), is(true));
122+
assertThat(this.em.isJoinedToTransaction(), is(false));
122123

123-
assertNotNull(this.emf);
124-
assertTrue(this.emf.isOpen());
124+
assertThat(this.emf, notNullValue());
125+
assertThat(this.emf.isOpen(), is(true));
125126
EntityManager em = null;
126127
try {
127128
em = this.emf.createEntityManager();
128-
assertNotNull(em);
129-
assertTrue(em.isOpen());
130-
assertFalse(em.isJoinedToTransaction());
129+
assertThat(em, notNullValue());
130+
assertThat(em.isOpen(), is(true));
131+
assertThat(em.isJoinedToTransaction(), is(false));
131132
} finally {
132133
if (em != null && !em.isOpen()) {
133134
em.close();
@@ -163,9 +164,9 @@ void testNonTransactionalEntityManager() {
163164
qualifiers.add(ContainerManaged.Literal.INSTANCE);
164165
qualifiers.add(JpaTransactionScoped.Literal.INSTANCE);
165166
final EntityManager entityManager = this.cdiContainer.select(EntityManager.class, qualifiers.toArray(new Annotation[qualifiers.size()])).get();
166-
assertTrue(entityManager instanceof DelegatingEntityManager);
167-
assertTrue(entityManager.isOpen());
168-
assertFalse(entityManager.isJoinedToTransaction());
167+
assertThat(entityManager, instanceOf(DelegatingEntityManager.class));
168+
assertThat(entityManager.isOpen(), is(true));
169+
assertThat(entityManager.isJoinedToTransaction(), is(false));
169170
try {
170171
entityManager.persist(new Object());
171172
fail("A TransactionRequiredException should have been thrown");
@@ -188,14 +189,14 @@ void testTransactionalEntityManager() {
188189
.fire(new TestIsRunning("testTransactionalEntityManager"));
189190
final Instance<TestAnnotationRewriting> instance = this.cdiContainer.select(TestAnnotationRewriting.class);
190191
final TestAnnotationRewriting test = instance.get();
191-
assertNotNull(test);
192+
assertThat(test, notNullValue());
192193
test.testEntityManagerIsJoinedToTransactionInTransactionalAnnotatedMethod();
193194
}
194195

195196
@Transactional
196197
void testEntityManagerIsJoinedToTransactionInTransactionalAnnotatedMethod() {
197-
assertNotNull(this.em);
198-
assertTrue(this.em.isJoinedToTransaction());
198+
assertThat(this.em, notNullValue());
199+
assertThat(this.em.isJoinedToTransaction(), is(true));
199200
try {
200201
this.em.close();
201202
fail("Closed EntityManager; should not have been able to");

integrations/cdi/jpa-cdi/src/test/java/io/helidon/integrations/cdi/jpa/TestMessages.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,16 +17,17 @@
1717

1818
import org.junit.jupiter.api.Test;
1919

20-
import static org.junit.jupiter.api.Assertions.assertNotNull;
21-
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
import static org.hamcrest.CoreMatchers.notNullValue;
21+
import static org.hamcrest.CoreMatchers.startsWith;
22+
import static org.hamcrest.MatcherAssert.assertThat;
2223

2324
final class TestMessages {
2425

2526
@Test
2627
final void testMessages() {
2728
final String message = Messages.format("resourceLocalPersistenceUnitWarning");
28-
assertNotNull(message);
29-
assertTrue(message.startsWith("The persistence unit "));
29+
assertThat(message, notNullValue());
30+
assertThat(message, startsWith("The persistence unit "));
3031
}
3132

3233
}

integrations/cdi/jpa-cdi/src/test/java/io/helidon/integrations/cdi/jpa/TestMultiplePersistenceUnits.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,10 +42,9 @@
4242
import org.junit.jupiter.api.BeforeEach;
4343
import org.junit.jupiter.api.Test;
4444

45-
import static org.junit.jupiter.api.Assertions.assertFalse;
46-
import static org.junit.jupiter.api.Assertions.assertNotNull;
47-
import static org.junit.jupiter.api.Assertions.assertTrue;
48-
import static org.junit.jupiter.api.Assertions.fail;
45+
import static org.hamcrest.CoreMatchers.is;
46+
import static org.hamcrest.CoreMatchers.notNullValue;
47+
import static org.hamcrest.MatcherAssert.assertThat;
4948

5049
@ApplicationScoped
5150
@DataSourceDefinition(
@@ -84,7 +83,7 @@ class TestMultiplePersistenceUnits {
8483
void startCdiContainer() {
8584
final SeContainerInitializer initializer = SeContainerInitializer.newInstance()
8685
.addBeanClasses(this.getClass());
87-
assertNotNull(initializer);
86+
assertThat(initializer, notNullValue());
8887
this.cdiContainer = initializer.initialize();
8988
}
9089

@@ -123,15 +122,15 @@ private void onShutdown(@Observes @BeforeDestroyed(ApplicationScoped.class) fina
123122
@Test
124123
void testMultiplePersistenceUnits() {
125124
TestMultiplePersistenceUnits self = this.cdiContainer.select(TestMultiplePersistenceUnits.class).get();
126-
assertNotNull(self);
125+
assertThat(self, notNullValue());
127126

128127
EntityManager testEm = self.getTestEntityManager();
129-
assertNotNull(testEm);
130-
assertTrue(testEm.isOpen());
128+
assertThat(testEm, notNullValue());
129+
assertThat(testEm.isOpen(), is(true));
131130

132131
EntityManager test2Em = self.getTest2EntityManager();
133-
assertNotNull(test2Em);
134-
assertTrue(test2Em.isOpen());
132+
assertThat(test2Em, notNullValue());
133+
assertThat(test2Em.isOpen(), is(true));
135134
}
136135

137136
}

integrations/cdi/jpa-cdi/src/test/java/io/helidon/integrations/cdi/jpa/chirp/TestCascadePersist.java

+44-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,12 +45,11 @@
4545
import org.junit.jupiter.api.BeforeEach;
4646
import org.junit.jupiter.api.Test;
4747

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;
5453
import static org.junit.jupiter.api.Assertions.fail;
5554

5655
@ApplicationScoped
@@ -97,13 +96,13 @@ class TestCascadePersist {
9796
void startCdiContainerAndRunDDL() throws SQLException {
9897
final SeContainerInitializer initializer = SeContainerInitializer.newInstance()
9998
.addBeanClasses(this.getClass());
100-
assertNotNull(initializer);
99+
assertThat(initializer, notNullValue());
101100
this.cdiContainer = initializer.initialize();
102101
final DataSource ds = this.cdiContainer.select(DataSource.class).get();
103-
assertNotNull(ds);
102+
assertThat(ds, notNullValue());
104103
try (final Connection connection = ds.getConnection();
105104
final Statement statement = connection.createStatement()) {
106-
assertNotNull(statement);
105+
assertThat(statement, notNullValue());
107106
statement.executeUpdate("RUNSCRIPT FROM 'classpath:chirp.ddl'");
108107
}
109108
}
@@ -170,83 +169,83 @@ void testCascadePersist()
170169
// Get a CDI contextual reference to this test instance. It
171170
// is important to use "self" in this test instead of "this".
172171
final TestCascadePersist self = self();
173-
assertNotNull(self);
172+
assertThat(self, notNullValue());
174173

175174
// Get the EntityManager that is synchronized with and scoped
176175
// to a JTA transaction.
177176
final EntityManager em = self.getEntityManager();
178-
assertNotNull(em);
179-
assertTrue(em.isOpen());
177+
assertThat(em, notNullValue());
178+
assertThat(em.isOpen(), is(true));
180179

181180
// We haven't started any kind of transaction yet and we
182181
// aren't testing anything using
183182
// the @javax.transaction.Transactional annotation so there is
184183
// no transaction in effect so the EntityManager cannot be
185184
// joined to one.
186-
assertFalse(em.isJoinedToTransaction());
185+
assertThat(em.isJoinedToTransaction(), is(false));
187186

188187
// Get the TransactionManager that normally is behind the
189188
// scenes and use it to start a Transaction. This simulates
190189
// entering a method annotated
191190
// with @Transactional(TxType.REQUIRES_NEW) or similar.
192191
final TransactionManager tm = self.getTransactionManager();
193-
assertNotNull(tm);
192+
assertThat(tm, notNullValue());
194193
tm.begin();
195-
assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
194+
assertThat(tm.getStatus(), is(Status.STATUS_ACTIVE));
196195

197196
// Now magically our EntityManager should be joined to it.
198-
assertTrue(em.isJoinedToTransaction());
197+
assertThat(em.isJoinedToTransaction(), is(true));
199198

200199
// Create an author but don't persist him explicitly.
201200
Author author = new Author("Abraham Lincoln");
202201

203202
// No trip to the database has happened yet, so the author's
204203
// identifier isn't set yet.
205-
assertNull(author.getId());
204+
assertThat(author.getId(), nullValue());
206205

207206
// Set up a blog for that Author.
208207
Microblog blog = new Microblog(author, "Gettysburg Address Draft 1");
209208

210209
// Persist the blog. The Author should be persisted too.
211210
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));
214213

215214
// Commit the transaction. Because we're relying on the
216215
// default flush mode, this will cause a flush to the
217216
// database, which, in turn, will result in identifier
218217
// generation.
219218
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));
223222

224223
// We're no longer in a transaction.
225-
assertFalse(em.isJoinedToTransaction());
224+
assertThat(em.isJoinedToTransaction(), is(false));
226225

227226
// 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));
230229

231230
// Let's check the database directly.
232231
final DataSource ds = this.cdiContainer.select(DataSource.class).get();
233-
assertNotNull(ds);
232+
assertThat(ds, notNullValue());
234233
try (final Connection connection = ds.getConnection();
235234
final Statement statement = connection.createStatement()) {
236-
assertNotNull(statement);
235+
assertThat(statement, notNullValue());
237236
ResultSet rs = statement.executeQuery("SELECT COUNT(1) FROM MICROBLOG");
238-
assertNotNull(rs);
237+
assertThat(rs, notNullValue());
239238
try {
240-
assertTrue(rs.next());
241-
assertEquals(1, rs.getInt(1));
239+
assertThat(rs.next(), is(true));
240+
assertThat(rs.getInt(1), is(1));
242241
} finally {
243242
rs.close();
244243
}
245244
rs = statement.executeQuery("SELECT COUNT(1) FROM AUTHOR");
246-
assertNotNull(rs);
245+
assertThat(rs, notNullValue());
247246
try {
248-
assertTrue(rs.next());
249-
assertEquals(1, rs.getInt(1));
247+
assertThat(rs.next(), is(true));
248+
assertThat(rs.getInt(1), is(1));
250249
} finally {
251250
rs.close();
252251
}
@@ -255,12 +254,12 @@ void testCascadePersist()
255254
// Start a new transaction.
256255
tm.begin();
257256

258-
assertFalse(em.contains(blog));
257+
assertThat(em.contains(blog), is(false));
259258
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));
262261

263-
assertEquals(blog.getId(), newBlog.getId());
262+
assertThat(newBlog.getId(), is(blog.getId()));
264263
blog = newBlog;
265264

266265
// Now let's have our author write some stuff.
@@ -272,11 +271,11 @@ void testCascadePersist()
272271
+ "whether that nation, or any nation so conceived and so "
273272
+ "dedicated, can long endure.");
274273
blog.addChirp(chirp1);
275-
assertSame(blog, chirp1.getMicroblog());
274+
assertThat(chirp1.getMicroblog(), sameInstance(blog));
276275
blog.addChirp(chirp2);
277-
assertSame(blog, chirp2.getMicroblog());
276+
assertThat(chirp2.getMicroblog(), sameInstance(blog));
278277
blog.addChirp(chirp3);
279-
assertSame(blog, chirp3.getMicroblog());
278+
assertThat(chirp3.getMicroblog(), sameInstance(blog));
280279

281280
// Commit the transaction. The changes should be propagated.
282281
// However, this will fail, because the third chirp above is
@@ -293,12 +292,12 @@ void testCascadePersist()
293292
// functioned properly. Let's make sure.
294293
try (final Connection connection = ds.getConnection();
295294
final Statement statement = connection.createStatement()) {
296-
assertNotNull(statement);
295+
assertThat(statement, notNullValue());
297296
ResultSet rs = statement.executeQuery("SELECT COUNT(1) FROM CHIRP");
298-
assertNotNull(rs);
297+
assertThat(rs, notNullValue());
299298
try {
300-
assertTrue(rs.next());
301-
assertEquals(0, rs.getInt(1));
299+
assertThat(rs.next(), is(true));
300+
assertThat(rs.getInt(1), is(0));
302301
} finally {
303302
rs.close();
304303
}

0 commit comments

Comments
 (0)