[seam-commits] Seam SVN: r13409 - in modules/persistence/trunk/src: test/java/org/jboss/seam/transactions/test and 1 other directory.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Thu Jul 15 10:46:23 EDT 2010
Author: swd847
Date: 2010-07-15 10:46:21 -0400 (Thu, 15 Jul 2010)
New Revision: 13409
Added:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/AfterTransactionCompletion.java
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/BeforeTransactionCompletion.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java
Modified:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EjbSynchronizations.java
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SeSynchronizations.java
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SynchronizationRegistry.java
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
Log:
add transaction events
Added: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/AfterTransactionCompletion.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/AfterTransactionCompletion.java (rev 0)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/AfterTransactionCompletion.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -0,0 +1,16 @@
+package org.jboss.seam.transaction;
+
+public class AfterTransactionCompletion
+{
+ private final boolean success;
+
+ public AfterTransactionCompletion(boolean success)
+ {
+ this.success = success;
+ }
+
+ public boolean success()
+ {
+ return success;
+ }
+}
Added: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/BeforeTransactionCompletion.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/BeforeTransactionCompletion.java (rev 0)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/BeforeTransactionCompletion.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -0,0 +1,6 @@
+package org.jboss.seam.transaction;
+
+public class BeforeTransactionCompletion
+{
+
+}
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EjbSynchronizations.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EjbSynchronizations.java 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EjbSynchronizations.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -10,6 +10,8 @@
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.context.RequestScoped;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
import javax.transaction.Synchronization;
import org.slf4j.Logger;
@@ -31,6 +33,9 @@
{
private static final Logger log = LoggerFactory.getLogger(EjbSynchronizations.class);
+ @Inject
+ private BeanManager beanManager;
+
// maintain two lists to work around a bug in JBoss EJB3 where a new
// SessionSynchronization
// gets registered each time the bean is called
@@ -40,7 +45,7 @@
public void afterBegin()
{
log.debug("afterBegin");
- synchronizations.addLast(new SynchronizationRegistry());
+ synchronizations.addLast(new SynchronizationRegistry(beanManager));
}
public void beforeCompletion() throws EJBException, RemoteException
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SeSynchronizations.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SeSynchronizations.java 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SeSynchronizations.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -4,6 +4,8 @@
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Alternative;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
import javax.transaction.Synchronization;
/**
@@ -22,9 +24,12 @@
{
protected Stack<SynchronizationRegistry> synchronizations = new Stack<SynchronizationRegistry>();
+ @Inject
+ private BeanManager beanManager;
+
public void afterTransactionBegin()
{
- synchronizations.push(new SynchronizationRegistry());
+ synchronizations.push(new SynchronizationRegistry(beanManager));
}
public void afterTransactionCommit(boolean success)
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SynchronizationRegistry.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SynchronizationRegistry.java 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SynchronizationRegistry.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;
+import javax.enterprise.inject.spi.BeanManager;
import javax.transaction.Status;
import javax.transaction.Synchronization;
@@ -19,6 +20,14 @@
*/
class SynchronizationRegistry
{
+
+ private final BeanManager beanManager;
+
+ public SynchronizationRegistry(BeanManager beanManager)
+ {
+ this.beanManager = beanManager;
+ }
+
private static final Logger log = LoggerFactory.getLogger(SynchronizationRegistry.class);
private List<Synchronization> synchronizations = new ArrayList<Synchronization>();
@@ -30,11 +39,7 @@
void afterTransactionCompletion(boolean success)
{
- // if ( Events.exists() )
- // {
- // Events.instance().raiseEvent("org.jboss.seam.afterTransactionCompletion",
- // success);
- // }
+ beanManager.fireEvent(new AfterTransactionCompletion(success));
for (Synchronization sync : synchronizations)
{
try
@@ -51,10 +56,7 @@
void beforeTransactionCompletion()
{
- // if ( Events.exists() )
- // {
- // Events.instance().raiseEvent("org.jboss.seam.beforeTransactionCompletion");
- // }
+ beanManager.fireEvent(new BeforeTransactionCompletion());
for (Synchronization sync : synchronizations)
{
try
Modified: 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 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -39,7 +39,7 @@
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, DontRollBackException.class);
+ war.addClasses(TransactionInterceptorTest.class, TransactionManagedBean.class, Hotel.class, EntityManagerProvider.class, DontRollBackException.class, TransactionObserver.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");
@@ -55,12 +55,20 @@
@PersistenceContext
EntityManager em;
+ @Inject
+ TransactionObserver observer;
+
@Test
public void testTransactionInterceptor() throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException
{
-
+ observer.setEnabled(true);
+ try
+ {
+ observer.reset(true);
bean.addHotel();
assertHotels(1);
+ observer.verify();
+ observer.reset(false);
try
{
bean.failToAddHotel();
@@ -69,7 +77,8 @@
{
}
assertHotels(1);
-
+ observer.verify();
+ observer.reset(true);
try
{
bean.addHotelWithApplicationException();
@@ -78,6 +87,13 @@
{
}
assertHotels(2);
+ observer.verify();
+ }
+ catch (Exception e)
+ {
+ observer.setEnabled(false);
+ throw new RuntimeException(e);
+ }
}
Modified: 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 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -14,6 +14,7 @@
@Inject
EntityManager entityManager;
+
public void addHotel()
{
entityManager.joinTransaction();
@@ -39,4 +40,5 @@
entityManager.flush();
throw new DontRollBackException();
}
+
}
Added: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java (rev 0)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -0,0 +1,63 @@
+package org.jboss.seam.transactions.test;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
+
+import org.jboss.seam.transaction.AfterTransactionCompletion;
+import org.jboss.seam.transaction.BeforeTransactionCompletion;
+
+ at ApplicationScoped
+public class TransactionObserver
+{
+ private boolean expectSuccess, beforeTransaction, afterTransaction;
+ private boolean enabled = false;
+
+ public boolean isEnabled()
+ {
+ return enabled;
+ }
+
+ public void setEnabled(boolean enabled)
+ {
+ this.enabled = enabled;
+ }
+
+ public void reset(boolean expected)
+ {
+ beforeTransaction = false;
+ afterTransaction = false;
+ expectSuccess = expected;
+ }
+
+ public boolean isBeforeTransaction()
+ {
+ return beforeTransaction;
+ }
+
+ public boolean isAfterTransaction()
+ {
+ return afterTransaction;
+ }
+
+ public void observeBeforeTransactionCommit(@Observes BeforeTransactionCompletion event)
+ {
+ beforeTransaction = true;
+ }
+
+ public void observeBeforeTransactionCommit(@Observes AfterTransactionCompletion event)
+ {
+ afterTransaction = true;
+ if (expectSuccess != event.success())
+ {
+ throw new RuntimeException("Expected success to be " + expectSuccess);
+ }
+ }
+
+ public void verify()
+ {
+ if (!((beforeTransaction || !expectSuccess) && afterTransaction))
+ {
+ throw new RuntimeException("Events not recieved before:" + beforeTransaction + " after:" + afterTransaction);
+ }
+ }
+}
More information about the seam-commits
mailing list