Skip to content

Commit 6f6be27

Browse files
committed
Polish AutowireUtils[Tests]
See gh-2060
1 parent d77b36b commit 6f6be27

File tree

2 files changed

+111
-35
lines changed

2 files changed

+111
-35
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,15 @@ else if (arg instanceof TypedStringValue) {
300300
* {@link Qualifier @Qualifier}, or {@link Value @Value}.
301301
* <p>Note that {@link #resolveDependency} may still be able to resolve the
302302
* dependency for the supplied parameter even if this method returns {@code false}.
303-
* @param parameter the parameter whose dependency should be autowired
303+
* @param parameter the parameter whose dependency should be autowired (must not be
304+
* {@code null})
304305
* @param parameterIndex the index of the parameter in the constructor or method
305306
* that declares the parameter
306307
* @see #resolveDependency
307308
* @since 5.2
308309
*/
309310
public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
311+
Assert.notNull(parameter, "Parameter must not be null");
310312
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
311313
return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
312314
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
@@ -326,14 +328,15 @@ public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
326328
* flag set to {@code false}.
327329
* <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter
328330
* will be used as the qualifier for resolving ambiguities.
329-
* @param parameter the parameter whose dependency should be resolved
331+
* @param parameter the parameter whose dependency should be resolved (must not be
332+
* {@code null})
330333
* @param parameterIndex the index of the parameter in the constructor or method
331334
* that declares the parameter
332335
* @param containingClass the concrete class that contains the parameter; this may
333336
* differ from the class that declares the parameter in that it may be a subclass
334337
* thereof, potentially substituting type variables
335338
* @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve
336-
* the dependency
339+
* the dependency (must not be {@code null})
337340
* @return the resolved object, or {@code null} if none found
338341
* @throws BeansException if dependency resolution failed
339342
* @see #isAutowirable
@@ -347,6 +350,9 @@ public static Object resolveDependency(
347350
Parameter parameter, int parameterIndex, Class<?> containingClass, AutowireCapableBeanFactory beanFactory)
348351
throws BeansException {
349352

353+
Assert.notNull(parameter, "Parameter must not be null");
354+
Assert.notNull(beanFactory, "AutowireCapableBeanFactory must not be null");
355+
350356
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
351357
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
352358
boolean required = (autowired == null || autowired.required());

spring-beans/src/test/java/org/springframework/beans/factory/support/AutowireUtilsTests.java

Lines changed: 102 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,33 +16,40 @@
1616

1717
package org.springframework.beans.factory.support;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertTrue;
22-
import static org.mockito.ArgumentMatchers.any;
23-
import static org.mockito.ArgumentMatchers.isNull;
24-
import static org.mockito.Mockito.when;
25-
2619
import java.lang.reflect.Constructor;
20+
import java.lang.reflect.Executable;
2721
import java.lang.reflect.Method;
2822
import java.lang.reflect.Parameter;
2923
import java.util.HashMap;
3024
import java.util.Map;
3125

26+
import org.junit.Rule;
3227
import org.junit.Test;
33-
import org.mockito.Mockito;
28+
import org.junit.rules.ExpectedException;
29+
3430
import org.springframework.beans.factory.annotation.Autowired;
3531
import org.springframework.beans.factory.annotation.Qualifier;
3632
import org.springframework.beans.factory.annotation.Value;
3733
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
3834
import org.springframework.beans.factory.config.DependencyDescriptor;
35+
import org.springframework.util.ClassUtils;
3936
import org.springframework.util.ReflectionUtils;
4037

38+
import static org.junit.Assert.*;
39+
import static org.mockito.ArgumentMatchers.*;
40+
import static org.mockito.Mockito.*;
41+
4142
/**
43+
* Unit tests for {@link AutowireUtils}.
44+
*
4245
* @author Juergen Hoeller
4346
* @author Sam Brannen
47+
* @author Loïc Ledoyen
4448
*/
4549
public class AutowireUtilsTests {
50+
51+
@Rule
52+
public final ExpectedException exception = ExpectedException.none();
4653

4754
@Test
4855
public void genericMethodReturnTypes() {
@@ -97,46 +104,96 @@ public void genericMethodReturnTypes() {
97104
}
98105

99106
@Test
100-
public void marked_parameters_are_candidate_for_autowiring() throws NoSuchMethodException {
101-
Constructor<AutowirableClass> autowirableConstructor = ReflectionUtils.accessibleConstructor(
102-
AutowirableClass.class, String.class, String.class, String.class, String.class);
107+
public void isAutowirablePreconditions() {
108+
exception.expect(IllegalArgumentException.class);
109+
exception.expectMessage("Parameter must not be null");
110+
AutowireUtils.isAutowirable(null, 0);
111+
}
112+
113+
@Test
114+
public void annotatedParametersInMethodAreCandidatesForAutowiring() throws Exception {
115+
Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class);
116+
assertAutowirableParameters(method);
117+
}
118+
119+
@Test
120+
public void annotatedParametersInTopLevelClassConstructorAreCandidatesForAutowiring() throws Exception {
121+
Constructor<?> constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class);
122+
assertAutowirableParameters(constructor);
123+
}
124+
125+
@Test
126+
public void annotatedParametersInInnerClassConstructorAreCandidatesForAutowiring() throws Exception {
127+
Class<?> innerClass = AutowirableClass.InnerAutowirableClass.class;
128+
assertTrue(ClassUtils.isInnerClass(innerClass));
129+
Constructor<?> constructor = innerClass.getConstructor(AutowirableClass.class, String.class, String.class);
130+
assertAutowirableParameters(constructor);
131+
}
103132

104-
for (int parameterIndex = 0; parameterIndex < autowirableConstructor.getParameterCount(); parameterIndex++) {
105-
Parameter parameter = autowirableConstructor.getParameters()[parameterIndex];
133+
private void assertAutowirableParameters(Executable executable) {
134+
int startIndex = (executable instanceof Constructor)
135+
&& ClassUtils.isInnerClass(executable.getDeclaringClass()) ? 1 : 0;
136+
Parameter[] parameters = executable.getParameters();
137+
for (int parameterIndex = startIndex; parameterIndex < parameters.length; parameterIndex++) {
138+
Parameter parameter = parameters[parameterIndex];
106139
assertTrue("Parameter " + parameter + " must be autowirable", AutowireUtils.isAutowirable(parameter, parameterIndex));
107140
}
108141
}
109142

110143
@Test
111-
public void not_marked_parameters_are_not_candidate_for_autowiring() throws NoSuchMethodException {
112-
Constructor<AutowirableClass> notAutowirableConstructor = ReflectionUtils.accessibleConstructor(AutowirableClass.class, String.class);
144+
public void nonAnnotatedParametersInTopLevelClassConstructorAreNotCandidatesForAutowiring() throws Exception {
145+
Constructor<?> notAutowirableConstructor = AutowirableClass.class.getConstructor(String.class);
113146

114-
for (int parameterIndex = 0; parameterIndex < notAutowirableConstructor.getParameterCount(); parameterIndex++) {
115-
Parameter parameter = notAutowirableConstructor.getParameters()[parameterIndex];
116-
assertFalse("Parameter " + parameter + " must not be autowirable", AutowireUtils.isAutowirable(parameter, 0));
147+
Parameter[] parameters = notAutowirableConstructor.getParameters();
148+
for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
149+
Parameter parameter = parameters[parameterIndex];
150+
assertFalse("Parameter " + parameter + " must not be autowirable", AutowireUtils.isAutowirable(parameter, parameterIndex));
117151
}
118152
}
119153

120154
@Test
121-
public void dependency_resolution_for_marked_parameters() throws NoSuchMethodException {
122-
Constructor<AutowirableClass> autowirableConstructor = ReflectionUtils.accessibleConstructor(
123-
AutowirableClass.class, String.class, String.class, String.class, String.class);
124-
AutowireCapableBeanFactory beanFactory = Mockito.mock(AutowireCapableBeanFactory.class);
125-
// BeanFactory will return the DependencyDescriptor for convenience and to avoid using an ArgumentCaptor
126-
when(beanFactory.resolveDependency(any(), isNull())).thenAnswer(iom -> iom.getArgument(0));
127-
128-
for (int parameterIndex = 0; parameterIndex < autowirableConstructor.getParameterCount(); parameterIndex++) {
129-
Parameter parameter = autowirableConstructor.getParameters()[parameterIndex];
155+
public void resolveDependencyPreconditionsForParameter() {
156+
exception.expect(IllegalArgumentException.class);
157+
exception.expectMessage("Parameter must not be null");
158+
AutowireUtils.resolveDependency(null, 0, null, mock(AutowireCapableBeanFactory.class));
159+
}
160+
161+
@Test
162+
public void resolveDependencyPreconditionsForBeanFactory() throws Exception {
163+
Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class);
164+
Parameter parameter = method.getParameters()[0];
165+
166+
exception.expect(IllegalArgumentException.class);
167+
exception.expectMessage("AutowireCapableBeanFactory must not be null");
168+
AutowireUtils.resolveDependency(parameter, 0, null, null);
169+
}
170+
171+
@Test
172+
public void resolveDependencyForAnnotatedParametersInTopLevelClassConstructor() throws Exception {
173+
Constructor<?> constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class);
174+
175+
AutowireCapableBeanFactory beanFactory = mock(AutowireCapableBeanFactory.class);
176+
// Configure the mocked BeanFactory to return the DependencyDescriptor for convenience and
177+
// to avoid using an ArgumentCaptor.
178+
when(beanFactory.resolveDependency(any(), isNull())).thenAnswer(invocation -> invocation.getArgument(0));
179+
180+
Parameter[] parameters = constructor.getParameters();
181+
for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
182+
Parameter parameter = parameters[parameterIndex];
130183
DependencyDescriptor intermediateDependencyDescriptor = (DependencyDescriptor) AutowireUtils.resolveDependency(
131184
parameter, parameterIndex, AutowirableClass.class, beanFactory);
132-
assertEquals(intermediateDependencyDescriptor.getAnnotatedElement(), autowirableConstructor);
133-
assertEquals(intermediateDependencyDescriptor.getMethodParameter().getParameter(), parameter);
185+
assertEquals(constructor, intermediateDependencyDescriptor.getAnnotatedElement());
186+
assertEquals(parameter, intermediateDependencyDescriptor.getMethodParameter().getParameter());
134187
}
135188
}
136189

190+
137191
public interface MyInterfaceType<T> {
138192
}
139193

194+
public class MySimpleInterfaceType implements MyInterfaceType<String> {
195+
}
196+
140197
public static class MyTypeWithMethods<T> {
141198

142199
/**
@@ -229,7 +286,15 @@ public void readGenericArrayInputMessage(T[] message) {
229286
}
230287
}
231288

289+
void autowirableMethod(
290+
@Autowired String firstParameter,
291+
@Qualifier("someQualifier") String secondParameter,
292+
@Value("${someValue}") String thirdParameter,
293+
@Autowired(required = false) String fourthParameter) {
294+
}
295+
232296
public static class AutowirableClass {
297+
233298
public AutowirableClass(@Autowired String firstParameter,
234299
@Qualifier("someQualifier") String secondParameter,
235300
@Value("${someValue}") String thirdParameter,
@@ -238,8 +303,13 @@ public AutowirableClass(@Autowired String firstParameter,
238303

239304
public AutowirableClass(String notAutowirableParameter) {
240305
}
241-
}
242306

243-
public class MySimpleInterfaceType implements MyInterfaceType<String> {
307+
public class InnerAutowirableClass {
308+
309+
public InnerAutowirableClass(@Autowired String firstParameter,
310+
@Qualifier("someQualifier") String secondParameter) {
311+
}
312+
}
244313
}
314+
245315
}

0 commit comments

Comments
 (0)