forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#2683 from mkouba/issue-2673
Observers - add support for Reception.IF_EXISTS
- Loading branch information
Showing
4 changed files
with
145 additions
and
14 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
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
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
90 changes: 90 additions & 0 deletions
90
...arc/tests/src/test/java/io/quarkus/arc/test/observers/ifexists/ReceptionIfExistsTest.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,90 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.quarkus.arc.test.observers.ifexists; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.ArcContainer; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import javax.annotation.Priority; | ||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.context.RequestScoped; | ||
import javax.enterprise.event.Observes; | ||
import javax.enterprise.event.Reception; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class ReceptionIfExistsTest { | ||
|
||
static final List<String> EVENTS = new CopyOnWriteArrayList<>(); | ||
|
||
@Rule | ||
public ArcTestContainer container = new ArcTestContainer(DependentObserver.class, RequestScopedObserver.class); | ||
|
||
@Test | ||
public void testObserver() { | ||
ArcContainer container = Arc.container(); | ||
container.beanManager().fireEvent("foo"); | ||
assertEquals(1, EVENTS.size()); | ||
assertEquals(DependentObserver.class.getName() + "foo", EVENTS.get(0)); | ||
|
||
// Activate the request context but the instance still does not exist | ||
EVENTS.clear(); | ||
container.requestContext().activate(); | ||
container.beanManager().fireEvent("foo"); | ||
assertEquals(1, EVENTS.size()); | ||
assertEquals(DependentObserver.class.getName() + "foo", EVENTS.get(0)); | ||
container.requestContext().deactivate(); | ||
|
||
// Activate the request context and the instance exists | ||
EVENTS.clear(); | ||
container.requestContext().activate(); | ||
// Force bean instance creation | ||
container.instance(RequestScopedObserver.class).get().ping(); | ||
container.beanManager().fireEvent("foo"); | ||
assertEquals(2, EVENTS.size()); | ||
assertEquals(RequestScopedObserver.class.getName() + "foo", EVENTS.get(0)); | ||
assertEquals(DependentObserver.class.getName() + "foo", EVENTS.get(1)); | ||
container.requestContext().deactivate(); | ||
} | ||
|
||
@RequestScoped | ||
static class RequestScopedObserver { | ||
|
||
void ping() { | ||
} | ||
|
||
void observeString(@Priority(1) @Observes(notifyObserver = Reception.IF_EXISTS) String value) { | ||
EVENTS.add(RequestScopedObserver.class.getName() + value); | ||
} | ||
|
||
} | ||
|
||
@Dependent | ||
static class DependentObserver { | ||
|
||
void observeString(@Priority(2) @Observes String value) { | ||
EVENTS.add(DependentObserver.class.getName() + value); | ||
} | ||
|
||
} | ||
|
||
} |