Skip to content

Commit

Permalink
Fix failing test, make defaulting annotations for events hacky and si…
Browse files Browse the repository at this point in the history
…mple for now

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@2951 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Jul 1, 2009
1 parent 0bcc231 commit 24c718b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 91 deletions.
11 changes: 10 additions & 1 deletion impl/src/main/java/org/jboss/webbeans/BeanManagerImpl.java
Expand Up @@ -80,6 +80,8 @@
import org.jboss.webbeans.event.EventObserver;
import org.jboss.webbeans.event.ObserverImpl;
import org.jboss.webbeans.introspector.WBAnnotated;
import org.jboss.webbeans.literal.AnyLiteral;
import org.jboss.webbeans.literal.CurrentLiteral;
import org.jboss.webbeans.log.Log;
import org.jboss.webbeans.log.Logging;
import org.jboss.webbeans.manager.api.WebBeansManager;
Expand Down Expand Up @@ -487,6 +489,13 @@ public <T> Set<Observer<T>> resolveObservers(T event, Annotation... bindings)
{
throw new IllegalArgumentException("Duplicate binding types: " + bindings);
}

// Manually hack in the default annotations here. We need to redo all the annotation defaulting throughout. PLM
if (bindingAnnotations.size() == 0)
{
bindingAnnotations.add(new CurrentLiteral());
}
bindingAnnotations.add(new AnyLiteral());
checkEventType(clazz);
Set<Observer<T>> observers = new HashSet<Observer<T>>();
Set<EventObserver<?>> eventObservers = observerResolver.resolve(ResolvableFactory.of(new Reflections.HierarchyDiscovery(clazz).getFlattenedTypes(), bindingAnnotations));
Expand Down Expand Up @@ -769,7 +778,7 @@ public void fireEvent(Object event, Annotation... bindings)
throw new IllegalArgumentException("Event type " + event.getClass().getName() + " cannot be fired with non-binding type " + binding.getClass().getName() + " specified");
}
}

Set<Observer<Object>> observers = resolveObservers(event, bindings);
try
{
Expand Down
8 changes: 0 additions & 8 deletions impl/src/main/java/org/jboss/webbeans/FacadeImpl.java
Expand Up @@ -19,7 +19,6 @@
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -95,13 +94,6 @@ protected BeanManagerImpl getManager()
return manager.getCurrent();
}

protected static Set<Annotation> removeBindings(Set<Annotation> bindings, Annotation...remove)
{
Set<Annotation> a = new HashSet<Annotation>(bindings);
a.removeAll(Arrays.asList(remove));
return a;
}

protected Set<? extends Annotation> getBindings()
{
return Collections.unmodifiableSet(bindings);
Expand Down
Expand Up @@ -19,7 +19,6 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
Expand Down Expand Up @@ -52,7 +51,7 @@ public T create(CreationalContext<T> creationalContext)
if (genericType instanceof ParameterizedType )
{
Type type = ((ParameterizedType) genericType).getActualTypeArguments()[0];
return newInstance(type, fixBindings(injectionPoint.getBindings()));
return newInstance(type, injectionPoint.getBindings());
}
else
{
Expand All @@ -71,28 +70,6 @@ public T create(CreationalContext<T> creationalContext)
}
}

/**
* Merges and validates the current and new bindings
*
* Checks with an abstract method for annotations to exclude
*
* @param currentBindings Existing bindings
* @param newBindings New bindings
* @return The union of the bindings
*/
protected Set<Annotation> fixBindings(Set<? extends Annotation> bindings)
{
Set<Annotation> result = new HashSet<Annotation>();
for (Annotation newAnnotation : bindings)
{
if (!getFilteredAnnotationTypes().contains(newAnnotation.annotationType()))
{
result.add(newAnnotation);
}
}
return result;
}

