Skip to content

Commit

Permalink
4.x: Reenables failing JPA test (helidon-io#9037)
Browse files Browse the repository at this point in the history
* Reenables failing JPA test

Signed-off-by: Laird Nelson <laird.nelson@oracle.com>

* Squashable commit; streamlines use of System.setProperty

Signed-off-by: Laird Nelson <laird.nelson@oracle.com>

---------

Signed-off-by: Laird Nelson <laird.nelson@oracle.com>
  • Loading branch information
ljnelson authored Jul 24, 2024
1 parent c974591 commit 1e4a511
Showing 1 changed file with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
Expand All @@ -58,40 +57,59 @@
"user=sa"
}
)
// Note that this class tests JpaExtension behavior, not PersistenceExtension behavior. JpaExtension is deprecated and
// this test suite exists for backwards-compatibility tests only.
class TestAnnotationRewriting {

@PersistenceUnit(unitName = "test-resource-local")
private EntityManagerFactory emf;

@PersistenceContext(name = "bogus", unitName = "test")
private EntityManager em;

@PersistenceContext(unitName = "test", type = PersistenceContextType.EXTENDED)
private EntityManager extendedEm;


private String persistenceExtensionEnabledProperty;

private String jpaExtensionEnabledProperty;

private SeContainer cdiContainer;

TestAnnotationRewriting() {
super();
}

@BeforeEach
void startCdiContainer() {
final SeContainerInitializer initializer = SeContainerInitializer.newInstance()
this.jpaExtensionEnabledProperty = System.setProperty(JpaExtension.class.getName() + ".enabled", "true");
this.persistenceExtensionEnabledProperty = System.setProperty(PersistenceExtension.class.getName() + ".enabled", "false");

SeContainerInitializer initializer = SeContainerInitializer.newInstance()
.addBeanClasses(this.getClass());
assertThat(initializer, notNullValue());
this.cdiContainer = initializer.initialize();
}

@AfterEach
void shutDownCdiContainer() {
if (this.cdiContainer != null) {
this.cdiContainer.close();
}
if (this.jpaExtensionEnabledProperty == null) {
System.clearProperty(JpaExtension.class.getName() + ".enabled");
} else {
System.setProperty(JpaExtension.class.getName() + ".enabled", this.jpaExtensionEnabledProperty);
}
if (this.persistenceExtensionEnabledProperty == null) {
System.clearProperty(PersistenceExtension.class.getName() + ".enabled");
} else {
System.setProperty(PersistenceExtension.class.getName() + ".enabled", this.persistenceExtensionEnabledProperty);
}
}

private void onShutdown(@Observes @BeforeDestroyed(ApplicationScoped.class) final Object event,
final TransactionManager tm) throws SystemException {
private void onShutdown(@Observes @BeforeDestroyed(ApplicationScoped.class) Object event,
TransactionManager tm) throws SystemException {
// If an assertion fails, or some other error happens in the
// CDI container, there may be a current transaction that has
// neither been committed nor rolled back. Because the
Expand All @@ -106,10 +124,10 @@ private void onShutdown(@Observes @BeforeDestroyed(ApplicationScoped.class) fina
tm.rollback();
}
}

@PersistenceContext(unitName = "test")
private void observerMethod(@Observes final TestIsRunning event,
final EntityManager emParameter) {
private void observerMethod(@Observes TestIsRunning event,
EntityManager emParameter) {
assertThat(event, notNullValue());

assertThat(emParameter, notNullValue());
Expand All @@ -136,8 +154,8 @@ private void observerMethod(@Observes final TestIsRunning event,
try {
em = this.emf.createEntityManager(SynchronizationType.UNSYNCHRONIZED, null);
fail("Was able to pass a non-null SynchronizationType");
} catch (final IllegalStateException expected) {
} catch (IllegalStateException expected) {

} finally {
if (em != null && !em.isOpen()) {
em.close();
Expand All @@ -154,29 +172,28 @@ void testAnnotationRewriting() {
}

@Test
@Disabled // TODO See https://github.com/helidon-io/helidon/issues/8122
void testNonTransactionalEntityManager() {
this.cdiContainer.getBeanManager()
.getEvent()
.select(TestIsRunning.class)
.fire(new TestIsRunning("testNonTransactionalEntityManager"));
final Set<Annotation> qualifiers = new HashSet<>();
Set<Annotation> qualifiers = new HashSet<>();
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();
qualifiers.add(JpaTransactionScoped.Literal.INSTANCE); // Note that this is from the old stuff and is deprecated.
EntityManager entityManager = this.cdiContainer.select(EntityManager.class, qualifiers.toArray(new Annotation[qualifiers.size()])).get();
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");
} catch (final TransactionRequiredException expected) {
} catch (TransactionRequiredException expected) {

}
try {
entityManager.close();
fail("Closed EntityManager; should not have been able to");
} catch (final IllegalStateException expected) {
} catch (IllegalStateException expected) {

}
}
Expand All @@ -187,35 +204,35 @@ void testTransactionalEntityManager() {
.getEvent()
.select(TestIsRunning.class)
.fire(new TestIsRunning("testTransactionalEntityManager"));
final Instance<TestAnnotationRewriting> instance = this.cdiContainer.select(TestAnnotationRewriting.class);
final TestAnnotationRewriting test = instance.get();
Instance<TestAnnotationRewriting> instance = this.cdiContainer.select(TestAnnotationRewriting.class);
TestAnnotationRewriting test = instance.get();
assertThat(test, notNullValue());
test.testEntityManagerIsJoinedToTransactionInTransactionalAnnotatedMethod();
}

@Transactional
void testEntityManagerIsJoinedToTransactionInTransactionalAnnotatedMethod() {
assertThat(this.em, notNullValue());
assertThat(this.em.isJoinedToTransaction(), is(true));
try {
this.em.close();
fail("Closed EntityManager; should not have been able to");
} catch (final IllegalStateException expected) {
} catch (IllegalStateException expected) {

}
}

private static final class TestIsRunning {
private static class TestIsRunning {

private final String test;

private TestIsRunning(final String test) {
private TestIsRunning(String test) {
super();
this.test = test;
}

@Override
public final String toString() {
public String toString() {
return this.test;
}
}
Expand Down

0 comments on commit 1e4a511

Please sign in to comment.