Skip to content

Commit

Permalink
WELD-2775 Make sure BM#getEvent() adds @default qualifier correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Jan 29, 2024
1 parent f324a81 commit 6c05786
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Set;

import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.spi.EventMetadata;
import jakarta.enterprise.inject.spi.InjectionPoint;

Expand Down Expand Up @@ -59,6 +60,9 @@ private EventMetadataImpl(Type type, InjectionPoint injectionPoint, Set<Annotati
public Set<Annotation> getQualifiers() {
ImmutableSet.Builder<Annotation> builder = ImmutableSet.<Annotation> builder();
builder.add(Any.Literal.INSTANCE);
if ((qualifiers != null && qualifiers.isEmpty()) || (qualifierArray != null && qualifierArray.length == 0)) {
builder.add(Default.Literal.INSTANCE);
}
if (qualifiers != null) {
return builder.addAll(qualifiers).build();
} else if (qualifierArray != null) {
Expand Down
11 changes: 9 additions & 2 deletions impl/src/main/java/org/jboss/weld/event/ObserverNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import jakarta.enterprise.event.NotificationOptions;
import jakarta.enterprise.event.ObserverException;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.spi.EventMetadata;
import jakarta.enterprise.inject.spi.ObserverMethod;

Expand Down Expand Up @@ -200,9 +201,15 @@ public void fireEvent(Object event, Resolvable resolvable) {
protected Resolvable buildEventResolvable(Type eventType, Set<Annotation> qualifiers) {
// We can always cache as this is only ever called by Weld where we avoid non-static inner classes for annotation literals
Set<Type> typeClosure = sharedObjectCache.getTypeClosureHolder(eventType).get();
return new ResolvableBuilder(resolver.getMetaAnnotationStore()).addTypes(typeClosure).addType(Object.class)
ResolvableBuilder resolvableBuilder = new ResolvableBuilder(resolver.getMetaAnnotationStore()).addTypes(typeClosure)
.addType(Object.class)
.addQualifiers(qualifiers)
.addQualifierUnchecked(QualifierInstance.ANY).create();
.addQualifierUnchecked(QualifierInstance.ANY);
if (qualifiers.isEmpty()) {
resolvableBuilder.addQualifier(Default.Literal.INSTANCE);
}
return resolvableBuilder.create();

}

protected Resolvable buildEventResolvable(Type eventType, Annotation... qualifiers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
*/
package org.jboss.weld.tests.event;

import java.lang.annotation.Annotation;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
Expand All @@ -34,9 +45,9 @@ public static Archive<?> deploy() {
.addPackage(EventQualifierTest.class.getPackage());
}

/*
* description = "WELD-226"
*/
@Inject
BeanManager bm;

@Test
public void testDefaultQualifierNotRequired(Bar bar) {
bar.fireWithNoQualifiers();
Expand All @@ -60,4 +71,42 @@ public void testDefaultQualifierNotRequired(Bar bar) {
Assert.assertTrue(bar.isUpdatedObserved());
}

@Test
public void testDefaultQualifierPresent(ObservingBean bean)
throws ExecutionException, InterruptedException, TimeoutException {
bean.reset();

Set<Annotation> expectedQualifiers = Set.of(Any.Literal.INSTANCE, Default.Literal.INSTANCE);

// just get event fire right away - @Default should be included
bm.getEvent().fire(new Payload());
Assert.assertEquals(1, bean.getDefaultObjectNotified());
Assert.assertEquals(1, bean.getDefaultPayloadNotified());
Assert.assertEquals(expectedQualifiers, bean.getDefaultObjectQualifiers());
Assert.assertEquals(expectedQualifiers, bean.getDefaultPayloadQualifiers());

// select Payload and fire - @Default should be included
bm.getEvent().select(Payload.class).fire(new Payload());
Assert.assertEquals(2, bean.getDefaultObjectNotified());
Assert.assertEquals(2, bean.getDefaultPayloadNotified());
Assert.assertEquals(expectedQualifiers, bean.getDefaultObjectQualifiers());
Assert.assertEquals(expectedQualifiers, bean.getDefaultPayloadQualifiers());

// same in async variant
// just get event fire right away - @Default should be included
bm.getEvent().fireAsync(new Payload()).toCompletableFuture().get(2, TimeUnit.SECONDS);
Assert.assertEquals(1, bean.getDefaultObjectAsyncNotified());
Assert.assertEquals(1, bean.getDefaultPayloadAsyncNotified());
Assert.assertEquals(expectedQualifiers, bean.getDefaultObjectAsyncQualifiers());
Assert.assertEquals(expectedQualifiers, bean.getDefaultPayloadAsyncQualifiers());

// select Payload and fire - @Default should be included
bm.getEvent().select(Payload.class).fireAsync(new Payload()).toCompletableFuture().get(2, TimeUnit.SECONDS);
Assert.assertEquals(2, bean.getDefaultObjectAsyncNotified());
Assert.assertEquals(2, bean.getDefaultPayloadAsyncNotified());
Assert.assertEquals(expectedQualifiers, bean.getDefaultObjectAsyncQualifiers());
Assert.assertEquals(expectedQualifiers, bean.getDefaultPayloadAsyncQualifiers());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.jboss.weld.tests.event;

import java.lang.annotation.Annotation;
import java.util.Set;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.event.ObservesAsync;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.spi.EventMetadata;

@ApplicationScoped
public class ObservingBean {

private volatile int defaultObjectNotified = 0;
private volatile int defaultObjectAsyncNotified = 0;
private volatile int defaultPayloadNotified = 0;
private volatile int defaultPayloadAsyncNotified = 0;
private volatile Set<Annotation> defaultObjectQualifiers;
private volatile Set<Annotation> defaultObjectAsyncQualifiers;
private volatile Set<Annotation> defaultPayloadQualifiers;
private volatile Set<Annotation> defaultPayloadAsyncQualifiers;

public void observeDefaultObject(@Observes @Default Object payload, EventMetadata em) {
// object type is very broad, only look for Payload runtime type
if (em.getType().equals(Payload.class)) {
this.defaultObjectNotified++;
this.defaultObjectQualifiers = em.getQualifiers();
}
}

public void observeDefaultPayload(@Observes @Default Payload payload, EventMetadata em) {
this.defaultPayloadNotified++;
this.defaultPayloadQualifiers = em.getQualifiers();
}

public void observeDefaultObjectAsync(@ObservesAsync @Default Object payload, EventMetadata em) {
// object type is very broad, only look for Payload runtime type
if (em.getType().equals(Payload.class)) {
this.defaultObjectAsyncNotified++;
this.defaultObjectAsyncQualifiers = em.getQualifiers();
}
}

public void observeDefaultPayloadAsync(@ObservesAsync @Default Payload payload, EventMetadata em) {
this.defaultPayloadAsyncNotified++;
this.defaultPayloadAsyncQualifiers = em.getQualifiers();
}

public int getDefaultObjectNotified() {
return defaultObjectNotified;
}

public int getDefaultPayloadNotified() {
return defaultPayloadNotified;
}

public Set<Annotation> getDefaultObjectQualifiers() {
return defaultObjectQualifiers;
}

public Set<Annotation> getDefaultPayloadQualifiers() {
return defaultPayloadQualifiers;
}

public int getDefaultObjectAsyncNotified() {
return defaultObjectAsyncNotified;
}

public int getDefaultPayloadAsyncNotified() {
return defaultPayloadAsyncNotified;
}

public Set<Annotation> getDefaultObjectAsyncQualifiers() {
return defaultObjectAsyncQualifiers;
}

public Set<Annotation> getDefaultPayloadAsyncQualifiers() {
return defaultPayloadAsyncQualifiers;
}

public void reset() {
this.defaultPayloadNotified = 0;
this.defaultPayloadAsyncNotified = 0;
this.defaultObjectNotified = 0;
this.defaultObjectAsyncNotified = 0;
this.defaultObjectQualifiers = null;
this.defaultObjectAsyncQualifiers = null;
this.defaultPayloadQualifiers = null;
this.defaultPayloadAsyncQualifiers = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.jboss.weld.tests.event;

// serves as an event payload
public class Payload {
}

0 comments on commit 6c05786

Please sign in to comment.