[webbeans-commits] Webbeans SVN: r112 - in ri/trunk/webbeans-ri: src/main/java/org/jboss/webbeans/event and 2 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Wed Oct 1 15:24:16 EDT 2008


Author: dallen6
Date: 2008-10-01 15:24:15 -0400 (Wed, 01 Oct 2008)
New Revision: 112

Added:
   ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockObserverImpl.java
Modified:
   ri/trunk/webbeans-ri/
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
   ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/DeferredEventNotificationTest.java
   ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ObserverTest.java
Log:
Completed a few unit tests for the event bus.


Property changes on: ri/trunk/webbeans-ri
___________________________________________________________________
Name: svn:ignore
   - .project

.classpath

target

   + .project

.classpath

target

test-output


Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java	2008-09-28 11:39:24 UTC (rev 111)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java	2008-10-01 19:24:15 UTC (rev 112)
@@ -9,6 +9,7 @@
 
 import javax.webbeans.manager.Manager;
 import javax.webbeans.manager.Observer;
+import javax.webbeans.Observes;
 
 import org.jboss.webbeans.injectable.Parameter;
 import org.jboss.webbeans.model.AbstractComponentModel;
@@ -33,26 +34,23 @@
 public class ObserverImpl<T> implements Observer<T>
 {
 
-   private final AbstractComponentModel<?, ?> compModel;
-   private final ObserverMethod            observerMethod;
-   private final Set<Annotation>           eventBindings;
-   private final Class<T>                  eventType;
+   private final AbstractComponentModel<?, ?> componentModel;
+   private final ObserverMethod observerMethod;
+   private final Set<Annotation> eventBindings;
+   private final Class<T> eventType;
 
    /**
-    * Creates an Observer which describes an observer method (7.3).
+    * Creates an Observer which describes and encapsulates an observer method (7.3).
     * 
-    * @param componentModel
-    *           The model for the component which defines the observer method
-    * @param observer
-    *           The observer method to notify
-    * @param eventType
-    *           The type of event being observed
+    * @param componentModel The model for the component which defines the
+    *           observer method
+    * @param observer The observer method to notify
+    * @param eventType The type of event being observed
     */
    @SuppressWarnings("unchecked")
-   public ObserverImpl(AbstractComponentModel<?, ?> componentModel,
-         ObserverMethod observer, Class<T> eventType)
+   public ObserverImpl(AbstractComponentModel<?, ?> componentModel, ObserverMethod observer, Class<T> eventType)
    {
-      this.compModel = componentModel;
+      this.componentModel = componentModel;
       this.observerMethod = observer;
       this.eventType = eventType;
       List<Parameter> parms = observer.getParameters();
@@ -61,10 +59,16 @@
       {
          if (p.getType().equals(eventType))
          {
-            if ((p.getBindingTypes() != null)
-                  && (p.getBindingTypes().length > 0))
+            if ((p.getBindingTypes() != null) && (p.getBindingTypes().length > 0))
             {
                eventBindings.addAll(Arrays.asList(p.getBindingTypes()));
+               // Remove the @Observes annotation since it is not an event
+               // binding type
+               for (Annotation annotation : eventBindings)
+               {
+                  if (Observes.class.isAssignableFrom(annotation.getClass()))
+                     eventBindings.remove(annotation);
+               }
                break;
             }
          }
@@ -95,14 +99,26 @@
     * (non-Javadoc)
     * 
     * @see javax.webbeans.Observer#notify(javax.webbeans.Container,
-    *      java.lang.Object)
+    * java.lang.Object)
     */
-   public void notify(Manager container, T event)
+   public void notify(Manager manager, T event)
    {
       // Get the most specialized instance of the component
-      Object instance = container.getInstanceByType(compModel.getType(),
-            compModel.getBindingTypes().toArray(new Annotation[0]));
+      Object instance = getInstance(manager);
       if (instance != null)
-         this.observerMethod.invoke(container, instance, event);
+         this.observerMethod.invoke(manager, instance, event);
    }
+
+   /**
+    * Uses the container to retrieve the most specialized instance of this
+    * observer.
+    * 
+    * @param container The WebBeans manager
+    * @return the most specialized instance
+    */
+   protected Object getInstance(Manager manager)
+   {
+      // Return the most specialized instance of the component
+      return manager.getInstanceByType(componentModel.getType(), componentModel.getBindingTypes().toArray(new Annotation[0]));
+   }
 }

Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/DeferredEventNotificationTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/DeferredEventNotificationTest.java	2008-09-28 11:39:24 UTC (rev 111)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/DeferredEventNotificationTest.java	2008-10-01 19:24:15 UTC (rev 112)
@@ -1,8 +1,27 @@
-/**
- * 
- */
 package org.jboss.webbeans.test;
 
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.webbeans.manager.Observer;
+import javax.webbeans.Observes;
+
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.bindings.StandardAnnotationLiteral;
+import org.jboss.webbeans.event.DeferredEventNotification;
+import org.jboss.webbeans.event.ObserverMethod;
+import org.jboss.webbeans.introspector.AnnotatedType;
+import org.jboss.webbeans.introspector.SimpleAnnotatedType;
+import org.jboss.webbeans.model.SimpleComponentModel;
+import org.jboss.webbeans.test.annotations.Asynchronous;
+import org.jboss.webbeans.test.bindings.AnotherDeploymentTypeAnnotationLiteral;
+import org.jboss.webbeans.test.bindings.AsynchronousAnnotationLiteral;
+import org.jboss.webbeans.test.components.Tuna;
+import org.jboss.webbeans.test.mock.MockContainerImpl;
+import org.jboss.webbeans.test.mock.MockObserverImpl;
 import org.testng.annotations.Test;
 
 /**
@@ -10,20 +29,60 @@
  * notification till the end of a transaction.
  * 
  * @author David Allen
- *
+ * 
  */
 public class DeferredEventNotificationTest
 {
 
+   public class Event
+   {
+      // Simple class used for testing
+   }
+
+   public class AnObserver
+   {
+      protected boolean notified = false;
+
+      public void observe(@Observes @Asynchronous Event e)
+      {
+         // An observer method
+         this.notified = true;
+      }
+   }
+
    /**
-    * Test method for {@link org.jboss.webbeans.event.DeferredEventNotification#beforeCompletion()}.
+    * Test method for
+    * {@link org.jboss.webbeans.event.DeferredEventNotification#beforeCompletion()}
+    * .
     */
    @Test
-   public final void testBeforeCompletion()
+   public final void testBeforeCompletion() throws Exception
    {
       // When the transaction is committed, the beforeCompletion() method is
-      // invoked which in turn invokes the observer.  Here the mock observer
+      // invoked which in turn invokes the observer. Here the mock observer
       // is used to keep track of the event being fired.
+      ManagerImpl manager;
+      SimpleComponentModel<Tuna> tuna;
+      ObserverMethod om;
+      List<Annotation> enabledDeploymentTypes = new ArrayList<Annotation>();
+      enabledDeploymentTypes.add(new StandardAnnotationLiteral());
+      enabledDeploymentTypes.add(new AnotherDeploymentTypeAnnotationLiteral());
+      manager = new MockContainerImpl(enabledDeploymentTypes);
+
+      // Create an observer with known binding types
+      Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+      annotations.put(Asynchronous.class, new AsynchronousAnnotationLiteral());
+      AnnotatedType<Tuna> annotatedItem = new SimpleAnnotatedType<Tuna>(Tuna.class, annotations);
+      tuna = new SimpleComponentModel<Tuna>(new SimpleAnnotatedType<Tuna>(Tuna.class), annotatedItem, manager);
+      om = new ObserverMethod(AnObserver.class.getMethod("observe", new Class[] { Event.class }));
+
+      AnObserver observerInstance = new AnObserver();
+      Observer<Event> observer = new MockObserverImpl<Event>(tuna, om, Event.class);
+      ((MockObserverImpl<Event>) observer).setInstance(observerInstance);
+      Event event = new Event();
+      DeferredEventNotification deferredNotification = new DeferredEventNotification(manager, event, observer);
+      deferredNotification.beforeCompletion();
+      assert observerInstance.notified;
    }
 
 }

Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ObserverTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ObserverTest.java	2008-09-28 11:39:24 UTC (rev 111)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ObserverTest.java	2008-10-01 19:24:15 UTC (rev 112)
@@ -1,6 +1,3 @@
-/**
- * 
- */
 package org.jboss.webbeans.test;
 
 import java.lang.annotation.Annotation;
