Skip to content

Commit c222229

Browse files
committed
Adding demo showing how to use CDI + JPA + JTA (@transactional, transactional observer methods) in unittests
1 parent b13741c commit c222229

19 files changed

+706
-3
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ transaction.log
4646
infinispan.log
4747

4848
.gradle
49+
50+
#Misc.
51+
ObjectStore

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ This repository contains demos used live during presentations or in blog posts;
2929
Accompanies the blog post http://in.relation.to/2018/02/26/putting-bean-validation-constraints-to-multimaps/
3030

3131
* Java 9
32-
- multi-release-jar-demo: Shows how to build multi-release JARs with Java 9.
32+
- _multi-release-jar-demo_: Shows how to build multi-release JARs with Java 9.
3333
Accompanies the blog post http://in.relation.to/2017/02/13/building-multi-release-jars-with-maven/
34-
- custom-jlink-plugin: Shows how to customize Java 9 modular runtime images with jlink plug-ins. The example shows a plug-in for adding a Jandex annotation index for one or more modules to the runtime image.
34+
- _custom-jlink-plugin_: Shows how to customize Java 9 modular runtime images with jlink plug-ins. The example shows a plug-in for adding a Jandex annotation index for one or more modules to the runtime image.
3535
* Other
36-
- wildfly-patch-creation: How to create WildFly patch files.
36+
- _wildfly-patch-creation_: How to create WildFly patch files.
3737
Accompanies the blog post http://in.relation.to/2017/05/29/creating-patches-for-wildfly/
38+
- _cdi-jpa-testing_: How to run JUnit tests using CDI, JPA and JTA
3839

3940
## License
4041

