Skip to content

Commit

Permalink
Introduce tests for BeanContainer#getContexts
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Oct 13, 2023
1 parent 9827b95 commit ff3fd1b
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -142,4 +146,21 @@ public void testResolveWithEmptySet() {
assertNull(getCurrentBeanContainer().resolve(new HashSet<Bean<? extends String>>()));
}

@Test
@SpecAssertion(section = BM_OBTAIN_CONTEXTS, id = "a")
public void testGetContexts() {
// test for scope without any impl, this should be empty set
Collection<Context> noImpl = getCurrentBeanContainer().getContexts(NoImplScope.class);
assertEquals(noImpl.size(), 0);

// custom scope with two existing implementations
Collection<Context> 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<Context> builtInContextImpls = getCurrentBeanContainer().getContexts(Singleton.class);
assertTrue(builtInContextImpls.size() >= 1);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends Annotation> getScope() {
return CustomScoped.class;
}

@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
return null;
}

@Override
public <T> T get(Contextual<T> contextual) {
return null;
}

@Override
public boolean isActive() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends Annotation> getScope() {
return CustomScoped.class;
}

@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
return null;
}

@Override
public <T> T get(Contextual<T> contextual) {
return null;
}

@Override
public boolean isActive() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 {
}
8 changes: 8 additions & 0 deletions impl/src/main/resources/tck-audit-cdi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6810,6 +6810,14 @@
</assertion>
</section>

<section id="bm_obtain_contexts" title="Obtaining Contexts for a scope" level="3">
<assertion id="a">
<text>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".
</text>
</assertion>
</section>

<section id="bm_obtain_elresolver" title="Obtaining the ELResolver" level="3">
<assertion id="a">
<text>The method |BeanManager.getELResolver()| returns the |jakarta.el.ELResolver| specified in Section 12.4, "Integration with Unified EL".</text>
Expand Down

0 comments on commit ff3fd1b

Please sign in to comment.