Skip to content

Commit

Permalink
Test BeanContainer assignability API methods
Browse files Browse the repository at this point in the history
Tests for .isMatchingBean and .isMatchingEvent.
  • Loading branch information
Azquelt committed Jan 12, 2024
1 parent 09bf860 commit 8521084
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* Copyright 2024, IBM Corp., 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.beanContainer.assignability;

import static org.jboss.cdi.tck.cdi.Sections.BM_BEAN_EVENT_ASSIGNABILITY;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;

import java.lang.reflect.Type;
import java.util.Set;

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

import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.literal.NamedLiteral;

/**
* @author Andrew Rouse
*/
@SpecVersion(spec = "cdi", version = "4.1")
public class BeanObserverAssignabilityTest extends AbstractTest {

@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder().withTestClassPackage(BeanObserverAssignabilityTest.class).build();
}

@Test
@SpecAssertion(section = BM_BEAN_EVENT_ASSIGNABILITY, id = "aa")
public void testBeanMatching() {
Set<Type> beanTypes = Set.of(MyBean.class, MyBeanInterface.class, Object.class);

// Basic checks with qualifiers
assertTrue(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(), MyBean.class, Set.of()),
"Bean did not match its own type");
assertFalse(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(), MyBean.class, Set.of(Qualifier1.Literal.INSTANCE)),
"Bean matched despite not having required qualifier");
assertTrue(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(Qualifier1.Literal.INSTANCE), MyBean.class, Set.of(Qualifier1.Literal.INSTANCE)),
"Bean did not match despite having required qualifier");
assertTrue(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(Qualifier1.Literal.INSTANCE, Qualifier2.Literal.INSTANCE), MyBean.class, Set.of(Qualifier1.Literal.INSTANCE)),
"Bean did not match despite having a superset of the required qualifiers");

// Only bean types passed in should be considered
Set<Type> reducedBeanTypes = Set.of(MyBean.class);
assertTrue(getCurrentBeanContainer().isMatchingBean(reducedBeanTypes, Set.of(), MyBean.class, Set.of()),
"Bean did not match its own type");
assertFalse(getCurrentBeanContainer().isMatchingBean(reducedBeanTypes, Set.of(), MyBeanInterface.class, Set.of()),
"Bean matched MyBeanInterface despite it not being in bean types");
assertFalse(getCurrentBeanContainer().isMatchingBean(reducedBeanTypes, Set.of(), Object.class, Set.of()),
"Bean matched Object despite it not being in bean types");

// Qualifier annotations on bean type classes should not be considered
assertTrue(getCurrentBeanContainer().isMatchingBean(Set.of(MyQualifiedBean.class), Set.of(), MyQualifiedBean.class, Set.of()),
"MyQualifiedBean should match, qualifier on bean class should be ignored");
assertTrue(getCurrentBeanContainer().isMatchingBean(Set.of(MyQualifiedBean.class), Set.of(), MyQualifiedBean.class, Set.of(Qualifier1.Literal.INSTANCE)),
"MyQualifiedBean should not match, qualifier on bean class should be ignored");
}

@Test
@SpecAssertion(section = BM_BEAN_EVENT_ASSIGNABILITY, id = "ab")
public void testBeanMatchingDefaultQualifiers() {
Set<Type> beanTypes = Set.of(MyBean.class, MyBeanInterface.class, Object.class);

assertTrue(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(Default.Literal.INSTANCE), MyBean.class, Set.of()),
"Bean with @Default should match when no qualifiers required");

assertTrue(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(), MyBean.class, Set.of(Default.Literal.INSTANCE)),
"Bean with no qualifiers should match when @Default required");

assertTrue(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(Any.Literal.INSTANCE), MyBean.class, Set.of()),
"Bean with explicit @Any should match when no qualifiers required");

assertTrue(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(NamedLiteral.of("foo")), MyBean.class, Set.of()),
"Bean with @Named should match when no qualifiers required");

assertFalse(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(Qualifier1.Literal.INSTANCE), MyBean.class, Set.of()),
"Bean with @Qualifier1 should not match when no qualifiers required (@Default implied required)");

assertTrue(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(), MyBean.class, Set.of(Any.Literal.INSTANCE)),
"Bean with no qualifiers should match when @Any required");

assertTrue(getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(Qualifier1.Literal.INSTANCE), MyBean.class, Set.of(Any.Literal.INSTANCE)),
"Bean with @Qualifier1 should match when @Any required");
}

@Test
@SpecAssertion(section = Sections.BM_BEAN_EVENT_ASSIGNABILITY, id = "ac")
public void testBeanMatchingException() {

Set<Type> beanTypes = Set.of(MyBean.class, MyBeanInterface.class, Object.class);

assertThrows("Null bean type", IllegalArgumentException.class, () -> getCurrentBeanContainer().isMatchingBean(null, Set.of(), MyBean.class, Set.of()));
assertThrows("Null bean qualifiers", IllegalArgumentException.class, () -> getCurrentBeanContainer().isMatchingBean(beanTypes, null, MyBean.class, Set.of()));
assertThrows("Null required type", IllegalArgumentException.class, () -> getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(), null, Set.of()));
assertThrows("Null required qualifiers", IllegalArgumentException.class, () -> getCurrentBeanContainer().isMatchingBean(beanTypes, Set.of(), MyBean.class, null));
}

