openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r830858 - in /incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test: component/library/BookShop.java component/portable/events/MyExtension.java mock/MockManager.java unittests/portable/events/ExtensionTest.java
Date Thu, 29 Oct 2009 08:17:27 GMT
Author: struberg
Date: Thu Oct 29 08:17:26 2009
New Revision: 830858

URL: http://svn.apache.org/viewvc?rev=830858&view=rev
Log:
[OWB-148] start adding a test to observe all lifecycle events from within an Extension

I also added the BeanManager itself to the context so it can be injected as @Default BeanManager

Modified:
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/library/BookShop.java
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/portable/events/ExtensionTest.java

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/library/BookShop.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/library/BookShop.java?rev=830858&r1=830857&r2=830858&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/library/BookShop.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/library/BookShop.java
Thu Oct 29 08:17:26 2009
@@ -14,10 +14,12 @@
 package org.apache.webbeans.test.component.library;
 
 import javax.enterprise.context.RequestScoped;
+import javax.enterprise.event.Observes;
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.InvocationContext;
 
 import org.apache.webbeans.annotation.deployment.Production;
+import org.apache.webbeans.test.event.LoggedInEvent;
 
 @Production
 @RequestScoped
@@ -34,5 +36,10 @@
     {
         return context.proceed();
     }
+    
+    public void observeSomething(@Observes LoggedInEvent lie)
+    {
+        // this is purely for checking if the Extension mechanism works
+    }
 
 }

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java?rev=830858&r1=830857&r2=830858&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java
Thu Oct 29 08:17:26 2009
@@ -20,20 +20,97 @@
 
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.Default;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.AfterDeploymentValidation;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+import javax.enterprise.inject.spi.BeforeShutdown;
 import javax.enterprise.inject.spi.Extension;
 import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.inject.spi.ProcessBean;
+import javax.enterprise.inject.spi.ProcessInjectionTarget;
+import javax.enterprise.inject.spi.ProcessObserverMethod;
+import javax.enterprise.inject.spi.ProcessProducer;
 
+/**
+ * Test extension which only sets a few static members which will be validated in a test
case.
+ */
 public class MyExtension implements Extension
 {
-    public static ProcessAnnotatedType<?> event = null;
+    public static ProcessAnnotatedType<?>     processAnnotatedTypeEvent;
+    public static BeforeBeanDiscovery         lastBeforeBeanDiscovery;
+    public static AfterBeanDiscovery          lastAfterBeanDiscovery;
+    public static BeforeShutdown              beforeShutdownEvent;
+    public static AfterDeploymentValidation   afterDeploymentValidation;
+    public static ProcessInjectionTarget<?>   processInjectionTarget;
+    public static ProcessProducer<?,?>        processProducer;
+    public static ProcessBean<?>              processBean;
+    public static ProcessObserverMethod<?, ?> processObserverMethod;
+    
+    
+    /**
+     * Reset all static fields before the test starts
+     */
+    public static void reset() {
+        processAnnotatedTypeEvent = null;
+        lastBeforeBeanDiscovery = null;
+        lastAfterBeanDiscovery = null;
+        beforeShutdownEvent = null;
+        afterDeploymentValidation = null;
+        processInjectionTarget = null;
+        processProducer = null;
+        processBean = null;
+        processObserverMethod = null;
+    }
     
     public MyExtension()
     {
-        
+        reset();
+    }
+
+    public void observeBeforeBeanDiscovery(@Observes BeforeBeanDiscovery beforeBean) 
+    {
+        lastBeforeBeanDiscovery = beforeBean;
+    }
+    
+    public void observeAfterBeanDiscovery(@Observes AfterBeanDiscovery afterBean, BeanManager
beanManager) 
+    {
+        lastAfterBeanDiscovery = afterBean;
+    }
+
+    public void observeAfterDeploymentValidation(@Observes AfterDeploymentValidation adv,
BeanManager bm) 
+    {
+        afterDeploymentValidation = adv;
+    }
+
+    public void observeShutdownEvent(@Observes BeforeShutdown bs, BeanManager bm) 
+    {
+        beforeShutdownEvent = bs;
     }
 
-    public void observer(@Default @Observes ProcessAnnotatedType<?> annotatedType)
+    public void observeProcessAnnotatedTypeEvent(@Observes @Default ProcessAnnotatedType<?>
annotatedType)
+    {
+        processAnnotatedTypeEvent = annotatedType;
+    }
+    
+    public <T> void observeProcessInjectionTarget(@Observes ProcessInjectionTarget<T>
pit)
+    {
+        processInjectionTarget = pit;
+    }
+    
+    public <T, X> void observeProcessProducer(@Observes ProcessProducer<T, X>
pp)
     {
-        event = annotatedType;
+        processProducer = pp;
     }
+    
+    public <X> void observeProcessBean(@Observes ProcessBean<X> pb)
+    {
+        processBean = pb;
+    }
+    
+    public <X, T> void processObserverMethod(@Observes ProcessObserverMethod<X,
T> pom)
+    {
+        processObserverMethod = pom;
+    }
+    
 }

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java?rev=830858&r1=830857&r2=830858&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java
Thu Oct 29 08:17:26 2009
@@ -36,8 +36,10 @@
 import javax.enterprise.inject.spi.ObserverMethod;
 
 import org.apache.webbeans.component.AbstractBean;
