3939import jakarta .transaction .Transactional ;
4040import org .junit .jupiter .api .AfterEach ;
4141import org .junit .jupiter .api .BeforeEach ;
42- import org .junit .jupiter .api .Disabled ;
4342import org .junit .jupiter .api .Test ;
4443
4544import static org .hamcrest .CoreMatchers .instanceOf ;
5857 "user=sa"
5958 }
6059)
60+ // Note that this class tests JpaExtension behavior, not PersistenceExtension behavior. JpaExtension is deprecated and
61+ // this test suite exists for backwards-compatibility tests only.
6162class TestAnnotationRewriting {
6263
6364 @ PersistenceUnit (unitName = "test-resource-local" )
6465 private EntityManagerFactory emf ;
65-
66+
6667 @ PersistenceContext (name = "bogus" , unitName = "test" )
6768 private EntityManager em ;
6869
6970 @ PersistenceContext (unitName = "test" , type = PersistenceContextType .EXTENDED )
7071 private EntityManager extendedEm ;
71-
72+
73+ private String persistenceExtensionEnabledProperty ;
74+
75+ private String jpaExtensionEnabledProperty ;
76+
7277 private SeContainer cdiContainer ;
7378
7479 TestAnnotationRewriting () {
7580 super ();
7681 }
77-
82+
7883 @ BeforeEach
7984 void startCdiContainer () {
80- final SeContainerInitializer initializer = SeContainerInitializer .newInstance ()
85+ this .jpaExtensionEnabledProperty = System .setProperty (JpaExtension .class .getName () + ".enabled" , "true" );
86+ this .persistenceExtensionEnabledProperty = System .setProperty (PersistenceExtension .class .getName () + ".enabled" , "false" );
87+
88+ SeContainerInitializer initializer = SeContainerInitializer .newInstance ()
8189 .addBeanClasses (this .getClass ());
8290 assertThat (initializer , notNullValue ());
8391 this .cdiContainer = initializer .initialize ();
8492 }
85-
93+
8694 @ AfterEach
8795 void shutDownCdiContainer () {
8896 if (this .cdiContainer != null ) {
8997 this .cdiContainer .close ();
9098 }
99+ if (this .jpaExtensionEnabledProperty == null ) {
100+ System .clearProperty (JpaExtension .class .getName () + ".enabled" );
101+ } else {
102+ System .setProperty (JpaExtension .class .getName () + ".enabled" , this .jpaExtensionEnabledProperty );
103+ }
104+ if (this .persistenceExtensionEnabledProperty == null ) {
105+ System .clearProperty (PersistenceExtension .class .getName () + ".enabled" );
106+ } else {
107+ System .setProperty (PersistenceExtension .class .getName () + ".enabled" , this .persistenceExtensionEnabledProperty );
108+ }
91109 }
92110
93- private void onShutdown (@ Observes @ BeforeDestroyed (ApplicationScoped .class ) final Object event ,
94- final TransactionManager tm ) throws SystemException {
111+ private void onShutdown (@ Observes @ BeforeDestroyed (ApplicationScoped .class ) Object event ,
112+ TransactionManager tm ) throws SystemException {
95113 // If an assertion fails, or some other error happens in the
96114 // CDI container, there may be a current transaction that has
97115 // neither been committed nor rolled back. Because the
@@ -106,10 +124,10 @@ private void onShutdown(@Observes @BeforeDestroyed(ApplicationScoped.class) fina
106124 tm .rollback ();
107125 }
108126 }
109-
127+
110128 @ PersistenceContext (unitName = "test" )
111- private void observerMethod (@ Observes final TestIsRunning event ,
112- final EntityManager emParameter ) {
129+ private void observerMethod (@ Observes TestIsRunning event ,
130+ EntityManager emParameter ) {
113131 assertThat (event , notNullValue ());
114132
115133 assertThat (emParameter , notNullValue ());
@@ -136,8 +154,8 @@ private void observerMethod(@Observes final TestIsRunning event,
136154 try {
137155 em = this .emf .createEntityManager (SynchronizationType .UNSYNCHRONIZED , null );
138156 fail ("Was able to pass a non-null SynchronizationType" );
139- } catch (final IllegalStateException expected ) {
140-
157+ } catch (IllegalStateException expected ) {
158+
141159 } finally {
142160 if (em != null && !em .isOpen ()) {
143161 em .close ();
@@ -154,29 +172,28 @@ void testAnnotationRewriting() {
154172 }
155173
156174 @ Test
157- @ Disabled // TODO See https://github.com/helidon-io/helidon/issues/8122
158175 void testNonTransactionalEntityManager () {
159176 this .cdiContainer .getBeanManager ()
160177 .getEvent ()
161178 .select (TestIsRunning .class )
162179 .fire (new TestIsRunning ("testNonTransactionalEntityManager" ));
163- final Set <Annotation > qualifiers = new HashSet <>();
180+ Set <Annotation > qualifiers = new HashSet <>();
164181 qualifiers .add (ContainerManaged .Literal .INSTANCE );
165- qualifiers .add (JpaTransactionScoped .Literal .INSTANCE );
166- final EntityManager entityManager = this .cdiContainer .select (EntityManager .class , qualifiers .toArray (new Annotation [qualifiers .size ()])).get ();
182+ qualifiers .add (JpaTransactionScoped .Literal .INSTANCE ); // Note that this is from the old stuff and is deprecated.
183+ EntityManager entityManager = this .cdiContainer .select (EntityManager .class , qualifiers .toArray (new Annotation [qualifiers .size ()])).get ();
167184 assertThat (entityManager , instanceOf (DelegatingEntityManager .class ));
168185 assertThat (entityManager .isOpen (), is (true ));
169186 assertThat (entityManager .isJoinedToTransaction (), is (false ));
170187 try {
171188 entityManager .persist (new Object ());
172189 fail ("A TransactionRequiredException should have been thrown" );
173- } catch (final TransactionRequiredException expected ) {
190+ } catch (TransactionRequiredException expected ) {
174191
175192 }
176193 try {
177194 entityManager .close ();
178195 fail ("Closed EntityManager; should not have been able to" );
179- } catch (final IllegalStateException expected ) {
196+ } catch (IllegalStateException expected ) {
180197
181198 }
182199 }
@@ -187,35 +204,35 @@ void testTransactionalEntityManager() {
187204 .getEvent ()
188205 .select (TestIsRunning .class )
189206 .fire (new TestIsRunning ("testTransactionalEntityManager" ));
190- final Instance <TestAnnotationRewriting > instance = this .cdiContainer .select (TestAnnotationRewriting .class );
191- final TestAnnotationRewriting test = instance .get ();
207+ Instance <TestAnnotationRewriting > instance = this .cdiContainer .select (TestAnnotationRewriting .class );
208+ TestAnnotationRewriting test = instance .get ();
192209 assertThat (test , notNullValue ());
193210 test .testEntityManagerIsJoinedToTransactionInTransactionalAnnotatedMethod ();
194211 }
195-
212+
196213 @ Transactional
197214 void testEntityManagerIsJoinedToTransactionInTransactionalAnnotatedMethod () {
198215 assertThat (this .em , notNullValue ());
199216 assertThat (this .em .isJoinedToTransaction (), is (true ));
200217 try {
201218 this .em .close ();
202219 fail ("Closed EntityManager; should not have been able to" );
203- } catch (final IllegalStateException expected ) {
220+ } catch (IllegalStateException expected ) {
204221
205222 }
206223 }
207224
208- private static final class TestIsRunning {
225+ private static class TestIsRunning {
209226
210227 private final String test ;
211228
212- private TestIsRunning (final String test ) {
229+ private TestIsRunning (String test ) {
213230 super ();
214231 this .test = test ;
215232 }
216233
217234 @ Override
218- public final String toString () {
235+ public String toString () {
219236 return this .test ;
220237 }
221238 }
0 commit comments