Browse Source

SPR-8429 Add test cases for missing header, cookie, and pathvar values

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4616 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/merge
Rossen Stoyanchev 15 years ago
parent
commit
b521a6ac7c
  1. 9
      org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/AbstractCookieValueMethodArgumentResolver.java
  2. 10
      org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/ErrorsMethodArgumentResolver.java
  3. 9
      org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/RequestHeaderMethodArgumentResolver.java
  4. 2
      org.springframework.web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java
  5. 10
      org.springframework.web/src/test/java/org/springframework/web/method/annotation/support/CookieValueMethodArgumentResolverTests.java
  6. 10
      org.springframework.web/src/test/java/org/springframework/web/method/annotation/support/RequestHeaderMethodArgumentResolverTests.java

9
org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/AbstractCookieValueMethodArgumentResolver.java

@ -18,6 +18,7 @@ package org.springframework.web.method.annotation.support; @@ -18,6 +18,7 @@ package org.springframework.web.method.annotation.support;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CookieValue;
@ -57,10 +58,10 @@ public abstract class AbstractCookieValueMethodArgumentResolver extends Abstract @@ -57,10 +58,10 @@ public abstract class AbstractCookieValueMethodArgumentResolver extends Abstract
}
@Override
protected void handleMissingValue(String cookieName, MethodParameter parameter) {
String paramTypeName = parameter.getParameterType().getName();
throw new IllegalStateException(
"Missing cookie named '" + cookieName + "' for method parameter type [" + paramTypeName + "]");
protected void handleMissingValue(String cookieName, MethodParameter param) throws ServletRequestBindingException {
String paramType = param.getParameterType().getName();
throw new ServletRequestBindingException(
"Missing cookie named '" + cookieName + "' for method parameter type [" + paramType + "]");
}
private static class CookieValueNamedValueInfo extends NamedValueInfo {

10
org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/ErrorsMethodArgumentResolver.java

@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
package org.springframework.web.method.annotation.support;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.MethodParameter;
import org.springframework.ui.ModelMap;
@ -50,9 +49,9 @@ public class ErrorsMethodArgumentResolver implements HandlerMethodArgumentResolv @@ -50,9 +49,9 @@ public class ErrorsMethodArgumentResolver implements HandlerMethodArgumentResolv
WebDataBinderFactory binderFactory) throws Exception {
ModelMap model = mavContainer.getModel();
if (model.size() > 0) {
List<String> keys = new ArrayList<String>(model.keySet());
String lastKey = keys.get(model.size()-1);
if (isBindingResultKey(lastKey)) {
int lastIndex = model.size()-1;
String lastKey = new ArrayList<String>(model.keySet()).get(lastIndex);
if (lastKey.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
return model.get(lastKey);
}
}
@ -61,7 +60,4 @@ public class ErrorsMethodArgumentResolver implements HandlerMethodArgumentResolv @@ -61,7 +60,4 @@ public class ErrorsMethodArgumentResolver implements HandlerMethodArgumentResolv
+ "without preceding model attribute. Check your handler method signature!");
}
private boolean isBindingResultKey(String key) {
return key.startsWith(BindingResult.MODEL_KEY_PREFIX);
}
}

9
org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/RequestHeaderMethodArgumentResolver.java

@ -20,6 +20,7 @@ import java.util.Map; @@ -20,6 +20,7 @@ import java.util.Map;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.NativeWebRequest;
@ -72,10 +73,10 @@ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMetho @@ -72,10 +73,10 @@ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMetho
}
@Override
protected void handleMissingValue(String headerName, MethodParameter parameter) {
String paramTypeName = parameter.getParameterType().getName();
throw new IllegalStateException(
"Missing header '" + headerName + "' for method parameter type [" + paramTypeName + "]");
protected void handleMissingValue(String headerName, MethodParameter param) throws ServletRequestBindingException {
String paramType = param.getParameterType().getName();
throw new ServletRequestBindingException(
"Missing header '" + headerName + "' for method parameter type [" + paramType + "]");
}
private static class RequestHeaderNamedValueInfo extends NamedValueInfo {

2
org.springframework.web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

@ -165,7 +165,7 @@ public class InvocableHandlerMethod extends HandlerMethod { @@ -165,7 +165,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
* Attempt to resolve a method parameter from the list of provided argument values.
*/
private Object resolveProvidedArgument(MethodParameter parameter, Object... providedArgs) {
if (providedArgs == null) {
if (providedArgs == null || parameter.hasParameterAnnotations()) {
return null;
}
for (Object providedArg : providedArgs) {

10
org.springframework.web/src/test/java/org/springframework/web/method/annotation/support/CookieValueMethodArgumentResolverTests.java

@ -19,6 +19,7 @@ package org.springframework.web.method.annotation.support; @@ -19,6 +19,7 @@ package org.springframework.web.method.annotation.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Method;
@ -29,6 +30,7 @@ import org.junit.Test; @@ -29,6 +30,7 @@ import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
@ -81,12 +83,10 @@ public class CookieValueMethodArgumentResolverTests { @@ -81,12 +83,10 @@ public class CookieValueMethodArgumentResolverTests {
assertEquals("Invalid result", "bar", result);
}
@Test(expected = IllegalStateException.class)
@Test(expected = ServletRequestBindingException.class)
public void notFound() throws Exception {
Object result = resolver.resolveArgument(paramNamedCookie, null, webRequest, null);
assertTrue(result instanceof String);
assertEquals("Invalid result", "bar", result);
resolver.resolveArgument(paramNamedCookie, null, webRequest, null);
fail("Expected exception");
}
private static class TestCookieValueMethodArgumentResolver extends AbstractCookieValueMethodArgumentResolver {

10
org.springframework.web/src/test/java/org/springframework/web/method/annotation/support/RequestHeaderMethodArgumentResolverTests.java

@ -20,6 +20,7 @@ import static org.junit.Assert.assertArrayEquals; @@ -20,6 +20,7 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Method;
import java.util.Map;
@ -30,6 +31,7 @@ import org.junit.Test; @@ -30,6 +31,7 @@ import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestContextHolder;
@ -137,12 +139,10 @@ public class RequestHeaderMethodArgumentResolverTests { @@ -137,12 +139,10 @@ public class RequestHeaderMethodArgumentResolverTests {
assertEquals("/bar", result);
}
@Test(expected = IllegalStateException.class)
@Test(expected = ServletRequestBindingException.class)
public void notFound() throws Exception {
Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
assertTrue(result instanceof String);
assertEquals("Invalid result", "bar", result);
resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
fail("Expected exception");
}
public void params(@RequestHeader(value = "name", defaultValue = "bar") String param1,

Loading…
Cancel
Save