+import org.apache.webbeans.component.BeanManagerBean;
 import org.apache.webbeans.container.BeanManagerImpl;
 import org.apache.webbeans.container.activity.ActivityManager;
+import org.apache.webbeans.util.WebBeansUtil;
 
 public class MockManager implements BeanManager
 {
@@ -49,6 +51,7 @@
     {
         this.manager = new BeanManagerImpl();
         ActivityManager.getInstance().setRootActivity(this.manager);
+        manager.addBean(WebBeansUtil.getManagerBean());
     }
 
 

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/portable/events/ExtensionTest.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/portable/events/ExtensionTest.java?rev=830858&r1=830857&r2=830858&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/portable/events/ExtensionTest.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/portable/events/ExtensionTest.java
Thu Oct 29 08:17:26 2009
@@ -18,6 +18,8 @@
  */
 package org.apache.webbeans.test.unittests.portable.events;
 
+import java.lang.annotation.Annotation;
+
 import javax.enterprise.inject.spi.Bean;
 
 import junit.framework.Assert;
@@ -25,6 +27,7 @@
 import org.apache.webbeans.annotation.DefaultLiteral;
 import org.apache.webbeans.context.ContextFactory;
 import org.apache.webbeans.portable.events.ExtensionLoader;
+import org.apache.webbeans.portable.events.discovery.BeforeShutdownImpl;
 import org.apache.webbeans.test.component.library.BookShop;
 import org.apache.webbeans.test.component.portable.events.MyExtension;
 import org.apache.webbeans.test.mock.MockServletContext;
@@ -49,19 +52,32 @@
     @SuppressWarnings("unchecked")
     public void testExtensionServices()
     {
+        MyExtension.reset();
+        
         ExtensionLoader.getInstance().loadExtensionServices();
         
         MockServletContext servletContext = new MockServletContext();
         ContextFactory.initApplicationContext(servletContext);
+
         Bean<MyExtension> extension = (Bean<MyExtension>)getManager().resolveByType(MyExtension.class,
new DefaultLiteral()).iterator().next();
         
         MyExtension ext = getManager().getInstance(extension);
+        
         System.out.println(ext.toString());
         
         defineSimpleWebBean(BookShop.class);
         
-        Assert.assertNotNull(MyExtension.event);
+        Assert.assertNotNull(MyExtension.processAnnotatedTypeEvent);
+
+        //X TODO should work after fixing the test lifecycle Assert.assertNotNull(MyExtension.processBean);
+
+        //X TODO should work after fixing the test lifecycle Assert.assertNotNull(MyExtension.processObserverMethod);
         
+        //Fire shut down
+        getManager().fireEvent(new BeforeShutdownImpl(), new Annotation[0]);
+
         ContextFactory.destroyApplicationContext(servletContext);
+        
+        Assert.assertNotNull(MyExtension.beforeShutdownEvent);
     }
 }



Mime
View raw message