openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1299063 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/util/ test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/ test/java/org/apache/webbeans/newtests/portable/ test/java/org/apache/webbeans/newt...
Date Fri, 09 Mar 2012 21:36:59 GMT
Author: struberg
Date: Fri Mar  9 21:36:59 2012
New Revision: 1299063

URL: http://svn.apache.org/viewvc?rev=1299063&view=rev
Log:
OWB-643 allow dynamic adding of @Alternative

Please note that this is not fully usable in CDI-1.0 because
there is currently no way to also set alternative in the beans.xml
dynamically. This will only get added in CDI-1.1 via the 
ProcessModule event.

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/Egg.java
      - copied, changed from r1296585, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/beans/Apple.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/HalfEgg.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/extensions/AlternativeExtension.java
      - copied, changed from r1296585, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/extensions/AppleExtension.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative2Test.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative3Test.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/PortableEventTest.java
    openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeTest5.xml

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=1299063&r1=1299062&r2=1299063&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
Fri Mar  9 21:36:59 2012
@@ -2296,14 +2296,22 @@ public final class WebBeansUtil
     public void setInjectionTargetBeanEnableFlag(InjectionTargetBean<?> bean)
     {
         Asserts.assertNotNull(bean, "bean can not be null");
+        
+        boolean isAlternative = hasInjectionTargetBeanAnnotatedWithAlternative(bean); 
 
-        if(hasInjectionTargetBeanAnnotatedWithAlternative(bean))
+        if(!isAlternative)
         {
-            if(!webBeansContext.getAlternativesManager().isBeanHasAlternative(bean))
+            AnnotatedType at = bean.getAnnotatedType();
+            if (at != null)
             {
-                bean.setEnabled(false);
+                isAlternative =  at.getAnnotation(Alternative.class) != null;
             }
         }
+        
+        if(isAlternative && !webBeansContext.getAlternativesManager().isBeanHasAlternative(bean))
+        {
+            bean.setEnabled(false);
+        }
     }
 
 

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative2Test.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative2Test.java?rev=1299063&r1=1299062&r2=1299063&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative2Test.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative2Test.java
Fri Mar  9 21:36:59 2012
@@ -37,7 +37,7 @@ import org.apache.webbeans.newtests.conc
 import org.junit.Test;
 
 public class Alternative2Test  extends AbstractUnitTest {
-	
+
     @Test
     @SuppressWarnings("unchecked")
     public void testDisposerMethodInAlternativeBean()
@@ -65,8 +65,6 @@ public class Alternative2Test  extends A
         shutDownContainer();
         
         Assert.assertTrue(Boolean.TRUE);
-        
-    	
     }
 
 }

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative3Test.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative3Test.java?rev=1299063&r1=1299062&r2=1299063&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative3Test.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/Alternative3Test.java
Fri Mar  9 21:36:59 2012
@@ -37,7 +37,7 @@ import org.apache.webbeans.newtests.conc
 import org.junit.Test;
 
 public class Alternative3Test  extends AbstractUnitTest {
-	
+
     @Test
     @SuppressWarnings("unchecked")
     public void testDisposerMethodInAlternativeBean()

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.java?rev=1299063&r1=1299062&r2=1299063&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.java
Fri Mar  9 21:36:59 2012
@@ -20,6 +20,7 @@ package org.apache.webbeans.newtests.por
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Set;
 
 import javax.enterprise.inject.spi.Bean;
 
@@ -29,6 +30,9 @@ import org.apache.webbeans.annotation.De
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.exception.inject.DefinitionException;
 import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.apache.webbeans.newtests.portable.alternative.Egg;
+import org.apache.webbeans.newtests.portable.alternative.HalfEgg;
+import org.apache.webbeans.newtests.portable.events.extensions.AlternativeExtension;
 import org.apache.webbeans.newtests.portable.scopeextension.ExternalTestScopeExtension;
 import org.apache.webbeans.newtests.portable.scopeextension.ExternalTestScoped;
 import org.apache.webbeans.newtests.portable.scopeextension.ExternalTestScopedBean;
@@ -103,4 +107,32 @@ public class ExtensionTest extends Abstr
         }
     }
 
+    /**
+     * This will test if an &#0064;Alternative can get added dynamically.
+     * Please note that we cannot do much more than checking if the
+     * dynamically enabled &#0064;Alternative is now disabled.
+     * That's because with CDI-1.0 it's not possible to also add
+     * the Alternative to beans.xml nor programmatically.
+     * This feature only gets added in CDI-1.1 with
+     * ProcessModule#getAlternatives() which can be modified.
+     *
+     * TODO: extend test for CDI-1.1
+     */
+    @Test
+    public void testAlternativeExtenson()
+    {
+        Collection<Class<?>> classes = new ArrayList<Class<?>>();
+        classes.add(Egg.class);
+        classes.add(HalfEgg.class);
+        addExtension(new AlternativeExtension());
+        startContainer(classes);
+
+        Egg egg = getInstance(Egg.class);
+        Assert.assertTrue(egg instanceof Egg);
+        Set<Bean<?>> beans = getBeanManager().getBeans(HalfEgg.class);
+        Assert.assertTrue(beans == null || beans.size() == 0);
+
+        shutDownContainer();
+    }
+
 }

