Skip to content

Commit a1a296d

Browse files
committed
adding a new test for default datasource
1 parent 9544725 commit a1a296d

File tree

7 files changed

+161
-0
lines changed

7 files changed

+161
-0
lines changed

jpa/default-datasource/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jpa</groupId>
6+
<artifactId>jpa-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<artifactId>default-datasource</artifactId>
12+
<packaging>war</packaging>
13+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.javaee7.jpa.defaultdatasource;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Column;
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.NamedQueries;
10+
import javax.persistence.NamedQuery;
11+
import javax.persistence.Table;
12+
13+
/**
14+
* @author Arun Gupta
15+
*/
16+
@Entity
17+
@Table(name = "EMPLOYEE_SCHEMA_DEFAULT_DATASOURCE")
18+
@NamedQueries({
19+
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
20+
})
21+
public class Employee implements Serializable {
22+
private static final long serialVersionUID = 1L;
23+
@Id
24+
@GeneratedValue(strategy = GenerationType.AUTO)
25+
private int id;
26+
27+
@Column(length=40)
28+
private String name;
29+
30+
public Employee() { }
31+
32+
public Employee(String name) {
33+
this.name = name;
34+
}
35+
36+
public int getId() {
37+
return id;
38+
}
39+
40+
public void setId(int id) {
41+
this.id = id;
42+
}
43+
44+
public String getName() {
45+
return name;
46+
}
47+
48+
public void setName(String name) {
49+
this.name = name;
50+
}
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.javaee7.jpa.defaultdatasource;
2+
3+
import java.util.List;
4+
import javax.ejb.Stateless;
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.PersistenceContext;
7+
8+
/**
9+
* @author Arun Gupta
10+
*/
11+
@Stateless
12+
public class EmployeeBean {
13+
14+
@PersistenceContext
15+
EntityManager em;
16+
17+
public void persist(Employee e) {
18+
em.persist(e);
19+
}
20+
21+
public List<Employee> get() {
22+
return em.createNamedQuery("Employee.findAll", Employee.class).getResultList();
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
INSERT INTO EMPLOYEE_SCHEMA_DEFAULT_DATASOURCE("ID", "NAME") VALUES (1, 'Penny')
2+
INSERT INTO EMPLOYEE_SCHEMA_DEFAULT_DATASOURCE("ID", "NAME") VALUES (2, 'Sheldon')
3+
INSERT INTO EMPLOYEE_SCHEMA_DEFAULT_DATASOURCE("ID", "NAME") VALUES (3, 'Amy')
4+
INSERT INTO EMPLOYEE_SCHEMA_DEFAULT_DATASOURCE("ID", "NAME") VALUES (4, 'Leonard')
5+
INSERT INTO EMPLOYEE_SCHEMA_DEFAULT_DATASOURCE("ID", "NAME") VALUES (5, 'Bernadette')
6+
INSERT INTO EMPLOYEE_SCHEMA_DEFAULT_DATASOURCE("ID", "NAME") VALUES (6, 'Raj')
7+
INSERT INTO EMPLOYEE_SCHEMA_DEFAULT_DATASOURCE("ID", "NAME") VALUES (7, 'Howard')
8+
INSERT INTO EMPLOYEE_SCHEMA_DEFAULT_DATASOURCE("ID", "NAME") VALUES (8, 'Priya')
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
3+
version="2.1"
4+
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
7+
<persistence-unit name="MyPU" transaction-type="JTA">
8+
<properties>
9+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
10+
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
11+
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
12+
<property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>
13+
</properties>
14+
</persistence-unit>
15+
</persistence>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.javaee7.jpa.defaultdatasource;
2+
3+
import org.javaee7.jpa.defaultdatasource.Employee;
4+
import org.javaee7.jpa.defaultdatasource.EmployeeBean;
5+
import java.util.List;
6+
import javax.inject.Inject;
7+
import org.jboss.arquillian.container.test.api.Deployment;
8+
import org.jboss.arquillian.junit.Arquillian;
9+
import org.jboss.shrinkwrap.api.ShrinkWrap;
10+
import org.jboss.shrinkwrap.api.spec.WebArchive;
11+
import org.junit.Test;
12+
import static org.junit.Assert.*;
13+
import org.junit.runner.RunWith;
14+
15+
/**
16+
* @author Arun Gupta
17+
*/
18+
@RunWith(Arquillian.class)
19+
public class EmployeeBeanTest {
20+
21+
@Inject
22+
EmployeeBean bean;
23+
24+
@Deployment
25+
public static WebArchive createDeployment() {
26+
return ShrinkWrap.create(WebArchive.class)
27+
.addClasses(Employee.class,
28+
EmployeeBean.class)
29+
.addAsResource("META-INF/persistence.xml")
30+
.addAsResource("META-INF/load.sql");
31+
}
32+
33+
@Test
34+
public void testGet() throws Exception {
35+
assertNotNull(bean);
36+
List<Employee> list = bean.get();
37+
assertNotNull(list);
38+
assertEquals(8, list.size());
39+
assertFalse(list.contains(new Employee("Penny")));
40+
assertFalse(list.contains(new Employee("Sheldon")));
41+
assertFalse(list.contains(new Employee("Amy")));
42+
assertFalse(list.contains(new Employee("Leonard")));
43+
assertFalse(list.contains(new Employee("Bernadette")));
44+
assertFalse(list.contains(new Employee("Raj")));
45+
assertFalse(list.contains(new Employee("Howard")));
46+
assertFalse(list.contains(new Employee("Priya")));
47+
}
48+
49+
}

jpa/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<module>unsynchronized-pc</module>
4141
<module>extended-pc</module>
4242
<module>jpa-converter</module>
43+
<module>default-datasource</module>
4344
</modules>
4445

4546
</project>

0 commit comments

Comments
 (0)