Skip to content

Commit

Permalink
Merge pull request #1623 from jakartaee/issue1405
Browse files Browse the repository at this point in the history
Initial draft of Jakarta Persistence/CDI EE integration tests
  • Loading branch information
arjantijms authored Dec 4, 2024
2 parents 2c0c9b9 + 41775bf commit 27aa660
Show file tree
Hide file tree
Showing 13 changed files with 386 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

<defaultProtocol type="Servlet 5.0" />
<engine>
<property name="deploymentExportPath">target/deployments</property>
</engine>
<extension qualifier="glassfish-descriptors">
<property name="descriptorDir">target/</property>
</extension>

<group qualifier="glassfish-servers" default="true">
<container qualifier="tck-rest" default="true">
<configuration>
<property name="glassFishHome">target/glassfish8</property>
</configuration>
</container>
</group>

</arquillian>
46 changes: 45 additions & 1 deletion glassfish-runner/persistence-platform-tck/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@
<dependency>
<groupId>org.omnifaces.arquillian</groupId>
<artifactId>arquillian-glassfish-server-managed</artifactId>
<version>1.6</version>
<version>1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.omnifaces.arquillian</groupId>
<artifactId>glassfish-client-ee11</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -519,7 +525,27 @@
<version>3.5.0</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
<dependenciesToScan>jakarta.tck:persistence-platform-tck-tests</dependenciesToScan>

<systemPropertyVariables>
<glassfish.home>${project.build.directory}/${glassfish.toplevel.dir}</glassfish.home>
<glassfish.postBootCommands>set server-config.network-config.protocols.protocol.http-listener-1.http.trace-enabled=true
create-file-user --groups guest --passwordfile ${project.basedir}/javajoe.pass javajoe
create-file-user --groups staff:mgr --passwordfile ${project.basedir}/j2ee.pass j2ee
create-jdbc-connection-pool --restype javax.sql.DataSource --datasourceclassname org.apache.derby.jdbc.ClientDataSource --property DatabaseName=derbyDB:serverName=localhost:portNumber=1527:user=cts1:password=cts1:connectionAttributes="create=true" --steadypoolsize 32 --maxpoolsize 64 cts-derby-pool
create-jdbc-connection-pool --restype javax.sql.DataSource --datasourceclassname org.apache.derby.jdbc.ClientDataSource --property DatabaseName=derbyDB:serverName=localhost:PortNumber=1527:User=cts1:Password=cts1 --steadypoolsize 32 --maxpoolsize 64 cts-derby-pool_no_tx
create-jdbc-resource --connectionpoolid cts-derby-pool jdbc/DB1
create-jdbc-resource --connectionpoolid cts-derby-pool_no_tx jdbc/DB_no_tx
list-jdbc-connection-pools
list-jdbc-resources
list-file-users</glassfish.postBootCommands>
<junit.log.traceflag>true</junit.log.traceflag>
<harness.log.traceflag>true</harness.log.traceflag>
<cts.harness.debug>true</cts.harness.debug>
<project.basedir>${project.basedir}</project.basedir>
</systemPropertyVariables>
</configuration>

<executions>
<execution>
<id>jpa-tests-appclient</id>
Expand Down Expand Up @@ -556,6 +582,24 @@
</systemPropertyVariables>
</configuration>
</execution>

