Skip to content

Commit

Permalink
initial TCK tests for invoker lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Feb 15, 2024
1 parent fb5d6ef commit 931215d
Show file tree
Hide file tree
Showing 16 changed files with 1,506 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, InvokerInfo> invokers = new LinkedHashMap<>();

protected final void registerInvokers(BeanInfo bean, InvokerFactory invokers, Set<String> methods) {
registerInvokers(bean, invokers, methods, builder -> {});
}

protected final void registerInvokers(BeanInfo bean, InvokerFactory invokers, Set<String> methods,
Consumer<InvokerBuilder<?>> action) {
bean.declaringClass()
.methods()
.stream()
.filter(it -> methods.contains(it.name()))
.forEach(it -> registerInvoker(it.name(), invokers.createInvoker(bean, it).build()));
.forEach(it -> {
InvokerBuilder<InvokerInfo> builder = invokers.createInvoker(bean, it);
action.accept(builder);
registerInvoker(it.name(), builder.build());
});
}

protected final void registerInvoker(String id, InvokerInfo invoker) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MyService, String> 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";
}
}
}
Original file line number Diff line number Diff line change
@@ -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<MyService, String> 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<String> observed = new ArrayList<>();

public String hello(BeanManager beanManager) {
Instance<Object> instance = beanManager.createInstance();
MyDependency dependency = instance.select(MyDependency.class).get();
int id = dependency.getId();
instance.destroy(dependency);

beanManager.getEvent().select(new TypeLiteral<List<String>>() {}).fire(List.of("foo", "bar"));

return "foobar" + id;
}

public void observe(@Observes List<String> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<MyService, String> 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;
}
}
}
Loading

0 comments on commit 931215d

Please sign in to comment.