[jboss-cvs] JBossAS SVN: r106082 - in projects/ejb3/trunk/transactions/src: test/java/org/jboss/ejb3/tx/test and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jun 16 07:49:51 EDT 2010


Author: wolfc
Date: 2010-06-16 07:49:50 -0400 (Wed, 16 Jun 2010)
New Revision: 106082

Added:
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/SetRollbackTestBean.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/unit/
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/unit/SetRollbackTestCase.java
Modified:
   projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java
Log:
EJBTHREE-2115: throw IllegalStateException on setRollbackOnly in NOT_SUPPORTED

Modified: projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java
===================================================================
--- projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java	2010-06-16 09:59:49 UTC (rev 106081)
+++ projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java	2010-06-16 11:49:50 UTC (rev 106082)
@@ -21,18 +21,6 @@
  */
 package org.jboss.ejb3.tx;
 
-import java.lang.reflect.Method;
-
-import javax.ejb.ApplicationException;
-import javax.ejb.TransactionAttribute;
-import javax.ejb.TransactionAttributeType;
-import javax.ejb.TransactionManagement;
-import javax.ejb.TransactionManagementType;
-import javax.transaction.Status;
-import javax.transaction.SystemException;
-import javax.transaction.TransactionManager;
-import javax.transaction.UserTransaction;
-
 import org.jboss.aop.Advisor;
 import org.jboss.aop.joinpoint.Invocation;
 import org.jboss.aop.joinpoint.MethodInvocation;
@@ -42,6 +30,13 @@
 import org.jboss.logging.Logger;
 import org.jboss.tm.TransactionManagerLocator;
 
