From ac37374fe0b44644b1415dd75b71e6191140913f Mon Sep 17 00:00:00 2001 From: Matej Novotny Date: Thu, 4 Jan 2024 15:20:19 +0100 Subject: [PATCH] Test obtaining interceptor bindings along with interception factory --- ...torBindingsWithInterceptorFactoryTest.java | 49 +++++++++++++++++++ .../invocationContext/ProducerBean.java | 24 +++++++++ .../contract/invocationContext/Product.java | 12 +++++ .../ProductInterceptor1.java | 28 +++++++++++ .../ProductInterceptor2.java | 28 +++++++++++ .../ProductInterceptorBinding1.java | 25 ++++++++++ .../ProductInterceptorBinding2.java | 25 ++++++++++ .../ProductInterceptorBinding3.java | 25 ++++++++++ 8 files changed, 216 insertions(+) create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/InterceptorBindingsWithInterceptorFactoryTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProducerBean.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/Product.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptor1.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptor2.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding1.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding2.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding3.java diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/InterceptorBindingsWithInterceptorFactoryTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/InterceptorBindingsWithInterceptorFactoryTest.java new file mode 100644 index 000000000..a2e216847 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/InterceptorBindingsWithInterceptorFactoryTest.java @@ -0,0 +1,49 @@ +package org.jboss.cdi.tck.tests.full.interceptors.contract.invocationContext; + +import static org.jboss.cdi.tck.TestGroups.CDI_FULL; +import static org.jboss.cdi.tck.interceptors.InterceptorsSections.INVOCATIONCONTEXT; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.test.audit.annotations.SpecAssertion; +import org.jboss.test.audit.annotations.SpecVersion; +import org.testng.annotations.Test; + +import java.lang.annotation.Annotation; +import java.util.Set; + +@SpecVersion(spec = "interceptors", version = "2.2") +@Test(groups = CDI_FULL) +public class InterceptorBindingsWithInterceptorFactoryTest extends AbstractTest { + + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder().withTestClassPackage(InterceptorBindingsWithInterceptorFactoryTest.class).build(); + } + + @Test + @SpecAssertion(section = INVOCATIONCONTEXT, id = "n") + public void testInterceptorBindingsAppliedViaInterceptorFactory() { + Product product = getContextualReference(Product.class); + assertEquals(product.ping(), 42); + assertEquals(product.pong(), 42); + + Set interceptor1Bindings = ProductInterceptor1.getAllBindings(); + assertNotNull(interceptor1Bindings); + assertEquals(interceptor1Bindings.size(), 2); + assertTrue(interceptor1Bindings.contains(ProductInterceptorBinding1.Literal.INSTANCE)); + assertTrue(interceptor1Bindings.contains(ProductInterceptorBinding2.Literal.INSTANCE)); + + Set interceptor2Bindings = ProductInterceptor2.getAllBindings(); + assertNotNull(interceptor2Bindings); + assertEquals(interceptor2Bindings.size(), 2); + assertTrue(interceptor2Bindings.contains(ProductInterceptorBinding1.Literal.INSTANCE)); + assertTrue(interceptor2Bindings.contains(ProductInterceptorBinding3.Literal.INSTANCE)); + + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProducerBean.java b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProducerBean.java new file mode 100644 index 000000000..fd352dae4 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProducerBean.java @@ -0,0 +1,24 @@ +package org.jboss.cdi.tck.tests.full.interceptors.contract.invocationContext; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Produces; +import jakarta.enterprise.inject.spi.InterceptionFactory; +import jakarta.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator; + +@ApplicationScoped +public class ProducerBean { + + @Produces + @ApplicationScoped + public Product createProduct(InterceptionFactory interceptionFactory) { + AnnotatedTypeConfigurator configurator = interceptionFactory.configure(); + configurator.add(ProductInterceptorBinding1.Literal.INSTANCE) + .filterMethods(m -> m.getJavaMember().getName().equals("ping")) + .findFirst().get() + .add(ProductInterceptorBinding2.Literal.INSTANCE); + configurator.filterMethods(m -> m.getJavaMember().getName().equals("pong")) + .findFirst().get() + .add(ProductInterceptorBinding3.Literal.INSTANCE); + return interceptionFactory.createInterceptedInstance(new Product()); + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/Product.java b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/Product.java new file mode 100644 index 000000000..f43976fc1 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/Product.java @@ -0,0 +1,12 @@ +package org.jboss.cdi.tck.tests.full.interceptors.contract.invocationContext; + +public class Product { + + public int ping() { + return 41; + } + + public int pong() { + return 41; + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptor1.java b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptor1.java new file mode 100644 index 000000000..a2d8c1ba6 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptor1.java @@ -0,0 +1,28 @@ +package org.jboss.cdi.tck.tests.full.interceptors.contract.invocationContext; + +import jakarta.annotation.Priority; +import jakarta.interceptor.AroundInvoke; +import jakarta.interceptor.Interceptor; +import jakarta.interceptor.InvocationContext; + +import java.lang.annotation.Annotation; +import java.util.Set; + +@Priority(Interceptor.Priority.APPLICATION + 400) +@ProductInterceptorBinding2 +@Interceptor +public class ProductInterceptor1 { + + private static Set allBindings; + + @AroundInvoke + public Object aroundInvoke(InvocationContext invocationContext) throws Exception { + allBindings = invocationContext.getInterceptorBindings(); + return (1 + (Integer)invocationContext.proceed()); + } + + public static Set getAllBindings() { + return allBindings; + } + +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptor2.java b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptor2.java new file mode 100644 index 000000000..e6af52ca8 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptor2.java @@ -0,0 +1,28 @@ +package org.jboss.cdi.tck.tests.full.interceptors.contract.invocationContext; + +import jakarta.annotation.Priority; +import jakarta.interceptor.AroundInvoke; +import jakarta.interceptor.Interceptor; +import jakarta.interceptor.InvocationContext; + +import java.lang.annotation.Annotation; +import java.util.Set; + +@Priority(Interceptor.Priority.APPLICATION + 400) +@ProductInterceptorBinding3 +@Interceptor +public class ProductInterceptor2 { + + private static Set allBindings; + + @AroundInvoke + public Object aroundInvoke(InvocationContext invocationContext) throws Exception { + allBindings = invocationContext.getInterceptorBindings(); + return (1 + (Integer)invocationContext.proceed()); + } + + public static Set getAllBindings() { + return allBindings; + } + +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding1.java b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding1.java new file mode 100644 index 000000000..b06b1b0cc --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding1.java @@ -0,0 +1,25 @@ +package org.jboss.cdi.tck.tests.full.interceptors.contract.invocationContext; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import jakarta.enterprise.util.AnnotationLiteral; +import jakarta.interceptor.InterceptorBinding; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Target({TYPE, METHOD}) +@Retention(RUNTIME) +@Documented +@InterceptorBinding +public @interface ProductInterceptorBinding1 { + + class Literal extends AnnotationLiteral implements ProductInterceptorBinding1 { + + public static Literal INSTANCE = new Literal(); + + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding2.java b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding2.java new file mode 100644 index 000000000..416d3af71 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding2.java @@ -0,0 +1,25 @@ +package org.jboss.cdi.tck.tests.full.interceptors.contract.invocationContext; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import jakarta.enterprise.util.AnnotationLiteral; +import jakarta.interceptor.InterceptorBinding; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Target({ TYPE, METHOD }) +@Retention(RUNTIME) +@Documented +@InterceptorBinding +public @interface ProductInterceptorBinding2 { + + class Literal extends AnnotationLiteral implements ProductInterceptorBinding2 { + + public static Literal INSTANCE = new Literal(); + + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding3.java b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding3.java new file mode 100644 index 000000000..f4149d449 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/full/interceptors/contract/invocationContext/ProductInterceptorBinding3.java @@ -0,0 +1,25 @@ +package org.jboss.cdi.tck.tests.full.interceptors.contract.invocationContext; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import jakarta.enterprise.util.AnnotationLiteral; +import jakarta.interceptor.InterceptorBinding; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Target({ TYPE, METHOD }) +@Retention(RUNTIME) +@Documented +@InterceptorBinding +public @interface ProductInterceptorBinding3 { + + class Literal extends AnnotationLiteral implements ProductInterceptorBinding3 { + + public static Literal INSTANCE = new Literal(); + + } +}