Skip to content

Commit

Permalink
tests for Event object now working
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@419 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
Gavin King authored and gavin.king@gmail.com committed Dec 5, 2008
1 parent 6bbbe9d commit 3ce902e
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 110 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -164,7 +165,8 @@ private String getLocation()
@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());
}

}
53 changes: 38 additions & 15 deletions webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
Expand Up @@ -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;
}

/**
Expand All @@ -66,28 +68,49 @@ public EventImpl(Annotation... bindingTypes)
* 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;
}

/**
Expand Down Expand Up @@ -115,5 +138,5 @@ public void observe(Observer<T> observer, Annotation... bindingTypes)
bindingParameters.addAll(this.bindingTypes);
manager.addObserver(observer, eventType, bindingParameters.toArray(new Annotation[0]));
}

}
}
181 changes: 94 additions & 87 deletions webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java
Expand Up @@ -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.
Expand Down Expand Up @@ -47,93 +59,88 @@ public void before() throws Exception
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);

//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>();
// 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;
// }
/**
* 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;
}

}
Expand Up @@ -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)
{
Expand All @@ -26,6 +25,13 @@ public void fireEvent(Object event, Annotation... bindings)
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.
Expand All @@ -50,9 +56,9 @@ public final Set<Annotation> getEventBindings()
/**
* @return the eventType
*/
public final Class<?> getEventType()
public final Class<?> getObservedEventType()
{
return eventType;
return observedEventType;
}

public void setEnabledDeploymentTypes(Class<? extends Annotation>... enabledDeploymentTypes)
Expand Down

0 comments on commit 3ce902e

Please sign in to comment.