Skip to content

Commit e546d6c

Browse files
committed
Merge pull request #227 from arjantijms/master
Test that data-source can be defined in application.xml and used in persistence.xml.
2 parents f2d74e8 + efa82a6 commit e546d6c

File tree

9 files changed

+337
-0
lines changed

9 files changed

+337
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.javaee7.jpa</groupId>
8+
<artifactId>jpa-samples</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>datasourcedefinition-applicationxml-pu</artifactId>
13+
<packaging>war</packaging>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.h2database</groupId>
18+
<artifactId>h2</artifactId>
19+
<version>1.3.173</version>
20+
</dependency>
21+
</dependencies>
22+
23+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.javaee7.jpa.datasourcedefinition_applicationxml_pu.entity;
2+
3+
import static javax.persistence.GenerationType.IDENTITY;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
9+
/**
10+
* A very simple JPA entity that will be used for testing
11+
*
12+
* @author Arjan Tijms
13+
*
14+
*/
15+
@Entity
16+
public class TestEntity {
17+
18+
@Id
19+
@GeneratedValue(strategy = IDENTITY)
20+
private Long id;
21+
private String value;
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
31+
public String getValue() {
32+
return value;
33+
}
34+
35+
public void setValue(String value) {
36+
this.value = value;
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.javaee7.jpa.datasourcedefinition_applicationxml_pu.service;
2+
3+
import java.util.List;
4+
5+
import javax.ejb.Stateless;
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.PersistenceContext;
8+
9+
import org.javaee7.jpa.datasourcedefinition_applicationxml_pu.entity.TestEntity;
10+
11+
/**
12+
* This service does some JPA operations. The purpose of this entire test
13+
* is just to see whether the data source can be used so the actual operations
14+
* don't matter much.
15+
*
16+
* @author Arjan Tijms
17+
*
18+
*/
19+
@Stateless
20+
public class TestService {
21+
22+
@PersistenceContext
23+
private EntityManager entityManager;
24+
25+
public void saveNewEntity() {
26+
27+
TestEntity testEntity = new TestEntity();
28+
testEntity.setValue("mytest");
29+
30+
entityManager.persist(testEntity);
31+
}
32+
33+
public List<TestEntity> getAllEntities() {
34+
return entityManager.createQuery("SELECT _testEntity FROM TestEntity _testEntity", TestEntity.class)
35+
.getResultList();
36+
}
37+
38+
39+
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
4+
5+
<persistence-unit name="testPU">
6+
7+
<!-- This data source is defined from within the application via the data-source element in application.xml of the ear's
8+
META_INF folder. For the EJB variant of this test, the structure looks as follows:
9+
10+
EAR
11+
META-INF
12+
application.xml (defines data source)
13+
test.jar (EJB module)
14+
META-INF
15+
persistence.xml (this file: connects data source to persistence unit)
16+
org.javaee7.jpa.datasourcedefinition_applicationxml_pu.service.TestService (injects PU via @PersistenceContext)
17+
18+
-->
19+
<jta-data-source>java:app/MyApp/MyDS</jta-data-source>
20+
21+
<properties>
22+
<!--
23+
Very unfortunate workaround to get the data source to work with JPA in WildFly 8.
24+
See https://issues.jboss.org/browse/WFLY-2727
25+
-->
26+
<property name="wildfly.jpa.twophasebootstrap" value="false" />
27+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
28+
</properties>
29+
</persistence-unit>
30+
31+
</persistence>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd"
4+
version="7">
5+
6+
<module>
7+
<web>
8+
<web-uri>test.war</web-uri>
9+
<context-root>/test</context-root>
10+
</web>
11+
</module>
12+
<module>
13+
<ejb>test.jar</ejb>
14+
</module>
15+
16+
<data-source>
17+
<name>java:app/MyApp/MyDS</name>
18+
<class-name>org.h2.jdbcx.JdbcDataSource</class-name>
19+
<url>jdbc:h2:mem:test</url>
20+
</data-source>
21+
22+
</application>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd"
4+
version="7">
5+
6+
<module>
7+
<web>
8+
<web-uri>test.war</web-uri>
9+
<context-root>/test</context-root>
10+
</web>
11+
</module>
12+
13+
<data-source>
14+
<name>java:app/MyApp/MyDS</name>
15+
<class-name>org.h2.jdbcx.JdbcDataSource</class-name>
16+
<url>jdbc:h2:mem:test</url>
17+
</data-source>
18+
19+
</application>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.javaee7.jpa.datasourcedefinition_applicationxml_pu;
2+
3+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.List;
7+
8+
import javax.inject.Inject;
9+
10+
import org.javaee7.jpa.datasourcedefinition_applicationxml_pu.entity.TestEntity;
11+
import org.javaee7.jpa.datasourcedefinition_applicationxml_pu.service.TestService;
12+
import org.jboss.arquillian.container.test.api.Deployment;
13+
import org.jboss.arquillian.junit.Arquillian;
14+
import org.jboss.shrinkwrap.api.Archive;
15+
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
16+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
17+
import org.jboss.shrinkwrap.api.spec.WebArchive;
18+
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
/**
22+
* This tests that a data source defined via the data-source element in an EAR's application.xml can be used by JPA.
23+
* <p>
24+
* In this test the persistence unit is defined inside an EJB module (.jar)
25+
*
26+
* <p>
27+
* The actual JPA code being run is not specifically relevant; any kind of JPA operation that
28+
* uses the data source is okay here.
29+
*
30+
* @author Arjan Tijms
31+
*/
32+
@RunWith(Arquillian.class)
33+
public class DataSourceDefinitionApplicationXMLPuEJBTest {
34+
35+
@Inject
36+
private TestService testService;
37+
38+
@Deployment
39+
public static Archive<?> deploy() {
40+
return
41+
// EAR archive
42+
create(EnterpriseArchive.class, "test.ear")
43+
44+
// Data-source is defined here
45+
.setApplicationXML("application-ejb.xml")
46+
47+
// JDBC driver for data source
48+
.addAsLibraries(Maven.resolver()
49+
.loadPomFromFile("pom.xml")
50+
.resolve("com.h2database:h2")
51+
.withoutTransitivity()
52+
.asSingleFile())
53+
54+
// EJB module
55+
.addAsModule(
56+
create(JavaArchive.class, "test.jar")
57+
58+
// Persistence unit is defined here, references data source
59+
.addAsResource("META-INF/persistence.xml")
60+
61+
// Service class that uses persistence unit
62+
.addClasses(TestEntity.class, TestService.class)
63+
)
64+
65+
// Web module
66+
// This is needed to prevent Arquillian generating an illegal application.xml
67+
.addAsModule(
68+
create(WebArchive.class, "test.war")
69+
// This class containing the test
70+
.addClass(DataSourceDefinitionApplicationXMLPuEJBTest.class)
71+
72+
);
73+
}
74+
75+
@Test
76+
public void insertAndQueryEntity() throws Exception {
77+
78+
testService.saveNewEntity();
79+
80+
List<TestEntity> testEntities = testService.getAllEntities();
81+
82+
assertTrue(testEntities.size() == 1);
83+
assertTrue(testEntities.get(0).getValue().equals("mytest"));
84+
}
85+
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.javaee7.jpa.datasourcedefinition_applicationxml_pu;
2+
3+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.List;
7+
8+
import javax.inject.Inject;
9+
10+
import org.javaee7.jpa.datasourcedefinition_applicationxml_pu.entity.TestEntity;
11+
import org.javaee7.jpa.datasourcedefinition_applicationxml_pu.service.TestService;
12+
import org.jboss.arquillian.container.test.api.Deployment;
13+
import org.jboss.arquillian.junit.Arquillian;
14+
import org.jboss.shrinkwrap.api.Archive;
15+
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
16+
import org.jboss.shrinkwrap.api.spec.WebArchive;
17+
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
/**
21+
* This tests that a data source defined via the data-source element in an EAR's application.xml can be used by JPA.
22+
* <p>
23+
* In this test the persistence unit is defined inside a web module (.war)
24+
*
25+
* <p>
26+
* The actual JPA code being run is not specifically relevant; any kind of JPA operation that
27+
* uses the data source is okay here.
28+
*
29+
* @author Arjan Tijms
30+
*/
31+
@RunWith(Arquillian.class)
32+
public class DataSourceDefinitionApplicationXMLPuWebTest {
33+
34+
@Inject
35+
private TestService testService;
36+
37+
@Deployment
38+
public static Archive<?> deploy() {
39+
return
40+
// EAR archive
41+
create(EnterpriseArchive.class, "test.ear")
42+
43+
// Data-source is defined here
44+
.setApplicationXML("application-web.xml")
45+
46+
// JDBC driver for data source
47+
.addAsLibraries(Maven.resolver()
48+
.loadPomFromFile("pom.xml")
49+
.resolve("com.h2database:h2")
50+
.withoutTransitivity()
51+
.asSingleFile())
52+
53+
// WAR module
54+
.addAsModule(
55+
create(WebArchive.class, "test.war")
56+
57+
// Persistence unit is defined here, references data source
58+
.addAsResource("META-INF/persistence.xml")
59+
60+
// Service class that uses persistence unit
61+
.addPackages(true, DataSourceDefinitionApplicationXMLPuWebTest.class.getPackage())
62+
);
63+
}
64+
65+
@Test
66+
public void insertAndQueryEntity() throws Exception {
67+
68+
testService.saveNewEntity();
69+
70+
List<TestEntity> testEntities = testService.getAllEntities();
71+
72+
assertTrue(testEntities.size() == 1);
73+
assertTrue(testEntities.get(0).getValue().equals("mytest"));
74+
}
75+
76+
}

jpa/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<module>datasourcedefinition</module>
2020
<module>datasourcedefinition-webxml-pu</module>
2121
<module>datasourcedefinition-annotation-pu</module>
22+
<module>datasourcedefinition-applicationxml-pu</module>
2223
<module>dynamic-named-query</module>
2324
<module>entitygraph</module>
2425
<module>listeners</module>

0 commit comments

Comments
 (0)