Author: swd847
Date: 2010-07-15 09:14:06 -0400 (Thu, 15 Jul 2010)
New Revision: 13406
Added:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/EntityManagerProvider.java
Modified:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionInterceptor.java
modules/persistence/trunk/src/main/resources/META-INF/beans.xml
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java
Log:
added test of the transaction interceptor
Modified:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionInterceptor.java
===================================================================
---
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionInterceptor.java 2010-07-15
12:52:34 UTC (rev 13405)
+++
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionInterceptor.java 2010-07-15
13:14:06 UTC (rev 13406)
@@ -7,6 +7,7 @@
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
/**
@@ -16,6 +17,7 @@
* @author Shane Bryzak
*/
@Transactional
+@Interceptor
public class TransactionInterceptor
{
private static final long serialVersionUID = -4364203056333738988L;
Modified: modules/persistence/trunk/src/main/resources/META-INF/beans.xml
===================================================================
--- modules/persistence/trunk/src/main/resources/META-INF/beans.xml 2010-07-15 12:52:34
UTC (rev 13405)
+++ modules/persistence/trunk/src/main/resources/META-INF/beans.xml 2010-07-15 13:14:06
UTC (rev 13406)
@@ -8,4 +8,7 @@
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
+ <interceptors>
+ <class>org.jboss.seam.transaction.TransactionInterceptor</class>
+ </interceptors>
</beans>
Modified:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java
===================================================================
---
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java 2010-07-15
12:52:34 UTC (rev 13405)
+++
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java 2010-07-15
13:14:06 UTC (rev 13406)
@@ -34,7 +34,11 @@
public static Archive<?> createTestArchive()
{
- WebArchive war = ShrinkWrap.create("test.war",
WebArchive.class).addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS)).addPackage(Transaction.class.getPackage()).addPackage(SimpleTest.class.getPackage()).addWebResource("META-INF/persistence.xml",
"classes/META-INF/persistence.xml").addWebResource(new ByteArrayAsset(new
byte[0]), "beans.xml");
+ WebArchive war = ShrinkWrap.create(WebArchive.class);
+ war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
+ war.addPackage(Transaction.class.getPackage()).addClasses(SimpleTest.class,
Hotel.class);
+ war.addWebResource("META-INF/persistence.xml",
"classes/META-INF/persistence.xml");
+ war.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
return war;
}
Added:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
===================================================================
---
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
(rev 0)
+++
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-15
13:14:06 UTC (rev 13406)
@@ -0,0 +1,79 @@
+package org.jboss.seam.transactions.test;
+
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.transaction.Transaction;
+import org.jboss.seam.transaction.TransactionInterceptor;
+import org.jboss.seam.transaction.UserTransaction;
+import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.EntityManagerProvider;
+import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+(a)RunWith(Arquillian.class)
+public class TransactionInterceptorTest
+{
+ @Deployment
+ public static Archive<?> createTestArchive()
+ {
+
+ WebArchive war = ShrinkWrap.create("test.war", WebArchive.class);
+ war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
+ war.addPackage(Transaction.class.getPackage());
+ war.addClasses(TransactionInterceptorTest.class, TransactionManagedBean.class,
Hotel.class, EntityManagerProvider.class);
+ war.addWebResource("META-INF/persistence.xml",
"classes/META-INF/persistence.xml");
+ war.addWebResource(new
ByteArrayAsset(("<beans><interceptors><class>" +
TransactionInterceptor.class.getName() +
"</class></interceptors></beans>").getBytes()),
"beans.xml");
+
+ return war;
+ }
+
+ @Inject
+ TransactionManagedBean bean;
+
+ @Inject
+ UserTransaction transaction;
+
+ @PersistenceContext
+ EntityManager em;
+
+ @Test
+ public void testTransactionInterceptor() throws NotSupportedException,
SystemException, SecurityException, IllegalStateException, RollbackException,
HeuristicMixedException, HeuristicRollbackException
+ {
+
+ bean.addHotel();
+
+ try
+ {
+ bean.failToAddHotel();
+ }
+ catch (Exception e)
+ {
+
+ }
+
+ transaction.begin();
+ em.joinTransaction();
+ List<Hotel> hotels = em.createQuery("select h from Hotel
h").getResultList();
+ Assert.assertTrue("Wrong number of hotels: " + hotels.size(),
hotels.size() == 1);
+ transaction.rollback();
+
+ }
+}
Added:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
===================================================================
---
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
(rev 0)
+++
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-15
13:14:06 UTC (rev 13406)
@@ -0,0 +1,32 @@
+package org.jboss.seam.transactions.test;
+
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+
+import org.jboss.seam.transaction.TransactionPropagation;
+import org.jboss.seam.transaction.Transactional;
+
+(a)Transactional(TransactionPropagation.REQUIRED)
+public class TransactionManagedBean
+{
+
+ @Inject
+ EntityManager entityManager;
+
+ public void addHotel()
+ {
+ entityManager.joinTransaction();
+ Hotel h = new Hotel("test", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
+ entityManager.persist(h);
+ entityManager.flush();
+ }
+
+ public void failToAddHotel()
+ {
+ entityManager.joinTransaction();
+ Hotel h = new Hotel("test2", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
+ entityManager.persist(h);
+ entityManager.flush();
+ throw new RuntimeException("Roll back transaction");
+ }
+}
Added:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/EntityManagerProvider.java
===================================================================
---
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/EntityManagerProvider.java
(rev 0)
+++
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/EntityManagerProvider.java 2010-07-15
13:14:06 UTC (rev 13406)
@@ -0,0 +1,12 @@
+package org.jboss.seam.transactions.test.util;
+
+import javax.enterprise.inject.Produces;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+public class EntityManagerProvider
+{
+ @PersistenceContext
+ @Produces
+ EntityManager entityManager;
+}