<execution>
<id>jpa-tests-cdi</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>ee/jakarta/tck/persistence/ee/cdi/*Test.java</include>
</includes>

<systemPropertyVariables>
<arquillian.xml>rest-arquillian.xml</arquillian.xml>
</systemPropertyVariables>
</configuration>
</execution>

<execution>
<id>jta-tests-javatest</id>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<property name="deploymentExportPath">target/deployments</property>
</engine>
<extension qualifier="glassfish-descriptors">
<property name="descriptorDir">target/</property>
<property name="descriptorDir">target/</property>
</extension>

<group qualifier="glassfish-servers" default="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

<defaultProtocol type="Servlet 5.0" />
<engine>
<property name="deploymentExportPath">target/deployments</property>
</engine>
<extension qualifier="glassfish-descriptors">
<property name="descriptorDir">target/</property>
</extension>

<group qualifier="glassfish-servers" default="true">
<container qualifier="tck-rest" default="true">
<configuration>
<property name="glassFishHome">target/glassfish8</property>
</configuration>
</container>
</group>

</arquillian>
9 changes: 9 additions & 0 deletions jpa/platform-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@
<artifactId>arquillian-protocol-common</artifactId>
<version>${arquillian.jakarta.tck.version}</version>
</dependency>
<!-- For the CDI integration tests -->
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>

</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ee.jakarta.tck.persistence.ee.cdi;

import jakarta.inject.Qualifier;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Qualifier for selecting the CTS-EM2 persistence unit.
*/
@Qualifier
@Documented
@Retention(RUNTIME)
@Target({ FIELD, METHOD })
public @interface CtsEm2Qualifier {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ee.jakarta.tck.persistence.ee.cdi;

import jakarta.inject.Qualifier;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Qualifier for selecting the CTS-EM persistence unit.
*/
@Qualifier
@Documented
@Retention(RUNTIME)
@Target({ FIELD, METHOD })
public @interface CtsEmNoTxQualifier {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ee.jakarta.tck.persistence.ee.cdi;

import jakarta.inject.Qualifier;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Qualifier for selecting the CTS-EM persistence unit.
*/
@Qualifier
@Documented
@Retention(RUNTIME)
@Target({ FIELD, METHOD })
public @interface CtsEmQualifier {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ee.jakarta.tck.persistence.ee.cdi;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ee.jakarta.tck.persistence.ee.cdi;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

/**
* Called by test client using the arquillian rest protocol
*/
@Path("/cdi-jpa")
public class RestEndpoint {
@Inject
TestBeanEM testBean;

@GET
@Path("/injectEmUsingQualifier")
public String injectEmUsingQualifier() throws Exception {
testBean.injectEmUsingQualifier();
return "PASSED";
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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 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 static jakarta.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

/**
* 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);

// 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
);
// 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);

// Web content
war.addAsLibrary(jpa_ee_entityManager);

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

/*
* @testName: injectEmUsingQualifierTest
*
* @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 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ee.jakarta.tck.persistence.ee.cdi;

import jakarta.inject.Inject;
import jakarta.enterprise.context.RequestScoped;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

import java.util.logging.Logger;

/**
* A request scoped bean that uses the CTS-EM and CTS-EM-NOTX persistence units
*/
@RequestScoped
public class TestBeanEM {
Logger logger = Logger.getLogger(TestBeanEM.class.getName());

//@PersistenceContext(unitName = "CTS-EM")
@CtsEmQualifier
@Inject
private EntityManager em1;
//@PersistenceContext(unitName = "CTS-EM2")
@CtsEmQualifier
@Inject
private EntityManager em2;

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);
}

public void injectEmUsingQualifier() throws Exception {
boolean pass1 = false;
boolean pass2 = false;
try {
logMsg( "Test CtsEmNoTxQualifier");
if (em1 != null) {
logTrace( "Received non-null EntityManager");
pass1 = true;
} else {
logErr( "Received null EntityManager");
}
} catch (Exception e) {
logErr( "Received unexpected exception", e);
}
try {
logMsg( "Test CtsEmNoTxQualifier");
if (em2 != null) {
logTrace( "Received non-null EntityManager");
pass2 = true;
} else {
logErr( "Received null EntityManager");
}
} catch (Exception e) {
logErr( "Received unexpected exception", e);
}
if (!pass1 || !pass2) {
throw new Exception("createEntityManagerSynchronizationTypeMapTest failed");
}
logMsg( "Test PASSED");
}

}
Loading

0 comments on commit 27aa660

Please sign in to comment.