Author: dallen6
Date: 2008-12-01 17:12:52 -0500 (Mon, 01 Dec 2008)
New Revision: 386
Removed:
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBeanModelTest.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/NewEventTest.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java
Log:
Fixed implementations for add/removeObserver() methods and added the corresponding test
implementations.
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-12-01
21:30:33 UTC (rev 385)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-12-01
22:12:52 UTC (rev 386)
@@ -378,9 +378,7 @@
@SuppressWarnings("unchecked")
public <T> Manager addObserver(Observer<T> observer, TypeLiteral<T>
eventType, Annotation... bindings)
{
- // TODO Using the eventType TypeLiteral<T>, the Class<T> object must
be
- // retrieved
- this.eventManager.addObserver(observer, (Class<T>)
Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
+ this.eventManager.addObserver(observer, (Class<T>) eventType.getType(),
bindings);
return this;
}
@@ -592,9 +590,7 @@
@SuppressWarnings("unchecked")
public <T> Manager removeObserver(Observer<T> observer,
TypeLiteral<T> eventType, Annotation... bindings)
{
- // TODO The Class<T> for the event type must be retrieved from the
- // TypeLiteral<T> instance
- this.eventManager.removeObserver(observer, (Class<T>)
Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
+ this.eventManager.removeObserver(observer, (Class<T>) eventType.getType(),
bindings);
return this;
}
Deleted:
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBeanModelTest.java
===================================================================
---
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBeanModelTest.java 2008-12-01
21:30:33 UTC (rev 385)
+++
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBeanModelTest.java 2008-12-01
22:12:52 UTC (rev 386)
@@ -1,86 +0,0 @@
-package org.jboss.webbeans.test;
-
-import java.lang.reflect.Field;
-
-import javax.webbeans.Standard;
-
-import org.jboss.webbeans.event.EventImpl;
-import org.jboss.webbeans.test.annotations.AnotherDeploymentType;
-import org.jboss.webbeans.test.beans.DangerCall;
-import org.jboss.webbeans.test.mock.MockManagerImpl;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-/**
- * Unit tests for the bean model used only for the container supplied
- * Event bean.
- *
- * @author David Allen
- *
- */
-public class EventBeanModelTest
-{
- private MockManagerImpl manager = null;
- //private EventBeanModel<EventImpl<DangerCall>> eventBeanModel = null;
- EventImpl<DangerCall> eventModelField = null;
-
- @BeforeMethod
- public void before() throws Exception
- {
- manager = new MockManagerImpl();
- manager.setEnabledDeploymentTypes(Standard.class, AnotherDeploymentType.class);
- Field eventModelField =
this.getClass().getDeclaredField("eventModelField");
- /*eventBeanModel = new EventBeanModel<EventImpl<DangerCall>>(
- new
SimpleAnnotatedField<EventImpl<DangerCall>>(eventModelField),
- new
SimpleAnnotatedField<EventImpl<DangerCall>>(eventModelField),
- manager);*/
-
- }
-
- /**
- * The name should always be null since this type of bean is not allowed to have a
name.
- */
- @Test(groups = "event")
- public void testName()
- {
- //assert eventBeanModel.getName() == null;
- }
-
- /**
- * The scope type should always be @Dependent
- */
- @Test(groups = "event")
- public void testScopeType()
- {
- //assert eventBeanModel.getScopeType().equals(Dependent.class);
- }
-
- /**
- * The deployment type should always be @Standard
- */
- @Test(groups = "event")
- public void testDeploymentType()
- {
- //assert eventBeanModel.getDeploymentType().equals(Standard.class);
- }
-
- @Test(groups = "event")
- public void testApiTypes()
- {
- //Set<Class<?>> apis = eventBeanModel.getApiTypes();
- //assert apis.size() >= 1;
- //for (Class<?> api : apis)
- //{
- // api.equals(Event.class);
- //}
- }
-
- // TODO Fix this @Test(groups = "event")
- public void testConstructor()
- {
- /*BeanConstructor<EventImpl<DangerCall>, ?> constructor =
eventBeanModel.getConstructor();
- assert constructor != null;
- Event<DangerCall> event = constructor.invoke(manager, null);
- assert event != null;*/
- }
-}
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/NewEventTest.java
===================================================================
---
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/NewEventTest.java 2008-12-01
21:30:33 UTC (rev 385)
+++
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/NewEventTest.java 2008-12-01
22:12:52 UTC (rev 386)
@@ -1,5 +1,12 @@
package org.jboss.webbeans.test;
+import java.lang.annotation.Annotation;
+import java.util.Set;
+
+import javax.webbeans.Observer;
+import javax.webbeans.TypeLiteral;
+
+import org.jboss.webbeans.test.bindings.RoleBinding;
import org.testng.annotations.Test;
/**
@@ -11,6 +18,19 @@
@SpecVersion("PDR")
public class NewEventTest extends AbstractTest
{
+ public static class AnEventType
+ {
+ }
+
+ public static class AnObserver implements Observer<AnEventType>
+ {
+
+ public void notify(AnEventType event)
+ {
+ }
+
+ }
+
@Test(groups={"stub", "events"})
@SpecAssertion(section="7.1")
public void testEventTypeIncludesAllSuperclassesAndInterfacesOfEventObject()
@@ -34,7 +54,7 @@
@Test(groups={"stub", "events"})
@SpecAssertion(section="7.1")
- public void testConsumerNotifiedWhenEventTypeAndAllBindingMathces()
+ public void testConsumerNotifiedWhenEventTypeAndAllBindingsMatch()
{
assert false;
}
@@ -46,18 +66,83 @@
assert false;
}
- @Test(groups={"stub", "events"})
+ @Test(groups={"events"})
@SpecAssertion(section="7.3")
public void testManagerAddObserver()
{
- assert false;
+ Observer<AnEventType> observer = new AnObserver();
+
+ // First test with the Class<T> of the event type
+ manager.addObserver(observer, AnEventType.class);
+ Set<Observer<AnEventType>> resolvedObservers =
manager.resolveObservers(new AnEventType());
+ assert !resolvedObservers.isEmpty();
+ assert resolvedObservers.size() == 1;
+ assert resolvedObservers.iterator().next() == observer;
+
+ // Now test with the TypeLiteral<T> of the event type
+ observer = new AnObserver();
+ manager.addObserver(observer, new TypeLiteral<AnEventType>(){});
+ resolvedObservers = manager.resolveObservers(new AnEventType());
+ assert !resolvedObservers.isEmpty();
+ assert resolvedObservers.size() == 2;
+ boolean foundObserver = false;
+ for (Observer<AnEventType> obs : resolvedObservers)
+ {
+ if (obs == observer)
+ {
+ foundObserver = true;
+ break;
+ }
+ }
+ assert foundObserver;
+
+ // Try adding an observer with some binding types
+ observer = new AnObserver();
+ Annotation[] bindingTypes = new Annotation[] { new RoleBinding("Admin"),
new RoleBinding("Manager") };
+ manager.addObserver(observer, AnEventType.class, bindingTypes);
+ resolvedObservers = manager.resolveObservers(new AnEventType(), bindingTypes);
+ assert !resolvedObservers.isEmpty();
+ assert resolvedObservers.size() == 3;
+ foundObserver = false;
+ for (Observer<AnEventType> obs : resolvedObservers)
+ {
+ if (obs == observer)
+ {
+ foundObserver = true;
+ break;
+ }
+ }
+ assert foundObserver;
}
- @Test(groups={"stub", "events"})
+ @Test(groups={"events"})
@SpecAssertion(section="7.3")
public void testManagerRemoveObserver()
{
- assert false;
+ Observer<AnEventType> observer = new AnObserver();
+
+ // First test with the Class<T> of the event type
+ manager.addObserver(observer, AnEventType.class);
+ manager.removeObserver(observer, AnEventType.class);
+ Set<Observer<AnEventType>> resolvedObservers =
manager.resolveObservers(new AnEventType());
+ assert resolvedObservers.isEmpty();
+
+ // Now test with the TypeLiteral<T> of the event type
+ observer = new AnObserver();
+ manager.addObserver(observer, new TypeLiteral<AnEventType>(){});
+ manager.removeObserver(observer, new TypeLiteral<AnEventType>(){});
+ resolvedObservers = manager.resolveObservers(new AnEventType());
+ assert resolvedObservers.isEmpty();
+
+ // Also test with binding types
+ Annotation[] bindings = new Annotation[] { new RoleBinding("Admin") };
+ manager.addObserver(observer, AnEventType.class, bindings);
+ manager.removeObserver(observer, AnEventType.class);
+ resolvedObservers = manager.resolveObservers(new AnEventType(), bindings);
+ assert !resolvedObservers.isEmpty();
+ manager.removeObserver(observer, AnEventType.class, new
RoleBinding("Admin"));
+ resolvedObservers = manager.resolveObservers(new AnEventType(), bindings);
+ assert resolvedObservers.isEmpty();
}
@Test(groups={"stub", "events"})
@@ -196,7 +281,7 @@
@Test(groups={"stub", "events"})
@SpecAssertion(section="7.5.4")
- public void testObserverMethodRecievesInjectionsOnNonObservesParameters()
+ public void testObserverMethodReceivesInjectionsOnNonObservesParameters()
{
assert false;
}
Modified:
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java
===================================================================
---
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java 2008-12-01
21:30:33 UTC (rev 385)
+++
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java 2008-12-01
22:12:52 UTC (rev 386)
@@ -17,7 +17,6 @@
private Object event = null;
private Class<? extends Object> eventType = null;
private Annotation[] eventBindings = null;
- private Observer<?> observer = null;
/* (non-Javadoc)
* @see org.jboss.webbeans.ManagerImpl#fireEvent(java.lang.Object,
java.lang.annotation.Annotation[])
@@ -28,35 +27,9 @@
// Record the event
this.event = event;
this.eventBindings = bindings;
+ super.fireEvent(event, bindings);
}
- /* (non-Javadoc)
- * @see org.jboss.webbeans.ManagerImpl#addObserver(javax.webbeans.Observer,
java.lang.Class, java.lang.annotation.Annotation[])
- */
- @Override
- public <T> Manager addObserver(Observer<T> observer, Class<T>
eventType,
- Annotation... bindings)
- {
- this.observer = observer;
- this.eventType = eventType;
- this.eventBindings = bindings;
- return this;
- }
-
- /* (non-Javadoc)
- * @see org.jboss.webbeans.ManagerImpl#addObserver(javax.webbeans.Observer,
javax.webbeans.TypeLiteral, java.lang.annotation.Annotation[])
- */
- @Override
- public <T> Manager addObserver(Observer<T> observer,
- TypeLiteral<T> eventType, Annotation... bindings)
- {
- this.observer = observer;
- // TODO Fix the event type based on the type literal being passed. Not clear how
to get the actual T.
- this.eventType = null;
- this.eventBindings = bindings;
- return this;
- }
-
/**
* Retrieves the event which was last fired with this manager.
* @return the event
@@ -85,14 +58,6 @@
return eventType;
}
- /**
- * @return the observer
- */
- public final Observer<?> getObserver()
- {
- return observer;
- }
-
public void setEnabledDeploymentTypes(Class<? extends Annotation>...
enabledDeploymentTypes)
{
initEnabledDeploymentTypes(enabledDeploymentTypes);