+import javax.ejb.*;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.TransactionManager;
+import javax.transaction.UserTransaction;
+import java.lang.reflect.Method;
+
 /**
  * Comment
  *
@@ -51,7 +46,29 @@
 public class TxUtil
 {
    private static final Logger log = Logger.getLogger(TxUtil.class);
-   
+
+   private static void check(String publicMethod, String specReferenceTxAttr)
+   {
+      // getRollbackOnly is not allowed during construction and injection EJB 3 4.4.1 and EJB 3 4.5.2
+      Invocation currentInvocation = CurrentInvocation.getCurrentInvocation();
+      if(currentInvocation == null)
+         throw new IllegalStateException("It's not allowed to do " + publicMethod + " during construction and injection");
+      Advisor advisor = currentInvocation.getAdvisor();
+      String containerName = advisor.getName();
+
+      // EJB1.1 11.6.1: Must throw IllegalStateException if BMT
+      TransactionManagementType type = TxUtil.getTransactionManagementType(advisor);
+      if (type != TransactionManagementType.CONTAINER)
+         throw new IllegalStateException("Container " + containerName + ": it is illegal to call " + publicMethod + " from BMT: " + type);
+
+      if(isLifecycleCallback(currentInvocation))
+         throw new IllegalStateException(containerName + ": " + publicMethod + " not allowed during lifecycle callbacks (EJB3 4.4.1 & 4.5.2)");
+
+      // TODO: we should really ask a TxType object to handle getRollbackOnly()
+      if(getTxType(currentInvocation) == TransactionAttributeType.SUPPORTS)
+         throw new IllegalStateException(containerName + ": " + publicMethod + " not allowed with TransactionAttributeType.SUPPORTS (" + specReferenceTxAttr + ")");
+   }
+
    // TODO: should really be protected
    public static TransactionManager getTransactionManager()
    {
@@ -73,24 +90,8 @@
 
    public static boolean getRollbackOnly()
    {
-      // getRollbackOnly is not allowed during construction and injection EJB 3 4.4.1 and EJB 3 4.5.2
-      Invocation currentInvocation = CurrentInvocation.getCurrentInvocation();
-      if(currentInvocation == null)
-         throw new IllegalStateException("It's not allowed to do getRollbackOnly() during construction and injection");
-      Advisor advisor = currentInvocation.getAdvisor();
-      
-      // EJB1.1 11.6.1: Must throw IllegalStateException if BMT
-      TransactionManagementType type = TxUtil.getTransactionManagementType(advisor);
-      if (type != TransactionManagementType.CONTAINER)
-         throw new IllegalStateException("Container " + advisor.getName() + ": it is illegal to call getRollbackOnly from BMT: " + type);
+      check("getRollbackOnly", "EJB 3.0 FR 13.6.2.9");
 
-      if(isLifecycleCallback(currentInvocation))
-         throw new IllegalStateException("getRollbackOnly() not allowed during lifecycle callbacks (EJB3 4.4.1 & 4.5.2)");
-      
-      // TODO: we should really ask a TxType object to handle getRollbackOnly()
-      if(getTxType(currentInvocation) == TransactionAttributeType.SUPPORTS)
-         throw new IllegalStateException("getRollbackOnly() not allowed with TransactionAttributeType.SUPPORTS (EJB 3 13.6.2.9)");
-      
       try
       {
          TransactionManager tm = TxUtil.getTransactionManager();
@@ -175,19 +176,8 @@
    
    public static void setRollbackOnly()
    {
-      // getRollbackOnly is not allowed during construction and injection EJB 3 4.4.1 and EJB 3 4.5.2
-      Invocation currentInvocation = CurrentInvocation.getCurrentInvocation();
-      if(currentInvocation == null)
-         throw new IllegalStateException("It's not allowed to do setRollbackOnly() during construction and injection");
-      Advisor advisor = currentInvocation.getAdvisor();
-      
-      // EJB1.1 11.6.1: Must throw IllegalStateException if BMT
-      TransactionManagementType type = TxUtil.getTransactionManagementType(advisor);
-      if (type != TransactionManagementType.CONTAINER) throw new IllegalStateException("Container " + advisor.getName() + ": it is illegal to call setRollbackOnly from BMT: " + type);
+      check("setRollbackOnly", "EJB 3.0 FR 13.6.2.8");
 
-      if(isLifecycleCallback(currentInvocation))
-         throw new IllegalStateException("setRollbackOnly() not allowed during lifecycle callbacks (EJB3 4.4.1 & 4.5.2)");
-      
       try
       {
          TransactionManager tm = TxUtil.getTransactionManager();

Copied: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/SetRollbackTestBean.java (from rev 105205, projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/getrollback/GetRollbackTestBean.java)
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/SetRollbackTestBean.java	                        (rev 0)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/SetRollbackTestBean.java	2010-06-16 11:49:50 UTC (rev 106082)
@@ -0,0 +1,90 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright (c) 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.tx.test.ejbthree2115;
+
+import org.jboss.ejb3.test.tx.common.MockEJBContext;
+
+import javax.annotation.Resource;
+import javax.ejb.*;
+
+import static javax.ejb.TransactionAttributeType.*;
+
+/**
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ */
+ at Stateful
+public class SetRollbackTestBean
+{
+   private EJBContext ctx = new MockEJBContext();
+
+   public boolean prePassivateRan = false;
+
+   @TransactionAttribute(MANDATORY)
+   public void mandatory()
+   {
+      ctx.setRollbackOnly();
+   }
+
+   @TransactionAttribute(NEVER)
+   public void never()
+   {
+      ctx.setRollbackOnly();
+   }
+
+   @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
+   public void notSupported()
+   {
+      ctx.setRollbackOnly();
+   }
+
+   @PrePassivate
+   public void prePassivate()
+   {
+      ctx.setRollbackOnly();
+
+      prePassivateRan = true;
+   }
+
+   @TransactionAttribute(REQUIRED)
+   public void required()
+   {
+      ctx.setRollbackOnly();
+   }
+
+   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+   public void requiresNew()
+   {
+      ctx.setRollbackOnly();
+   }
+
+   @Resource
+   public void setSessionContext(SessionContext ctx)
+   {
+      ctx.setRollbackOnly();
+   }
+
+   @TransactionAttribute(TransactionAttributeType.SUPPORTS)
+   public void supports()
+   {
+      ctx.setRollbackOnly();
+   }
+}
\ No newline at end of file