@@ -10,7 +7,6 @@
 import java.util.Map;
 
 import javax.webbeans.Observes;
-import javax.webbeans.Production;
 import javax.webbeans.manager.Observer;
 
 import org.jboss.webbeans.ManagerImpl;
@@ -20,16 +16,12 @@
 import org.jboss.webbeans.introspector.AnnotatedType;
 import org.jboss.webbeans.introspector.SimpleAnnotatedType;
 import org.jboss.webbeans.model.SimpleComponentModel;
-import org.jboss.webbeans.model.StereotypeModel;
-import org.jboss.webbeans.test.annotations.AnimalStereotype;
 import org.jboss.webbeans.test.annotations.Asynchronous;
-import org.jboss.webbeans.test.annotations.FishStereotype;
-import org.jboss.webbeans.test.annotations.RequestScopedAnimalStereotype;
-import org.jboss.webbeans.test.annotations.RiverFishStereotype;
 import org.jboss.webbeans.test.bindings.AnotherDeploymentTypeAnnotationLiteral;
 import org.jboss.webbeans.test.bindings.AsynchronousAnnotationLiteral;
 import org.jboss.webbeans.test.components.Tuna;
 import org.jboss.webbeans.test.mock.MockContainerImpl;
+import org.jboss.webbeans.test.mock.MockObserverImpl;
 import org.jboss.webbeans.util.Reflections;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
