[webbeans-commits] Webbeans SVN: r419 - in ri/trunk/webbeans-ri: src/main/java/org/jboss/webbeans/bean and 3 other directories.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Fri Dec 5 15:15:59 EST 2008
Author: gavin.king at jboss.com
Date: 2008-12-05 15:15:58 -0500 (Fri, 05 Dec 2008)
New Revision: 419
Modified:
ri/trunk/webbeans-ri/
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java
Log:
tests for Event object now working
Property changes on: ri/trunk/webbeans-ri
___________________________________________________________________
Name: svn:ignore
- .project
.classpath
target
test-output
temp-testng-customsuite.xml
+ .project
.classpath
target
test-output
temp-testng-customsuite.xml
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java 2008-12-05 14:17:46 UTC (rev 418)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java 2008-12-05 20:15:58 UTC (rev 419)
@@ -25,6 +25,7 @@
import javax.webbeans.Event;
import javax.webbeans.Standard;
+import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.event.EventImpl;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedItem;
@@ -164,7 +165,8 @@
@Override
public Event<T> create()
{
- return new EventImpl<T>(annotatedItem.getBindingTypesAsArray());
+ Class<T> eventType = (Class<T>) annotatedItem.getType().getTypeParameters()[0].getClass();
+ return new EventImpl<T>(ManagerImpl.instance(), eventType, annotatedItem.getBindingTypesAsArray());
}
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java 2008-12-05 14:17:46 UTC (rev 418)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java 2008-12-05 20:15:58 UTC (rev 419)
@@ -44,20 +44,22 @@
public class EventImpl<T> implements Event<T>
{
// The set of binding types
- private Set<? extends Annotation> bindingTypes;
+ private final Set<? extends Annotation> bindingTypes;
// The event type
- private Class<T> eventType;
+ private final Class<T> eventType;
// The Web Beans manager
- protected static final ManagerImpl manager = ManagerImpl.instance();
+ protected final ManagerImpl manager;
/**
* Constructor
*
* @param bindingTypes The binding types
*/
- public EventImpl(Annotation... bindingTypes)
+ public EventImpl(ManagerImpl manager, Class<T> eventType, Annotation... bindingTypes)
{
- this.bindingTypes = checkBindingTypes(bindingTypes);
+ this.bindingTypes = getBindingTypes(bindingTypes);
+ this.eventType = eventType;
+ this.manager = manager;
}
/**
@@ -66,28 +68,49 @@
* Removes @Observable from the list
*
* @param annotations The annotations to validate
- * @return A set of unique binding type annotations (minus @Observable, if it
+ * @return A set of binding type annotations (minus @Observable, if it
* was present)
*/
- private Set<Annotation> checkBindingTypes(Annotation... annotations)
+ private static Set<Annotation> getBindingTypes(Annotation... annotations)
{
- Set<Annotation> uniqueAnnotations = new HashSet<Annotation>();
+ Set<Annotation> result = new HashSet<Annotation>();
for (Annotation annotation : annotations)
{
if (!annotation.annotationType().isAnnotationPresent(BindingType.class))
{
throw new IllegalArgumentException(annotation + " is not a binding type");
}
- if (uniqueAnnotations.contains(annotation))
+ if (!annotation.annotationType().equals(Observable.class))
{
- throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list");
+ result.add(annotation);
}
- if (!annotation.annotationType().equals(Observable.class))
+ }
+ return result;
+ }
+
+ /**
+ * Validates the binding types and checks for dupes
+ *
+ * @param annotations The annotations to validate
+ * @return A set of unique binding type annotations
+ */
+ private Set<Annotation> checkBindingTypes(Annotation... annotations)
+ {
+ Set<Annotation> result = new HashSet<Annotation>();
+ for (Annotation annotation : annotations)
+ {
+ if (!annotation.annotationType().isAnnotationPresent(BindingType.class))
{
- uniqueAnnotations.add(annotation);
+ throw new IllegalArgumentException(annotation + " is not a binding type");
}
+ for (Annotation bindingAnnotation: this.bindingTypes) {
+ if (bindingAnnotation.annotationType().equals(annotation.annotationType())) {
+ throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list");
+ }
+ }
+ result.add(annotation);
}
- return uniqueAnnotations;
+ return result;
}
/**
@@ -115,5 +138,5 @@
bindingParameters.addAll(this.bindingTypes);
manager.addObserver(observer, eventType, bindingParameters.toArray(new Annotation[0]));
}
-
-}
\ No newline at end of file
+
+}
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java 2008-12-05 14:17:46 UTC (rev 418)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java 2008-12-05 20:15:58 UTC (rev 419)
@@ -4,12 +4,24 @@
import java.util.ArrayList;
import java.util.List;
+import javax.webbeans.DuplicateBindingTypeException;
+import javax.webbeans.Event;
import javax.webbeans.Observer;
import javax.webbeans.Standard;
+import org.jboss.webbeans.event.EventImpl;
import org.jboss.webbeans.test.annotations.AnotherDeploymentType;
+import org.jboss.webbeans.test.annotations.Synchronous;
+import org.jboss.webbeans.test.annotations.Tame;
+import org.jboss.webbeans.test.beans.DangerCall;
+import org.jboss.webbeans.test.bindings.FishStereotypeAnnotationLiteral;
+import org.jboss.webbeans.test.bindings.RiverFishStereotypeAnnotationLiteral;
+import org.jboss.webbeans.test.bindings.SynchronousAnnotationLiteral;
+import org.jboss.webbeans.test.bindings.TameAnnotationLiteral;
import org.jboss.webbeans.test.mock.MockManagerImpl;
+import org.jboss.webbeans.util.Reflections;
import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
/**
* Tests for the implementation of an Event component.
@@ -47,93 +59,88 @@
manager.setEnabledDeploymentTypes(Standard.class, AnotherDeploymentType.class);
}
-// /**
-// * Tests the {@link Event#fire(Object, Annotation...)} method with a locally
-// * instantiated implementation.
-// */
-// @SuppressWarnings("unchecked")
-// @Test(groups = "event")
-// @SpecAssertion(section = "7.6")
-// public void testFireEvent()
-// {
-// DangerCall anEvent = new DangerCall();
-// // Create a test annotation for the event and use it to construct the
-// // event object
-// Annotation[] annotations = new Annotation[] { new TameAnnotationLiteral() };
-// EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>();
-// eventComponent.setEventBindings(annotations);
-// eventComponent.setManager(manager);
-// eventComponent.fire(anEvent, new SynchronousAnnotationLiteral());
-// assert anEvent.equals(manager.getEvent());
-// assert Reflections.annotationSetMatches(manager.getEventBindings(),
-// Tame.class, Synchronous.class);
-//
-// // Test duplicate annotations on the fire method call
-// boolean duplicateDetected = false;
-// try
-// {
-// eventComponent.fire(anEvent, new TameAnnotationLiteral(),
-// new TameAnnotationLiteral());
-// } catch (DuplicateBindingTypeException e)
-// {
-// duplicateDetected = true;
-// }
-// assert duplicateDetected;
-//
-// // Test annotations that are not binding types
-// boolean nonBindingTypeDetected = false;
-// try
-// {
-// eventComponent.fire(anEvent, new FishStereotypeAnnotationLiteral());
-// } catch (IllegalArgumentException e)
-// {
-// nonBindingTypeDetected = true;
-// }
-// assert nonBindingTypeDetected;
-// }
+ /**
+ * Tests the {@link Event#fire(Object, Annotation...)} method with a locally
+ * instantiated implementation.
+ */
+ @SuppressWarnings("unchecked")
+ @Test(groups = "event")
+ @SpecAssertion(section = "7.6")
+ public void testFireEvent()
+ {
+ DangerCall anEvent = new DangerCall();
+ //Create a test annotation for the event and use it to construct the
+ //event object
+ Annotation[] annotations = new Annotation[] { new TameAnnotationLiteral() };
+ EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>(manager, DangerCall.class, annotations);
+ eventComponent.fire(anEvent, new SynchronousAnnotationLiteral());
+ assert anEvent.equals(manager.getEvent());
+ assert Reflections.annotationSetMatches(manager.getEventBindings(),
+ Tame.class, Synchronous.class);
-// /**
-// * Tests the {@link Event#observe(javax.webbeans.Observer, Annotation...)}
-// * method with a locally instantiated implementation.
-// */
-// @Test(groups = {"observerMethod"})
-// @SpecAssertion(section = "7.6")
-// public void testObserve()
-// {
-// // Create a test annotation for the event and use it to construct the
-// // event object
-// Annotation[] annotations = new Annotation[] { new TameAnnotationLiteral() };
-// EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>();
-// eventComponent.setEventType(DangerCall.class);
-// eventComponent.setEventBindings(annotations);
-// eventComponent.setManager(manager);
-// Observer<DangerCall> observer = new AnObserver<DangerCall>();
-// eventComponent.observe(observer, new SynchronousAnnotationLiteral());
-// assert manager.getEventType().equals(DangerCall.class);
-//
-// // Try duplicate annotation bindings
-// boolean duplicateDetected = false;
-// try
-// {
-// eventComponent.observe(observer,
-// new TameAnnotationLiteral());
-// } catch (DuplicateBindingTypeException e)
-// {
-// duplicateDetected = true;
-// }
-// assert duplicateDetected;
-//
-// // Try an invalid binding type
-// boolean nonBindingTypeDetected = false;
-// try
-// {
-// eventComponent.observe(observer,
-// new RiverFishStereotypeAnnotationLiteral());
-// } catch (IllegalArgumentException e)
-// {
-// nonBindingTypeDetected = true;
-// }
-// assert nonBindingTypeDetected;
-// }
+ //Test duplicate annotations on the fire method call
+ boolean duplicateDetected = false;
+ try
+ {
+ eventComponent.fire(anEvent, new TameAnnotationLiteral(),
+ new TameAnnotationLiteral());
+ } catch (DuplicateBindingTypeException e)
+ {
+ duplicateDetected = true;
+ }
+ assert duplicateDetected;
+ //Test annotations that are not binding types
+ boolean nonBindingTypeDetected = false;
+ try
+ {
+ eventComponent.fire(anEvent, new FishStereotypeAnnotationLiteral());
+ } catch (IllegalArgumentException e)
+ {
+ nonBindingTypeDetected = true;
+ }
+ assert nonBindingTypeDetected;
+ }
+
+ /**
+ * Tests the {@link Event#observe(javax.webbeans.Observer, Annotation...)}
+ * method with a locally instantiated implementation.
+ */
+ @Test(groups = {"observerMethod"})
+ @SpecAssertion(section = "7.6")
+ public void testObserve()
+ {
+ //Create a test annotation for the event and use it to construct the
+ //event object
+ Annotation[] annotations = new Annotation[] { new TameAnnotationLiteral() };
+ EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>(manager, DangerCall.class, annotations);
+ Observer<DangerCall> observer = new AnObserver<DangerCall>();
+ eventComponent.observe(observer, new SynchronousAnnotationLiteral());
+ assert manager.getObservedEventType().equals(DangerCall.class);
+
+ //Try duplicate annotation bindings
+ boolean duplicateDetected = false;
+ try
+ {
+ eventComponent.observe(observer,
+ new TameAnnotationLiteral());
+ } catch (DuplicateBindingTypeException e)
+ {
+ duplicateDetected = true;
+ }
+ assert duplicateDetected;
+
+ //Try an invalid binding type
+ boolean nonBindingTypeDetected = false;
+ try
+ {
+ eventComponent.observe(observer,
+ new RiverFishStereotypeAnnotationLiteral());
+ } catch (IllegalArgumentException e)
+ {
+ nonBindingTypeDetected = true;
+ }
+ assert nonBindingTypeDetected;
+ }
+
}
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-05 14:17:46 UTC (rev 418)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java 2008-12-05 20:15:58 UTC (rev 419)
@@ -5,19 +5,18 @@
import java.util.HashSet;
import java.util.Set;
+import javax.webbeans.Observer;
import javax.webbeans.manager.Context;
+import javax.webbeans.manager.Manager;
import org.jboss.webbeans.ManagerImpl;
public class MockManagerImpl extends ManagerImpl
{
- private Object event = null;
- private Class<? extends Object> eventType = null;
+ private Object event = null;
private Annotation[] eventBindings = null;
+ private Class<? extends Object> observedEventType = null;
- /* (non-Javadoc)
- * @see org.jboss.webbeans.ManagerImpl#fireEvent(java.lang.Object, java.lang.annotation.Annotation[])
- */
@Override
public void fireEvent(Object event, Annotation... bindings)
{
@@ -26,6 +25,13 @@
this.eventBindings = bindings;
super.fireEvent(event, bindings);
}
+
+ @Override
+ public <T> Manager addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
+ {
+ this.observedEventType = eventType;
+ return super.addObserver(observer, eventType, bindings);
+ }
/**
* Retrieves the event which was last fired with this manager.
@@ -50,9 +56,9 @@
/**
* @return the eventType
*/
- public final Class<?> getEventType()
+ public final Class<?> getObservedEventType()
{
- return eventType;
+ return observedEventType;
}
public void setEnabledDeploymentTypes(Class<? extends Annotation>... enabledDeploymentTypes)
More information about the weld-commits
mailing list