Copied: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/unit/SetRollbackTestCase.java (from rev 105205, projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/getrollback/unit/GetRollbackTestCase.java)
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/unit/SetRollbackTestCase.java	                        (rev 0)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/ejbthree2115/unit/SetRollbackTestCase.java	2010-06-16 11:49:50 UTC (rev 106082)
@@ -0,0 +1,273 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright (c) 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.tx.test.ejbthree2115.unit;
+
+import org.jboss.aop.Advisor;
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.joinpoint.ConstructionInvocation;
+import org.jboss.ejb3.interceptors.aop.LifecycleCallbacks;
+import org.jboss.ejb3.interceptors.container.BeanContext;
+import org.jboss.ejb3.interceptors.direct.DirectContainer;
+import org.jboss.ejb3.test.tx.common.AbstractTxTestCase;
+import org.jboss.ejb3.test.tx.common.MockSessionContext;
+import org.jboss.ejb3.tx.test.ejbthree2115.SetRollbackTestBean;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import javax.ejb.EJBTransactionRolledbackException;
+import javax.ejb.PrePassivate;
+import javax.ejb.SessionContext;
+import javax.naming.InitialContext;
+import javax.transaction.TransactionManager;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+/**
+ * EJB 3 13.6.2.8
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ */
+public class SetRollbackTestCase extends AbstractTxTestCase
+{
+   private static TestContainer<SetRollbackTestBean> container;
+   private BeanContext<SetRollbackTestBean> instance;
+
+   private static class TestContainer<T> extends DirectContainer<T>
+   {
+      public TestContainer(String name, String domainName, Class<? extends T> beanClass)
+      {
+         super(name, domainName, beanClass);
+      }
+
+      /**
+       * Emulate a callback invocation. The real thing is still in ejb3-core.
+       *
+       * @deprecated ejb3-interceptors 1.0.5 introduces a method with the same signature and similar functionality (TODO: refactor this class)
+       * @param component
+       * @param lifecycleAnnotationType
+       * @throws Throwable
+       */
+      @Deprecated
+      protected void invokeCallbackDeprecated(BeanContext<?> component, Class<? extends Annotation> lifecycleAnnotationType) throws Throwable
+      {
+         List<Class<?>> lifecycleInterceptorClasses = getInterceptorRegistry().getLifecycleInterceptorClasses();
+         Advisor advisor = getAdvisor();
+         Interceptor interceptors[] = LifecycleCallbacks.createLifecycleCallbackInterceptors(advisor, lifecycleInterceptorClasses, component, lifecycleAnnotationType);
+
+         Constructor<?> constructor = getBeanClass().getConstructor();
+         Object initargs[] = null;
+         ConstructionInvocation invocation = new ConstructionInvocation(interceptors, constructor, initargs);
+         invocation.setAdvisor(advisor);
+         invocation.setTargetObject(component.getInstance());
+         invocation.invokeNext();
+      }
+   }
+
+   private void expectException(String methodName, Class<? extends Exception> exceptionClass) throws Throwable
+   {
+      try
+      {
+         container.invoke(instance, methodName);
+         fail("Expecting an IllegalStateException on " + methodName);
+      }
+      catch(Exception e)
+      {
+         assertEquals(exceptionClass, e.getClass());
+      }
+   }
+
+   private void expectIllegalState(String methodName) throws Throwable
+   {
+      try
+      {
+         container.invoke(instance, methodName);
+         fail("Expecting an IllegalStateException on " + methodName);
+      }
+      catch(IllegalStateException e)
+      {
+         // Happy, happy, joy, joy
+      }
+   }
+
+   private void invoke(String methodName) throws Throwable
+   {
+      container.invoke(instance, methodName);
+   }
+
+   @BeforeClass
+   public static void setUpBeforeClass() throws Throwable
+   {
+      AbstractTxTestCase.beforeClass();
+
+      // TODO: should not use Stateful Container
+      container = new TestContainer<SetRollbackTestBean>("GetRollbackTest", "Stateless Container", SetRollbackTestBean.class);
+   }
+
+   @Before
+   public void setUp() throws Exception
+   {
+      instance = container.construct();
+   }
+
+   @After
+   public void tearDown() throws Exception
+   {
+      if(instance != null)
+         container.destroy(instance);
+   }
+
+   @Test
+   public void testInjection() throws Throwable
+   {
+      SessionContext ctx = new MockSessionContext();
+
+      try
+      {
+         instance.getInstance().setSessionContext(ctx);
+
+         fail("EJB3 4.5.2 getRollbackOnly during injection is not allowed");
+      }
+      catch(IllegalStateException e)
+      {
+         // Good
+      }
+   }
+
+   /**
+    * Test method for {@link SetRollbackTestBean#mandatory()}.
+    * @throws Throwable
+    */
+   @Test
+   public void testMandatory() throws Throwable
+   {
+      InitialContext ctx = new InitialContext();
+      TransactionManager tm = (TransactionManager) ctx.lookup("java:/TransactionManager");
+      tm.begin();
+      try
+      {
+         invoke("mandatory");
+      }
+      finally
+      {
+         tm.rollback();
+      }
+   }
+
+   /**
+    * Test method for {@link SetRollbackTestBean#never()}.
+    * @throws Throwable
+    */
+   @Test
+   public void testNever() throws Throwable
+   {
+      expectIllegalState("never");
+   }
+
+   /**
+    * Test method for {@link SetRollbackTestBean#notSupported()}.
+    * @throws Throwable
+    */
+   @Test
+   public void testNotSupported() throws Throwable
+   {
+      expectIllegalState("notSupported");
+   }
+
+   /**
+    * This test is to make sure there is no ClassCastException in TxUtil.getTxType.
+    * It's actually not allowed by spec to do PrePassivate on a Stateless and it's
+    * also not allowed to do getRollbackOnly within a lifecycle method.
+    */
+   @Test
+   public void testPrePassivate() throws Throwable
+   {
+      InitialContext ctx = new InitialContext();
+      TransactionManager tm = (TransactionManager) ctx.lookup("java:/TransactionManager");
+      tm.begin();
+      try
+      {
+         container.invokeCallbackDeprecated(instance, PrePassivate.class);
+
+         assertTrue(instance.getInstance().prePassivateRan);
+      }
+      catch(IllegalStateException e)
+      {
+         // The original intent of the test is document above, but
+         // since we're now following spec you get an IllegalStateException
+      }
+      finally
+      {
+         tm.rollback();
+      }
+   }
+
+   /**
+    * Test method for {@link SetRollbackTestBean#required()}.
+    * @throws Throwable
+    */
+   @Test
+   public void testRequired() throws Throwable
+   {
+      invoke("required");
+   }
+
+   /**
+    * Test method for {@link SetRollbackTestBean#requiresNew()}.
+    * @throws Throwable
+    */
+   @Test
+   public void testRequiresNew() throws Throwable
+   {
+      invoke("requiresNew");
+   }
+
+   /**
+    * Test method for {@link SetRollbackTestBean#supports()}.
+    * @throws Throwable
+    */
+   @Test
+   public void testSupports() throws Throwable
+   {
+      expectIllegalState("supports");
+   }
+
+   @Test
+   public void testSupportsWithTransaction() throws Throwable
+   {
+      InitialContext ctx = new InitialContext();
+      TransactionManager tm = (TransactionManager) ctx.lookup("java:/TransactionManager");
+      tm.begin();
+      try
+      {
+         expectException("supports", EJBTransactionRolledbackException.class);
+      }
+      finally
+      {
+         tm.rollback();
+      }
+   }
+}
\ No newline at end of file



More information about the jboss-cvs-commits mailing list