-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arc - add default delegating impl of ObserverMethod#notify(T event)
- Loading branch information
Showing
6 changed files
with
117 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventContextImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.quarkus.arc.impl; | ||
|
||
import jakarta.enterprise.inject.spi.EventContext; | ||
import jakarta.enterprise.inject.spi.EventMetadata; | ||
|
||
// this class is public because it is used in io.quarkus.arc.InjectableObserverMethod | ||
public class EventContextImpl<T> implements EventContext<T> { | ||
|
||
private final T payload; | ||
|
||
private final EventMetadata metadata; | ||
|
||
public EventContextImpl(T payload, EventMetadata metadata) { | ||
this.payload = payload; | ||
this.metadata = metadata; | ||
} | ||
|
||
@Override | ||
public T getEvent() { | ||
return payload; | ||
} | ||
|
||
@Override | ||
public EventMetadata getMetadata() { | ||
return metadata; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventMetadataImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.arc.impl; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.Set; | ||
|
||
import jakarta.enterprise.inject.spi.EventMetadata; | ||
import jakarta.enterprise.inject.spi.InjectionPoint; | ||
|
||
// this class is public because it is used in io.quarkus.arc.InjectableObserverMethod | ||
public class EventMetadataImpl implements EventMetadata { | ||
|
||
private final Set<Annotation> qualifiers; | ||
|
||
private final Type eventType; | ||
|
||
public EventMetadataImpl(Set<Annotation> qualifiers, Type eventType) { | ||
this.qualifiers = qualifiers; | ||
this.eventType = eventType; | ||
} | ||
|
||
@Override | ||
public Set<Annotation> getQualifiers() { | ||
return qualifiers; | ||
} | ||
|
||
@Override | ||
public InjectionPoint getInjectionPoint() { | ||
// Currently we do not support injection point of the injected Event instance which fired the event | ||
return null; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return eventType; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
.../src/test/java/io/quarkus/arc/test/observers/notification/ManualNotifyInvocationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.quarkus.arc.test.observers.notification; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.enterprise.inject.spi.ObserverMethod; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class ManualNotifyInvocationTest { | ||
|
||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer(StringObserver.class); | ||
|
||
@Test | ||
public void testManualNotifyInvocation() { | ||
assertEquals(0, StringObserver.NOTIFIED.get()); | ||
Arc.container().beanManager().getEvent().fire("hello"); | ||
assertEquals(1, StringObserver.NOTIFIED.get()); | ||
|
||
Set<ObserverMethod<? super String>> foundOM = Arc.container().beanManager().resolveObserverMethods("foo"); | ||
assertEquals(1, foundOM.size()); | ||
foundOM.iterator().next().notify("test"); | ||
assertEquals(2, StringObserver.NOTIFIED.get()); | ||
} | ||
|
||
@ApplicationScoped | ||
static class StringObserver { | ||
private static final AtomicInteger NOTIFIED = new AtomicInteger(); | ||
|
||
void observeString(@Observes String value) { | ||
NOTIFIED.incrementAndGet(); | ||
} | ||
} | ||
} |