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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jul 30 10:04:41 EDT 2008


Author: wolfc
Date: 2008-07-30 10:04:41 -0400 (Wed, 30 Jul 2008)
New Revision: 76471

Added:
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/MockSessionContext.java
Modified:
   projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java
   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/test/tx/getrollback/unit/GetRollbackTestCase.java
Log:
EJBTHREE-1444: fixed NPEs

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	2008-07-30 13:54:57 UTC (rev 76470)
+++ projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java	2008-07-30 14:04:41 UTC (rev 76471)
@@ -72,8 +72,12 @@
 
    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)
@@ -155,7 +159,12 @@
    
    public static void setRollbackOnly()
    {
-      Advisor advisor = CurrentInvocation.getCurrentInvocation().getAdvisor();
+      // 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);

Added: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/MockSessionContext.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/MockSessionContext.java	                        (rev 0)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/MockSessionContext.java	2008-07-30 14:04:41 UTC (rev 76471)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.test.tx.common;
+
+import javax.ejb.EJBLocalObject;
+import javax.ejb.EJBObject;
+import javax.ejb.SessionContext;
+import javax.xml.rpc.handler.MessageContext;
+
+/**
+ * A mock session context which doesn't do anything.
+ * 
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class MockSessionContext extends MockEJBContext implements SessionContext
+{
+   public <T> T getBusinessObject(Class<T> businessInterface) throws IllegalStateException
+   {
+      throw new IllegalStateException("N/A");
+   }
+
+   public EJBLocalObject getEJBLocalObject() throws IllegalStateException
+   {
+      throw new IllegalStateException("N/A");
+   }
+
+   public EJBObject getEJBObject() throws IllegalStateException
+   {
+      throw new IllegalStateException("N/A");
+   }
+
+   public Class<?> getInvokedBusinessInterface() throws IllegalStateException
+   {
+      throw new IllegalStateException("N/A");
+   }
+
+   public MessageContext getMessageContext() throws IllegalStateException
+   {
+      throw new IllegalStateException("N/A");
+   }
+}

Modified: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java	2008-07-30 13:54:57 UTC (rev 76470)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java	2008-07-30 14:04:41 UTC (rev 76471)
@@ -205,7 +205,7 @@
       }
    }
    
-   public StatefulContainer(String name, String domainName, Class<T> beanClass)
+   public StatefulContainer(String name, String domainName, Class<? extends T> beanClass)
    {
       super(name, domainName, beanClass);
       setBeanContextFactory(beanContextFactory);

Modified: 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/test/tx/getrollback/GetRollbackTestBean.java	2008-07-30 13:54:57 UTC (rev 76470)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/getrollback/GetRollbackTestBean.java	2008-07-30 14:04:41 UTC (rev 76471)
@@ -25,9 +25,11 @@
 import static javax.ejb.TransactionAttributeType.NEVER;
 import static javax.ejb.TransactionAttributeType.REQUIRED;
 
+import javax.annotation.Resource;
 import javax.ejb.EJBContext;
 import javax.ejb.PrePassivate;
-import javax.ejb.Stateless;
+import javax.ejb.SessionContext;
+import javax.ejb.Stateful;
 import javax.ejb.TransactionAttribute;
 import javax.ejb.TransactionAttributeType;
 
@@ -37,7 +39,7 @@
  * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
  * @version $Revision: $
  */
- at Stateless
+ at Stateful
 public class GetRollbackTestBean
 {
    private EJBContext ctx = new MockEJBContext();
@@ -82,6 +84,12 @@
       return ctx.getRollbackOnly();
    }
    
+   @Resource
+   public void setSessionContext(SessionContext ctx)
+   {
+      ctx.getRollbackOnly();
+   }
+   
    @TransactionAttribute(TransactionAttributeType.SUPPORTS)
    public boolean supports()
    {

Modified: 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/test/tx/getrollback/unit/GetRollbackTestCase.java	2008-07-30 13:54:57 UTC (rev 76470)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/getrollback/unit/GetRollbackTestCase.java	2008-07-30 14:04:41 UTC (rev 76471)
@@ -33,6 +33,7 @@
 
 import javax.ejb.EJBTransactionRolledbackException;
 import javax.ejb.PrePassivate;
+import javax.ejb.SessionContext;
 import javax.naming.InitialContext;
 import javax.transaction.TransactionManager;
 
@@ -42,6 +43,7 @@
 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.MockSessionContext;
 import org.jboss.ejb3.test.tx.getrollback.GetRollbackTestBean;
 import org.jboss.ejb3.test.tx.mc.UnitTestBootstrap;
 import org.junit.After;
@@ -70,6 +72,13 @@
          super(name, domainName, beanClass);
       }
       
+      /**
+       * Emulate a callback invocation. The real thing is still in ejb3-core.
+       * 
+       * @param component
+       * @param lifecycleAnnotationType
+       * @throws Throwable
+       */
       protected void invokeCallback(BeanContext<?> component, Class<? extends Annotation> lifecycleAnnotationType) throws Throwable
       {
          List<Class<?>> lifecycleInterceptorClasses = getInterceptorRegistry().getLifecycleInterceptorClasses();
@@ -152,6 +161,22 @@
          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 org.jboss.ejb3.test.tx.getrollback.GetRollbackTestBean#mandatory()}.




More information about the jboss-cvs-commits mailing list