Skip to content

Commit

Permalink
Merge pull request #1686 from arjantijms/cdi_entitymanagerfactory_test
Browse files Browse the repository at this point in the history
Add test for EntityManagerFactory injection using CDI
  • Loading branch information
arjantijms authored Dec 4, 2024
2 parents 27aa660 + d440320 commit f905a17
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
import jakarta.ws.rs.core.Application;

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
public class RestActivator extends Application {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

}
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -51,6 +61,7 @@ public static WebArchive deployment(@ArquillianResource TestArchiveProcessor arc

// Call the archive processor
archiveProcessor.processWebArchive(war, ServletEMLookupTest.class, null);

return war;
}

Expand All @@ -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());
}
}


}
Original file line number Diff line number Diff line change
@@ -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");
}

}

0 comments on commit f905a17

Please sign in to comment.