@Test
@SpecAssertion(section = BM_BEAN_EVENT_ASSIGNABILITY, id = "ad")
public void testEventMatching() {
assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(), MyEvent.class, Set.of()),
"Event did not match its own type");

assertFalse(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(), MyEvent.class, Set.of(Qualifier1.Literal.INSTANCE)),
"Event matched despite not having required qualifier");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(Qualifier1.Literal.INSTANCE), MyEvent.class, Set.of(Qualifier1.Literal.INSTANCE)),
"Event did not match despite having required qualifier");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(Qualifier1.Literal.INSTANCE, Qualifier2.Literal.INSTANCE), MyEvent.class, Set.of(Qualifier1.Literal.INSTANCE)),
"Event did not match despite having a superset of the required qualifiers");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(), MyEventInterface.class, Set.of()),
"Event should match when a supertype is required");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(), Object.class, Set.of()),
"Event should match when Object is required");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(Default.Literal.INSTANCE), MyEvent.class, Set.of()),
"Event with @Default should match when no qualifiers required");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(Any.Literal.INSTANCE), MyEvent.class, Set.of()),
"Event with explicit @Any should match when no qualifiers required");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(Qualifier1.Literal.INSTANCE), MyEvent.class, Set.of()),
"Event with @Qualifier1 should match when no qualifiers required");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(NamedLiteral.of("foo")), MyEvent.class, Set.of()),
"Event with @Named should match when no qualifiers required");

}

@Test
@SpecAssertion(section = BM_BEAN_EVENT_ASSIGNABILITY, id = "ad")
public void testEventMatchingDefaultQualifier() {
assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(), MyEvent.class, Set.of(Default.Literal.INSTANCE)),
"Event with no qualifiers should match when @Default required");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(Default.Literal.INSTANCE), MyEvent.class, Set.of(Default.Literal.INSTANCE)),
"Event with @Default should match when @Default required");

assertFalse(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(Qualifier1.Literal.INSTANCE), MyEvent.class, Set.of(Default.Literal.INSTANCE)),
"Event with @Qualifier1 should not match when @Default required");
}

@Test
@SpecAssertion(section = BM_BEAN_EVENT_ASSIGNABILITY, id = "ae")
public void testEventMatchingAnyQualifier() {
assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(), MyEvent.class, Set.of(Any.Literal.INSTANCE)),
"Event with no qualifiers should match when @Any required");

assertTrue(getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(Qualifier1.Literal.INSTANCE), MyEvent.class, Set.of(Any.Literal.INSTANCE)),
"Event with @Qualifier1 should match when @Any required");
}

@Test
@SpecAssertion(section = Sections.BM_BEAN_EVENT_ASSIGNABILITY, id = "af")
public void testEventMatchingException() {
assertThrows("Null event type", IllegalArgumentException.class, () -> getCurrentBeanContainer().isMatchingEvent(null, Set.of(), MyBean.class, Set.of()));
assertThrows("Null event qualifiers", IllegalArgumentException.class, () -> getCurrentBeanContainer().isMatchingEvent(MyEvent.class, null, MyEvent.class, Set.of()));
assertThrows("Null required type", IllegalArgumentException.class, () -> getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(), null, Set.of()));
assertThrows("Null required qualifiers", IllegalArgumentException.class, () -> getCurrentBeanContainer().isMatchingEvent(MyEvent.class, Set.of(), MyEvent.class, null));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024, IBM Corp., 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.beanContainer.assignability;

public class MyBean implements MyBeanInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2024, IBM Corp., 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.beanContainer.assignability;

public interface MyBeanInterface {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2024, IBM Corp., 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.beanContainer.assignability;

public class MyEvent implements MyEventInterface {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2024, IBM Corp., 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.beanContainer.assignability;

public interface MyEventInterface {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2024, IBM Corp., 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.beanContainer.assignability;

@Qualifier1
public class MyQualifiedBean {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024, IBM Corp., 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.beanContainer.assignability;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

@Qualifier
@Retention(RUNTIME)
@Target({ TYPE, METHOD, FIELD, PARAMETER })
public @interface Qualifier1 {

static class Literal extends AnnotationLiteral<Qualifier1> implements Qualifier1 {
static final Qualifier1 INSTANCE = new Literal();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024, IBM Corp., 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.beanContainer.assignability;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

@Qualifier
@Retention(RUNTIME)
@Target({ TYPE, METHOD, FIELD, PARAMETER })
public @interface Qualifier2 {

static class Literal extends AnnotationLiteral<Qualifier2> implements Qualifier2 {
static final Qualifier2 INSTANCE = new Literal();
}

}
Loading

0 comments on commit 8521084

Please sign in to comment.