Copied: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/Egg.java
(from r1296585, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/beans/Apple.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/Egg.java?p2=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/Egg.java&p1=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/beans/Apple.java&r1=1296585&r2=1299063&rev=1299063&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/beans/Apple.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/Egg.java
Fri Mar  9 21:36:59 2012
@@ -16,12 +16,18 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.webbeans.newtests.portable.events.beans;
+package org.apache.webbeans.newtests.portable.alternative;
 
+import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Named;
 
 @Named
-public class Apple
+@ApplicationScoped
+public class Egg
 {
+    public int getSize()
+    {
+        return 42;
+    }
 
 }

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/HalfEgg.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/HalfEgg.java?rev=1299063&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/HalfEgg.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/alternative/HalfEgg.java
Fri Mar  9 21:36:59 2012
@@ -0,0 +1,37 @@
+/*
+ * 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.portable.alternative;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Named;
+
+/**
+ * This class is NOT annotated as Alternative, but it will get set as such
+ * via the AlternativeExtension
+ */
+@Named
+@ApplicationScoped
+public class HalfEgg extends Egg
+{
+    public int getSize()
+    {
+        return 21;
+    }
+
+}

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/PortableEventTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/PortableEventTest.java?rev=1299063&r1=1299062&r2=1299063&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/PortableEventTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/PortableEventTest.java
Fri Mar  9 21:36:59 2012
@@ -68,7 +68,6 @@ public class PortableEventTest extends A
         Assert.assertFalse(NotAppleExtnsion.CALLED);
         
         shutDownContainer();
-        
     }
     
     @Test
@@ -86,7 +85,6 @@ public class PortableEventTest extends A
         Assert.assertTrue(RawTypeExtension.CALLED);
         
         shutDownContainer();
-        
     }
     
     @Test
@@ -104,7 +102,6 @@ public class PortableEventTest extends A
         Assert.assertTrue(TypeVariableExtension.CALLED);
         
         shutDownContainer();
-        
     }
     
     @Test
@@ -122,7 +119,6 @@ public class PortableEventTest extends A
         Assert.assertTrue(WildcardExtension.CALLED);
         
         shutDownContainer();
-
     }
     
     @Test
@@ -140,7 +136,6 @@ public class PortableEventTest extends A
         Assert.assertFalse(WrongTypeVariableExtension.CALLED);
         
         shutDownContainer();
-        
     }
     
     @Test
@@ -158,6 +153,6 @@ public class PortableEventTest extends A
         Assert.assertFalse(WrongWildcardExtension.CALLED);
         
         shutDownContainer();
-        
     }
+
 }

Copied: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/extensions/AlternativeExtension.java
(from r1296585, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/extensions/AppleExtension.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/extensions/AlternativeExtension.java?p2=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/extensions/AlternativeExtension.java&p1=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/extensions/AppleExtension.java&r1=1296585&r2=1299063&rev=1299063&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/extensions/AppleExtension.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/events/extensions/AlternativeExtension.java
Fri Mar  9 21:36:59 2012
@@ -19,20 +19,20 @@
 package org.apache.webbeans.newtests.portable.events.extensions;
 
 import javax.enterprise.event.Observes;
-import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.Alternative;
 import javax.enterprise.inject.spi.Extension;
-import javax.enterprise.inject.spi.ProcessBean;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.util.AnnotationLiteral;
 
-import org.apache.webbeans.newtests.portable.events.beans.Apple;
+import org.apache.webbeans.newtests.portable.alternative.HalfEgg;
 
 
-public class AppleExtension implements Extension
+public class AlternativeExtension implements Extension
 {
-    public static String NAME = "";
-    
-    public void observeProcessBean(@Observes ProcessBean<Apple> event)
+    public void observeProcessAnnotatedType(@Observes ProcessAnnotatedType<HalfEgg>
pat)
     {
-        Bean<Apple> bean = event.getBean();
-        NAME = bean.getName();
+        // this just works with OWB and assumes a mutable annotation Set.
+        pat.getAnnotatedType().getAnnotations().add(new AnnotationLiteral<Alternative>()
{});
+        pat.setAnnotatedType(pat.getAnnotatedType());
     } 
 }

Modified: openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeTest5.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeTest5.xml?rev=1299063&r1=1299062&r2=1299063&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeTest5.xml
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeTest5.xml
Fri Mar  9 21:36:59 2012
@@ -18,7 +18,7 @@ specific language governing permissions 
 under the License.
 -->
 <beans>
-	  <alternatives>
-                <class>org.apache.webbeans.newtests.concepts.alternatives.common.AlternativeBeanProducer4</class>
-        </alternatives>
+    <alternatives>
+        <class>org.apache.webbeans.newtests.concepts.alternatives.common.AlternativeBeanProducer4</class>
+    </alternatives>
 </beans>



Mime
View raw message