-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC: fix searching for inherited scopes
The CDI specification mandates that if a superclass of a bean declares a scope annotation, scope annotations from further superclasses are not inherited. This holds even if the annotation is not `@Inherited`. This commit fixes searching for inherited scopes to honor this.
- Loading branch information
Showing
3 changed files
with
81 additions
and
8 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
76 changes: 76 additions & 0 deletions
76
...projects/arc/tests/src/test/java/io/quarkus/arc/test/bean/scope/ScopeInheritanceTest.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,76 @@ | ||
package io.quarkus.arc.test.bean.scope; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.context.NormalScope; | ||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.enterprise.inject.Stereotype; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
import jakarta.enterprise.inject.spi.BeanManager; | ||
|
||
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 ScopeInheritanceTest { | ||
@RegisterExtension | ||
ArcTestContainer container = new ArcTestContainer(MyScope.class, MyStereotype.class, MyBean1.class, MyBean2.class); | ||
|
||
@Test | ||
public void nonInheritedScopeOnDirectSuperclass() { | ||
BeanManager bm = Arc.container().beanManager(); | ||
Bean<MyBean1> bean = (Bean<MyBean1>) bm.resolve(bm.getBeans(MyBean1.class)); | ||
assertEquals(Dependent.class, bean.getScope()); | ||
} | ||
|
||
@Test | ||
public void inheritedScopeOnDirectSuperclass() { | ||
BeanManager bm = Arc.container().beanManager(); | ||
Bean<MyBean2> bean = (Bean<MyBean2>) bm.resolve(bm.getBeans(MyBean2.class)); | ||
assertEquals(RequestScoped.class, bean.getScope()); | ||
} | ||
|
||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@NormalScope | ||
@interface MyScope { | ||
} | ||
|
||
// just a bean defining annotation | ||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Stereotype | ||
@interface MyStereotype { | ||
} | ||
|
||
@MyStereotype | ||
static class MyBean1 extends DirectSuperclassWithNonInheritedScope { | ||
} | ||
|
||
@MyStereotype | ||
static class MyBean2 extends DirectSuperclassWithInheritedScope { | ||
} | ||
|
||
// `MyScope` is not `@Inherited`, so `MyBean1` will not inherit it, but it will | ||
// also prevent inheriting `@ApplicationScoped` from `IndirectSuperclass` | ||
@MyScope | ||
static class DirectSuperclassWithNonInheritedScope extends IndirectSuperclass { | ||
} | ||
|
||
@RequestScoped | ||
static class DirectSuperclassWithInheritedScope extends IndirectSuperclass { | ||
} | ||
|
||
@ApplicationScoped | ||
static class IndirectSuperclass { | ||
} | ||
} |