Polishing

This commit is contained in:
Juergen Hoeller
2016-01-26 22:23:56 +01:00
parent a58eee6ad1
commit 30ef893c28
18 changed files with 161 additions and 167 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 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.
@@ -55,7 +55,7 @@ public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConve
/**
* Construct a new {@code MarshallingHttpMessageConverter} with no {@link Marshaller} or
* {@link Unmarshaller} set. The Marshaller and Unmarshaller must be set after construction
* by invoking {@link #setMarshaller(Marshaller)} and {@link #setUnmarshaller(Unmarshaller)} .
* by invoking {@link #setMarshaller(Marshaller)} and {@link #setUnmarshaller(Unmarshaller)}.
*/
public MarshallingHttpMessageConverter() {
}
@@ -104,14 +104,15 @@ public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConve
this.unmarshaller = unmarshaller;
}
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return canRead(mediaType) && (this.unmarshaller != null) && this.unmarshaller.supports(clazz);
return (canRead(mediaType) && this.unmarshaller != null && this.unmarshaller.supports(clazz));
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return canWrite(mediaType) && (this.marshaller != null) && this.marshaller.supports(clazz);
return (canWrite(mediaType) && this.marshaller != null && this.marshaller.supports(clazz));
}
@Override

View File

@@ -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.
@@ -55,7 +55,6 @@ import org.springframework.web.multipart.MultipartResolver;
* @author Arjen Poutsma
* @author Sam Brannen
* @since 3.1
*
* @see RequestParam
* @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
*/

View File

