[webbeans-commits] Webbeans SVN: r434 - in ri/trunk/webbeans-ri/src: main/java/org/jboss/webbeans/event and 3 other directories.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Sun Dec 7 10:27:01 EST 2008
Author: dallen6
Date: 2008-12-07 10:27:01 -0500 (Sun, 07 Dec 2008)
New Revision: 434
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventObserver.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.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/annotations/Role.java
Log:
Some new tests added and functionality for detection of errors related to events and observers.
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-07 14:32:47 UTC (rev 433)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-12-07 15:27:01 UTC (rev 434)
@@ -365,9 +365,18 @@
{
throw new IllegalArgumentException("Event type " + event.getClass().getName() + " is not allowed because it is a generic");
}
+ // Also check that the binding types are truly binding types
+ for (Annotation binding : bindings)
+ {
+ if (!Reflections.isBindingType(binding))
+ {
+ throw new IllegalArgumentException("Event type " + event.getClass().getName() + " cannot be fired with non-binding type " + binding.getClass().getName() + " specified");
+ }
+ }
+
// Get the observers for this event. Although resolveObservers is
- // parameterized, this
- // method is not, so we have to use Observer<Object> for observers.
+ // parameterized, this method is not, so we have to use
+ // Observer<Object> for observers.
Set<Observer<Object>> observers = this.resolveObservers(event, bindings);
this.eventManager.notifyObservers(observers, event);
}
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-07 14:32:47 UTC (rev 433)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java 2008-12-07 15:27:01 UTC (rev 434)
@@ -21,13 +21,14 @@
import java.util.HashSet;
import java.util.Set;
-import javax.webbeans.BindingType;
import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Event;
import javax.webbeans.Observable;
import javax.webbeans.Observer;
import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.util.Reflections;
+import org.jboss.webbeans.util.Strings;
/**
* Implementation of the Event interface
@@ -71,7 +72,7 @@
Set<Annotation> result = new HashSet<Annotation>();
for (Annotation annotation : annotations)
{
- if (!annotation.annotationType().isAnnotationPresent(BindingType.class))
+ if (!Reflections.isBindingType(annotation))
{
throw new IllegalArgumentException(annotation + " is not a binding type");
}
@@ -84,7 +85,7 @@
}
/**
- * Validates the binding types and checks for dupes
+ * Validates the binding types and checks for duplicates among the annotations.
*
* @param annotations The annotations to validate
* @return A set of unique binding type annotations
@@ -94,16 +95,13 @@
Set<Annotation> result = new HashSet<Annotation>();
for (Annotation annotation : annotations)
{
- if (!annotation.annotationType().isAnnotationPresent(BindingType.class))
+ if (!Reflections.isBindingType(annotation))
{
- throw new IllegalArgumentException(annotation + " is not a binding type");
+ throw new IllegalArgumentException(annotation + " is not a binding type for " + this);
}
- for (Annotation bindingAnnotation : this.bindingTypes)
+ if (result.contains(annotation) || this.bindingTypes.contains(annotation))
{
- if (bindingAnnotation.annotationType().equals(annotation.annotationType()))
- {
- throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list");
- }
+ throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list for " + this);
}
result.add(annotation);
}
@@ -136,4 +134,14 @@
manager.addObserver(observer, eventType, bindingParameters.toArray(new Annotation[0]));
}
+ @Override
+ public String toString()
+ {
+ StringBuilder buffer = new StringBuilder();
+ buffer.append("Observable Event:\n");
+ buffer.append(" Event Type: " + eventType.getName() +"\n");
+ buffer.append(Strings.collectionToString(" Event Bindings: ", bindingTypes));
+ return buffer.toString();
+ }
+
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventObserver.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventObserver.java 2008-12-07 14:32:47 UTC (rev 433)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventObserver.java 2008-12-07 15:27:01 UTC (rev 434)
@@ -19,11 +19,15 @@
import java.lang.annotation.Annotation;
import java.util.Arrays;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Observer;
import org.jboss.webbeans.MetaDataCache;
+import org.jboss.webbeans.util.Reflections;
import org.jboss.webbeans.util.Strings;
/**
@@ -58,9 +62,34 @@
this.observer = observer;
this.eventType = eventType;
this.eventBindings = Arrays.asList(eventBindings);
+ checkEventBindings(eventBindings);
}
/**
+ * Checks that each event binding specified on the observer is indeed a binding
+ * type (annotated with @BindingType) and that there are no duplicate bindings
+ * specified.
+ *
+ * @param observerEventBindings The list of event bindings for the observer
+ */
+ private void checkEventBindings(Annotation[] observerEventBindings)
+ {
+ Set<Annotation> checkedBindings = new HashSet<Annotation>();
+ for (Annotation annotation : observerEventBindings)
+ {
+ if (!Reflections.isBindingType(annotation))
+ {
+ throw new IllegalArgumentException(annotation + " is not a binding type for " + this);
+ }
+ if (checkedBindings.contains(annotation))
+ {
+ throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list for " + this);
+ }
+ checkedBindings.add(annotation);
+ }
+ }
+
+ /**
* @return the eventType
*/
public final Class<T> getEventType()
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.java 2008-12-07 14:32:47 UTC (rev 433)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.java 2008-12-07 15:27:01 UTC (rev 434)
@@ -33,6 +33,7 @@
import java.util.List;
import java.util.Set;
+import javax.webbeans.BindingType;
import javax.webbeans.ExecutionException;
/**
@@ -532,4 +533,20 @@
return classes;
}
+ /**
+ * Checks the bindingType to make sure the annotation was declared properly
+ * as a binding type (annotated with @BindingType).
+ *
+ * @param bindingType The binding type to check
+ * @return true only if the annotation is really a binding type
+ */
+ public static boolean isBindingType(Annotation bindingType)
+ {
+ boolean isBindingAnnotation = false;
+ if (bindingType.annotationType().isAnnotationPresent(BindingType.class))
+ {
+ isBindingAnnotation = true;
+ }
+ return isBindingAnnotation;
+ }
}
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-07 14:32:47 UTC (rev 433)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/NewEventTest.java 2008-12-07 15:27:01 UTC (rev 434)
@@ -3,6 +3,7 @@
import java.lang.annotation.Annotation;
import java.util.Set;
+import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Event;
import javax.webbeans.Observer;
import javax.webbeans.TypeLiteral;
@@ -10,7 +11,9 @@
import org.jboss.webbeans.bean.EventBean;
import org.jboss.webbeans.bean.SimpleBean;
import org.jboss.webbeans.introspector.AnnotatedField;
+import org.jboss.webbeans.test.bindings.AnimalStereotypeAnnotationLiteral;
import org.jboss.webbeans.test.bindings.RoleBinding;
+import org.jboss.webbeans.test.bindings.TameAnnotationLiteral;
import org.jboss.webbeans.util.BeanFactory;
import org.testng.annotations.Test;
@@ -27,6 +30,10 @@
{
}
+ public static class ATemplatedEventType<T>
+ {
+ }
+
public static class AnObserver implements Observer<AnEventType>
{
@@ -37,13 +44,13 @@
}
@SuppressWarnings("unchecked")
- @Test
- public void create() {
+ @Test(groups="events")
+ public void testEventBeanCreation() {
SimpleBean<MyTest> myTestBean = BeanFactory.createSimpleBean(MyTest.class);
for (AnnotatedField<Object> field : myTestBean.getEventFields()) {
EventBean eventBean = BeanFactory.createEventBean(field);
- @SuppressWarnings("unused")
Event<Param> event = eventBean.create();
+ assert event != null;
}
}
@@ -56,32 +63,73 @@
@Test(groups={"stub", "events"})
@SpecAssertion(section="7.1")
- public void testEventObjectWithTypeVariablesFails()
+ public void testConsumerNotifiedWhenEventTypeAndAllBindingsMatch()
{
assert false;
}
-
- @Test(groups={"stub", "events"})
- @SpecAssertion(section="7.1")
- public void testEventObjectWithWildcardsFails()
+
+ @Test(groups={"events"})
+ @SpecAssertion(section="7.2")
+ public void testManagerFireEvent()
{
- assert false;
+ // First a simple event with no bindings is fired
+ AnEventType anEvent = new AnEventType();
+ manager.fireEvent(anEvent);
+
+ // Next an event with some event bindings is fired
+ manager.fireEvent(anEvent, new RoleBinding("Admin"));
}
- @Test(groups={"stub", "events"})
- @SpecAssertion(section="7.1")
- public void testConsumerNotifiedWhenEventTypeAndAllBindingsMatch()
+ @Test(groups={"events"})
+ @SpecAssertion(section="7.1,7.2")
+ public void testManagerFireEventWithParametersFails()
{
- assert false;
+ boolean fireEventFailed = false;
+ try
+ {
+ ATemplatedEventType<String> anEvent = new ATemplatedEventType<String>();
+ manager.fireEvent(anEvent);
+ }
+ catch (IllegalArgumentException e)
+ {
+ fireEventFailed = true;
+ }
+ assert fireEventFailed;
+
+ // Although the above is really the same as with a wildcard, we will test
+ // it anyhow since the specification calls it out separately.
+ fireEventFailed = false;
+ try
+ {
+ ATemplatedEventType<?> anEventOnAnyType = new ATemplatedEventType<String>();
+ manager.fireEvent(anEventOnAnyType);
+ }
+ catch (IllegalArgumentException e)
+ {
+ fireEventFailed = true;
+ }
+ assert fireEventFailed;
}
- @Test(groups={"stub", "events"})
- @SpecAssertion(section="7.2")
- public void testManagerFireEvent()
+ @Test(groups={"events"})
+ @SpecAssertion(section="7.1,7.2")
+ public void testManagerFireEventWithNonBindingAnnotationsFails()
{
- assert false;
+ // The specs are not exactly clear on what is supposed to happen here, but borrowing
+ // from Section 7.x, we'll expect the same behavior here for a consistent API.
+ boolean fireEventFailed = false;
+ try
+ {
+ AnEventType anEvent = new AnEventType();
+ manager.fireEvent(anEvent, new AnimalStereotypeAnnotationLiteral());
+ }
+ catch (IllegalArgumentException e)
+ {
+ fireEventFailed = true;
+ }
+ assert fireEventFailed;
}
-
+
@Test(groups={"events"})
@SpecAssertion(section="7.3")
public void testManagerAddObserver()
@@ -161,18 +209,39 @@
assert resolvedObservers.isEmpty();
}
- @Test(groups={"stub", "events"})
+ @Test(groups={"events"})
@SpecAssertion(section="7.3")
public void testMultipleInstancesOfSameBindingTypeWhenAddingObserverFails()
{
- assert false;
+ boolean failedAddingObserver = false;
+ try
+ {
+ Observer<AnEventType> observer = new AnObserver();
+ manager.addObserver(observer, AnEventType.class, new RoleBinding("Admin"), new TameAnnotationLiteral(), new TameAnnotationLiteral());
+ }
+ catch (DuplicateBindingTypeException e)
+ {
+ failedAddingObserver = true;
+ }
+ assert failedAddingObserver;
}
- @Test(groups={"stub", "events"})
+ @Test(groups={"events"})
@SpecAssertion(section="7.3")
public void testMultipleInstancesOfSameBindingTypeWhenRemovingObserverFails()
{
- assert false;
+ boolean failedAddingObserver = false;
+ try
+ {
+ Observer<AnEventType> observer = new AnObserver();
+ manager.addObserver(observer, AnEventType.class);
+ manager.removeObserver(observer, AnEventType.class, new RoleBinding("Admin"), new TameAnnotationLiteral(), new TameAnnotationLiteral());
+ }
+ catch (DuplicateBindingTypeException e)
+ {
+ failedAddingObserver = true;
+ }
+ assert failedAddingObserver;
}
@Test(groups={"stub", "events"})
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/annotations/Role.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/annotations/Role.java 2008-12-07 14:32:47 UTC (rev 433)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/annotations/Role.java 2008-12-07 15:27:01 UTC (rev 434)
@@ -1,5 +1,8 @@
package org.jboss.webbeans.test.annotations;
+import javax.webbeans.BindingType;
+
+ at BindingType
public @interface Role
{
String value();
More information about the weld-commits
mailing list