Name attributes in method argument annotations allow for placeholders and expressions
Issue: SPR-13952
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,6 +27,7 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
@@ -50,6 +51,7 @@ public class RequestHeaderMethodArgumentResolverTests {
|
||||
private MethodParameter paramNamedValueStringArray;
|
||||
private MethodParameter paramSystemProperty;
|
||||
private MethodParameter paramContextPath;
|
||||
private MethodParameter paramResolvedName;
|
||||
private MethodParameter paramNamedValueMap;
|
||||
|
||||
private MockHttpServletRequest servletRequest;
|
||||
@@ -64,12 +66,13 @@ public class RequestHeaderMethodArgumentResolverTests {
|
||||
context.refresh();
|
||||
resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory());
|
||||
|
||||
Method method = getClass().getMethod("params", String.class, String[].class, String.class, String.class, Map.class);
|
||||
Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
|
||||
paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
|
||||
paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
|
||||
paramSystemProperty = new SynthesizingMethodParameter(method, 2);
|
||||
paramContextPath = new SynthesizingMethodParameter(method, 3);
|
||||
paramNamedValueMap = new SynthesizingMethodParameter(method, 4);
|
||||
paramResolvedName = new SynthesizingMethodParameter(method, 4);
|
||||
paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
|
||||
|
||||
servletRequest = new MockHttpServletRequest();
|
||||
webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
|
||||
@@ -97,18 +100,16 @@ public class RequestHeaderMethodArgumentResolverTests {
|
||||
servletRequest.addHeader("name", expected);
|
||||
|
||||
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveStringArrayArgument() throws Exception {
|
||||
String[] expected = new String[]{"foo", "bar"};
|
||||
String[] expected = new String[] {"foo", "bar"};
|
||||
servletRequest.addHeader("name", expected);
|
||||
|
||||
Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof String[]);
|
||||
assertArrayEquals("Invalid result", expected, (String[]) result);
|
||||
}
|
||||
@@ -116,7 +117,6 @@ public class RequestHeaderMethodArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveDefaultValue() throws Exception {
|
||||
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("Invalid result", "bar", result);
|
||||
}
|
||||
@@ -124,18 +124,37 @@ public class RequestHeaderMethodArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveDefaultValueFromSystemProperty() throws Exception {
|
||||
System.setProperty("systemProperty", "bar");
|
||||
Object result = resolver.resolveArgument(paramSystemProperty, null, webRequest, null);
|
||||
System.clearProperty("systemProperty");
|
||||
try {
|
||||
Object result = resolver.resolveArgument(paramSystemProperty, null, webRequest, null);
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("bar", result);
|
||||
}
|
||||
finally {
|
||||
System.clearProperty("systemProperty");
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("bar", result);
|
||||
@Test
|
||||
public void resolveNameFromSystemProperty() throws Exception {
|
||||
String expected = "foo";
|
||||
servletRequest.addHeader("bar", expected);
|
||||
|
||||
System.setProperty("systemProperty", "bar");
|
||||
try {
|
||||
Object result = resolver.resolveArgument(paramResolvedName, null, webRequest, null);
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
}
|
||||
finally {
|
||||
System.clearProperty("systemProperty");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveDefaultValueFromRequest() throws Exception {
|
||||
servletRequest.setContextPath("/bar");
|
||||
Object result = resolver.resolveArgument(paramContextPath, null, webRequest, null);
|
||||
|
||||
Object result = resolver.resolveArgument(paramContextPath, null, webRequest, null);
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("/bar", result);
|
||||
}
|
||||
@@ -146,11 +165,13 @@ public class RequestHeaderMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
|
||||
public void params(@RequestHeader(name = "name", defaultValue = "bar") String param1,
|
||||
@RequestHeader("name") String[] param2,
|
||||
@RequestHeader(name = "name", defaultValue="#{systemProperties.systemProperty}") String param3,
|
||||
@RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4,
|
||||
@RequestHeader("name") Map<?, ?> unsupported) {
|
||||
public void params(
|
||||
@RequestHeader(name = "name", defaultValue = "bar") String param1,
|
||||
@RequestHeader("name") String[] param2,
|
||||
@RequestHeader(name = "name", defaultValue="#{systemProperties.systemProperty}") String param3,
|
||||
@RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4,
|
||||
@RequestHeader("#{systemProperties.systemProperty}") String param5,
|
||||
@RequestHeader("name") Map<?, ?> unsupported) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user