Browse Source

Polishing

pull/940/head
Juergen Hoeller 9 years ago
parent
commit
997278e3ed
  1. 29
      spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
  2. 4
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java
  3. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java

29
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

@ -142,7 +142,7 @@ public class MockHttpServletRequestBuilder
/** /**
* Add a request parameter to the {@link MockHttpServletRequest}. * Add a request parameter to the {@link MockHttpServletRequest}.
* If called more than once, the new values are added. * <p>If called more than once, new values get added to existing ones.
* @param name the parameter name * @param name the parameter name
* @param values one or more values * @param values one or more values
*/ */
@ -152,10 +152,11 @@ public class MockHttpServletRequestBuilder
} }
/** /**
* Add request parameters to the {@link MockHttpServletRequest} for example * Add a map of request parameters to the {@link MockHttpServletRequest},
* such as when testing a form submission. If called more than once, the new * for example when testing a form submission.
* values are added. * <p>If called more than once, new values get added to existing ones.
* @param params the parameters to add * @param params the parameters to add
* @since 4.2.4
*/ */
public MockHttpServletRequestBuilder params(MultiValueMap<String, String> params) { public MockHttpServletRequestBuilder params(MultiValueMap<String, String> params) {
for (String name : params.keySet()) { for (String name : params.keySet()) {
@ -300,7 +301,7 @@ public class MockHttpServletRequestBuilder
* @param value the attribute value * @param value the attribute value
*/ */
public MockHttpServletRequestBuilder requestAttr(String name, Object value) { public MockHttpServletRequestBuilder requestAttr(String name, Object value) {
addAttributeToMap(this.attributes, name, value); addToMap(this.attributes, name, value);
return this; return this;
} }
@ -310,7 +311,7 @@ public class MockHttpServletRequestBuilder
* @param value the session attribute value * @param value the session attribute value
*/ */
public MockHttpServletRequestBuilder sessionAttr(String name, Object value) { public MockHttpServletRequestBuilder sessionAttr(String name, Object value) {
addAttributeToMap(this.sessionAttributes, name, value); addToMap(this.sessionAttributes, name, value);
return this; return this;
} }
@ -332,7 +333,7 @@ public class MockHttpServletRequestBuilder
* @param value the flash attribute value * @param value the flash attribute value
*/ */
public MockHttpServletRequestBuilder flashAttr(String name, Object value) { public MockHttpServletRequestBuilder flashAttr(String name, Object value) {
addAttributeToMap(this.flashAttributes, name, value); addToMap(this.flashAttributes, name, value);
return this; return this;
} }
@ -712,19 +713,19 @@ public class MockHttpServletRequestBuilder
return request; return request;
} }
private static void addToMap(Map<String, Object> map, String name, Object value) {
Assert.hasLength(name, "'name' must not be empty");
Assert.notNull(value, "'value' must not be null");
map.put(name, value);
}
private static <T> void addToMultiValueMap(MultiValueMap<String, T> map, String name, T[] values) { private static <T> void addToMultiValueMap(MultiValueMap<String, T> map, String name, T[] values) {
Assert.hasLength(name, "'name' must not be empty"); Assert.hasLength(name, "'name' must not be empty");
Assert.notNull(values, "'values' is required");
Assert.notEmpty(values, "'values' must not be empty"); Assert.notEmpty(values, "'values' must not be empty");
for (T value : values) { for (T value : values) {
map.add(name, value); map.add(name, value);
} }
} }
private static void addAttributeToMap(Map<String, Object> map, String name, Object value) {
Assert.hasLength(name, "'name' must not be empty");
Assert.notNull(value, "'value' must not be null");
map.put(name, value);
}
} }

4
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java

@ -165,7 +165,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
* URL path to expose for public use. * URL path to expose for public use.
* @param request the current request * @param request the current request
* @param requestUrl the request URL path to resolve * @param requestUrl the request URL path to resolve
* @return the resolved public URL path or {@code null} if unresolved * @return the resolved public URL path, or {@code null} if unresolved
*/ */
public final String getForRequestUrl(HttpServletRequest request, String requestUrl) { public final String getForRequestUrl(HttpServletRequest request, String requestUrl) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
@ -177,7 +177,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
String suffix = requestUrl.substring(suffixIndex); String suffix = requestUrl.substring(suffixIndex);
String lookupPath = requestUrl.substring(prefixIndex, suffixIndex); String lookupPath = requestUrl.substring(prefixIndex, suffixIndex);
String resolvedLookupPath = getForLookupPath(lookupPath); String resolvedLookupPath = getForLookupPath(lookupPath);
return (resolvedLookupPath != null) ? prefix + resolvedLookupPath + suffix : null; return (resolvedLookupPath != null ? prefix + resolvedLookupPath + suffix : null);
} }
private int getLookupPathIndex(HttpServletRequest request) { private int getLookupPathIndex(HttpServletRequest request) {

2
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java

@ -168,7 +168,7 @@ public class VersionResourceResolver extends AbstractResourceResolver {
String actualVersion = versionStrategy.getResourceVersion(baseResource); String actualVersion = versionStrategy.getResourceVersion(baseResource);
if (candidateVersion.equals(actualVersion)) { if (candidateVersion.equals(actualVersion)) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Resource matches extracted version: "+ candidateVersion); logger.trace("Resource matches extracted version ["+ candidateVersion + "]");
} }
if (request != null) { if (request != null) {
request.setAttribute(RESOURCE_VERSION_ATTRIBUTE, candidateVersion); request.setAttribute(RESOURCE_VERSION_ATTRIBUTE, candidateVersion);

Loading…
Cancel
Save