Skip to content

Commit

Permalink
Fixed some more bugs with observer methods and implemented the tests …
Browse files Browse the repository at this point in the history
…through 8.5.2 for events.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@472 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Dec 8, 2008
1 parent 07b8dac commit 5b4deab
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
Expand Up @@ -18,11 +18,10 @@
package org.jboss.webbeans.event;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.webbeans.Current;
import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Observer;

Expand Down Expand Up @@ -61,31 +60,33 @@ public EventObserver(final Observer<T> observer, final Class<T> eventType, final
{
this.observer = observer;
this.eventType = eventType;
this.eventBindings = Arrays.asList(eventBindings);
this.eventBindings = new ArrayList<Annotation>();
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
* bindings specified. If the @Current binding type is found, it is removed
* since this is only a default supplied by the container but no applicable
* for the actual event objects which get fired.
*/
private void checkEventBindings(Annotation[] observerEventBindings)
private void checkEventBindings(Annotation[] bindingAnnotations)
{
Set<Annotation> checkedBindings = new HashSet<Annotation>();
for (Annotation annotation : observerEventBindings)
for (Annotation annotation : bindingAnnotations)
{
if (!Reflections.isBindingType(annotation))
{
throw new IllegalArgumentException(annotation + " is not a binding type for " + this);
}
if (checkedBindings.contains(annotation))
if (eventBindings.contains(annotation))
{
throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list for " + this);
}
checkedBindings.add(annotation);
if (!annotation.annotationType().equals(Current.class))
{
eventBindings.add(annotation);
}
}
}

Expand Down Expand Up @@ -162,6 +163,7 @@ public int hashCode()
return result;
}

@SuppressWarnings("unchecked")
@Override
public boolean equals(Object other)
{
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.jboss.webbeans.test.ejb.model.invalid.FoxTerrier;
import org.jboss.webbeans.test.ejb.model.invalid.TibetanTerrier;
import org.jboss.webbeans.test.ejb.model.invalid.YorkshireTerrier;
import org.jboss.webbeans.test.ejb.model.valid.BullTerrier;
import org.jboss.webbeans.test.ejb.model.valid.Pomeranian;
import org.jboss.webbeans.test.mock.MockManagerImpl;
import org.jboss.webbeans.util.BeanFactory;
Expand Down Expand Up @@ -76,8 +77,9 @@ public void testEventBeanCreation()
{
SimpleBean<MyTest> myTestBean = BeanFactory.createSimpleBean(MyTest.class);
boolean found = false;
for (AnnotatedField field : myTestBean.getInjectableFields()) {
if ( field.isAnnotationPresent(Observable.class) )
for (AnnotatedField field : myTestBean.getInjectableFields())
{
if (field.isAnnotationPresent(Observable.class))
{
EventBean eventBean = BeanFactory.createEventBean(field);
Event<Param> event = eventBean.create();
Expand Down Expand Up @@ -438,6 +440,20 @@ public void testObserverMethodCannotObserveParameterizedEvents()
assert definitionException;
}

@Test(groups = { "events" })
@SpecAssertion(section = "8.5.1")
public void testObserverMethodWithoutBindingTypesObservesEventsWithoutBindingTypes()
{
// This observer has no binding types specified
Set<AbstractBean<?, ?>> beans = bootstrap.createBeans(Pomeranian.class);
assert beans.size() == 1;

// Resolve registered observers with an event containing no binding types
Set<Observer<String>> resolvedObservers = manager.resolveObservers("A new event");
assert !resolvedObservers.isEmpty();
assert resolvedObservers.size() == 1;
}

@Test(groups = { "events" })
@SpecAssertion(section = "8.5.2")
public void testObserverMethodAnnotatedProducesFails()
Expand Down Expand Up @@ -506,11 +522,18 @@ public void testObserverMethodWithDisposesParamFails()
assert definitionException;
}

@Test(groups = { "stub", "events" })
@SpecAssertion(section = "8.5.1")
public void testObserverMethodWithoutBindingTypesObservesEventsWithoutBindingTypes()
@Test(groups = { "events" })
@SpecAssertion(section = "8.5.2")
public void testObserverMethodMayHaveMultipleBindingTypes()
{
assert false;
Set<AbstractBean<?, ?>> beans = bootstrap.createBeans(BullTerrier.class);
assert beans != null;
// If we can resolve the observer with the two binding types,
// then it worked
Set<Observer<String>> resolvedObservers = manager.resolveObservers("An event object", new RoleBinding("Admin"), new TameAnnotationLiteral());
assert !resolvedObservers.isEmpty();
assert resolvedObservers.size() == 1;

}

@Test(groups = { "stub", "events" })
Expand Down
@@ -0,0 +1,15 @@
package org.jboss.webbeans.test.ejb.model.valid;

import javax.ejb.Stateless;
import javax.webbeans.Observes;

import org.jboss.webbeans.test.annotations.Role;
import org.jboss.webbeans.test.annotations.Tame;

@Stateless
public class BullTerrier
{
public void observesBadEvent(@Observes @Role("Admin") @Tame String someEvent)
{
}
}
Expand Up @@ -17,6 +17,10 @@ public void observeInitialized(@Observes @Initialized Manager manager)
{
}

public void observeSimpleString(@Observes String someString)
{
}

public static void staticallyObserveInitialized(@Observes @Initialized Manager manager)
{
}
Expand Down

0 comments on commit 5b4deab

Please sign in to comment.