diff --git a/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/JaxRsActivator.java b/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/RestActivator.java similarity index 75% rename from jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/JaxRsActivator.java rename to jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/RestActivator.java index 6e8e82fb20..b9fcef7734 100644 --- a/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/JaxRsActivator.java +++ b/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/RestActivator.java @@ -4,6 +4,6 @@ import jakarta.ws.rs.core.Application; @ApplicationPath("/rest") -public class JaxRsActivator extends Application { +public class RestActivator extends Application { } \ No newline at end of file diff --git a/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/RestEndpoint.java b/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/RestEndpoint.java index 6e1f31330e..67a0a2a884 100644 --- a/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/RestEndpoint.java +++ b/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/RestEndpoint.java @@ -7,17 +7,24 @@ /** * Called by test client using the arquillian rest protocol */ -@Path("/cdi-jpa") +@Path("/cdi-persistence") public class RestEndpoint { + @Inject TestBeanEM testBean; @GET - @Path("/injectEmUsingQualifier") - public String injectEmUsingQualifier() throws Exception { - testBean.injectEmUsingQualifier(); + @Path("/injectEntityManagerUsingQualifier") + public String injectEntityManagerUsingQualifier() throws Exception { + testBean.injectEntityManagerUsingQualifier(); return "PASSED"; } + @GET + @Path("/injectEntityManagerFactoryUsingQualifier") + public String injectEntityManagerFactoryUsingQualifier() throws Exception { + testBean.injectEntityManagerFactoryUsingQualifier(); + return "PASSED"; + } } diff --git a/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/ServletEMLookupTest.java b/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/ServletEMLookupTest.java index a265ed5b8b..6483d3ea8a 100644 --- a/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/ServletEMLookupTest.java +++ b/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/ServletEMLookupTest.java @@ -1,48 +1,58 @@ package ee.jakarta.tck.persistence.ee.cdi; -import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.ClientBuilder; import jakarta.ws.rs.core.Response; + +import java.net.URL; + import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit5.ArquillianExtension; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.jboss.shrinkwrap.api.spec.WebArchive; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import tck.arquillian.porting.lib.spi.TestArchiveProcessor; -import java.net.URI; -import java.net.URL; +import ee.jakarta.tck.persistence.ee.entityManager.Order; +import tck.arquillian.porting.lib.spi.TestArchiveProcessor; import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON_TYPE; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Test for EntityManager lookup via CDI */ @ExtendWith(ArquillianExtension.class) public class ServletEMLookupTest { + @ArquillianResource URL contextPath; @Deployment(name = "jpa-cdi-em-inject", testable = false) public static WebArchive deployment(@ArquillianResource TestArchiveProcessor archiveProcessor) { WebArchive war = ShrinkWrap.create(WebArchive.class) - .addClasses(CtsEmQualifier.class, CtsEm2Qualifier.class, CtsEmNoTxQualifier.class, JaxRsActivator.class, - TestBeanEM.class, RestEndpoint.class); + .addClasses( + CtsEmQualifier.class, + CtsEm2Qualifier.class, + CtsEmNoTxQualifier.class, + RestActivator.class, + TestBeanEM.class, + RestEndpoint.class); // Par // the jar with the correct archive name JavaArchive jpa_ee_entityManager = ShrinkWrap.create(JavaArchive.class, "jpa_ee_entityManager.jar"); + // The class files jpa_ee_entityManager.addClasses( - ee.jakarta.tck.persistence.ee.entityManager.Order.class + Order.class ); + // The persistence.xml descriptor URL parURL = ServletEMLookupTest.class.getResource("persistence.xml"); jpa_ee_entityManager.addAsManifestResource(parURL, "persistence.xml"); + // Call the archive processor archiveProcessor.processParArchive(jpa_ee_entityManager, ServletEMLookupTest.class, parURL); @@ -51,6 +61,7 @@ public static WebArchive deployment(@ArquillianResource TestArchiveProcessor arc // Call the archive processor archiveProcessor.processWebArchive(war, ServletEMLookupTest.class, null); + return war; } @@ -64,13 +75,37 @@ public static WebArchive deployment(@ArquillianResource TestArchiveProcessor arc */ @Test public void injectEmUsingQualifierTest() throws Exception { - URI appIdURI = contextPath.toURI().resolve("rest/cdi-jpa/injectEmUsingQualifier"); - final Client client = ClientBuilder.newBuilder().build(); - final Response response = client.target(appIdURI) - .request(APPLICATION_JSON_TYPE) - .get(); - int status = response.getStatus(); - Assertions.assertEquals(200, status); - response.close(); + try (Response response = + ClientBuilder.newBuilder() + .build() + .target(contextPath.toURI().resolve("rest/cdi-persistence/injectEntityManagerUsingQualifier")) + .request(APPLICATION_JSON_TYPE) + .get()) { + + assertEquals(200, response.getStatus()); + } } + + /* + * @testName: injectEntityManagerFactoryUsingQualifier + * + * @assertion_ids: PERSISTENCE:JAVADOC:3318; PERSISTENCE:SPEC:1801; + * PERSISTENCE:SPEC:1804; PERSISTENCE:SPEC:1883.2; + * + * @test_Strategy: Inject an EntityManager using a qualifier and injection + */ + @Test + public void injectEntityManagerFactoryUsingQualifier() throws Exception { + try (Response response = + ClientBuilder.newBuilder() + .build() + .target(contextPath.toURI().resolve("rest/cdi-persistence/injectEntityManagerFactoryUsingQualifier")) + .request(APPLICATION_JSON_TYPE) + .get()) { + + assertEquals(200, response.getStatus()); + } + } + + } diff --git a/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/TestBeanEM.java b/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/TestBeanEM.java index 90ada4fab4..61eb91e757 100644 --- a/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/TestBeanEM.java +++ b/jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/TestBeanEM.java @@ -1,70 +1,121 @@ package ee.jakarta.tck.persistence.ee.cdi; -import jakarta.inject.Inject; import jakarta.enterprise.context.RequestScoped; +import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; +import jakarta.persistence.EntityManagerFactory; import java.util.logging.Logger; +import static java.util.logging.Level.SEVERE; + /** - * A request scoped bean that uses the CTS-EM and CTS-EM-NOTX persistence units + * A request scoped bean that uses the CTS-EM and CTS-EM2 persistence units */ @RequestScoped public class TestBeanEM { Logger logger = Logger.getLogger(TestBeanEM.class.getName()); - //@PersistenceContext(unitName = "CTS-EM") + @Inject @CtsEmQualifier + private EntityManager entityManager1; + + @Inject + @CtsEm2Qualifier + private EntityManager entityManager2; + @Inject - private EntityManager em1; - //@PersistenceContext(unitName = "CTS-EM2") @CtsEmQualifier + private EntityManagerFactory entityManagerFactory1; + @Inject - private EntityManager em2; + @CtsEm2Qualifier + private EntityManagerFactory entityManagerFactory2; public void logMsg(String msg) { logger.info(msg); } + public void logTrace(String msg) { logger.fine(msg); } + public void logErr(String msg) { logger.severe(msg); } + public void logErr(String msg, Throwable e) { - logger.log(java.util.logging.Level.SEVERE, msg, e); + logger.log(SEVERE, msg, e); } - public void injectEmUsingQualifier() throws Exception { + public void injectEntityManagerUsingQualifier() throws Exception { boolean pass1 = false; boolean pass2 = false; + try { - logMsg( "Test CtsEmNoTxQualifier"); - if (em1 != null) { - logTrace( "Received non-null EntityManager"); + logMsg("Test @CtsEmQualifier"); + if (entityManager1 != null) { + logTrace("Received non-null EntityManager"); pass1 = true; } else { - logErr( "Received null EntityManager"); + logErr("Received null EntityManager"); } } catch (Exception e) { - logErr( "Received unexpected exception", e); + logErr("Received unexpected exception", e); } + try { - logMsg( "Test CtsEmNoTxQualifier"); - if (em2 != null) { - logTrace( "Received non-null EntityManager"); + logMsg("Test @CtsEm2Qualifier"); + if (entityManager2 != null) { + logTrace("Received non-null EntityManager"); pass2 = true; } else { - logErr( "Received null EntityManager"); + logErr("Received null EntityManager"); } } catch (Exception e) { - logErr( "Received unexpected exception", e); + logErr("Received unexpected exception", e); } + if (!pass1 || !pass2) { - throw new Exception("createEntityManagerSynchronizationTypeMapTest failed"); + throw new Exception("injectEntityManagerUsingQualifier failed"); } - logMsg( "Test PASSED"); + + logMsg("Test PASSED"); + } + + public void injectEntityManagerFactoryUsingQualifier() throws Exception { + boolean pass1 = false; + boolean pass2 = false; + + try { + logMsg("Test @CtsEmQualifier"); + if (entityManagerFactory1 != null) { + logTrace("Received non-null EntityManagerFactory1"); + pass1 = true; + } else { + logErr("Received null EntityManagerFactory1"); + } + } catch (Exception e) { + logErr("Received unexpected exception", e); + } + + try { + logMsg("Test @CtsEm2Qualifier"); + if (entityManagerFactory2 != null) { + logTrace("Received non-null EntityManagerFactory2"); + pass2 = true; + } else { + logErr("Received null EntityManagerFactory2"); + } + } catch (Exception e) { + logErr("Received unexpected exception", e); + } + + if (!pass1 || !pass2) { + throw new Exception("injectEntityManagerFactoryUsingQualifier failed"); + } + + logMsg("Test PASSED"); } }