Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find Bean Classes In Inherited '@Inject'-ed Field #112

Merged
merged 8 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private boolean methodRequiresExplicitParamInjection(ParameterContext pc) {
}

private TestInstance.Lifecycle determineTestLifecycle(ExtensionContext ec) {
// check the test for import org.junit.jupiter.api.TestInstance annotation
// check the test for org.junit.jupiter.api.TestInstance annotation
TestInstance annotation = ec.getRequiredTestClass().getAnnotation(TestInstance.class);
if (annotation != null) {
return annotation.value();
Expand Down
20 changes: 15 additions & 5 deletions junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ static void scanForRequiredBeanClasses(List<Class<?>> testClasses, Weld weld, bo

foundClasses.add(currClass);

findAnnotatedFields(currClass, ExcludeBean.class).stream()
AnnotationSupport.findAnnotatedFields(currClass, ExcludeBean.class).stream()
.map(Field::getType)
.forEach(excludedBeanTypes::add);

AnnotationSupport.findAnnotatedMethods(currClass, ExcludeBean.class, HierarchyTraversalMode.BOTTOM_UP).stream()
.map(Method::getReturnType)
.forEach(excludedBeanTypes::add);

findAnnotatedFields(currClass, Inject.class).stream()
AnnotationSupport.findAnnotatedFields(currClass, Inject.class).stream()
.map(Field::getType)
.forEach(cls -> addClassesToProcess(classesToProcess, cls));

Expand All @@ -107,11 +107,11 @@ static void scanForRequiredBeanClasses(List<Class<?>> testClasses, Weld weld, bo
.flatMap(cons -> getExecutableParameterTypes(cons, explicitInjection).stream())
.forEach(cls -> addClassesToProcess(classesToProcess, cls));

findAnnotatedFields(currClass, Produces.class).stream()
findAnnotatedDeclaredFields(currClass, Produces.class).stream()
.map(Field::getType)
.forEach(cls -> addClassesToProcess(classesToProcess, cls));

AnnotationSupport.findAnnotatedMethods(currClass, Produces.class, HierarchyTraversalMode.BOTTOM_UP).stream()
findAnnotatedDeclaredMethods(currClass, Produces.class).stream()
.flatMap(method ->
Stream.concat(
getExecutableParameterTypes(method, explicitInjection).stream(),
Expand Down Expand Up @@ -288,6 +288,10 @@ private static List<Field> getDeclaredFields(Class<?> clazz) {
return asList(clazz.getDeclaredFields());
}

private static List<Method> getDeclaredMethods(Class<?> clazz) {
return asList(clazz.getDeclaredMethods());
}

private static boolean isMethodShadowedByLocalFields(Field field, List<Field> localFields) {
return localFields.stream()
.anyMatch((local) -> isFieldShadowedBy(field, local));
Expand All @@ -297,12 +301,18 @@ private static boolean isFieldShadowedBy(Field upper, Field lower) {
return upper.getType().equals(lower.getType());
}

private static List<Field> findAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotationType) {
private static List<Field> findAnnotatedDeclaredFields(Class<?> clazz, Class<? extends Annotation> annotationType) {
return getDeclaredFields(clazz).stream()
.filter((field) -> isAnnotated(field, annotationType))
.collect(CollectionUtils.toUnmodifiableList());
}

private static List<Method> findAnnotatedDeclaredMethods(Class<?> clazz, Class<? extends Annotation> annotationType) {
return getDeclaredMethods(clazz).stream()
.filter((field) -> isAnnotated(field, annotationType))
.collect(CollectionUtils.toUnmodifiableList());
}

private static List<Constructor<?>> getDeclaredConstructors(Class<?> clazz) {
return asList(clazz.getDeclaredConstructors());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,107 +1,107 @@
package org.jboss.weld.junit5.auto;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import javax.inject.Inject;
import org.jboss.weld.junit5.auto.beans.Engine;
import org.jboss.weld.junit5.auto.beans.V8;
import org.jboss.weld.junit5.auto.extension.AddedExtension;
import org.jboss.weld.junit5.auto.interceptorAndDecorator.DecoratedBean;
import org.jboss.weld.junit5.auto.interceptorAndDecorator.InterceptedBean;
import org.jboss.weld.junit5.auto.interceptorAndDecorator.TestDecorator;
import org.jboss.weld.junit5.auto.interceptorAndDecorator.TestInterceptor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
/**
* Tests the inheritance of Annotations.
*/
public class InheritanceTest {
@EnableAutoWeld
@AddBeanClasses(V8.class)
class BaseAddBeanClassesTest {
}
@Nested
class AddBeanClassesTest extends BaseAddBeanClassesTest {
@Inject
private Engine engine;
@Test
@DisplayName("Test that @AddBeanClasses pulls in V8 to fulfill the injected Engine interface")
void test() {
assertNotNull(engine);
}
}
@EnableAutoWeld
@AddEnabledDecorators(TestDecorator.class)
class BaseAddDecoratorTest {
}
@Nested
class AddDecoratorTest extends BaseAddDecoratorTest {
@Inject
DecoratedBean bean;
@Test
public void testBeanIsDecorated() {
Assertions.assertNotNull(bean);
Assertions.assertEquals(TestDecorator.class.toString() + DecoratedBean.class.toString(), bean.ping());
}
}
@EnableAutoWeld
@AddExtensions(AddedExtension.class)
class BaseAddExtensionsTest {
}
@Nested
class AddExtensionsTest extends BaseAddExtensionsTest {
@Test
@DisplayName("Test that @AddExtensions adds the specified extensions")
void test() {
assertTrue(AddedExtension.isEnabled());
}
}
@EnableAutoWeld
@AddEnabledInterceptors(TestInterceptor.class)
class BaseAddInterceptorTest {
}
@Nested
class AddInterceptorTest extends BaseAddInterceptorTest {
@Inject
InterceptedBean bean;
@Test
public void testBeanIsIntercepted() {
Assertions.assertNotNull(bean);
Assertions.assertEquals(TestInterceptor.class.toString() + InterceptedBean.class.toString(), bean.ping());
}
}
@EnableAutoWeld
@AddPackages(Engine.class)
class BaseAddPackagesTest {
}
@Nested
class AddPackagesTest extends BaseAddPackagesTest {
@Inject
private V8 engine;
@Test
@DisplayName("Test that @AddPackages pulls in V8 (without bean defining annotation) to fulfill the injected Engine interface")
void test() {
assertNotNull(engine);
}
}
}
package org.jboss.weld.junit5.auto;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import javax.inject.Inject;

import org.jboss.weld.junit5.auto.beans.Engine;
import org.jboss.weld.junit5.auto.beans.V8;
import org.jboss.weld.junit5.auto.extension.AddedExtension;
import org.jboss.weld.junit5.auto.interceptorAndDecorator.DecoratedBean;
import org.jboss.weld.junit5.auto.interceptorAndDecorator.InterceptedBean;
import org.jboss.weld.junit5.auto.interceptorAndDecorator.TestDecorator;
import org.jboss.weld.junit5.auto.interceptorAndDecorator.TestInterceptor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

/**
* Tests the inheritance of "Weld-JUnit" annotations from test parent classes.
*/
public class AnnotationsInheritanceTest {

@EnableAutoWeld
@AddBeanClasses(V8.class)
class BaseAddBeanClassesTest {
}

@Nested
class AddBeanClassesTest extends BaseAddBeanClassesTest {
@Inject
private Engine engine;

@Test
@DisplayName("Test that @AddBeanClasses pulls in V8 to fulfill the injected Engine interface")
void test() {
assertNotNull(engine);
}
}

@EnableAutoWeld
@AddEnabledDecorators(TestDecorator.class)
class BaseAddDecoratorTest {
}

@Nested
class AddDecoratorTest extends BaseAddDecoratorTest {
@Inject
DecoratedBean bean;

@Test
public void testBeanIsDecorated() {
Assertions.assertNotNull(bean);
Assertions.assertEquals(TestDecorator.class.toString() + DecoratedBean.class.toString(), bean.ping());
}
}

@EnableAutoWeld
@AddExtensions(AddedExtension.class)
class BaseAddExtensionsTest {
}

@Nested
class AddExtensionsTest extends BaseAddExtensionsTest {
@Test
@DisplayName("Test that @AddExtensions adds the specified extensions")
void test() {
assertTrue(AddedExtension.isEnabled());
}
}

@EnableAutoWeld
@AddEnabledInterceptors(TestInterceptor.class)
class BaseAddInterceptorTest {
}

@Nested
class AddInterceptorTest extends BaseAddInterceptorTest {
@Inject
InterceptedBean bean;

@Test
public void testBeanIsIntercepted() {
Assertions.assertNotNull(bean);
Assertions.assertEquals(TestInterceptor.class.toString() + InterceptedBean.class.toString(), bean.ping());
}
}

@EnableAutoWeld
@AddPackages(Engine.class)
class BaseAddPackagesTest {
}

@Nested
class AddPackagesTest extends BaseAddPackagesTest {
@Inject
private V8 engine;

@Test
@DisplayName("Test that @AddPackages pulls in V8 (without bean defining annotation) to fulfill the injected Engine interface")
void test() {
assertNotNull(engine);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.jboss.weld.junit5.auto;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import javax.enterprise.context.Dependent;
import javax.inject.Inject;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

@EnableAutoWeld
public class InheritedInjectedTest {

@Dependent
static class Foo {
}

static class BaseClass {
@Inject
Foo foo;
}

@Nested
class InjectIntoInheritedFieldsFromTestBaseClassTest extends BaseClass {

@Test
@DisplayName("Test injected fields a test class inherits from a base class")
void test() {
assertNotNull(foo);
}
}

@Dependent
static class SubClass extends BaseClass {
}

@Nested
class InjectIntoInheritedFieldsFromBeanBaseClassTest {

@Inject
SubClass inheritsInjected;

/**
* Injection of {@link #inheritsInjected} requires {@link InheritedInjectedTest.Foo} having been identified and added as a bean class to Weld.
*/
@Test
@DisplayName("Test inherited injected fields")
void test() {
assertNotNull(inheritsInjected.foo);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jboss.weld.junit5.auto;

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

@EnableAutoWeld
public class InheritedProducerFieldTest {

@Dependent
static class Foo {
}

static class BaseClass {
@Produces
Foo baseFooProducer = new Foo();
}

@Nested
class DontAddBeanClassesInheritedFromTestBaseClassTest extends BaseClass {
@Test
void test(BeanManager beanManager) {
assertEquals(0, beanManager.getBeans(Foo.class).size());
}
}

@Dependent
static class SubClass extends BaseClass {
}

@Nested
class DontAddBeanClassesInheritedFromBeanBaseClassTest {
@Inject
SubClass subClass;

@Test
void test(BeanManager beanManager) {
assertEquals(0, beanManager.getBeans(Foo.class).size());
}
}

}
Loading