@@ -38,60 +30,54 @@
  * Unit tests for the implementation of Observer.
  * 
  * @author David Allen
- *
+ * 
  */
 public class ObserverTest
 {
-   private ManagerImpl container;
-   
+   private ManagerImpl manager;
+   private SimpleComponentModel<Tuna> tuna;
+   private ObserverMethod om;
+
    public class Event
    {
       // Simple class used for testing
    }
-   
+
    public class AnObserver
    {
-      public void observe(@Observes Event e)
+      protected boolean notified = false;
+
+      public void observe(@Observes @Asynchronous Event e)
       {
          // An observer method
+         this.notified = true;
       }
    }
 
    @BeforeMethod
-   public void before()
+   public void before() throws Exception
    {
       List<Annotation> enabledDeploymentTypes = new ArrayList<Annotation>();
       enabledDeploymentTypes.add(new StandardAnnotationLiteral());
       enabledDeploymentTypes.add(new AnotherDeploymentTypeAnnotationLiteral());
-      container = new MockContainerImpl(enabledDeploymentTypes);
-      
-      initStereotypes(container);
+      manager = new MockContainerImpl(enabledDeploymentTypes);
+
+      // Create an observer with known binding types
+      Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+      annotations.put(Asynchronous.class, new AsynchronousAnnotationLiteral());
+      AnnotatedType<Tuna> annotatedItem = new SimpleAnnotatedType<Tuna>(Tuna.class, annotations);
+      tuna = new SimpleComponentModel<Tuna>(new SimpleAnnotatedType<Tuna>(Tuna.class), annotatedItem, manager);
+      om = new ObserverMethod(AnObserver.class.getMethod("observe", new Class[] { Event.class }));
    }
-   
-   private void initStereotypes(ManagerImpl container)
-   {
-      container.getModelManager().addStereotype(new StereotypeModel(new SimpleAnnotatedType(AnimalStereotype.class)));
-      container.getModelManager().addStereotype(new StereotypeModel(new SimpleAnnotatedType(FishStereotype.class)));
-      container.getModelManager().addStereotype(new StereotypeModel(new SimpleAnnotatedType(RiverFishStereotype.class)));
-      container.getModelManager().addStereotype(new StereotypeModel(new SimpleAnnotatedType(RequestScopedAnimalStereotype.class)));
-   }
-   
 
    /**
-    * Test method for {@link org.jboss.webbeans.event.ObserverImpl#getEventBindingTypes()}.
+    * Test method for
+    * {@link org.jboss.webbeans.event.ObserverImpl#getEventBindingTypes()}.
     */
    @SuppressWarnings("unchecked")
-   @Test(groups="eventbus")
+   @Test(groups = "eventbus")
    public final void testGetEventBindingTypes() throws Exception
    {
-      // Create an observer with known binding types
-      Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
-      annotations.put(Asynchronous.class, new AsynchronousAnnotationLiteral());
-      AnnotatedType annotatedItem = new SimpleAnnotatedType(Tuna.class, annotations);
-      SimpleComponentModel<Tuna> tuna = new SimpleComponentModel<Tuna>(new SimpleAnnotatedType(Tuna.class), annotatedItem, container);
-      assert tuna.getDeploymentType().annotationType().equals(Production.class);
-      ObserverMethod om = new ObserverMethod(AnObserver.class.getMethod("observe", new Class[]{Event.class}));
-
       Observer<Event> o = new ObserverImpl<Event>(tuna, om, Event.class);
       assert o.getEventBindingTypes().size() == 1;
       assert Reflections.annotationSetMatches(o.getEventBindingTypes(), Asynchronous.class);
@@ -99,21 +85,33 @@
    }
 
    /**
-    * Test method for {@link org.jboss.webbeans.event.ObserverImpl#getEventType()}.
+    * Test method for
+    * {@link org.jboss.webbeans.event.ObserverImpl#getEventType()}.
+    * 
+    * @throws
+    * @throws Exception
     */
-   @Test(groups="eventbus")
-   public final void testGetEventType()
+   @Test(groups = "eventbus")
+   public final void testGetEventType() throws Exception
    {
-      //TODO Implement
+      Observer<Event> o = new ObserverImpl<Event>(tuna, om, Event.class);
+      assert o.getEventType().equals(Event.class);
    }
 
    /**
-    * Test method for {@link org.jboss.webbeans.event.ObserverImpl#notify(javax.webbeans.Container, java.lang.Object)}.
+    * Test method for
+    * {@link org.jboss.webbeans.event.ObserverImpl#notify(javax.webbeans.Container, java.lang.Object)}
+    * .
     */
-   @Test(groups="eventbus")
-   public final void testNotify()
+   @Test(groups = "eventbus")
+   public final void testNotify() throws Exception
    {
-      //TODO Implement
+      AnObserver observerInstance = new AnObserver();
+      Observer<Event> observer = new MockObserverImpl<Event>(tuna, om, Event.class);
+      ((MockObserverImpl<Event>) observer).setInstance(observerInstance);
+      Event event = new Event();
+      observer.notify(manager, event);
+      assert observerInstance.notified;
    }
 
 }

