Skip to content

Commit 08902ff

Browse files
committed
[CDI + JPA + JTA] Using rule instead of base class for JTA management
1 parent a97abd9 commit 08902ff

File tree

3 files changed

+60
-60
lines changed

3 files changed

+60
-60
lines changed

other/cdi-jpa-testing/src/test/java/org/hibernate/demos/jpacditesting/CdiJpaTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,44 @@
1010
import java.util.List;
1111
import java.util.UUID;
1212

13+
import javax.enterprise.context.RequestScoped;
1314
import javax.inject.Inject;
1415
import javax.persistence.EntityManager;
1516
import javax.transaction.TransactionalException;
1617
import javax.transaction.UserTransaction;
1718

19+
import org.hibernate.demos.jpacditesting.support.JtaEnvironment;
20+
import org.jboss.weld.environment.se.Weld;
21+
import org.jboss.weld.junit4.WeldInitiator;
22+
import org.junit.ClassRule;
23+
import org.junit.Rule;
1824
import org.junit.Test;
1925

20-
public class CdiJpaTest extends CdiJpaTestBase {
26+
public class CdiJpaTest {
27+
28+
@ClassRule
29+
public static JtaEnvironment jtaEnvironment = new JtaEnvironment();
30+
31+
@Rule
32+
public WeldInitiator weld = WeldInitiator.from(new Weld())
33+
.activate(RequestScoped.class)
34+
.inject(this)
35+
.build();
36+
37+
// new Weld() above enables scanning of the classpath; alternatively, only the required beans can be listed explicitly:
38+
39+
// @Rule
40+
// public WeldInitiator weld = WeldInitiator.from(
41+
// ObserverTestBean.class,
42+
// TransactionalTestService.class,
43+
// TestService.class,
44+
// EntityManagerProducer.class,
45+
// EntityManagerFactoryProducer.class,
46+
// TransactionExtension.class
47+
// )
48+
// .activate(RequestScoped.class)
49+
// .inject(this)
50+
// .build();
2151

2252
@Inject
2353
private EntityManager entityManager;

other/cdi-jpa-testing/src/test/java/org/hibernate/demos/jpacditesting/CdiJpaTestBase.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.support;
6+
7+
import org.jnp.server.NamingBeanImpl;
8+
import org.junit.rules.ExternalResource;
9+
10+
import com.arjuna.ats.jta.utils.JNDIManager;
11+
12+
public class JtaEnvironment extends ExternalResource {
13+
14+
private NamingBeanImpl NAMING_BEAN;
15+
16+
@Override
17+
protected void before() throws Throwable {
18+
NAMING_BEAN = new NamingBeanImpl();
19+
NAMING_BEAN.start();
20+
21+
JNDIManager.bindJTAImplementation();
22+
TransactionalConnectionProvider.bindDataSource();
23+
}
24+
25+
@Override
26+
protected void after() {
27+
NAMING_BEAN.stop();
28+
}
29+
}

0 commit comments

Comments
 (0)