public void destroy(T instance, CreationalContext<T> creationalContext)
{
// TODO Auto-generated method stub
Expand All @@ -103,13 +80,6 @@ public boolean isSerializable()
{
return true;
}

/**
* Gets a set of annotation classes to ignore
*
* @return A set of annotation classes to ignore
*/
protected abstract Set<Class<? extends Annotation>> getFilteredAnnotationTypes();

protected abstract T newInstance(Type type, Set<Annotation> annotations);

Expand Down
Expand Up @@ -23,7 +23,6 @@
import java.util.Set;

import javax.enterprise.event.Event;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.TypeLiteral;

import org.jboss.webbeans.BeanManagerImpl;
Expand All @@ -39,7 +38,6 @@ public class EventBean extends AbstractFacadeBean<Event<?>>
private static final Annotation ANY = new AnyLiteral();
private static final Set<Annotation> DEFAULT_BINDINGS = new HashSet<Annotation>(Arrays.asList(ANY));
public static final ResolvableTransformer TRANSFORMER = new FacadeBeanResolvableTransformer(TYPE);
private static final Set<Class<? extends Annotation>> FILTERED_ANNOTATION_TYPES = new HashSet<Class<? extends Annotation>>(Arrays.asList(Any.class));


public static AbstractFacadeBean<Event<?>> of(BeanManagerImpl manager)
Expand Down Expand Up @@ -74,12 +72,6 @@ protected Event<?> newInstance(Type type, Set<Annotation> annotations)
{
return EventImpl.of(type, getManager(), annotations);
}

@Override
protected Set<Class<? extends Annotation>> getFilteredAnnotationTypes()
{
return FILTERED_ANNOTATION_TYPES;
}

@Override
public String toString()
Expand Down
Expand Up @@ -38,7 +38,6 @@ public class InstanceBean extends AbstractFacadeBean<Instance<?>>
private static final Set<Type> DEFAULT_TYPES = new HashSet<Type>(Arrays.asList(TYPE, Object.class));
private static final Any ANY = new AnyLiteral();
private static final Set<Annotation> DEFAULT_BINDINGS = new HashSet<Annotation>(Arrays.asList(ANY));
private static final Set<Class<? extends Annotation>> FILTERED_ANNOTATION_TYPES = new HashSet<Class<? extends Annotation>>(Arrays.asList(Any.class));
public static final ResolvableTransformer TRANSFORMER = new FacadeBeanResolvableTransformer(TYPE);


Expand Down Expand Up @@ -74,12 +73,6 @@ protected Instance<?> newInstance(Type type, Set<Annotation> annotations)
{
return InstanceImpl.of(type, getManager(), annotations);
}

@Override
protected Set<Class<? extends Annotation>> getFilteredAnnotationTypes()
{
return FILTERED_ANNOTATION_TYPES;
}

@Override
public String toString()
Expand Down
3 changes: 1 addition & 2 deletions impl/src/main/java/org/jboss/webbeans/event/EventImpl.java
Expand Up @@ -26,7 +26,6 @@

import org.jboss.webbeans.BeanManagerImpl;
import org.jboss.webbeans.FacadeImpl;
import org.jboss.webbeans.literal.AnyLiteral;
import org.jboss.webbeans.util.Strings;

/**
Expand Down Expand Up @@ -57,7 +56,7 @@ public static <E> Event<E> of(Type eventType, BeanManagerImpl manager, Set<Annot
*/
public EventImpl(Type eventType, BeanManagerImpl manager, Set<Annotation> bindings)
{
super(eventType, manager, removeBindings(bindings, new AnyLiteral()));
super(eventType, manager, bindings);
}

/**
Expand Down
Expand Up @@ -4,72 +4,70 @@
import javax.enterprise.event.Observes;
import javax.enterprise.inject.AnnotationLiteral;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Current;

import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.webbeans.BeanManagerImpl;
import org.jboss.webbeans.test.AbstractWebBeansTest;
import org.testng.annotations.Test;

@Artifact
@Artifact
public class SimpleEventTest extends AbstractWebBeansTest
{
private static boolean called_flag_for_BindingType;
private static boolean called_flag_for_NonBindingType;
private static boolean RECEIVE_1_OBSERVED;
private static boolean RECEIVE_2_OBSERVED;

private static void initCalledFlag() {
called_flag_for_BindingType = false;
called_flag_for_NonBindingType = false;
private static void initFlags() {
RECEIVE_1_OBSERVED = false;
RECEIVE_2_OBSERVED = false;
}

@Test
public void testEventUsingManager()
public void testFireEventOnManager()
{
BeanManagerImpl manager = getCurrentManager();

initCalledFlag();
initFlags();

manager.fireEvent("Fired using Manager Interface with AnnotationLiteral.",
new AnnotationLiteral<Updated>(){});
manager.fireEvent("Fired using Manager Interface with AnnotationLiteral.", new AnnotationLiteral<Updated>(){});

assert called_flag_for_NonBindingType == true;
assert called_flag_for_BindingType == true;
assert RECEIVE_2_OBSERVED == true;
assert RECEIVE_1_OBSERVED == true;

initCalledFlag();
initFlags();

manager.fireEvent("Fired using Manager Interface.");

assert called_flag_for_NonBindingType == true;
assert called_flag_for_BindingType == false; // not called
assert RECEIVE_2_OBSERVED == true;
assert RECEIVE_1_OBSERVED == false; // not called
}

@Test
public void testEventUsingEvent()
public void testFireEventOnEvent()
{
BeanManagerImpl manager = getCurrentManager();

App app = manager.getInstanceByType(App.class);
App app = createContextualInstance(App.class);

initCalledFlag();
initFlags();

// app.fireEventByBindingDeclaredAtInjectionPoint();
//
// assert called_flag_for_NonBindingType == true;
// assert called_flag_for_BindingType == true;
app.fireEventByBindingDeclaredAtInjectionPoint();

assert RECEIVE_1_OBSERVED == true;
assert RECEIVE_2_OBSERVED == true;

initCalledFlag();
initFlags();

app.fireEventByAnnotationLiteral();

assert called_flag_for_NonBindingType == true;
assert called_flag_for_BindingType == true;
assert RECEIVE_2_OBSERVED == true;
assert RECEIVE_1_OBSERVED == true;

initCalledFlag();
initFlags();

app.fireEventNonBindingType();
app.fireEventViaAny();

assert called_flag_for_NonBindingType == true;
assert called_flag_for_BindingType == false; // not called
assert RECEIVE_2_OBSERVED == true;
assert RECEIVE_1_OBSERVED == false; // not called
}

public static class App
Expand All @@ -94,7 +92,7 @@ public void fireEventByBindingDeclaredAtInjectionPoint()
event2.fire("Fired using Event Interface with Binding Declared.");
}

public void fireEventNonBindingType()
public void fireEventViaAny()
{
event3.fire("Fired using Event Interface with Non-BindingType.");
}
Expand All @@ -104,12 +102,12 @@ public static class Receiver
{
public void receive1(@Observes @Updated String s)
{
called_flag_for_BindingType = true;
RECEIVE_1_OBSERVED = true;
}

public void receive2(@Observes String s)
public void receive2(@Any @Observes String s)
{
called_flag_for_NonBindingType = true;
RECEIVE_2_OBSERVED = true;
}
}
}

0 comments on commit 24c718b

Please sign in to comment.