[jboss-cvs] JBossAS SVN: r84801 - in projects/ejb3/trunk/core/src: test/java/org/jboss/ejb3/core/test and 5 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Feb 26 08:12:42 EST 2009
Author: wolfc
Date: 2009-02-26 08:12:41 -0500 (Thu, 26 Feb 2009)
New Revision: 84801
Added:
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransaction.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransactionManager.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransactionService.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/TxSync.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/TxSyncBean.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/unit/
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/unit/TxSyncTestCase.java
projects/ejb3/trunk/core/src/test/resources/controlled-transactionmanager-beans.xml
Modified:
projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/SessionSynchronizationInterceptor.java
projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulContainer.java
Log:
EJBTHREE-1745: setting enc before doing SessionSynchronization callbacks
Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/SessionSynchronizationInterceptor.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/SessionSynchronizationInterceptor.java 2009-02-26 12:01:20 UTC (rev 84800)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/SessionSynchronizationInterceptor.java 2009-02-26 13:12:41 UTC (rev 84801)
@@ -35,6 +35,7 @@
import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.Invocation;
import org.jboss.ejb.AllowedOperationsAssociation;
+import org.jboss.ejb3.BeanContext;
import org.jboss.ejb3.tx.TxUtil;
import org.jboss.logging.Logger;
@@ -72,6 +73,10 @@
public void beforeCompletion()
{
SessionSynchronization bean = (SessionSynchronization) ctx.getInstance();
+ // The bean might be lost in action if an exception is thrown in afterBegin
+ if(bean == null)
+ return;
+ pushEnc();
try
{
// FIXME: This is a dirty hack to notify AS EJBTimerService about what's going on
@@ -86,6 +91,7 @@
finally
{
AllowedOperationsAssociation.popInMethodFlag();
+ popEnc();
}
}
@@ -93,6 +99,10 @@
{
ctx.setTxSynchronized(false);
SessionSynchronization bean = (SessionSynchronization) ctx.getInstance();
+ // The bean might be lost in action if an exception is thrown in afterBegin
+ if(bean == null)
+ return;
+ pushEnc();
try
{
if (status == Status.STATUS_COMMITTED)
@@ -109,10 +119,26 @@
}
finally
{
+ popEnc();
StatefulContainer container = (StatefulContainer) ctx.getContainer();
container.getCache().release(ctx);
}
}
+
+ private void popEnc()
+ {
+ StatefulContainer container = ctx.getContainer();
+ BeanContext<?> old = container.popContext();
+ assert old == ctx;
+ container.popEnc();
+ }
+
+ private void pushEnc()
+ {
+ StatefulContainer container = ctx.getContainer();
+ container.pushEnc();
+ container.pushContext(ctx);
+ }
}
protected void registerSessionSynchronization(StatefulBeanContext ctx) throws RemoteException, SystemException
Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulContainer.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulContainer.java 2009-02-26 12:01:20 UTC (rev 84800)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulContainer.java 2009-02-26 13:12:41 UTC (rev 84801)
@@ -1334,6 +1334,16 @@
// throw new IllegalStateException("Unable to create proxy for getBusinessObject as a proxy factory was not found");
}
+ protected void popEnc()
+ {
+ super.popEnc();
+ }
+
+ protected void pushEnc()
+ {
+ super.pushEnc();
+ }
+
/**
* Remove the given object. Called when remove on Home is invoked.
*
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransaction.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransaction.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransaction.java 2009-02-26 13:12:41 UTC (rev 84801)
@@ -0,0 +1,162 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.common.tx;
+
+import java.util.LinkedList;
+
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.xa.XAResource;
+
+import org.jboss.logging.Logger;
+
+/**
+ * We keep track of the synchronizations ourself.
+ *
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class ControlledTransaction implements Transaction
+{
+ protected class ControlledSynchronization implements Synchronization
+ {
+ public void afterCompletion(int status)
+ {
+ ControlledTransaction.this.afterCompletion(status);
+ }
+
+ public void beforeCompletion()
+ {
+ ControlledTransaction.this.beforeCompletion();
+ }
+ }
+
+ private static final Logger log = Logger.getLogger(ControlledTransaction.class);
+
+ private ControlledTransactionManager tm;
+ private Transaction delegate;
+ private ControlledSynchronization sync = new ControlledSynchronization();
+ private LinkedList<Synchronization> syncs = new LinkedList<Synchronization>();
+
+ private boolean reverseSyncRegistration = false;
+
+ protected ControlledTransaction(ControlledTransactionManager tm, Transaction tx) throws SystemException, IllegalStateException
+ {
+ this.tm = tm;
+ this.delegate = tx;
+ try
+ {
+ if(tx.getStatus() == Status.STATUS_ACTIVE)
+ tx.registerSynchronization(sync);
+ }
+ catch (RollbackException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected void afterCompletion(int status)
+ {
+ for(Synchronization sync : syncs)
+ {
+ try
+ {
+ sync.afterCompletion(status);
+ }
+ catch(Exception e)
+ {
+ log.error("afterCompletion", e);
+ tm.report(e);
+ }
+ }
+ }
+
+ protected void beforeCompletion()
+ {
+ for(Synchronization sync : syncs)
+ {
+ try
+ {
+ sync.beforeCompletion();
+ }
+ catch(Exception e)
+ {
+ log.error("beforeCompletion", e);
+ tm.report(e);
+ }
+ }
+ }
+
+ public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
+ SecurityException, IllegalStateException, SystemException
+ {
+ delegate.commit();
+ tm.flush(this);
+ }
+
+ public boolean delistResource(XAResource resource, int flag) throws IllegalStateException, SystemException
+ {
+ return delegate.delistResource(resource, flag);
+ }
+
+ public boolean enlistResource(XAResource resource) throws RollbackException, IllegalStateException, SystemException
+ {
+ return delegate.enlistResource(resource);
+ }
+
+ public int getStatus() throws SystemException
+ {
+ return delegate.getStatus();
+ }
+
+ public void registerSynchronization(Synchronization sync) throws RollbackException, IllegalStateException,
+ SystemException
+ {
+ //delegate.registerSynchronization(sync);
+ if(reverseSyncRegistration)
+ syncs.addFirst(sync);
+ else
+ syncs.addLast(sync);
+ }
+
+ public void rollback() throws IllegalStateException, SystemException
+ {
+ delegate.rollback();
+ tm.flush(this);
+ }
+
+ public void setReverseSyncRegistration(boolean flag)
+ {
+ this.reverseSyncRegistration = flag;
+ }
+
+ public void setRollbackOnly() throws IllegalStateException, SystemException
+ {
+ delegate.setRollbackOnly();
+ }
+
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransactionManager.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransactionManager.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransactionManager.java 2009-02-26 13:12:41 UTC (rev 84801)
@@ -0,0 +1,140 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.common.tx;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.InvalidTransactionException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+
+/**
+ * A transaction manager of which we have a better control.
+ *
+ * Note that we delegate to a true transaction manager for ease.
+ *
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class ControlledTransactionManager implements TransactionManager
+{
+ private TransactionManager delegate;
+ private ThreadLocal<ControlledTransaction> currentTx = new ThreadLocal<ControlledTransaction>();
+ private List<Exception> reports = new ArrayList<Exception>();
+
+ protected ControlledTransactionManager(TransactionManager delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ public void begin() throws NotSupportedException, SystemException
+ {
+ delegate.begin();
+ }
+
+ public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
+ SecurityException, IllegalStateException, SystemException
+ {
+ currentTx.remove();
+ delegate.commit();
+ }
+
+ protected void flush(ControlledTransaction tx)
+ {
+ if(currentTx.get().equals(tx))
+ currentTx.remove();
+ }
+
+ public void flushReports()
+ {
+ reports.clear();
+ }
+
+ public ControlledTransaction getControlledTransaction() throws SystemException
+ {
+ ControlledTransaction tx = currentTx.get();
+ if(tx == null)
+ {
+ Transaction delegateTx = delegate.getTransaction();
+ if(delegateTx != null)
+ {
+ tx = new ControlledTransaction(this, delegateTx);
+ currentTx.set(tx);
+ }
+ }
+ return tx;
+ }
+
+ public List<Exception> getReports()
+ {
+ return Collections.unmodifiableList(reports);
+ }
+
+ public int getStatus() throws SystemException
+ {
+ return delegate.getStatus();
+ }
+
+ public Transaction getTransaction() throws SystemException
+ {
+ return getControlledTransaction();
+ }
+
+ protected void report(Exception e)
+ {
+ reports.add(e);
+ }
+
+ public void resume(Transaction tx) throws InvalidTransactionException, IllegalStateException, SystemException
+ {
+ throw new RuntimeException("NYI");
+ }
+
+ public void rollback() throws IllegalStateException, SecurityException, SystemException
+ {
+ currentTx.remove();
+ delegate.rollback();
+ }
+
+ public void setRollbackOnly() throws IllegalStateException, SystemException
+ {
+ throw new RuntimeException("NYI");
+ }
+
+ public void setTransactionTimeout(int timeout) throws SystemException
+ {
+ throw new RuntimeException("NYI");
+ }
+
+ public Transaction suspend() throws SystemException
+ {
+ throw new RuntimeException("NYI");
+ }
+
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransactionService.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransactionService.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/tx/ControlledTransactionService.java 2009-02-26 13:12:41 UTC (rev 84801)
@@ -0,0 +1,50 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.common.tx;
+
+import javax.naming.InitialContext;
+import javax.transaction.TransactionManager;
+
+import org.jboss.util.naming.NonSerializableFactory;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class ControlledTransactionService
+{
+ private static final String TM_JNDI_NAME = "java:/TransactionManager";
+
+ private TransactionManager delegate;
+
+ public void setTransactionManager(TransactionManager delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ public void start() throws Exception
+ {
+ InitialContext ctx = new InitialContext();
+ // hack in the controlled tm
+ NonSerializableFactory.rebind(ctx, TM_JNDI_NAME, new ControlledTransactionManager(delegate));
+ }
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/TxSync.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/TxSync.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/TxSync.java 2009-02-26 13:12:41 UTC (rev 84801)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.txsync;
+
+import javax.ejb.Local;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Local
+public interface TxSync
+{
+ void remove();
+
+ String sayHi(String name);
+
+ void setThrowInAfterCompletion(boolean flag);
+
+ void setThrowInBeforeCompletion(boolean flag);
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/TxSyncBean.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/TxSyncBean.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/TxSyncBean.java 2009-02-26 13:12:41 UTC (rev 84801)
@@ -0,0 +1,128 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.txsync;
+
+import java.rmi.RemoteException;
+
+import javax.annotation.Resource;
+import javax.ejb.EJBException;
+import javax.ejb.Remove;
+import javax.ejb.SessionContext;
+import javax.ejb.SessionSynchronization;
+import javax.ejb.Stateful;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.logging.Logger;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Stateful
+ at TransactionAttribute(TransactionAttributeType.MANDATORY)
+public class TxSyncBean implements TxSync, SessionSynchronization
+{
+ private static final Logger log = Logger.getLogger(TxSyncBean.class);
+
+ public static int afterBeginCalls = 0;
+ public static int afterCompletionCalls = 0;
+ public static int beforeCompletionCalls = 0;
+
+ @Resource(name="test")
+ private SessionContext ctx;
+
+ private static boolean throwInAfterBegin = false;
+ private boolean throwInAfterCompletion = false;
+ private boolean throwInBeforeCompletion = false;
+
+ public void afterBegin() throws EJBException, RemoteException
+ {
+ log.info("afterBegin");
+ afterBeginCalls ++;
+ checkSessionContext();
+ if(throwInAfterBegin)
+ throw new RuntimeException("afterBegin");
+ }
+
+ public void afterCompletion(boolean committed) throws EJBException, RemoteException
+ {
+ log.info("afterCompletion(" + committed + ")");
+ afterCompletionCalls++;
+ checkSessionContext();
+ if(throwInAfterCompletion)
+ throw new RuntimeException("afterCompletion");
+ }
+
+ public void beforeCompletion() throws EJBException, RemoteException
+ {
+ log.info("beforeCompletion");
+ beforeCompletionCalls++;
+ checkSessionContext();
+ if(throwInBeforeCompletion)
+ throw new RuntimeException("beforeCompletion");
+ }
+
+ private void checkSessionContext()
+ {
+ try
+ {
+ SessionContext other = (SessionContext) new InitialContext().lookup("java:comp/env/test");
+ if(!other.equals(ctx))
+ throw new RuntimeException("found wrong SessionContext at java:comp/env/test " + other + " != " + ctx);
+ }
+ catch(NamingException e)
+ {
+ log.error("failed to lookup java:comp/env/test", e);
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Remove
+ public void remove()
+ {
+
+ }
+
+ public String sayHi(String name)
+ {
+ checkSessionContext();
+ return "Hi " + name;
+ }
+
+ public static void setThrowInAfterBegin(boolean flag)
+ {
+ throwInAfterBegin = flag;
+ }
+
+ public void setThrowInAfterCompletion(boolean flag)
+ {
+ this.throwInAfterCompletion = flag;
+ }
+
+ public void setThrowInBeforeCompletion(boolean flag)
+ {
+ this.throwInBeforeCompletion = flag;
+ }
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/unit/TxSyncTestCase.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/unit/TxSyncTestCase.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/txsync/unit/TxSyncTestCase.java 2009-02-26 13:12:41 UTC (rev 84801)
@@ -0,0 +1,296 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.txsync.unit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.Collection;
+
+import javax.ejb.EJBTransactionRolledbackException;
+import javax.ejb.NoSuchEJBException;
+import javax.naming.NamingException;
+import javax.transaction.RollbackException;
+
+import org.jboss.ejb3.core.test.common.AbstractEJB3TestCase;
+import org.jboss.ejb3.core.test.common.tx.ControlledTransactionManager;
+import org.jboss.ejb3.core.test.txsync.TxSync;
+import org.jboss.ejb3.core.test.txsync.TxSyncBean;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class TxSyncTestCase extends AbstractEJB3TestCase
+{
+ private ControlledTransactionManager tm = getTransactionManager();
+
+ private static void assertEmpty(Collection<?> collection)
+ {
+ if(!collection.isEmpty())
+ fail("collection was not empty, but " + collection);
+ }
+
+ @Before
+ public void before()
+ {
+ TxSyncBean.afterBeginCalls = 0;
+ TxSyncBean.afterCompletionCalls = 0;
+ TxSyncBean.beforeCompletionCalls = 0;
+ TxSyncBean.setThrowInAfterBegin(false);
+ tm.flushReports();
+ }
+
+ @BeforeClass
+ public static void beforeClass() throws Exception
+ {
+ AbstractEJB3TestCase.beforeClass();
+
+ deploy("controlled-transactionmanager-beans.xml");
+
+ AbstractEJB3TestCase.deploySessionEjb(TxSyncBean.class);
+ }
+
+ protected static ControlledTransactionManager getTransactionManager()
+ {
+ try
+ {
+ return lookup("java:/TransactionManager", ControlledTransactionManager.class);
+ }
+ catch (NamingException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ public void testAfterBegin() throws Exception
+ {
+ tm.begin();
+ try
+ {
+ TxSyncBean.setThrowInAfterBegin(true);
+ TxSync bean = lookup("TxSyncBean/local", TxSync.class);
+ try
+ {
+ bean.sayHi("me");
+ fail("Should have thrown EJBTransactionRolledbackException");
+ }
+ catch(EJBTransactionRolledbackException e)
+ {
+ assertEquals("afterBegin", e.getCause().getMessage());
+ }
+ }
+ finally
+ {
+ tm.rollback();
+ }
+ assertEquals(1, TxSyncBean.afterBeginCalls);
+ assertEquals(0, TxSyncBean.beforeCompletionCalls);
+ assertEquals(0, TxSyncBean.afterCompletionCalls);
+ assertEmpty(tm.getReports());
+ }
+
+ @Test
+ public void testAfterBeginCommit() throws Exception
+ {
+ tm.begin();
+ try
+ {
+ TxSyncBean.setThrowInAfterBegin(true);
+ TxSync bean = lookup("TxSyncBean/local", TxSync.class);
+ try
+ {
+ bean.sayHi("me");
+ fail("Should have thrown EJBTransactionRolledbackException");
+ }
+ catch(EJBTransactionRolledbackException e)
+ {
+ assertEquals("afterBegin", e.getCause().getMessage());
+ }
+ }
+ finally
+ {
+ // force commit
+ try
+ {
+ tm.commit();
+ fail("Expected RollbackException");
+ }
+ catch(RollbackException e)
+ {
+ // good
+ }
+ }
+ assertEquals(1, TxSyncBean.afterBeginCalls);
+ assertEquals(0, TxSyncBean.beforeCompletionCalls);
+ assertEquals(0, TxSyncBean.afterCompletionCalls);
+ assertEmpty(tm.getReports());
+ }
+
+ @Test
+ public void testAfterCompletion() throws Exception
+ {
+ tm.begin();
+ try
+ {
+ TxSync bean = lookup("TxSyncBean/local", TxSync.class);
+ bean.setThrowInAfterCompletion(true);
+ bean.sayHi("me");
+ }
+ finally
+ {
+ tm.rollback();
+ }
+ assertEquals(1, TxSyncBean.afterBeginCalls);
+ // we rollback, so no beforeCompletion
+ assertEquals(0, TxSyncBean.beforeCompletionCalls);
+ assertEquals(1, TxSyncBean.afterCompletionCalls);
+ assertEquals(1, tm.getReports().size());
+ Exception report = tm.getReports().get(0);
+ assertEquals("afterCompletion", report.getMessage());
+ }
+
+ @Test
+ public void testAfterCompletionCommit() throws Exception
+ {
+ tm.begin();
+ try
+ {
+ TxSync bean = lookup("TxSyncBean/local", TxSync.class);
+ bean.setThrowInAfterCompletion(true);
+ bean.sayHi("me");
+ }
+ finally
+ {
+ tm.commit();
+ }
+ assertEquals(1, TxSyncBean.afterBeginCalls);
+ assertEquals(1, TxSyncBean.beforeCompletionCalls);
+ assertEquals(1, TxSyncBean.afterCompletionCalls);
+ assertEquals(1, tm.getReports().size());
+ Exception report = tm.getReports().get(0);
+ assertEquals("afterCompletion", report.getMessage());
+ }
+
+ @Test
+ public void testBeforeCompletion() throws Exception
+ {
+ tm.begin();
+ try
+ {
+ TxSync bean = lookup("TxSyncBean/local", TxSync.class);
+ bean.setThrowInBeforeCompletion(true);
+ bean.sayHi("me");
+ }
+ finally
+ {
+ tm.commit();
+ }
+ assertEquals(1, TxSyncBean.afterBeginCalls);
+ assertEquals(1, TxSyncBean.beforeCompletionCalls);
+ // ignore any discrepancies here, because ControlledTM isn't compliant
+ //assertEquals(0, TxSyncBean.afterCompletionCalls);
+ assertEquals(1, tm.getReports().size());
+ Exception report = tm.getReports().get(0);
+ assertEquals("beforeCompletion", report.getMessage());
+ }
+
+ @Test
+ public void testRemove() throws Exception
+ {
+ TxSync bean;
+ tm.begin();
+ try
+ {
+ bean = lookup("TxSyncBean/local", TxSync.class);
+ bean.remove();
+ }
+ finally
+ {
+ tm.commit();
+ }
+ tm.begin();
+ try
+ {
+ try
+ {
+ bean.sayHi("me");
+ fail("Should have throw NoSuchEJBException");
+ }
+ catch(NoSuchEJBException e)
+ {
+ // good
+ }
+ }
+ finally
+ {
+ tm.rollback();
+ }
+ assertEquals(1, TxSyncBean.afterBeginCalls);
+ assertEquals(1, TxSyncBean.beforeCompletionCalls);
+ assertEquals(1, TxSyncBean.afterCompletionCalls);
+ assertEmpty(tm.getReports());
+ }
+
+ // Don't test this yet, because it's another issue
+ //@Test
+ public void testReverseSyncRemove() throws Exception
+ {
+ TxSync bean;
+ tm.begin();
+ tm.getControlledTransaction().setReverseSyncRegistration(true);
+ try
+ {
+ bean = lookup("TxSyncBean/local", TxSync.class);
+ bean.remove();
+ }
+ finally
+ {
+ tm.commit();
+ }
+ tm.begin();
+ try
+ {
+ try
+ {
+ bean.sayHi("me");
+ fail("Should have throw NoSuchEJBException");
+ }
+ catch(NoSuchEJBException e)
+ {
+ // good
+ }
+ }
+ finally
+ {
+ tm.rollback();
+ }
+ assertEquals(1, TxSyncBean.afterBeginCalls);
+ assertEquals(1, TxSyncBean.beforeCompletionCalls);
+ assertEquals(1, TxSyncBean.afterCompletionCalls);
+ assertEmpty(tm.getReports());
+ }
+}
Added: projects/ejb3/trunk/core/src/test/resources/controlled-transactionmanager-beans.xml
===================================================================
--- projects/ejb3/trunk/core/src/test/resources/controlled-transactionmanager-beans.xml (rev 0)
+++ projects/ejb3/trunk/core/src/test/resources/controlled-transactionmanager-beans.xml 2009-02-26 13:12:41 UTC (rev 84801)
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Do not deploy, unless you know what're doing -->
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+ <bean name="ControlledTransactionManager" class="org.jboss.ejb3.core.test.common.tx.ControlledTransactionService">
+ <property name="transactionManager"><inject bean="RealTransactionManager"/></property>
+ </bean>
+</deployment>
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list