Added: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockObserverImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockObserverImpl.java	                        (rev 0)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockObserverImpl.java	2008-10-01 19:24:15 UTC (rev 112)
@@ -0,0 +1,37 @@
+package org.jboss.webbeans.test.mock;
+
+import javax.webbeans.manager.Manager;
+
+import org.jboss.webbeans.event.ObserverImpl;
+import org.jboss.webbeans.event.ObserverMethod;
+import org.jboss.webbeans.model.AbstractComponentModel;
+
+/**
+ * An implementation used for unit testing only.
+ * @author David Allen
+ *
+ */
+public class MockObserverImpl<T> extends ObserverImpl<T> {
+
+	private Object specializedInstance;
+	
+	public MockObserverImpl(AbstractComponentModel<?, ?> componentModel,
+			ObserverMethod observer, Class<T> eventType) {
+		super(componentModel, observer, eventType);
+	}
+
+	@Override
+	protected final Object getInstance(Manager manager) {
+		return specializedInstance;
+	}
+
+	/**
+	 * The most specialized instance of this observer type.
+	 * @param instance The instance to use for testing
+	 */
+	public final void setInstance(Object instance)
+	{
+		specializedInstance = instance;
+	}
+
+}


Property changes on: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockObserverImpl.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain




More information about the weld-commits mailing list