@@ -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.
@@ -262,7 +262,8 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
}
RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ? parameter.getParameterName() : requestParam.name());
String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ?
parameter.getParameterName() : requestParam.name());
if (value == null) {
builder.queryParam(name);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 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.
@@ -24,27 +24,28 @@ import org.springframework.web.multipart.MultipartResolver;
* Raised when the part of a "multipart/form-data" request identified by its
* name cannot be found.
*
* <p>This may be because the request is not a multipart/form-data
*
* either because the part is not present in the request, or
* because the web application is not configured correctly for processing
* multipart requests -- e.g. no {@link MultipartResolver}.
* <p>This may be because the request is not a multipart/form-data request,
* because the part is not present in the request, or because the web
* application is not configured correctly for processing multipart requests,
* e.g. no {@link MultipartResolver}.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
@SuppressWarnings("serial")
public class MissingServletRequestPartException extends ServletException {
private static final long serialVersionUID = -1255077391966870705L;
private final String partName;
public MissingServletRequestPartException(String partName) {
super("Required request part '" + partName + "' is not present.");
super("Required request part '" + partName + "' is not present");
this.partName = partName;
}
public String getRequestPartName() {
return this.partName;
}
}

View File

@@ -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.
@@ -33,7 +33,6 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.util.WebUtils;
/**
* {@link ServerHttpRequest} implementation that accesses one part of a multipart
* request. If using {@link MultipartResolver} configuration the part is accessed
@@ -54,8 +53,8 @@ public class RequestPartServletServerHttpRequest extends ServletServerHttpReques
/**
* Create a new instance.
* @param request the current request
* Create a new {@code RequestPartServletServerHttpRequest} instance.
* @param request the current servlet request
* @param partName the name of the part to adapt to the {@link ServerHttpRequest} contract
* @throws MissingServletRequestPartException if the request part cannot be found
* @throws IllegalArgumentException if MultipartHttpServletRequest cannot be initialized

View File

@@ -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.
@@ -87,10 +87,10 @@ public class RequestParamMethodArgumentResolverTests {
private MockHttpServletRequest request;
@Before
public void setUp() throws Exception {
resolver = new RequestParamMethodArgumentResolver(null, true);
ParameterNameDiscoverer paramNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
Method method = getClass().getMethod("params", String.class, String[].class,
@@ -126,6 +126,7 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}
@Test
public void supportsParameter() {
resolver = new RequestParamMethodArgumentResolver(null, true);
@@ -154,18 +155,16 @@ public class RequestParamMethodArgumentResolverTests {
request.addParameter("name", expected);
Object result = resolver.resolveArgument(paramNamedDefaultValueString, null, webRequest, null);
assertTrue(result instanceof String);
assertEquals("Invalid result", expected, result);
}
@Test
public void resolveStringArray() throws Exception {
String[] expected = new String[]{"foo", "bar"};
String[] expected = new String[] {"foo", "bar"};
request.addParameter("name", expected);
Object result = resolver.resolveArgument(paramNamedStringArray, null, webRequest, null);
assertTrue(result instanceof String[]);
assertArrayEquals("Invalid result", expected, (String[]) result);
}
@@ -178,7 +177,6 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramMultipartFile, null, webRequest, null);
assertTrue(result instanceof MultipartFile);
assertEquals("Invalid result", expected, result);
}
@@ -193,7 +191,6 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramMultipartFileList, null, webRequest, null);
assertTrue(result instanceof List);
assertEquals(Arrays.asList(expected1, expected2), result);
}
@@ -208,9 +205,9 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramMultipartFileArray, null, webRequest, null);
assertTrue(result instanceof MultipartFile[]);
MultipartFile[] parts = (MultipartFile[]) result;
assertEquals(2, parts.length);
assertEquals(parts[0], expected1);
assertEquals(parts[1], expected2);
}
@@ -242,7 +239,6 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramPartList, null, webRequest, null);
assertTrue(result instanceof List);
assertEquals(Arrays.asList(expected1, expected2), result);
}
@@ -259,9 +255,9 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramPartArray, null, webRequest, null);
assertTrue(result instanceof Part[]);
Part[] parts = (Part[]) result;
assertEquals(2, parts.length);
assertEquals(parts[0], expected1);
assertEquals(parts[1], expected2);
}
@@ -274,7 +270,6 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramMultipartFileNotAnnot, null, webRequest, null);
assertTrue(result instanceof MultipartFile);
assertEquals("Invalid result", expected, result);
}
@@ -289,7 +284,6 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramMultipartFileListNotAnnot, null, webRequest, null);
assertTrue(result instanceof List);
assertEquals(Arrays.asList(expected1, expected2), result);
}
@@ -300,9 +294,7 @@ public class RequestParamMethodArgumentResolverTests {
fail("Expected exception: request is not a multipart request");
}
// SPR-9079
@Test
@Test // SPR-9079
public void isMultipartRequestHttpPut() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("multipartFileList", "Hello World".getBytes());
@@ -311,7 +303,6 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request);
Object actual = resolver.resolveArgument(paramMultipartFileListNotAnnot, null, webRequest, null);
assertTrue(actual instanceof List);
assertEquals(expected, ((List<?>) actual).get(0));
}
@@ -334,7 +325,6 @@ public class RequestParamMethodArgumentResolverTests {
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramPartNotAnnot, null, webRequest, null);
assertTrue(result instanceof Part);
assertEquals("Invalid result", expected, result);
}
@@ -342,7 +332,6 @@ public class RequestParamMethodArgumentResolverTests {
@Test
public void resolveDefaultValue() throws Exception {
Object result = resolver.resolveArgument(paramNamedDefaultValueString, null, webRequest, null);
assertTrue(result instanceof String);
assertEquals("Invalid result", "bar", result);
}
@@ -353,11 +342,8 @@ public class RequestParamMethodArgumentResolverTests {
fail("Expected exception");
}
// SPR-10578
@Test
@Test // SPR-10578
public void missingRequestParamEmptyValueConvertedToNull() throws Exception {
WebDataBinder binder = new WebRequestDataBinder(null);
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
@@ -367,13 +353,11 @@ public class RequestParamMethodArgumentResolverTests {
this.request.addParameter("stringNotAnnot", "");
Object arg = resolver.resolveArgument(paramStringNotAnnot, null, webRequest, binderFactory);
assertNull(arg);
}
@Test
public void missingRequestParamEmptyValueNotRequired() throws Exception {
WebDataBinder binder = new WebRequestDataBinder(null);
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
@@ -383,7 +367,6 @@ public class RequestParamMethodArgumentResolverTests {
this.request.addParameter("name", "");
Object arg = resolver.resolveArgument(paramNotRequired, null, webRequest, binderFactory);
assertNull(arg);
}
@@ -396,17 +379,13 @@ public class RequestParamMethodArgumentResolverTests {
assertEquals("plainValue", result);
}
// SPR-8561
@Test
@Test // SPR-8561
public void resolveSimpleTypeParamToNull() throws Exception {
Object result = resolver.resolveArgument(paramStringNotAnnot, null, webRequest, null);
assertNull(result);
}
// SPR-10180
@Test
@Test // SPR-10180
public void resolveEmptyValueToDefault() throws Exception {
this.request.addParameter("name", "");
Object result = resolver.resolveArgument(paramNamedDefaultValueString, null, webRequest, null);
@@ -429,13 +408,12 @@ public class RequestParamMethodArgumentResolverTests {
@Test
@SuppressWarnings("rawtypes")
public void resolveOptional() throws Exception {
public void resolveOptionalParamValue() throws Exception {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
Object result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory);
assertEquals(Optional.class, result.getClass());
assertEquals(Optional.empty(), result);
this.request.addParameter("name", "123");