diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/BeanContainerTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/BeanContainerTest.java index 621cba9592..d9060ba8fe 100644 --- a/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/BeanContainerTest.java +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/BeanContainerTest.java @@ -18,21 +18,23 @@ package org.jboss.cdi.tck.tests.beanContainer; import static org.jboss.cdi.tck.cdi.Sections.BM_DETERMINING_ANNOTATION; +import static org.jboss.cdi.tck.cdi.Sections.BM_OBTAIN_CONTEXTS; import static org.jboss.cdi.tck.cdi.Sections.BM_RESOLVE_AMBIGUOUS_DEP; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - + import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; import jakarta.enterprise.context.RequestScoped; import jakarta.enterprise.context.SessionScoped; +import jakarta.enterprise.context.spi.Context; import jakarta.enterprise.inject.AmbiguousResolutionException; import jakarta.enterprise.inject.Any; import jakarta.enterprise.inject.spi.Bean; +import jakarta.inject.Singleton; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.cdi.tck.AbstractTest; import org.jboss.cdi.tck.literals.RetentionLiteral; @@ -43,6 +45,7 @@ import org.jboss.test.audit.annotations.SpecVersion; import org.testng.annotations.Test; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -61,7 +64,8 @@ public class BeanContainerTest extends AbstractTest { @Deployment public static WebArchive createTestArchive() { return new WebArchiveBuilder().withTestClassPackage(BeanContainerTest.class) - .withClasses(RetentionLiteral.class, TargetLiteral.class).build(); + .withClasses(RetentionLiteral.class, TargetLiteral.class) + .withBuildCompatibleExtension(ContextRegisteringExtension.class).build(); } @Test @@ -142,4 +146,21 @@ public void testResolveWithEmptySet() { assertNull(getCurrentBeanContainer().resolve(new HashSet>())); } + @Test + @SpecAssertion(section = BM_OBTAIN_CONTEXTS, id = "a") + public void testGetContexts() { + // test for scope without any impl, this should be empty set + Collection noImpl = getCurrentBeanContainer().getContexts(NoImplScope.class); + assertEquals(noImpl.size(), 0); + + // custom scope with two existing implementations + Collection customContextImpls = getCurrentBeanContainer().getContexts(CustomScoped.class); + assertEquals(customContextImpls.size(), 2); + + // test any built-in scope for completeness + // deliberately skips assertions on exact number of implementations as that can differ + Collection builtInContextImpls = getCurrentBeanContainer().getContexts(Singleton.class); + assertTrue(builtInContextImpls.size() >= 1); + } + } diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/ContextRegisteringExtension.java b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/ContextRegisteringExtension.java new file mode 100644 index 0000000000..f4e6aeccd5 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/ContextRegisteringExtension.java @@ -0,0 +1,14 @@ +package org.jboss.cdi.tck.tests.beanContainer; + +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.Discovery; +import jakarta.enterprise.inject.build.compatible.spi.MetaAnnotations; + +public class ContextRegisteringExtension implements BuildCompatibleExtension { + + @Discovery + public void discovery(MetaAnnotations metaAnnotations) { + metaAnnotations.addContext(CustomScoped.class, CustomContextImpl1.class); + metaAnnotations.addContext(CustomScoped.class, CustomContextImpl2.class); + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/CustomContextImpl1.java b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/CustomContextImpl1.java new file mode 100644 index 0000000000..70e2eb1fb2 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/CustomContextImpl1.java @@ -0,0 +1,35 @@ +package org.jboss.cdi.tck.tests.beanContainer; + +import jakarta.enterprise.context.spi.AlterableContext; +import jakarta.enterprise.context.spi.Contextual; +import jakarta.enterprise.context.spi.CreationalContext; + +import java.lang.annotation.Annotation; + +// dummy context, always active +public class CustomContextImpl1 implements AlterableContext { + + @Override + public void destroy(Contextual contextual) { + } + + @Override + public Class getScope() { + return CustomScoped.class; + } + + @Override + public T get(Contextual contextual, CreationalContext creationalContext) { + return null; + } + + @Override + public T get(Contextual contextual) { + return null; + } + + @Override + public boolean isActive() { + return true; + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/CustomContextImpl2.java b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/CustomContextImpl2.java new file mode 100644 index 0000000000..65f70e5c25 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/CustomContextImpl2.java @@ -0,0 +1,35 @@ +package org.jboss.cdi.tck.tests.beanContainer; + +import jakarta.enterprise.context.spi.AlterableContext; +import jakarta.enterprise.context.spi.Contextual; +import jakarta.enterprise.context.spi.CreationalContext; + +import java.lang.annotation.Annotation; + +// dummy context, never active +public class CustomContextImpl2 implements AlterableContext { + + @Override + public void destroy(Contextual contextual) { + } + + @Override + public Class getScope() { + return CustomScoped.class; + } + + @Override + public T get(Contextual contextual, CreationalContext creationalContext) { + return null; + } + + @Override + public T get(Contextual contextual) { + return null; + } + + @Override + public boolean isActive() { + return false; + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/CustomScoped.java b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/CustomScoped.java new file mode 100644 index 0000000000..f4144c0038 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/CustomScoped.java @@ -0,0 +1,19 @@ +package org.jboss.cdi.tck.tests.beanContainer; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import jakarta.enterprise.context.NormalScope; + +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@NormalScope +@Inherited +@Target({ TYPE, METHOD, FIELD }) +@Retention(RUNTIME) +public @interface CustomScoped { +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/NoImplScope.java b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/NoImplScope.java new file mode 100644 index 0000000000..8c76096c96 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/beanContainer/NoImplScope.java @@ -0,0 +1,20 @@ +package org.jboss.cdi.tck.tests.beanContainer; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import jakarta.enterprise.context.NormalScope; + +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +// This scope has no context implementing it +@NormalScope +@Inherited +@Target({ TYPE, METHOD, FIELD }) +@Retention(RUNTIME) +public @interface NoImplScope { +} diff --git a/impl/src/main/resources/tck-audit-cdi.xml b/impl/src/main/resources/tck-audit-cdi.xml index 561bd80d99..1cc7a847b3 100644 --- a/impl/src/main/resources/tck-audit-cdi.xml +++ b/impl/src/main/resources/tck-audit-cdi.xml @@ -6810,6 +6810,14 @@ +
+ + The method `BeanContainer.getContexts()` retrieves all context objects, active and inactive, + associated with the given scope, as defined in Section 2.5, "Scopes and contexts". + + +
+
The method |BeanManager.getELResolver()| returns the |jakarta.el.ELResolver| specified in Section 12.4, "Integration with Unified EL".