Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5172f7

Browse files
committed
Added tests for using an @DataSourceDefinition data source with JPA
1 parent 5b1bd9a commit e5172f7

File tree

13 files changed

+393
-1
lines changed

13 files changed

+393
-1
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-annotation-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>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.javaee7.jpa.datasourcedefinition_annotation_pu.config;
2+
3+
import javax.annotation.sql.DataSourceDefinition;
4+
import javax.ejb.Stateless;
5+
6+
@DataSourceDefinition(
7+
name = "java:app/MyApp/MyDS",
8+
className = "org.h2.jdbcx.JdbcDataSource",
9+
url = "jdbc:h2:mem:test"
10+
)
11+
@Stateless
12+
public class DataSourceDefinitionConfig {
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.javaee7.jpa.datasourcedefinition_annotation_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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.javaee7.jpa.datasourcedefinition_annotation_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_annotation_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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
<!--
8+
This data source is defined from within the application via the @DataSourceDefinition annotation on
9+
class org.javaee7.jpa.datasourcedefinition_annotation_pu.config.DataSourceDefinitionConfig
10+
-->
11+
<jta-data-source>java:app/MyApp/MyDS</jta-data-source>
12+
13+
<properties>
14+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
15+
</properties>
16+
</persistence-unit>
17+
18+
</persistence>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.javaee7.jpa.datasourcedefinition_annotation_pu;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.List;
6+
7+
import javax.inject.Inject;
8+
9+
import org.javaee7.jpa.datasourcedefinition_annotation_pu.config.DataSourceDefinitionConfig;
10+
import org.javaee7.jpa.datasourcedefinition_annotation_pu.entity.TestEntity;
11+
import org.javaee7.jpa.datasourcedefinition_annotation_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.ShrinkWrap;
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+
/**
22+
* This tests that a data source defined via an annotation in {@link DataSourceDefinitionConfig} can be used by JPA.
23+
* <p>
24+
* The actual JPA code being run is not specifically relevant; any kind of JPA operation that
25+
* uses the data source is okay here.
26+
*
27+
* @author Arjan Tijms
28+
*/
29+
@RunWith(Arquillian.class)
30+
public class DataSourceDefinitionAnnotationPuTest {
31+
32+
@Inject
33+
private TestService testService;
34+
35+
@Deployment
36+
public static Archive<?> deploy() {
37+
return ShrinkWrap.create(WebArchive.class)
38+
.addPackages(true, DataSourceDefinitionAnnotationPuTest.class.getPackage())
39+
.addAsResource("META-INF/persistence.xml")
40+
.addAsLibraries(Maven.resolver()
41+
.loadPomFromFile("pom.xml")
42+
.resolve("com.h2database:h2")
43+
.withoutTransitivity()
44+
.asSingleFile())
45+
;
46+
}
47+
48+
@Test
49+
public void insertAndQueryEntity() throws Exception {
50+
51+
testService.saveNewEntity();
52+
53+
List<TestEntity> testEntities = testService.getAllEntities();
54+
55+
assertTrue(testEntities.size() == 1);
56+
assertTrue(testEntities.get(0).getValue().equals("mytest"));
57+
}
58+
}
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-webxml-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>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.javaee7.jpa.datasourcedefinition_webxml_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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.javaee7.jpa.datasourcedefinition_webxml_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_webxml_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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 web.xml -->
8+
<jta-data-source>java:app/MyApp/MyDS</jta-data-source>
9+
10+
<properties>
11+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
12+
</properties>
13+
</persistence-unit>
14+
15+
</persistence>

0 commit comments

Comments
 (0)