Skip to content

Commit

Permalink
Use Hamcrest assertions instead of JUnit in integrations/cdi/jpa-cdi …
Browse files Browse the repository at this point in the history
…- backport 2.x (helidon-io#1749)
  • Loading branch information
Captain1653 authored and romain-grecourt committed Jun 5, 2023
1 parent e837231 commit 0cb82a9
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 311 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,9 +42,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

@ApplicationScoped
Expand Down Expand Up @@ -79,7 +80,7 @@ void startCdiContainer() {
System.setProperty("jpaAnnotationRewritingEnabled", "true");
final SeContainerInitializer initializer = SeContainerInitializer.newInstance()
.addBeanClasses(this.getClass());
assertNotNull(initializer);
assertThat(initializer, notNullValue());
this.cdiContainer = initializer.initialize();
}

Expand Down Expand Up @@ -110,24 +111,24 @@ private void onShutdown(@Observes @BeforeDestroyed(ApplicationScoped.class) fina
@PersistenceContext(unitName = "test")
private void observerMethod(@Observes final TestIsRunning event,
final EntityManager emParameter) {
assertNotNull(event);
assertThat(event, notNullValue());

assertNotNull(emParameter);
assertTrue(emParameter.isOpen());
assertFalse(emParameter.isJoinedToTransaction());
assertThat(emParameter, notNullValue());
assertThat(emParameter.isOpen(), is(true));
assertThat(emParameter.isJoinedToTransaction(), is(false));

assertNotNull(this.em);
assertTrue(this.em.isOpen());
assertFalse(this.em.isJoinedToTransaction());
assertThat(this.em, notNullValue());
assertThat(this.em.isOpen(), is(true));
assertThat(this.em.isJoinedToTransaction(), is(false));

assertNotNull(this.emf);
assertTrue(this.emf.isOpen());
assertThat(this.emf, notNullValue());
assertThat(this.emf.isOpen(), is(true));
EntityManager em = null;
try {
em = this.emf.createEntityManager();
assertNotNull(em);
assertTrue(em.isOpen());
assertFalse(em.isJoinedToTransaction());
assertThat(em, notNullValue());
assertThat(em.isOpen(), is(true));
assertThat(em.isJoinedToTransaction(), is(false));
} finally {
if (em != null && !em.isOpen()) {
em.close();
Expand Down Expand Up @@ -163,9 +164,9 @@ void testNonTransactionalEntityManager() {
qualifiers.add(ContainerManaged.Literal.INSTANCE);
qualifiers.add(JpaTransactionScoped.Literal.INSTANCE);
final EntityManager entityManager = this.cdiContainer.select(EntityManager.class, qualifiers.toArray(new Annotation[qualifiers.size()])).get();
assertTrue(entityManager instanceof DelegatingEntityManager);
assertTrue(entityManager.isOpen());
assertFalse(entityManager.isJoinedToTransaction());
assertThat(entityManager, instanceOf(DelegatingEntityManager.class));
assertThat(entityManager.isOpen(), is(true));
assertThat(entityManager.isJoinedToTransaction(), is(false));
try {
entityManager.persist(new Object());
fail("A TransactionRequiredException should have been thrown");
Expand All @@ -188,14 +189,14 @@ void testTransactionalEntityManager() {
.fire(new TestIsRunning("testTransactionalEntityManager"));
final Instance<TestAnnotationRewriting> instance = this.cdiContainer.select(TestAnnotationRewriting.class);
final TestAnnotationRewriting test = instance.get();
assertNotNull(test);
assertThat(test, notNullValue());
test.testEntityManagerIsJoinedToTransactionInTransactionalAnnotatedMethod();
}

@Transactional
void testEntityManagerIsJoinedToTransactionInTransactionalAnnotatedMethod() {
assertNotNull(this.em);
assertTrue(this.em.isJoinedToTransaction());
assertThat(this.em, notNullValue());
assertThat(this.em.isJoinedToTransaction(), is(true));
try {
this.em.close();
fail("Closed EntityManager; should not have been able to");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,16 +17,17 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;

final class TestMessages {

@Test
final void testMessages() {
final String message = Messages.format("resourceLocalPersistenceUnitWarning");
assertNotNull(message);
assertTrue(message.startsWith("The persistence unit "));
assertThat(message, notNullValue());
assertThat(message, startsWith("The persistence unit "));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,10 +42,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

@ApplicationScoped
@DataSourceDefinition(
Expand Down Expand Up @@ -84,7 +83,7 @@ class TestMultiplePersistenceUnits {
void startCdiContainer() {
final SeContainerInitializer initializer = SeContainerInitializer.newInstance()
.addBeanClasses(this.getClass());
assertNotNull(initializer);
assertThat(initializer, notNullValue());
this.cdiContainer = initializer.initialize();
}

Expand Down Expand Up @@ -123,15 +122,15 @@ private void onShutdown(@Observes @BeforeDestroyed(ApplicationScoped.class) fina
@Test
void testMultiplePersistenceUnits() {
TestMultiplePersistenceUnits self = this.cdiContainer.select(TestMultiplePersistenceUnits.class).get();
assertNotNull(self);
assertThat(self, notNullValue());

EntityManager testEm = self.getTestEntityManager();
assertNotNull(testEm);
assertTrue(testEm.isOpen());
assertThat(testEm, notNullValue());
assertThat(testEm.isOpen(), is(true));

EntityManager test2Em = self.getTest2EntityManager();
assertNotNull(test2Em);
assertTrue(test2Em.isOpen());
assertThat(test2Em, notNullValue());
assertThat(test2Em.isOpen(), is(true));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,12 +45,11 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

@ApplicationScoped
Expand Down Expand Up @@ -97,13 +96,13 @@ class TestCascadePersist {
void startCdiContainerAndRunDDL() throws SQLException {
final SeContainerInitializer initializer = SeContainerInitializer.newInstance()
.addBeanClasses(this.getClass());
assertNotNull(initializer);
assertThat(initializer, notNullValue());
this.cdiContainer = initializer.initialize();
final DataSource ds = this.cdiContainer.select(DataSource.class).get();
assertNotNull(ds);
assertThat(ds, notNullValue());
try (final Connection connection = ds.getConnection();
final Statement statement = connection.createStatement()) {
assertNotNull(statement);
assertThat(statement, notNullValue());
statement.executeUpdate("RUNSCRIPT FROM 'classpath:chirp.ddl'");
}
}
Expand Down Expand Up @@ -170,83 +169,83 @@ void testCascadePersist()
// Get a CDI contextual reference to this test instance. It
// is important to use "self" in this test instead of "this".
final TestCascadePersist self = self();
assertNotNull(self);
assertThat(self, notNullValue());

// Get the EntityManager that is synchronized with and scoped
// to a JTA transaction.
final EntityManager em = self.getEntityManager();
assertNotNull(em);
assertTrue(em.isOpen());
assertThat(em, notNullValue());
assertThat(em.isOpen(), is(true));

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

// Get the TransactionManager that normally is behind the
// scenes and use it to start a Transaction. This simulates
// entering a method annotated
// with @Transactional(TxType.REQUIRES_NEW) or similar.
final TransactionManager tm = self.getTransactionManager();
assertNotNull(tm);
assertThat(tm, notNullValue());
tm.begin();
assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
assertThat(tm.getStatus(), is(Status.STATUS_ACTIVE));

// Now magically our EntityManager should be joined to it.
assertTrue(em.isJoinedToTransaction());
assertThat(em.isJoinedToTransaction(), is(true));

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

// No trip to the database has happened yet, so the author's
// identifier isn't set yet.
assertNull(author.getId());
assertThat(author.getId(), nullValue());

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

// Persist the blog. The Author should be persisted too.
em.persist(blog);
assertTrue(em.contains(blog));
assertTrue(em.contains(author));
assertThat(em.contains(blog), is(true));
assertThat(em.contains(author), is(true));

// Commit the transaction. Because we're relying on the
// default flush mode, this will cause a flush to the
// database, which, in turn, will result in identifier
// generation.
tm.commit();
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
assertEquals(Integer.valueOf(1), author.getId());
assertEquals(Integer.valueOf(1), blog.getId());
assertThat(tm.getStatus(), is(Status.STATUS_NO_TRANSACTION));
assertThat(author.getId(), is(1));
assertThat(blog.getId(), is(1));

// We're no longer in a transaction.
assertFalse(em.isJoinedToTransaction());
assertThat(em.isJoinedToTransaction(), is(false));

// The persistence context should be cleared.
assertFalse(em.contains(blog));
assertFalse(em.contains(author));
assertThat(em.contains(blog), is(false));
assertThat(em.contains(author), is(false));

// Let's check the database directly.
final DataSource ds = this.cdiContainer.select(DataSource.class).get();
assertNotNull(ds);
assertThat(ds, notNullValue());
try (final Connection connection = ds.getConnection();
final Statement statement = connection.createStatement()) {
assertNotNull(statement);
assertThat(statement, notNullValue());
ResultSet rs = statement.executeQuery("SELECT COUNT(1) FROM MICROBLOG");
assertNotNull(rs);
assertThat(rs, notNullValue());
try {
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertThat(rs.next(), is(true));
assertThat(rs.getInt(1), is(1));
} finally {
rs.close();
}
rs = statement.executeQuery("SELECT COUNT(1) FROM AUTHOR");
assertNotNull(rs);
assertThat(rs, notNullValue());
try {
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertThat(rs.next(), is(true));
assertThat(rs.getInt(1), is(1));
} finally {
rs.close();
}
Expand All @@ -255,12 +254,12 @@ void testCascadePersist()
// Start a new transaction.
tm.begin();

assertFalse(em.contains(blog));
assertThat(em.contains(blog), is(false));
final Microblog newBlog = em.find(Microblog.class, Integer.valueOf(1));
assertNotNull(newBlog);
assertTrue(em.contains(newBlog));
assertThat(newBlog, notNullValue());
assertThat(em.contains(newBlog), is(true));

assertEquals(blog.getId(), newBlog.getId());
assertThat(newBlog.getId(), is(blog.getId()));
blog = newBlog;

// Now let's have our author write some stuff.
Expand All @@ -272,11 +271,11 @@ void testCascadePersist()
+ "whether that nation, or any nation so conceived and so "
+ "dedicated, can long endure.");
blog.addChirp(chirp1);
assertSame(blog, chirp1.getMicroblog());
assertThat(chirp1.getMicroblog(), sameInstance(blog));
blog.addChirp(chirp2);
assertSame(blog, chirp2.getMicroblog());
assertThat(chirp2.getMicroblog(), sameInstance(blog));
blog.addChirp(chirp3);
assertSame(blog, chirp3.getMicroblog());
assertThat(chirp3.getMicroblog(), sameInstance(blog));

// Commit the transaction. The changes should be propagated.
// However, this will fail, because the third chirp above is
Expand All @@ -293,12 +292,12 @@ void testCascadePersist()
// functioned properly. Let's make sure.
try (final Connection connection = ds.getConnection();
final Statement statement = connection.createStatement()) {
assertNotNull(statement);
assertThat(statement, notNullValue());
ResultSet rs = statement.executeQuery("SELECT COUNT(1) FROM CHIRP");
assertNotNull(rs);
assertThat(rs, notNullValue());
try {
assertTrue(rs.next());
assertEquals(0, rs.getInt(1));
assertThat(rs.next(), is(true));
assertThat(rs.getInt(1), is(0));
} finally {
rs.close();
}
Expand Down
Loading

0 comments on commit 0cb82a9

Please sign in to comment.