You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2010/03/28 10:22:10 UTC

svn commit: r928358 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/util/ test/java/org/apache/webbeans/newtests/injection/noncontextual/

Author: struberg
Date: Sun Mar 28 08:22:09 2010
New Revision: 928358

URL: http://svn.apache.org/viewvc?rev=928358&view=rev
Log:
OWB-339 Injecting Non-Contextual Beans Causes NPE in WebBeansUtil

thanks to James Carman!

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/ContextualBean.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/InjectNonContextualTest.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/NonContextualBean.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=928358&r1=928357&r2=928358&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java Sun Mar 28 08:22:09 2010
@@ -2640,16 +2640,15 @@ public final class WebBeansUtil
             throw new IllegalArgumentException("Creational context is null");
         }
         
-        if(cc.getBean().equals(bean))
+        final Contextual<?> existing = cc.getBean();
+        if(existing != null && existing.equals(bean))
         {
-            return cc.getProxyInstance();   
-        }            
-        else
+            return cc.getProxyInstance();
+        }
+
+        if(cc.getOwnerCreational() != null)
         {
-            if(cc.getOwnerCreational() != null)
-            {
-                return getObjectFromCreationalContext(bean, cc.getOwnerCreational());   
-            }
+            return getObjectFromCreationalContext(bean, cc.getOwnerCreational());
         }
         
         return null;

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/ContextualBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/ContextualBean.java?rev=928358&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/ContextualBean.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/ContextualBean.java Sun Mar 28 08:22:09 2010
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.newtests.injection.noncontextual;
+
+import javax.enterprise.context.ApplicationScoped;
+
+@ApplicationScoped
+public class ContextualBean
+{
+}

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/InjectNonContextualTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/InjectNonContextualTest.java?rev=928358&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/InjectNonContextualTest.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/InjectNonContextualTest.java Sun Mar 28 08:22:09 2010
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.newtests.injection.noncontextual;
+
+import junit.framework.Assert;
+import org.apache.webbeans.container.BeanManagerImpl;
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.junit.Test;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionTarget;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+
+public class InjectNonContextualTest extends AbstractUnitTest
+{
+    @SuppressWarnings("unchecked")
+    public <T> void inject(T instance)
+    {
+        BeanManager mgr = BeanManagerImpl.getManager();
+        AnnotatedType<T> annotatedType = mgr.createAnnotatedType((Class<T>) instance.getClass());
+        InjectionTarget<T> injectionTarget = mgr.createInjectionTarget(annotatedType);
+        CreationalContext<T> context = mgr.createCreationalContext(null);
+        injectionTarget.inject(instance, context);
+    }
+
+
+    @Test
+    public void testInjectingNonContextualBean()
+    {
+        Collection<URL> beanXmls = new ArrayList<URL>();
+
+        Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
+        beanClasses.add(ContextualBean.class);
+        startContainer(beanClasses, beanXmls);
+        try
+        {
+            final NonContextualBean bean = new NonContextualBean();
+            inject(bean);
+            Assert.assertNotNull(bean.getContextual());
+        }
+        finally
+        {
+            shutDownContainer();
+        }
+    }
+
+}

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/NonContextualBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/NonContextualBean.java?rev=928358&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/NonContextualBean.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/noncontextual/NonContextualBean.java Sun Mar 28 08:22:09 2010
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.newtests.injection.noncontextual;
+
+import javax.inject.Inject;
+
+public class NonContextualBean
+{
+//**********************************************************************************************************************
+// Fields
+//**********************************************************************************************************************
+
+    @Inject private ContextualBean contextual;
+
+//**********************************************************************************************************************
+// Getter/Setter Methods
+//**********************************************************************************************************************
+
+    public ContextualBean getContextual()
+    {
+        return contextual;
+    }
+
+    public void setContextual(ContextualBean contextual)
+    {
+        this.contextual = contextual;
+    }
+}