Skip to content

Commit

Permalink
initial TCK tests for method invokers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Dec 1, 2023
1 parent 16b7faa commit 8a8166c
Show file tree
Hide file tree
Showing 22 changed files with 1,754 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jboss.cdi.tck.tests.invokers;

import jakarta.enterprise.invoke.Invoker;

import java.util.Map;

import static org.testng.Assert.assertNotNull;

public class InvokerHolder {
private final Map<String, Invoker<?, ?>> invokers;

public InvokerHolder(Map<String, Invoker<?, ?>> invokers) {
this.invokers = invokers;
}

public <T, R> Invoker<T, R> get(String id) {
Invoker<T, R> result = (Invoker<T, R>) invokers.get(id);
assertNotNull(result);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jboss.cdi.tck.tests.invokers;

import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.build.compatible.spi.Parameters;
import jakarta.enterprise.inject.build.compatible.spi.SyntheticBeanCreator;
import jakarta.enterprise.invoke.Invoker;

import java.util.HashMap;
import java.util.Map;

public class InvokerHolderCreator implements SyntheticBeanCreator<InvokerHolder> {
@Override
public InvokerHolder create(Instance<Object> lookup, Parameters params) {
String[] names = params.get("names", String[].class);
Invoker<?, ?>[] invokers = params.get("invokers", Invoker[].class);
Map<String, Invoker<?, ?>> map = new HashMap<>();
for (int i = 0; i < names.length; i++) {
map.put(names[i], invokers[i]);
}
return new InvokerHolder(map);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jboss.cdi.tck.tests.invokers;

import jakarta.enterprise.inject.build.compatible.spi.BeanInfo;
import jakarta.enterprise.inject.build.compatible.spi.InvokerInfo;
import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public abstract class InvokerHolderExtensionBase {
protected final Map<String, InvokerInfo> invokers = new LinkedHashMap<>();

protected final void registerInvokers(BeanInfo bean, Set<String> methods) {
bean.declaringClass()
.methods()
.stream()
.filter(it -> methods.contains(it.name()))
.forEach(it -> {
invokers.put(it.name(), bean.createInvoker(it).build());
});
}

protected final void synthesizeInvokerHolder(SyntheticComponents syn) {
syn.addBean(InvokerHolder.class)
.type(InvokerHolder.class)
.withParam("names", invokers.keySet().toArray(String[]::new))
.withParam("invokers", invokers.values().toArray(InvokerInfo[]::new))
.createWith(InvokerHolderCreator.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.jboss.cdi.tck.tests.invokers;

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.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.shrinkwrap.WebArchiveBuilder;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Set;

import static org.testng.Assert.assertEquals;

@SpecVersion(spec = "cdi", version = "4.1")
public class SimpleInvokerTest extends AbstractTest {
@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
.withTestClass(SimpleInvokerTest.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) {
registerInvokers(bean, Set.of("hello"));
}

@Synthesis
public void synthesis(SyntheticComponents syn) {
synthesizeInvokerHolder(syn);
}
}

@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
public void test(MyService service, InvokerHolder invokers) {
Invoker<MyService, String> hello = invokers.get("hello");
assertEquals(hello.invoke(service, new Object[]{0, List.of()}), "foobar0[]");
}

@ApplicationScoped
public static class MyService {
public String hello(int param1, List<String> param2) {
return "foobar" + param1 + param2;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.jboss.cdi.tck.tests.invokers.basic;

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.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.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.SpecVersion;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Set;

import static org.testng.Assert.assertEquals;

@SpecVersion(spec = "cdi", version = "4.1")
public class ExcessArgumentsInvokerTest extends AbstractTest {
@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
.withTestClass(ExcessArgumentsInvokerTest.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) {
registerInvokers(bean, Set.of("hello", "helloStatic"));
}

@Synthesis
public void synthesis(SyntheticComponents syn) {
synthesizeInvokerHolder(syn);
}
}

@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
public void test(MyService service, InvokerHolder invokers) {
Invoker<MyService, String> hello = invokers.get("hello");
assertEquals(hello.invoke(service, new Object[]{"a", "ignored"}), "foobar_a");
assertEquals(hello.invoke(new MyService(), new Object[]{"b", 1, 2, 3}), "foobar_b");

Invoker<MyService, String> helloStatic = invokers.get("helloStatic");
assertEquals(helloStatic.invoke(null, new Object[]{"c", new Object()}), "quux_c");
assertEquals(helloStatic.invoke(null, new Object[]{"d", List.of(), Set.of()}), "quux_d");
}

@ApplicationScoped
public static class MyService {
public String hello(String param) {
return "foobar_" + param;
}

public static String helloStatic(String param) {
return "quux_" + param;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.jboss.cdi.tck.tests.invokers.basic;

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.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.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.SpecVersion;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Set;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.expectThrows;

@SpecVersion(spec = "cdi", version = "4.1")
public class InstanceMethodInvokerTest extends AbstractTest {
@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
.withTestClass(InstanceMethodInvokerTest.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) {
registerInvokers(bean, Set.of("hello", "doSomething", "fail"));
}

@Synthesis
public void synthesis(SyntheticComponents syn) {
synthesizeInvokerHolder(syn);
}
}

@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
public void test(MyService service, InvokerHolder invokers) {
Invoker<MyService, String> hello = invokers.get("hello");
assertEquals(hello.invoke(service, new Object[]{0, List.of()}), "foobar0[]");
assertEquals(hello.invoke(new MyService(), new Object[]{1, List.of()}), "foobar1[]");
assertThrows(NullPointerException.class, () -> {
hello.invoke(null, new Object[]{2, List.of()});
});

Invoker<Object, Object> helloDetyped = (Invoker) hello;
assertEquals("foobar3[]", helloDetyped.invoke(service, new Object[]{3, List.of()}));
assertEquals("foobar4[]", helloDetyped.invoke(new MyService(), new Object[]{4, List.of()}));
assertThrows(NullPointerException.class, () -> {
helloDetyped.invoke(null, new Object[]{5, List.of()});
});

Invoker<MyService, Void> doSomething = invokers.get("doSomething");
assertEquals(0, MyService.counter);
assertNull(doSomething.invoke(service, null));
assertEquals(1, MyService.counter);
assertNull(doSomething.invoke(new MyService(), new Object[]{}));
assertEquals(2, MyService.counter);

Invoker<MyService, Void> fail = invokers.get("fail");
assertNull(fail.invoke(service, new Object[]{false}));
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> {
fail.invoke(service, new Object[]{true});
});
assertEquals("expected", ex.getMessage());
}

@ApplicationScoped
public static class MyService {
public static int counter = 0;

public String hello(int param1, List<String> param2) {
return "foobar" + param1 + param2;
}

public void doSomething() {
counter++;
}

public void fail(boolean doFail) {
if (doFail) {
throw new IllegalArgumentException("expected");
}
}
}
}
Loading

0 comments on commit 8a8166c

Please sign in to comment.