Skip to content

Commit 0bc29f8

Browse files
authored
initial TCK tests for method invokers (#502)
* initial TCK tests for method invokers * initial TCK tests for invoker lookups * synchronize CDI TCK audit XML in 'web' from 'impl'
1 parent 56b9d96 commit 0bc29f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4950
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2024, Red Hat, Inc., and individual contributors
3+
* by the @authors tag. See the copyright.txt in the distribution for a
4+
* full listing of individual contributors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.cdi.tck.tests.full.invokers;
17+
18+
import jakarta.enterprise.context.ApplicationScoped;
19+
import jakarta.enterprise.event.Observes;
20+
import jakarta.enterprise.inject.spi.Extension;
21+
import jakarta.enterprise.inject.spi.ProcessManagedBean;
22+
import jakarta.enterprise.invoke.Invoker;
23+
import org.jboss.arquillian.container.test.api.Deployment;
24+
import org.jboss.cdi.tck.AbstractTest;
25+
import org.jboss.cdi.tck.cdi.Sections;
26+
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder;
27+
import org.jboss.shrinkwrap.api.spec.WebArchive;
28+
import org.jboss.test.audit.annotations.SpecAssertion;
29+
import org.jboss.test.audit.annotations.SpecVersion;
30+
import org.testng.annotations.Test;
31+
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
import static org.jboss.cdi.tck.TestGroups.CDI_FULL;
37+
import static org.testng.Assert.assertEquals;
38+
39+
@SpecVersion(spec = "cdi", version = "4.1")
40+
@Test(groups = CDI_FULL)
41+
public class SimpleInvokerTest extends AbstractTest {
42+
@Deployment
43+
public static WebArchive createTestArchive() {
44+
return new WebArchiveBuilder()
45+
.withTestClass(SimpleInvokerTest.class)
46+
.withClasses(MyService.class)
47+
.withExtension(TestExtension.class)
48+
.build();
49+
}
50+
51+
public static class TestExtension implements Extension {
52+
private Map<String, Invoker<?, ?>> invokers = new HashMap<>();
53+
54+
public void myServiceRegistration(@Observes ProcessManagedBean<MyService> pmb) {
55+
pmb.getAnnotatedBeanClass()
56+
.getMethods()
57+
.stream()
58+
.filter(it -> "hello".equals(it.getJavaMember().getName()))
59+
.forEach(it -> invokers.put(it.getJavaMember().getName(), pmb.createInvoker(it).build()));
60+
}
61+
62+
public <T, R> Invoker<T, R> getInvoker(String name) {
63+
return (Invoker<T, R>) invokers.get(name);
64+
}
65+
}
66+
67+
@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
68+
@SpecAssertion(section = Sections.METHOD_INVOKERS_FULL, id = "a")
69+
public void test(MyService service) throws Exception {
70+
Invoker<MyService, String> hello = getCurrentManager().getExtension(TestExtension.class).getInvoker("hello");
71+
assertEquals(hello.invoke(service, new Object[]{1, List.of()}), "foobar1[]");
72+
}
73+
74+
@ApplicationScoped
75+
public static class MyService {
76+
public String hello(int param1, List<String> param2) {
77+
return "foobar" + param1 + param2;
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2024, Red Hat, Inc., and individual contributors
3+
* by the @authors tag. See the copyright.txt in the distribution for a
4+
* full listing of individual contributors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.cdi.tck.tests.full.invokers.invalid;
17+
18+
import jakarta.annotation.Priority;
19+
import jakarta.decorator.Decorator;
20+
import jakarta.decorator.Delegate;
21+
import jakarta.enterprise.event.Observes;
22+
import jakarta.enterprise.inject.spi.DeploymentException;
23+
import jakarta.enterprise.inject.spi.Extension;
24+
import jakarta.enterprise.inject.spi.ProcessManagedBean;
25+
import jakarta.inject.Inject;
26+
import org.jboss.arquillian.container.test.api.Deployment;
27+
import org.jboss.arquillian.container.test.api.ShouldThrowException;
28+
import org.jboss.cdi.tck.AbstractTest;
29+
import org.jboss.cdi.tck.cdi.Sections;
30+
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder;
31+
import org.jboss.shrinkwrap.api.spec.WebArchive;
32+
import org.jboss.test.audit.annotations.SpecAssertion;
33+
import org.jboss.test.audit.annotations.SpecVersion;
34+
import org.testng.annotations.Test;
35+
36+
import static org.jboss.cdi.tck.TestGroups.CDI_FULL;
37+
38+
@SpecVersion(spec = "cdi", version = "4.1")
39+
@Test(groups = CDI_FULL)
40+
public class DecoratorInvokerTest extends AbstractTest {
41+
@Deployment
42+
@ShouldThrowException(DeploymentException.class)
43+
public static WebArchive createTestArchive() {
44+
return new WebArchiveBuilder()
45+
.withTestClass(DecoratorInvokerTest.class)
46+
.withClasses(MyService.class, MyDecorator.class)
47+
.withExtension(TestExtension.class)
48+
.build();
49+
}
50+
51+
public static class TestExtension implements Extension {
52+
public void myDecoratorRegistration(@Observes ProcessManagedBean<MyDecorator> pmb) {
53+
pmb.getAnnotatedBeanClass()
54+
.getMethods()
55+
.stream()
56+
.filter(it -> "hello".equals(it.getJavaMember().getName()))
57+
.forEach(it -> pmb.createInvoker(it).build());
58+
}
59+
}
60+
61+
@Test
62+
@SpecAssertion(section = Sections.BUILDING_INVOKER_FULL, id = "a")
63+
public void trigger() {
64+
}
65+
66+
public interface MyService {
67+
String hello();
68+
}
69+
70+
@Decorator
71+
@Priority(1)
72+
public static class MyDecorator implements MyService {
73+
@Inject
74+
@Delegate
75+
MyService delegate;
76+
77+
@Override
78+
public String hello() {
79+
return "decorated: " + delegate.hello();
80+
}
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2024, Red Hat, Inc., and individual contributors
3+
* by the @authors tag. See the copyright.txt in the distribution for a
4+
* full listing of individual contributors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.cdi.tck.tests.invokers;
17+
18+
import jakarta.enterprise.invoke.Invoker;
19+
20+
import java.util.Map;
21+
22+
import static org.testng.Assert.assertNotNull;
23+
24+
public class InvokerHolder {
25+
private final Map<String, Invoker<?, ?>> invokers;
26+
27+
public InvokerHolder(Map<String, Invoker<?, ?>> invokers) {
28+
this.invokers = invokers;
29+
}
30+
31+
public <T, R> Invoker<T, R> get(String id) {
32+
Invoker<T, R> result = (Invoker<T, R>) invokers.get(id);
33+
assertNotNull(result);
34+
return result;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024, Red Hat, Inc., and individual contributors
3+
* by the @authors tag. See the copyright.txt in the distribution for a
4+
* full listing of individual contributors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.cdi.tck.tests.invokers;
17+
18+
import jakarta.enterprise.inject.Instance;
19+
import jakarta.enterprise.inject.build.compatible.spi.Parameters;
20+
import jakarta.enterprise.inject.build.compatible.spi.SyntheticBeanCreator;
21+
import jakarta.enterprise.invoke.Invoker;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
public class InvokerHolderCreator implements SyntheticBeanCreator<InvokerHolder> {
27+
@Override
28+
public InvokerHolder create(Instance<Object> lookup, Parameters params) {
29+
String[] names = params.get("names", String[].class);
30+
Invoker<?, ?>[] invokers = params.get("invokers", Invoker[].class);
31+
Map<String, Invoker<?, ?>> map = new HashMap<>();
32+
for (int i = 0; i < names.length; i++) {
33+
map.put(names[i], invokers[i]);
34+
}
35+
return new InvokerHolder(map);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2024, Red Hat, Inc., and individual contributors
3+
* by the @authors tag. See the copyright.txt in the distribution for a
4+
* full listing of individual contributors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.cdi.tck.tests.invokers;
17+
18+
import jakarta.enterprise.inject.build.compatible.spi.BeanInfo;
19+
import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory;
20+
import jakarta.enterprise.inject.build.compatible.spi.InvokerInfo;
21+
import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents;
22+
import jakarta.enterprise.invoke.InvokerBuilder;
23+
24+
import java.util.LinkedHashMap;
25+
import java.util.Map;
26+
import java.util.Set;
27+
import java.util.function.Consumer;
28+
29+
public abstract class InvokerHolderExtensionBase {
30+
private final Map<String, InvokerInfo> invokers = new LinkedHashMap<>();
31+
32+
protected final void registerInvokers(BeanInfo bean, InvokerFactory invokers, Set<String> methods) {
33+
registerInvokers(bean, invokers, methods, builder -> {});
34+
}
35+
36+
protected final void registerInvokers(BeanInfo bean, InvokerFactory invokers, Set<String> methods,
37+
Consumer<InvokerBuilder<?>> action) {
38+
bean.declaringClass()
39+
.methods()
40+
.stream()
41+
.filter(it -> methods.contains(it.name()))
42+
.forEach(it -> {
43+
InvokerBuilder<InvokerInfo> builder = invokers.createInvoker(bean, it);
44+
action.accept(builder);
45+
registerInvoker(it.name(), builder.build());
46+
});
47+
}
48+
49+
protected final void registerInvoker(String id, InvokerInfo invoker) {
50+
invokers.put(id, invoker);
51+
}
52+
53+
protected final void synthesizeInvokerHolder(SyntheticComponents syn) {
54+
syn.addBean(InvokerHolder.class)
55+
.type(InvokerHolder.class)
56+
.withParam("names", invokers.keySet().toArray(String[]::new))
57+
.withParam("invokers", invokers.values().toArray(InvokerInfo[]::new))
58+
.createWith(InvokerHolderCreator.class);
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2024, Red Hat, Inc., and individual contributors
3+
* by the @authors tag. See the copyright.txt in the distribution for a
4+
* full listing of individual contributors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.cdi.tck.tests.invokers;
17+
18+
import jakarta.enterprise.context.ApplicationScoped;
19+
import jakarta.enterprise.inject.build.compatible.spi.BeanInfo;
20+
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
21+
import jakarta.enterprise.inject.build.compatible.spi.InvokerFactory;
22+
import jakarta.enterprise.inject.build.compatible.spi.Registration;
23+
import jakarta.enterprise.inject.build.compatible.spi.Synthesis;
24+
import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents;
25+
import jakarta.enterprise.invoke.Invoker;
26+
import org.jboss.arquillian.container.test.api.Deployment;
27+
import org.jboss.cdi.tck.AbstractTest;
28+
import org.jboss.cdi.tck.cdi.Sections;
29+
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder;
30+
import org.jboss.shrinkwrap.api.spec.WebArchive;
31+
import org.jboss.test.audit.annotations.SpecAssertion;
32+
import org.jboss.test.audit.annotations.SpecVersion;
33+
import org.testng.annotations.Test;
34+
35+
import java.util.List;
36+
import java.util.Set;
37+
38+
import static org.testng.Assert.assertEquals;
39+
40+
@SpecVersion(spec = "cdi", version = "4.1")
41+
public class SimpleInvokerTest extends AbstractTest {
42+
@Deployment
43+
public static WebArchive createTestArchive() {
44+
return new WebArchiveBuilder()
45+
.withTestClass(SimpleInvokerTest.class)
46+
.withClasses(MyService.class)
47+
.withBuildCompatibleExtension(TestExtension.class)
48+
.withClasses(InvokerHolder.class, InvokerHolderCreator.class, InvokerHolderExtensionBase.class)
49+
.build();
50+
}
51+
52+
public static class TestExtension extends InvokerHolderExtensionBase implements BuildCompatibleExtension {
53+
@Registration(types = MyService.class)
54+
public void myServiceRegistration(BeanInfo bean, InvokerFactory invokers) {
55+
registerInvokers(bean, invokers, Set.of("hello"));
56+
}
57+
58+
@Synthesis
59+
public void synthesis(SyntheticComponents syn) {
60+
synthesizeInvokerHolder(syn);
61+
}
62+
}
63+
64+
@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
65+
@SpecAssertion(section = Sections.METHOD_INVOKERS, id = "a")
66+
@SpecAssertion(section = Sections.BUILDING_INVOKER, id = "a")
67+
@SpecAssertion(section = Sections.BUILDING_INVOKER, id = "e")
68+
@SpecAssertion(section = Sections.USING_INVOKER_BUILDER, id = "a")
69+
public void test(MyService service, InvokerHolder invokers) throws Exception {
70+
Invoker<MyService, String> hello = invokers.get("hello");
71+
assertEquals(hello.invoke(service, new Object[]{1, List.of()}), "foobar1[]");
72+
}
73+
74+
@ApplicationScoped
75+
public static class MyService {
76+
public String hello(int param1, List<String> param2) {
77+
return "foobar" + param1 + param2;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)