Polish AutowireUtils[Tests]

See gh-2060
This commit is contained in:
Sam Brannen 2019-02-27 16:25:34 +01:00
parent d77b36bf3b
commit 6f6be27aae
2 changed files with 110 additions and 34 deletions

View File

@ -300,13 +300,15 @@ public abstract class AutowireUtils {
* {@link Qualifier @Qualifier}, or {@link Value @Value}. * {@link Qualifier @Qualifier}, or {@link Value @Value}.
* <p>Note that {@link #resolveDependency} may still be able to resolve the * <p>Note that {@link #resolveDependency} may still be able to resolve the
* dependency for the supplied parameter even if this method returns {@code false}. * dependency for the supplied parameter even if this method returns {@code false}.
* @param parameter the parameter whose dependency should be autowired * @param parameter the parameter whose dependency should be autowired (must not be
* {@code null})
* @param parameterIndex the index of the parameter in the constructor or method * @param parameterIndex the index of the parameter in the constructor or method
* that declares the parameter * that declares the parameter
* @see #resolveDependency * @see #resolveDependency
* @since 5.2 * @since 5.2
*/ */
public static boolean isAutowirable(Parameter parameter, int parameterIndex) { public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
Assert.notNull(parameter, "Parameter must not be null");
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex); AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) || return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) || AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
@ -326,14 +328,15 @@ public abstract class AutowireUtils {
* flag set to {@code false}. * flag set to {@code false}.
* <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter * <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter
* will be used as the qualifier for resolving ambiguities. * will be used as the qualifier for resolving ambiguities.
* @param parameter the parameter whose dependency should be resolved * @param parameter the parameter whose dependency should be resolved (must not be
* {@code null})
* @param parameterIndex the index of the parameter in the constructor or method * @param parameterIndex the index of the parameter in the constructor or method
* that declares the parameter * that declares the parameter
* @param containingClass the concrete class that contains the parameter; this may * @param containingClass the concrete class that contains the parameter; this may
* differ from the class that declares the parameter in that it may be a subclass * differ from the class that declares the parameter in that it may be a subclass
* thereof, potentially substituting type variables * thereof, potentially substituting type variables
* @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve * @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve
* the dependency * the dependency (must not be {@code null})
* @return the resolved object, or {@code null} if none found * @return the resolved object, or {@code null} if none found
* @throws BeansException if dependency resolution failed * @throws BeansException if dependency resolution failed
* @see #isAutowirable * @see #isAutowirable
@ -347,6 +350,9 @@ public abstract class AutowireUtils {
Parameter parameter, int parameterIndex, Class<?> containingClass, AutowireCapableBeanFactory beanFactory) Parameter parameter, int parameterIndex, Class<?> containingClass, AutowireCapableBeanFactory beanFactory)
throws BeansException { throws BeansException {
Assert.notNull(parameter, "Parameter must not be null");
Assert.notNull(beanFactory, "AutowireCapableBeanFactory must not be null");
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex); AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class); Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
boolean required = (autowired == null || autowired.required()); boolean required = (autowired == null || autowired.required());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,33 +16,40 @@
package org.springframework.beans.factory.support; package org.springframework.beans.factory.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.when;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.DependencyDescriptor; import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/** /**
* Unit tests for {@link AutowireUtils}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
* @author Loïc Ledoyen
*/ */
public class AutowireUtilsTests { public class AutowireUtilsTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test @Test
public void genericMethodReturnTypes() { public void genericMethodReturnTypes() {
@ -97,46 +104,96 @@ public class AutowireUtilsTests {
} }
@Test @Test
public void marked_parameters_are_candidate_for_autowiring() throws NoSuchMethodException { public void isAutowirablePreconditions() {
Constructor<AutowirableClass> autowirableConstructor = ReflectionUtils.accessibleConstructor( exception.expect(IllegalArgumentException.class);
AutowirableClass.class, String.class, String.class, String.class, String.class); exception.expectMessage("Parameter must not be null");
AutowireUtils.isAutowirable(null, 0);
}
for (int parameterIndex = 0; parameterIndex < autowirableConstructor.getParameterCount(); parameterIndex++) { @Test
Parameter parameter = autowirableConstructor.getParameters()[parameterIndex]; public void annotatedParametersInMethodAreCandidatesForAutowiring() throws Exception {
Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class);
assertAutowirableParameters(method);
}
@Test
public void annotatedParametersInTopLevelClassConstructorAreCandidatesForAutowiring() throws Exception {
Constructor<?> constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class);
assertAutowirableParameters(constructor);
}
@Test
public void annotatedParametersInInnerClassConstructorAreCandidatesForAutowiring() throws Exception {
Class<?> innerClass = AutowirableClass.InnerAutowirableClass.class;
assertTrue(ClassUtils.isInnerClass(innerClass));
Constructor<?> constructor = innerClass.getConstructor(AutowirableClass.class, String.class, String.class);
assertAutowirableParameters(constructor);
}
private void assertAutowirableParameters(Executable executable) {
int startIndex = (executable instanceof Constructor)
&& ClassUtils.isInnerClass(executable.getDeclaringClass()) ? 1 : 0;
Parameter[] parameters = executable.getParameters();
for (int parameterIndex = startIndex; parameterIndex < parameters.length; parameterIndex++) {
Parameter parameter = parameters[parameterIndex];
assertTrue("Parameter " + parameter + " must be autowirable", AutowireUtils.isAutowirable(parameter, parameterIndex)); assertTrue("Parameter " + parameter + " must be autowirable", AutowireUtils.isAutowirable(parameter, parameterIndex));
} }
} }
@Test @Test
public void not_marked_parameters_are_not_candidate_for_autowiring() throws NoSuchMethodException { public void nonAnnotatedParametersInTopLevelClassConstructorAreNotCandidatesForAutowiring() throws Exception {
Constructor<AutowirableClass> notAutowirableConstructor = ReflectionUtils.accessibleConstructor(AutowirableClass.class, String.class); Constructor<?> notAutowirableConstructor = AutowirableClass.class.getConstructor(String.class);
for (int parameterIndex = 0; parameterIndex < notAutowirableConstructor.getParameterCount(); parameterIndex++) { Parameter[] parameters = notAutowirableConstructor.getParameters();
Parameter parameter = notAutowirableConstructor.getParameters()[parameterIndex]; for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
assertFalse("Parameter " + parameter + " must not be autowirable", AutowireUtils.isAutowirable(parameter, 0)); Parameter parameter = parameters[parameterIndex];
assertFalse("Parameter " + parameter + " must not be autowirable", AutowireUtils.isAutowirable(parameter, parameterIndex));
} }
} }
@Test @Test
public void dependency_resolution_for_marked_parameters() throws NoSuchMethodException { public void resolveDependencyPreconditionsForParameter() {
Constructor<AutowirableClass> autowirableConstructor = ReflectionUtils.accessibleConstructor( exception.expect(IllegalArgumentException.class);
AutowirableClass.class, String.class, String.class, String.class, String.class); exception.expectMessage("Parameter must not be null");
AutowireCapableBeanFactory beanFactory = Mockito.mock(AutowireCapableBeanFactory.class); AutowireUtils.resolveDependency(null, 0, null, mock(AutowireCapableBeanFactory.class));
// BeanFactory will return the DependencyDescriptor for convenience and to avoid using an ArgumentCaptor }
when(beanFactory.resolveDependency(any(), isNull())).thenAnswer(iom -> iom.getArgument(0));
for (int parameterIndex = 0; parameterIndex < autowirableConstructor.getParameterCount(); parameterIndex++) { @Test
Parameter parameter = autowirableConstructor.getParameters()[parameterIndex]; public void resolveDependencyPreconditionsForBeanFactory() throws Exception {
Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class);
Parameter parameter = method.getParameters()[0];
exception.expect(IllegalArgumentException.class);
exception.expectMessage("AutowireCapableBeanFactory must not be null");
AutowireUtils.resolveDependency(parameter, 0, null, null);
}
@Test
public void resolveDependencyForAnnotatedParametersInTopLevelClassConstructor() throws Exception {
Constructor<?> constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class);
AutowireCapableBeanFactory beanFactory = mock(AutowireCapableBeanFactory.class);
// Configure the mocked BeanFactory to return the DependencyDescriptor for convenience and
// to avoid using an ArgumentCaptor.
when(beanFactory.resolveDependency(any(), isNull())).thenAnswer(invocation -> invocation.getArgument(0));
Parameter[] parameters = constructor.getParameters();
for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
Parameter parameter = parameters[parameterIndex];
DependencyDescriptor intermediateDependencyDescriptor = (DependencyDescriptor) AutowireUtils.resolveDependency( DependencyDescriptor intermediateDependencyDescriptor = (DependencyDescriptor) AutowireUtils.resolveDependency(
parameter, parameterIndex, AutowirableClass.class, beanFactory); parameter, parameterIndex, AutowirableClass.class, beanFactory);
assertEquals(intermediateDependencyDescriptor.getAnnotatedElement(), autowirableConstructor); assertEquals(constructor, intermediateDependencyDescriptor.getAnnotatedElement());
assertEquals(intermediateDependencyDescriptor.getMethodParameter().getParameter(), parameter); assertEquals(parameter, intermediateDependencyDescriptor.getMethodParameter().getParameter());
} }
} }
public interface MyInterfaceType<T> { public interface MyInterfaceType<T> {
} }
public class MySimpleInterfaceType implements MyInterfaceType<String> {
}
public static class MyTypeWithMethods<T> { public static class MyTypeWithMethods<T> {
/** /**
@ -229,7 +286,15 @@ public class AutowireUtilsTests {
} }
} }
void autowirableMethod(
@Autowired String firstParameter,
@Qualifier("someQualifier") String secondParameter,
@Value("${someValue}") String thirdParameter,
@Autowired(required = false) String fourthParameter) {
}
public static class AutowirableClass { public static class AutowirableClass {
public AutowirableClass(@Autowired String firstParameter, public AutowirableClass(@Autowired String firstParameter,
@Qualifier("someQualifier") String secondParameter, @Qualifier("someQualifier") String secondParameter,
@Value("${someValue}") String thirdParameter, @Value("${someValue}") String thirdParameter,
@ -238,8 +303,13 @@ public class AutowireUtilsTests {
public AutowirableClass(String notAutowirableParameter) { public AutowirableClass(String notAutowirableParameter) {
} }
public class InnerAutowirableClass {
public InnerAutowirableClass(@Autowired String firstParameter,
@Qualifier("someQualifier") String secondParameter) {
}
}
} }
public class MySimpleInterfaceType implements MyInterfaceType<String> {
}
} }