other/cdi-jpa-testing/pom.xml

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
License: Apache License, Version 2.0
4+
See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
5+
-->
6+
7+
<project xmlns="http://maven.apache.org/POM/4.0.0"
8+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<groupId>org.hibernate.demos</groupId>
13+
<artifactId>jpa-cdi-testing</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
16+
<name>Demo for testing CDI + JPA + JTA under Java SE</name>
17+
<url>https://hibernate.org/orm</url>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<maven.compiler.source>8</maven.compiler.source>
22+
<maven.compiler.target>8</maven.compiler.target>
23+
24+
<version.com.h2database>1.4.197</version.com.h2database>
25+
<version.org.hibernate>5.4.0.Final</version.org.hibernate>
26+
<version.narayana>5.9.2.Final</version.narayana>
27+
<version.jnpserver>5.0.3.GA</version.jnpserver>
28+
<version.jboss-transaction-api>1.1.1.Final</version.jboss-transaction-api>
29+
<version.log4j>1.2.17</version.log4j>
30+
<version.weld>3.0.5.Final</version.weld>
31+
</properties>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>javax.inject</groupId>
36+
<artifactId>javax.inject</artifactId>
37+
<version>1</version>
38+
<scope>provided</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>javax.enterprise</groupId>
42+
<artifactId>cdi-api</artifactId>
43+
<version>2.0.SP1</version>
44+
<scope>provided</scope>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>junit</groupId>
49+
<artifactId>junit</artifactId>
50+
<version>4.12</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.hibernate</groupId>
55+
<artifactId>hibernate-core</artifactId>
56+
<version>${version.org.hibernate}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.h2database</groupId>
60+
<artifactId>h2</artifactId>
61+
<version>${version.com.h2database}</version>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.jboss.weld.se</groupId>
66+
<artifactId>weld-se-core</artifactId>
67+
<version>${version.weld}</version>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.jboss.weld.module</groupId>
72+
<artifactId>weld-jta</artifactId>
73+
<version>${version.weld}</version>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.jboss.narayana.jta</groupId>
78+
<artifactId>narayana-jta</artifactId>
79+
<version>${version.narayana}</version>
80+
<exclusions>
81+
<exclusion>
82+
<groupId>org.jboss.logmanager</groupId>
83+
<artifactId>jboss-logmanager</artifactId>
84+
</exclusion>
85+
</exclusions>
86+
<scope>test</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.jboss.spec.javax.transaction</groupId>
90+
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
91+
<version>${version.jboss-transaction-api}</version>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>log4j</groupId>
96+
<artifactId>log4j</artifactId>
97+
<version>${version.log4j}</version>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.jboss.naming</groupId>
101+
<artifactId>jnpserver</artifactId>
102+
<version>${version.jnpserver}</version>
103+
<exclusions>
104+
<exclusion>
105+
<groupId>org.jboss.logging</groupId>
106+
<artifactId>jboss-logging-spi</artifactId>
107+
</exclusion>
108+
</exclusions>
109+
<scope>test</scope>
110+
</dependency>
111+
<dependency>
112+
<groupId>org.jboss.weld</groupId>
113+
<artifactId>weld-junit4</artifactId>
114+
<version>1.3.1.Final</version>
115+
<scope>test</scope>
116+
</dependency>
117+
<dependency>
118+
<groupId>org.assertj</groupId>
119+
<artifactId>assertj-core</artifactId>
120+
<version>3.11.1</version>
121+
<scope>test</scope>
122+
</dependency>
123+
</dependencies>
124+
125+
<build>
126+
<pluginManagement>
127+
<plugins>
128+
<plugin>
129+
<artifactId>maven-clean-plugin</artifactId>
130+
<version>3.1.0</version>
131+
</plugin>
132+
<plugin>
133+
<artifactId>maven-resources-plugin</artifactId>
134+
<version>3.0.2</version>
135+
</plugin>
136+
<plugin>
137+
<artifactId>maven-compiler-plugin</artifactId>
138+
<version>3.8.0</version>
139+
</plugin>
140+
<plugin>
141+
<artifactId>maven-surefire-plugin</artifactId>
142+
<version>2.22.1</version>
143+
</plugin>
144+
<plugin>
145+
<artifactId>maven-jar-plugin</artifactId>
146+
<version>3.0.2</version>
147+
</plugin>
148+
<plugin>
149+
<artifactId>maven-install-plugin</artifactId>
150+
<version>2.5.2</version>
151+
</plugin>
152+
<plugin>
153+
<artifactId>maven-deploy-plugin</artifactId>
154+
<version>2.8.2</version>
155+
</plugin>
156+
<plugin>
157+
<artifactId>maven-site-plugin</artifactId>
158+
<version>3.7.1</version>
159+
</plugin>
160+
<plugin>
161+
<artifactId>maven-project-info-reports-plugin</artifactId>
162+
<version>3.0.0</version>
163+
</plugin>
164+
</plugins>
165+
</pluginManagement>
166+
167+
</build>
168+
<repositories>
169+
<repository>
170+
<id>jboss-public-repository-group</id>
171+
<name>JBoss Public Maven Repository Group</name>
172+
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
173+
<releases>
174+
<enabled>true</enabled>
175+
</releases>
176+
<snapshots>
177+
<enabled>true</enabled>
178+
</snapshots>
179+
</repository>
180+
</repositories>
181+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* License: Apache License, Version 2.0
3+
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
4+
*/
5+
package org.hibernate.demos.jpacditesting;
6+
7+
import java.util.UUID;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.EntityListeners;
11+
import javax.persistence.Id;
12+
13+
@Entity
14+
@EntityListeners(TestListener.class)
15+
public class TestEntity {
16+
17+
@Id
18+
public UUID id;
19+
20+
public String name;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* License: Apache License, Version 2.0
3+
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
4+
*/
5+
package org.hibernate.demos.jpacditesting;
6+
7+
import javax.enterprise.context.ApplicationScoped;
8+
import javax.inject.Inject;
9+
import javax.persistence.PostPersist;
10+
11+
@ApplicationScoped
12+
public class TestListener {
13+
14+
@Inject
15+
private TestService testService;
16+
17+
@PostPersist public void onPostPersist(TestEntity entity) {
18+
testService.addTestEntityName(entity.name);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* License: Apache License, Version 2.0
3+
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
4+
*/
5+
package org.hibernate.demos.jpacditesting;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import javax.enterprise.context.ApplicationScoped;
11+
12+
@ApplicationScoped
13+
public class TestService {
14+
15+
private final List<String> names = new ArrayList<>();
16+
17+
public List<String> getTestEntityNames() {
18+
return names;
19+
}
20+
21+
public void addTestEntityName(String name) {
22+
names.add(name);
23+
}
24+
}

other/cdi-jpa-testing/src/main/resources/META-INF/beans.xml

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
License: Apache License, Version 2.0
4+
See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
5+
-->
6+
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
7+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
9+
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
10+
version="2.1">
11+
12+
<persistence-unit name="sinusTestPu" transaction-type="JTA">
13+
<description>Hibernate test case template Persistence Unit</description>
14+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
15+
<jta-data-source>java:/sinusDs</jta-data-source>
16+
17+
<exclude-unlisted-classes>false</exclude-unlisted-classes>
18+
19+
<properties>
20+
<property name="hibernate.archive.autodetection" value="class, hbm"/>
21+
22+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
23+
24+
<property name="hibernate.connection.pool_size" value="5"/>
25+
26+
<property name="hibernate.show_sql" value="true"/>
27+
<property name="hibernate.format_sql" value="true"/>
28+
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
29+
30+
<property name="hibernate.max_fetch_depth" value="5"/>
31+
32+
<property name="hibernate.cache.region_prefix" value="hibernate.test"/>
33+
34+
<property name="javax.persistence.validation.mode" value="NONE"/>
35+
<property name="hibernate.service.allow_crawling" value="false"/>
36+
<property name="hibernate.session.events.log" value="true"/>
37+
</properties>
38+
39+
</persistence-unit>
40+
</persistence>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* License: Apache License, Version 2.0
3+
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
4+
*/
5+
package org.hibernate.demos.jpacditesting;
6+
7+
import javax.enterprise.context.RequestScoped;
8+
import javax.enterprise.inject.se.SeContainerInitializer;
9+
10+
import org.hibernate.demos.jpacditesting.support.TransactionalConnectionProvider;
11+
import org.jboss.weld.environment.se.Weld;
12+
import org.jboss.weld.junit4.WeldInitiator;
13+
import org.jnp.server.NamingBeanImpl;
14+
import org.junit.AfterClass;
15+
import org.junit.BeforeClass;
16+
import org.junit.Rule;
17+
18+
import com.arjuna.ats.jta.utils.JNDIManager;
19+
20+
public abstract class AbstractCdiTest {
21+
22+
private static NamingBeanImpl NAMING_BEAN;
23+
24+
@Rule
25+
public WeldInitiator weld = WeldInitiator.from(((Weld) SeContainerInitializer.newInstance()))
26+
.activate(RequestScoped.class)
27+
.inject(this)
28+
.build();
29+
30+
@BeforeClass
31+
public static void startJndiAndBindJtaAndDataSource() throws Exception {
32+
NAMING_BEAN = new NamingBeanImpl();
33+
NAMING_BEAN.start();
34+
35+
JNDIManager.bindJTAImplementation();
36+
TransactionalConnectionProvider.bindDataSource();
37+
}
38+
39+
@AfterClass
40+
public static void stopJndi() {
41+
NAMING_BEAN.stop();
42+
}
43+
}

0 commit comments

Comments
 (0)