From 3a6167de7ed5bfde9c505ec06493289ae4f150b9 Mon Sep 17 00:00:00 2001 From: Ladislav Thon Date: Mon, 29 Jan 2024 10:56:54 +0100 Subject: [PATCH] initial TCK tests for invoker lookups --- .../invokers/InvokerHolderExtensionBase.java | 13 +- .../ArgumentArraySizeWithLookupTest.java | 110 +++++++++++++++ .../lookup/ArgumentLookupBeanManagerTest.java | 132 ++++++++++++++++++ .../lookup/ArgumentLookupDependentTest.java | 112 +++++++++++++++ .../lookup/ArgumentLookupEventTest.java | 96 +++++++++++++ .../lookup/ArgumentLookupInstanceTest.java | 114 +++++++++++++++ .../invokers/lookup/ArgumentLookupTest.java | 110 +++++++++++++++ ...gumentLookupEqualToNumberOfParamsTest.java | 83 +++++++++++ ...ntLookupGreaterThanNumberOfParamsTest.java | 83 +++++++++++ .../BadArgumentLookupLessThanZeroTest.java | 83 +++++++++++ .../lookup/InstanceLookupDependentTest.java | 104 ++++++++++++++ .../InstanceLookupStaticMethodTest.java | 101 ++++++++++++++ .../invokers/lookup/InstanceLookupTest.java | 103 ++++++++++++++ .../invokers/lookup/LookupAmbiguousTest.java | 98 +++++++++++++ .../lookup/LookupUnsatisfiedTest.java | 81 +++++++++++ impl/src/main/resources/tck-audit-cdi.xml | 84 +++++++++++ 16 files changed, 1506 insertions(+), 1 deletion(-) create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentArraySizeWithLookupTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupBeanManagerTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupDependentTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupEventTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupInstanceTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupEqualToNumberOfParamsTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupGreaterThanNumberOfParamsTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupLessThanZeroTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupDependentTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupStaticMethodTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/LookupAmbiguousTest.java create mode 100644 impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/LookupUnsatisfiedTest.java diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/InvokerHolderExtensionBase.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/InvokerHolderExtensionBase.java index e627811b3..61880508f 100644 --- a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/InvokerHolderExtensionBase.java +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/InvokerHolderExtensionBase.java @@ -19,20 +19,31 @@ import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; import jakarta.enterprise.inject.build.compatible.spi.InvokerInfo; import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.invoke.InvokerBuilder; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; +import java.util.function.Consumer; public abstract class InvokerHolderExtensionBase { private final Map invokers = new LinkedHashMap<>(); protected final void registerInvokers(BeanInfo bean, InvokerFactory invokers, Set methods) { + registerInvokers(bean, invokers, methods, builder -> {}); + } + + protected final void registerInvokers(BeanInfo bean, InvokerFactory invokers, Set methods, + Consumer> action) { bean.declaringClass() .methods() .stream() .filter(it -> methods.contains(it.name())) - .forEach(it -> registerInvoker(it.name(), invokers.createInvoker(bean, it).build())); + .forEach(it -> { + InvokerBuilder builder = invokers.createInvoker(bean, it); + action.accept(builder); + registerInvoker(it.name(), builder.build()); + }); } protected final void registerInvoker(String id, InvokerInfo invoker) { diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentArraySizeWithLookupTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentArraySizeWithLookupTest.java new file mode 100644 index 000000000..f49711094 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentArraySizeWithLookupTest.java @@ -0,0 +1,110 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.Dependent; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.invoke.Invoker; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertThrows; + +@SpecVersion(spec = "cdi", version = "4.1") +public class ArgumentArraySizeWithLookupTest extends AbstractTest { + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(ArgumentArraySizeWithLookupTest.class) + .withClasses(MyService.class, MyDependency1.class, MyDependency2.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> builder + .withArgumentLookup(0) + .withArgumentLookup(1)); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "a") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "f") + public void test(MyService service, InvokerHolder invokers) throws Exception { + Invoker invoker = invokers.get("hello"); + assertThrows(RuntimeException.class, () -> { + invoker.invoke(service, null); + }); + assertThrows(RuntimeException.class, () -> { + invoker.invoke(service, new Object[]{}); + }); + assertThrows(RuntimeException.class, () -> { + invoker.invoke(service, new Object[]{null}); + }); + assertEquals("foobar_1_2", invoker.invoke(service, new Object[]{null, null})); + assertEquals("foobar_1_2", invoker.invoke(service, new Object[]{null, null, null})); + } + + @ApplicationScoped + static class MyService { + public String hello(MyDependency1 dependency1, MyDependency2 dependency2) { + return "foobar_" + dependency1 + "_" + dependency2; + } + } + + @Dependent + static class MyDependency1 { + @Override + public String toString() { + return "1"; + } + } + + @Dependent + static class MyDependency2 { + @Override + public String toString() { + return "2"; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupBeanManagerTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupBeanManagerTest.java new file mode 100644 index 000000000..39fe38550 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupBeanManagerTest.java @@ -0,0 +1,132 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.Dependent; +import jakarta.enterprise.event.Observes; +import jakarta.enterprise.inject.Instance; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.inject.spi.BeanManager; +import jakarta.enterprise.invoke.Invoker; +import jakarta.enterprise.util.TypeLiteral; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.ArrayList; +import java.util.List; +import java.util.Set; + +import static org.testng.Assert.assertEquals; + +@SpecVersion(spec = "cdi", version = "4.1") +public class ArgumentLookupBeanManagerTest extends AbstractTest { + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(ArgumentLookupBeanManagerTest.class) + .withClasses(MyService.class, MyDependency.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> builder.withArgumentLookup(0)); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "a") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "de") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "i") + public void test(MyService service, InvokerHolder invokers) throws Exception { + Invoker invoker = invokers.get("hello"); + assertEquals("foobar0", invoker.invoke(service, new Object[]{null})); + assertEquals("foobar1", invoker.invoke(service, new Object[]{null})); + + assertEquals(MyService.observed, List.of("foo", "bar", "foo", "bar")); + + assertEquals(2, MyDependency.CREATED); + assertEquals(2, MyDependency.DESTROYED); + } + + @ApplicationScoped + static class MyService { + static final List observed = new ArrayList<>(); + + public String hello(BeanManager beanManager) { + Instance instance = beanManager.createInstance(); + MyDependency dependency = instance.select(MyDependency.class).get(); + int id = dependency.getId(); + instance.destroy(dependency); + + beanManager.getEvent().select(new TypeLiteral>() {}).fire(List.of("foo", "bar")); + + return "foobar" + id; + } + + public void observe(@Observes List event) { + observed.addAll(event); + } + } + + @Dependent + static class MyDependency { + static int CREATED = 0; + + static int DESTROYED = 0; + + private int id; + + @PostConstruct + public void init() { + this.id = CREATED++; + } + + @PreDestroy + public void destroy() { + DESTROYED++; + } + + public int getId() { + return id; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupDependentTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupDependentTest.java new file mode 100644 index 000000000..80c7e10dc --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupDependentTest.java @@ -0,0 +1,112 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.Dependent; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.invoke.Invoker; +import jakarta.enterprise.invoke.InvokerBuilder; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +import static org.testng.Assert.assertEquals; + +@SpecVersion(spec = "cdi", version = "4.1") +public class ArgumentLookupDependentTest extends AbstractTest { + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(ArgumentLookupDependentTest.class) + .withClasses(MyService.class, MyDependency.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> builder.withArgumentLookup(0)); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "a") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "db") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "i") + public void test(MyService service, InvokerHolder invokers) throws Exception { + Invoker invoker = invokers.get("hello"); + assertEquals("foobar0", invoker.invoke(service, new Object[]{null})); + assertEquals("foobar1", invoker.invoke(service, new Object[]{null})); + assertEquals("foobar2", invoker.invoke(service, new Object[]{null})); + assertEquals(3, MyDependency.CREATED); + assertEquals(3, MyDependency.DESTROYED); + } + + @ApplicationScoped + static class MyService { + public String hello(MyDependency dependency) { + return "foobar" + dependency.getId(); + } + } + + @Dependent + static class MyDependency { + static int CREATED = 0; + + static int DESTROYED = 0; + + private int id; + + @PostConstruct + public void init() { + this.id = CREATED++; + } + + @PreDestroy + public void destroy() { + DESTROYED++; + } + + public int getId() { + return id; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupEventTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupEventTest.java new file mode 100644 index 000000000..65692d270 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupEventTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.Dependent; +import jakarta.enterprise.event.Event; +import jakarta.enterprise.event.Observes; +import jakarta.enterprise.inject.Instance; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.invoke.Invoker; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.ArrayList; +import java.util.List; +import java.util.Set; + +import static org.testng.Assert.assertEquals; + +@SpecVersion(spec = "cdi", version = "4.1") +public class ArgumentLookupEventTest extends AbstractTest { + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(ArgumentLookupEventTest.class) + .withClasses(MyService.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> builder.withArgumentLookup(0)); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "a") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "dd") + public void test(MyService service, InvokerHolder invokers) throws Exception { + Invoker invoker = invokers.get("hello"); + invoker.invoke(service, new Object[]{null}); + invoker.invoke(service, new Object[]{null}); + assertEquals(MyService.observed, List.of("foo", "bar", "foo", "bar")); + } + + @ApplicationScoped + static class MyService { + static final List observed = new ArrayList<>(); + + public void hello(Event> event) { + event.fire(List.of("foo", "bar")); + } + + public void observe(@Observes List event) { + observed.addAll(event); + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupInstanceTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupInstanceTest.java new file mode 100644 index 000000000..e85682b42 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupInstanceTest.java @@ -0,0 +1,114 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.Dependent; +import jakarta.enterprise.inject.Instance; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.invoke.Invoker; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +import static org.testng.Assert.assertEquals; + +@SpecVersion(spec = "cdi", version = "4.1") +public class ArgumentLookupInstanceTest extends AbstractTest { + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(ArgumentLookupInstanceTest.class) + .withClasses(MyService.class, MyDependency.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> builder.withArgumentLookup(0)); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "a") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "dc") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "i") + public void test(MyService service, InvokerHolder invokers) throws Exception { + Invoker invoker = invokers.get("hello"); + assertEquals("foobar0_MyDependency", invoker.invoke(service, new Object[]{null})); + assertEquals("foobar1_MyDependency", invoker.invoke(service, new Object[]{null})); + assertEquals("foobar2_MyDependency", invoker.invoke(service, new Object[]{null})); + assertEquals(3, MyDependency.CREATED); + assertEquals(3, MyDependency.DESTROYED); + } + + @ApplicationScoped + static class MyService { + public String hello(Instance instance) { + String beanClass = instance.getHandle().getBean().getBeanClass().getSimpleName(); + MyDependency dependency = instance.get(); + return "foobar" + dependency.getId() + "_" + beanClass; + } + } + + @Dependent + static class MyDependency { + static int CREATED = 0; + + static int DESTROYED = 0; + + private int id; + + @PostConstruct + public void init() { + this.id = CREATED++; + } + + @PreDestroy + public void destroy() { + DESTROYED++; + } + + public int getId() { + return id; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupTest.java new file mode 100644 index 000000000..7e3e8f2d7 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/ArgumentLookupTest.java @@ -0,0 +1,110 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.invoke.Invoker; +import jakarta.enterprise.invoke.InvokerBuilder; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +import static org.testng.Assert.assertEquals; + +@SpecVersion(spec = "cdi", version = "4.1") +public class ArgumentLookupTest extends AbstractTest { + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(ArgumentLookupTest.class) + .withClasses(MyService.class, MyDependency.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> builder.withArgumentLookup(0)); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "a") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "da") + public void test(MyService service, InvokerHolder invokers) throws Exception { + Invoker invoker = invokers.get("hello"); + assertEquals("foobar0", invoker.invoke(service, new Object[]{null})); + assertEquals("foobar0", invoker.invoke(service, new Object[]{null})); + assertEquals("foobar0", invoker.invoke(service, new Object[]{null})); + assertEquals(1, MyDependency.CREATED); + assertEquals(0, MyDependency.DESTROYED); + } + + @ApplicationScoped + static class MyService { + public String hello(MyDependency dependency) { + return "foobar" + dependency.getId(); + } + } + + @ApplicationScoped + static class MyDependency { + static int CREATED = 0; + + static int DESTROYED = 0; + + private int id; + + @PostConstruct + public void init() { + this.id = CREATED++; + } + + @PreDestroy + public void destroy() { + DESTROYED++; + } + + public int getId() { + return id; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupEqualToNumberOfParamsTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupEqualToNumberOfParamsTest.java new file mode 100644 index 000000000..05c5fef7c --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupEqualToNumberOfParamsTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Messages; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.ShouldThrowException; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +@SpecVersion(spec = "cdi", version = "4.1") +public class BadArgumentLookupEqualToNumberOfParamsTest extends AbstractTest { + @Deployment + @ShouldThrowException(IllegalArgumentException.class) + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(BadArgumentLookupEqualToNumberOfParamsTest.class) + .withClasses(MyService.class, MyDependency.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers, Messages msg) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> { + try { + builder.withArgumentLookup(1); + } catch (IllegalArgumentException expected) { + msg.error(expected); + } + }); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "ec") + public void trigger() { + } + + @ApplicationScoped + static class MyService { + public String hello(MyDependency dependency) { + return "foobar" + dependency.getId(); + } + } + + @ApplicationScoped + static class MyDependency { + public int getId() { + return 0; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupGreaterThanNumberOfParamsTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupGreaterThanNumberOfParamsTest.java new file mode 100644 index 000000000..499e0d0a9 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupGreaterThanNumberOfParamsTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Messages; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.ShouldThrowException; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +@SpecVersion(spec = "cdi", version = "4.1") +public class BadArgumentLookupGreaterThanNumberOfParamsTest extends AbstractTest { + @Deployment + @ShouldThrowException(IllegalArgumentException.class) + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(BadArgumentLookupGreaterThanNumberOfParamsTest.class) + .withClasses(MyService.class, MyDependency.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers, Messages msg) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> { + try { + builder.withArgumentLookup(2); + } catch (IllegalArgumentException expected) { + msg.error(expected); + } + }); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "eb") + public void trigger() { + } + + @ApplicationScoped + static class MyService { + public String hello(MyDependency dependency) { + return "foobar" + dependency.getId(); + } + } + + @ApplicationScoped + static class MyDependency { + public int getId() { + return 0; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupLessThanZeroTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupLessThanZeroTest.java new file mode 100644 index 000000000..37f312600 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/BadArgumentLookupLessThanZeroTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Messages; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.ShouldThrowException; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +@SpecVersion(spec = "cdi", version = "4.1") +public class BadArgumentLookupLessThanZeroTest extends AbstractTest { + @Deployment + @ShouldThrowException(IllegalArgumentException.class) + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(BadArgumentLookupLessThanZeroTest.class) + .withClasses(MyService.class, MyDependency.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers, Messages msg) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> { + try { + builder.withArgumentLookup(-1); + } catch (IllegalArgumentException expected) { + msg.error(expected); + } + }); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "ea") + public void trigger() { + } + + @ApplicationScoped + static class MyService { + public String hello(MyDependency dependency) { + return "foobar" + dependency.getId(); + } + } + + @ApplicationScoped + static class MyDependency { + public int getId() { + return 0; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupDependentTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupDependentTest.java new file mode 100644 index 000000000..2e5b06b87 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupDependentTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.Dependent; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.invoke.Invoker; +import jakarta.enterprise.invoke.InvokerBuilder; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +import static org.testng.Assert.assertEquals; + +@SpecVersion(spec = "cdi", version = "4.1") +public class InstanceLookupDependentTest extends AbstractTest { + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(InstanceLookupDependentTest.class) + .withClasses(MyService.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), InvokerBuilder::withInstanceLookup); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "a") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "bb") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "i") + public void test(InvokerHolder invokers) throws Exception { + Invoker hello = invokers.get("hello"); + assertEquals("foobar0", hello.invoke(null, null)); + assertEquals("foobar1", hello.invoke(null, null)); + assertEquals("foobar2", hello.invoke(null, null)); + assertEquals(3, MyService.CREATED); + assertEquals(3, MyService.DESTROYED); + } + + @Dependent + static class MyService { + static int CREATED = 0; + + static int DESTROYED = 0; + + private int id; + + @PostConstruct + public void init() { + this.id = CREATED++; + } + + @PreDestroy + public void destroy() { + DESTROYED++; + } + + public String hello() { + return "foobar" + id; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupStaticMethodTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupStaticMethodTest.java new file mode 100644 index 000000000..911c412f1 --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupStaticMethodTest.java @@ -0,0 +1,101 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.invoke.Invoker; +import jakarta.enterprise.invoke.InvokerBuilder; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +import static org.testng.Assert.assertEquals; + +@SpecVersion(spec = "cdi", version = "4.1") +public class InstanceLookupStaticMethodTest extends AbstractTest { + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(InstanceLookupStaticMethodTest.class) + .withClasses(MyService.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), InvokerBuilder::withInstanceLookup); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "a") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "c") + public void test(InvokerHolder invokers) throws Exception { + Invoker hello = invokers.get("hello"); + assertEquals("foobar", hello.invoke(null, null)); + assertEquals("foobar", hello.invoke(null, null)); + assertEquals("foobar", hello.invoke(null, null)); + assertEquals(0, MyService.CREATED); + assertEquals(0, MyService.DESTROYED); + } + + @ApplicationScoped + static class MyService { + static int CREATED = 0; + + static int DESTROYED = 0; + + @PostConstruct + public void init() { + CREATED++; + } + + @PreDestroy + public void destroy() { + DESTROYED++; + } + + public static String hello() { + return "foobar"; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupTest.java new file mode 100644 index 000000000..12a45d37b --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/InstanceLookupTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.invoke.Invoker; +import jakarta.enterprise.invoke.InvokerBuilder; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +import static org.testng.Assert.assertEquals; + +@SpecVersion(spec = "cdi", version = "4.1") +public class InstanceLookupTest extends AbstractTest { + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(InstanceLookupTest.class) + .withClasses(MyService.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), InvokerBuilder::withInstanceLookup); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "a") + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "ba") + public void test(InvokerHolder invokers) throws Exception { + Invoker hello = invokers.get("hello"); + assertEquals("foobar0", hello.invoke(null, null)); + assertEquals("foobar0", hello.invoke(null, null)); + assertEquals("foobar0", hello.invoke(null, null)); + assertEquals(1, MyService.CREATED); + assertEquals(0, MyService.DESTROYED); + } + + @ApplicationScoped + static class MyService { + static int CREATED = 0; + + static int DESTROYED = 0; + + private int id; + + @PostConstruct + public void init() { + this.id = CREATED++; + } + + @PreDestroy + public void destroy() { + DESTROYED++; + } + + public String hello() { + return "foobar" + id; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/LookupAmbiguousTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/LookupAmbiguousTest.java new file mode 100644 index 000000000..cbd5c477a --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/LookupAmbiguousTest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.inject.spi.DeploymentException; +import jakarta.inject.Singleton; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.ShouldThrowException; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +@SpecVersion(spec = "cdi", version = "4.1") +public class LookupAmbiguousTest extends AbstractTest { + @Deployment + @ShouldThrowException(DeploymentException.class) + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(LookupAmbiguousTest.class) + .withClasses(MyService.class, MyDependency.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> builder.withArgumentLookup(0)); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "gb") + public void trigger() { + } + + @ApplicationScoped + static class MyService { + public String hello(MyDependency dependency) { + return "foobar" + dependency.getId(); + } + } + + interface MyDependency { + int getId(); + } + + @ApplicationScoped + static class MyDependency1 implements MyDependency { + @Override + public int getId() { + return 1; + } + } + + @ApplicationScoped + static class MyDependency2 implements MyDependency { + @Override + public int getId() { + return 2; + } + } +} diff --git a/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/LookupUnsatisfiedTest.java b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/LookupUnsatisfiedTest.java new file mode 100644 index 000000000..654423b0f --- /dev/null +++ b/impl/src/main/java/org/jboss/cdi/tck/tests/invokers/lookup/LookupUnsatisfiedTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2024, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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 org.jboss.cdi.tck.tests.invokers.lookup; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.build.compatible.spi.BeanInfo; +import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; +import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory; +import jakarta.enterprise.inject.build.compatible.spi.Registration; +import jakarta.enterprise.inject.build.compatible.spi.Synthesis; +import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; +import jakarta.enterprise.inject.spi.DeploymentException; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.ShouldThrowException; +import org.jboss.cdi.tck.AbstractTest; +import org.jboss.cdi.tck.cdi.Sections; +import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolder; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderCreator; +import org.jboss.cdi.tck.tests.invokers.InvokerHolderExtensionBase; +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.util.Set; + +@SpecVersion(spec = "cdi", version = "4.1") +public class LookupUnsatisfiedTest extends AbstractTest { + @Deployment + @ShouldThrowException(DeploymentException.class) + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass(LookupUnsatisfiedTest.class) + .withClasses(MyService.class, MyDependency.class) + .withBuildCompatibleExtension(TestExtension.class) + .withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class) + .build(); + } + + public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension { + @Registration(types = MyService.class) + public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) { + registerInvokers(bean, invokers, Set.of("hello"), builder -> builder.withArgumentLookup(0)); + } + + @Synthesis + public void synthesis(SyntheticComponents syn) { + synthesizeInvokerHolder(syn); + } + } + + @Test + @SpecAssertion(section = Sections.CONFIGURING_LOOKUPS, id = "ga") + public void trigger() { + } + + @ApplicationScoped + static class MyService { + public String hello(MyDependency dependency) { + return "foobar" + dependency.getId(); + } + } + + interface MyDependency { + int getId(); + } +} diff --git a/impl/src/main/resources/tck-audit-cdi.xml b/impl/src/main/resources/tck-audit-cdi.xml index 2c60e0cf6..20f3230e5 100644 --- a/impl/src/main/resources/tck-audit-cdi.xml +++ b/impl/src/main/resources/tck-audit-cdi.xml @@ -6048,6 +6048,90 @@ +
+ + The |InvokerBuilder| allows specifying that the |instance| or any of the |arguments| passed to |Invoker.invoke()| should be ignored and a value should be looked up from the CDI container instead. + + + When |withInstanceLookup()| is called on an invoker builder and the target method is not |static|, the |invoke()| method of the built invoker shall ignore the |instance| argument and instead obtain and use a contextual reference for the target bean and the bean type that declares the target method. + + Test with target bean that is |@ApplicationScoped| + + + Test with target bean that is |@Dependent| + + + + Calling |withInstanceLookup()| on an invoker builder for a |static| target method has no effect. + + + + When `withArgumentLookup()` is called on an invoker builder, the `invoke()` method of the built invoker shall ignore the given element of the `arguments` array and instead: + + 1. identify a bean according to the rules of typesafe resolution, as defined in `Performing typesafe resolution`, where the required type is the declared type of the corresponding parameter of the target method and the required qualifiers are all qualifiers present on the parameter, resolving ambiguities according to `Unsatisfied and ambiguous dependencies`; + 2. obtain and use a contextual reference for the identified bean and the declared type of the parameter. + + + Test with parameter that resolves to an |@ApplicationScoped| bean + + + Test with parameter that resolves to a |@Dependent| bean + + + Test with parameter that resolves to an |Instance| bean + + + Test with parameter that resolves to an |Event| bean + + + Test with parameter that resolves to a |BeanManager| bean + + + + Calling |withArgumentLookup()| with |position| less than 0 or greater than or equal to the number of parameters of the target method leads to an |IllegalArgumentException|. + + Test for position less than zero + + + Test for position greater than the number of parameters + + + Test for position equal to the number of parameters + + + + + Configuring a lookup using |withInstanceLookup()| or |withArgumentLookup()| does not relax the requirements defined in `Behavior of |invoke()|`. + Notably, the |arguments| array must still have an element for each argument, regardless of whether a lookup was configured for it. + This means that for a target method with N parameters, the |arguments| array must always have at least N elements. + + + + + During deployment validation, implementations are required to identify all looked up beans for all built invokers, as described above. + It is a deployment problem if an attempt to identify a looked up bean results in an unsatisfied dependency or an ambiguous dependency that is not resolvable. + + + Test for unsatisfied lookup + + + Test for unresolvable ambiguous lookup + + + + Implementations are permitted to remember the identified beans and not repeat the resolution process for each invocation of `Invoker.invoke()`. + + + All instances of `@Dependent` looked up beans obtained during `Invoker.invoke()` are destroyed before the `invoke()` method returns or throws. + + + The order in which the instances of |@Dependent| looked up beans are destroyed is not specified. + + + The order in which instances of looked up beans are obtained during |Invoker.invoke()| in not specified. + +
+