Polishing (backports from 4.0.1)

This commit is contained in:
Juergen Hoeller 2014-01-13 23:36:33 +01:00
parent 1e0bf493cc
commit 354917c53d
6 changed files with 58 additions and 115 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,7 +32,7 @@ import org.springframework.expression.TypedValue;
public class EnvironmentAccessor implements PropertyAccessor { public class EnvironmentAccessor implements PropertyAccessor {
public Class<?>[] getSpecificTargetClasses() { public Class<?>[] getSpecificTargetClasses() {
return new Class[] { Environment.class }; return new Class<?>[] {Environment.class};
} }
/** /**
@ -48,19 +48,18 @@ public class EnvironmentAccessor implements PropertyAccessor {
* environment. * environment.
*/ */
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(((Environment)target).getProperty(name)); return new TypedValue(((Environment) target).getProperty(name));
} }
/** /**
* Read only. * Read-only: returns {@code false}.
* @return false
*/ */
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
return false; return false;
} }
/** /**
* Read only. No-op. * Read-only: no-op.
*/ */
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException { public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
} }

View File

@ -247,10 +247,8 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
* transaction in case of a freshly obtained JMS {@code Connection} and {@code Session}, * transaction in case of a freshly obtained JMS {@code Connection} and {@code Session},
* which is why this listener container by default does not cache any of those. * which is why this listener container by default does not cache any of those.
* However, if you want to optimize for a specific server, consider switching * However, if you want to optimize for a specific server, consider switching
* this setting to at least {@link #CACHE_CONNECTION} or {@link #CACHE_SESSION} even in * this setting to at least {@link #CACHE_CONNECTION} or {@link #CACHE_SESSION}
* conjunction with an external transaction manager. * even in conjunction with an external transaction manager.
* <p>Currently known servers that absolutely require {@link #CACHE_NONE} for XA
* transaction processing: JBoss 4. For any others, consider raising the cache level.
* @see #CACHE_NONE * @see #CACHE_NONE
* @see #CACHE_CONNECTION * @see #CACHE_CONNECTION
* @see #CACHE_SESSION * @see #CACHE_SESSION

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -305,9 +305,11 @@ public class DefaultPersistenceUnitManager
/** /**
* Specify the Spring LoadTimeWeaver to use for class instrumentation according * Specify the Spring LoadTimeWeaver to use for class instrumentation according
* to the JPA class transformer contract. * to the JPA class transformer contract.
* <p>It is not required to specify a LoadTimeWeaver: Most providers will be * <p>It is not required to specify a LoadTimeWeaver: Most providers will be able
* able to provide a subset of their functionality without class instrumentation * to provide a subset of their functionality without class instrumentation as well,
* as well, or operate with their VM agent specified on JVM startup. * or operate with their own VM agent specified on JVM startup. Furthermore,
* DefaultPersistenceUnitManager falls back to an InstrumentationLoadTimeWeaver
* if Spring's agent-based instrumentation is available at runtime.
* <p>In terms of Spring-provided weaving options, the most important ones are * <p>In terms of Spring-provided weaving options, the most important ones are
* InstrumentationLoadTimeWeaver, which requires a Spring-specific (but very general) * InstrumentationLoadTimeWeaver, which requires a Spring-specific (but very general)
* VM agent specified on JVM startup, and ReflectiveLoadTimeWeaver, which interacts * VM agent specified on JVM startup, and ReflectiveLoadTimeWeaver, which interacts
@ -315,12 +317,11 @@ public class DefaultPersistenceUnitManager
* on it (for example, interacting with Spring's TomcatInstrumentableClassLoader). * on it (for example, interacting with Spring's TomcatInstrumentableClassLoader).
* <p><b>NOTE:</b> As of Spring 2.5, the context's default LoadTimeWeaver (defined * <p><b>NOTE:</b> As of Spring 2.5, the context's default LoadTimeWeaver (defined
* as bean with name "loadTimeWeaver") will be picked up automatically, if available, * as bean with name "loadTimeWeaver") will be picked up automatically, if available,
* removing the need for LoadTimeWeaver configuration on each affected target bean.</b> * removing the need for LoadTimeWeaver configuration on each affected target bean.
* Consider using the {@code context:load-time-weaver} XML tag for creating * Consider using the {@code context:load-time-weaver} XML tag for creating
* such a shared LoadTimeWeaver (autodetecting the environment by default). * such a shared LoadTimeWeaver (autodetecting the environment by default).
* @see org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver * @see org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver
* @see org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver * @see org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver
* @see org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader
*/ */
public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) { public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
this.loadTimeWeaver = loadTimeWeaver; this.loadTimeWeaver = loadTimeWeaver;
@ -447,7 +448,7 @@ public class DefaultPersistenceUnitManager
} }
} }
catch (IOException ex) { catch (IOException ex) {
throw new PersistenceException("Failed to scan classpath for unlisted classes", ex); throw new PersistenceException("Failed to scan classpath for unlisted entity classes", ex);
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -69,16 +69,17 @@ import org.springframework.web.util.UriUtils;
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.2 * @since 3.2
*/ */
public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable { public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable {
static final boolean servlet3Present = ClassUtils.hasMethod(ServletRequest.class, "startAsync"); static final boolean servlet3Present = ClassUtils.hasMethod(ServletRequest.class, "startAsync");
private final UriComponents uriComponents;
private final HttpMethod method; private final HttpMethod method;
private final UriComponents uriComponents;
private final MultiValueMap<String, Object> headers = new LinkedMultiValueMap<String, Object>(); private final MultiValueMap<String, Object> headers = new LinkedMultiValueMap<String, Object>();
private String contentType; private String contentType;
@ -93,10 +94,10 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
private String characterEncoding; private String characterEncoding;
private Principal principal;
private Boolean secure; private Boolean secure;
private Principal principal;
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>(); private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
private MockHttpSession session; private MockHttpSession session;
@ -111,34 +112,28 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
private String pathInfo = ValueConstants.DEFAULT_NONE; private String pathInfo = ValueConstants.DEFAULT_NONE;
private final List<RequestPostProcessor> postProcessors = private final List<RequestPostProcessor> postProcessors = new ArrayList<RequestPostProcessor>();
new ArrayList<RequestPostProcessor>();
/** /**
* Package private constructor. To get an instance, use static factory * Package private constructor. To get an instance, use static factory
* methods in {@link MockMvcRequestBuilders}. * methods in {@link MockMvcRequestBuilders}.
*
* <p>Although this class cannot be extended, additional ways to initialize * <p>Although this class cannot be extended, additional ways to initialize
* the {@code MockHttpServletRequest} can be plugged in via * the {@code MockHttpServletRequest} can be plugged in via
* {@link #with(RequestPostProcessor)}. * {@link #with(RequestPostProcessor)}.
*
* @param urlTemplate a URL template; the resulting URL will be encoded * @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables * @param urlVariables zero or more URL variables
*/ */
MockHttpServletRequestBuilder(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) { MockHttpServletRequestBuilder(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) {
Assert.notNull(urlTemplate, "uriTemplate is required");
Assert.notNull(httpMethod, "httpMethod is required"); Assert.notNull(httpMethod, "httpMethod is required");
Assert.notNull(urlTemplate, "uriTemplate is required");
this.uriComponents = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(urlVariables).encode();
this.method = httpMethod; this.method = httpMethod;
this.uriComponents = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(urlVariables).encode();
} }
/** /**
* 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. * If called more than once, the new values are added.
*
* @param name the parameter name * @param name the parameter name
* @param values one or more values * @param values one or more values
*/ */
@ -149,7 +144,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Add a header to the request. Values are always added. * Add a header to the request. Values are always added.
*
* @param name the header name * @param name the header name
* @param values one or more header values * @param values one or more header values
*/ */
@ -160,7 +154,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Add all headers to the request. Values are always added. * Add all headers to the request. Values are always added.
*
* @param httpHeaders the headers and values to add * @param httpHeaders the headers and values to add
*/ */
public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) { public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) {
@ -173,7 +166,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set the 'Content-Type' header of the request. * Set the 'Content-Type' header of the request.
*
* @param mediaType the content type * @param mediaType the content type
*/ */
public MockHttpServletRequestBuilder contentType(MediaType mediaType) { public MockHttpServletRequestBuilder contentType(MediaType mediaType) {
@ -185,7 +177,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set the 'Accept' header to the given media type(s). * Set the 'Accept' header to the given media type(s).
*
* @param mediaTypes one or more media types * @param mediaTypes one or more media types
*/ */
public MockHttpServletRequestBuilder accept(MediaType... mediaTypes) { public MockHttpServletRequestBuilder accept(MediaType... mediaTypes) {
@ -196,7 +187,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set the request body. * Set the request body.
*
* @param content the body content * @param content the body content
*/ */
public MockHttpServletRequestBuilder content(byte[] content) { public MockHttpServletRequestBuilder content(byte[] content) {
@ -206,7 +196,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set the request body as a UTF-8 String. * Set the request body as a UTF-8 String.
*
* @param content the body content * @param content the body content
*/ */
public MockHttpServletRequestBuilder content(String content) { public MockHttpServletRequestBuilder content(String content) {
@ -221,7 +210,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Add the given cookies to the request. Cookies are always added. * Add the given cookies to the request. Cookies are always added.
*
* @param cookies the cookies to add * @param cookies the cookies to add
*/ */
public MockHttpServletRequestBuilder cookie(Cookie... cookies) { public MockHttpServletRequestBuilder cookie(Cookie... cookies) {
@ -233,7 +221,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set the locale of the request. * Set the locale of the request.
*
* @param locale the locale * @param locale the locale
*/ */
public MockHttpServletRequestBuilder locale(Locale locale) { public MockHttpServletRequestBuilder locale(Locale locale) {
@ -243,7 +230,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set the character encoding of the request. * Set the character encoding of the request.
*
* @param encoding the character encoding * @param encoding the character encoding
*/ */
public MockHttpServletRequestBuilder characterEncoding(String encoding) { public MockHttpServletRequestBuilder characterEncoding(String encoding) {
@ -253,7 +239,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set a request attribute. * Set a request attribute.
*
* @param name the attribute name * @param name the attribute name
* @param value the attribute value * @param value the attribute value
*/ */
@ -264,7 +249,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set a session attribute. * Set a session attribute.
*
* @param name the session attribute name * @param name the session attribute name
* @param value the session attribute value * @param value the session attribute value
*/ */
@ -275,7 +259,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set session attributes. * Set session attributes.
*
* @param sessionAttributes the session attributes * @param sessionAttributes the session attributes
*/ */
public MockHttpServletRequestBuilder sessionAttrs(Map<String, Object> sessionAttributes) { public MockHttpServletRequestBuilder sessionAttrs(Map<String, Object> sessionAttributes) {
@ -288,7 +271,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set an "input" flash attribute. * Set an "input" flash attribute.
*
* @param name the flash attribute name * @param name the flash attribute name
* @param value the flash attribute value * @param value the flash attribute value
*/ */
@ -299,7 +281,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set flash attributes. * Set flash attributes.
*
* @param flashAttributes the flash attributes * @param flashAttributes the flash attributes
*/ */
public MockHttpServletRequestBuilder flashAttrs(Map<String, Object> flashAttributes) { public MockHttpServletRequestBuilder flashAttrs(Map<String, Object> flashAttributes) {
@ -312,10 +293,8 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set the HTTP session to use, possibly re-used across requests. * Set the HTTP session to use, possibly re-used across requests.
*
* <p>Individual attributes provided via {@link #sessionAttr(String, Object)} * <p>Individual attributes provided via {@link #sessionAttr(String, Object)}
* override the content of the session provided here. * override the content of the session provided here.
*
* @param session the HTTP session * @param session the HTTP session
*/ */
public MockHttpServletRequestBuilder session(MockHttpSession session) { public MockHttpServletRequestBuilder session(MockHttpSession session) {
@ -326,7 +305,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set the principal of the request. * Set the principal of the request.
*
* @param principal the principal * @param principal the principal
*/ */
public MockHttpServletRequestBuilder principal(Principal principal) { public MockHttpServletRequestBuilder principal(Principal principal) {
@ -337,16 +315,12 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Specify the portion of the requestURI that represents the context path. * Specify the portion of the requestURI that represents the context path.
* The context path, if specified, must match to the start of the request * The context path, if specified, must match to the start of the request URI.
* URI.
*
* <p>In most cases, tests can be written by omitting the context path from * <p>In most cases, tests can be written by omitting the context path from
* the requestURI. This is because most applications don't actually depend * the requestURI. This is because most applications don't actually depend
* on the name under which they're deployed. If specified here, the context * on the name under which they're deployed. If specified here, the context
* path must start with a "/" and must not end with a "/". * path must start with a "/" and must not end with a "/".
* * @see <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getContextPath%28%29">HttpServletRequest.getContextPath()</a>
* @see <a
* href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getContextPath%28%29">HttpServletRequest.getContextPath()</a>
*/ */
public MockHttpServletRequestBuilder contextPath(String contextPath) { public MockHttpServletRequestBuilder contextPath(String contextPath) {
if (StringUtils.hasText(contextPath)) { if (StringUtils.hasText(contextPath)) {
@ -361,7 +335,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
* Specify the portion of the requestURI that represents the path to which * Specify the portion of the requestURI that represents the path to which
* the Servlet is mapped. This is typically a portion of the requestURI * the Servlet is mapped. This is typically a portion of the requestURI
* after the context path. * after the context path.
*
* <p>In most cases, tests can be written by omitting the servlet path from * <p>In most cases, tests can be written by omitting the servlet path from
* the requestURI. This is because most applications don't actually depend * the requestURI. This is because most applications don't actually depend
* on the prefix to which a servlet is mapped. For example if a Servlet is * on the prefix to which a servlet is mapped. For example if a Servlet is
@ -369,9 +342,7 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
* {@code "/accounts/1"} as opposed to {@code "/main/accounts/1"}. * {@code "/accounts/1"} as opposed to {@code "/main/accounts/1"}.
* If specified here, the servletPath must start with a "/" and must not * If specified here, the servletPath must start with a "/" and must not
* end with a "/". * end with a "/".
* * @see <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getServletPath%28%29">HttpServletRequest.getServletPath()</a>
* @see <a
* href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getServletPath%28%29">HttpServletRequest.getServletPath()</a>
*/ */
public MockHttpServletRequestBuilder servletPath(String servletPath) { public MockHttpServletRequestBuilder servletPath(String servletPath) {
if (StringUtils.hasText(servletPath)) { if (StringUtils.hasText(servletPath)) {
@ -384,16 +355,12 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Specify the portion of the requestURI that represents the pathInfo. * Specify the portion of the requestURI that represents the pathInfo.
*
* <p>If left unspecified (recommended), the pathInfo will be automatically * <p>If left unspecified (recommended), the pathInfo will be automatically
* derived by removing the contextPath and the servletPath from the * derived by removing the contextPath and the servletPath from the
* requestURI and using any remaining part. If specified here, the pathInfo * requestURI and using any remaining part. If specified here, the pathInfo
* must start with a "/". * must start with a "/".
*
* <p>If specified, the pathInfo will be used as is. * <p>If specified, the pathInfo will be used as is.
* * @see <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getPathInfo%28%29">HttpServletRequest.getServletPath()</a>
* @see <a
* href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getPathInfo%28%29">HttpServletRequest.getServletPath()</a>
*/ */
public MockHttpServletRequestBuilder pathInfo(String pathInfo) { public MockHttpServletRequestBuilder pathInfo(String pathInfo) {
if (StringUtils.hasText(pathInfo)) { if (StringUtils.hasText(pathInfo)) {
@ -406,7 +373,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Set the secure property of the {@link ServletRequest} indicating use of a * Set the secure property of the {@link ServletRequest} indicating use of a
* secure channel, such as HTTPS. * secure channel, such as HTTPS.
*
* @param secure whether the request is using a secure channel * @param secure whether the request is using a secure channel
*/ */
public MockHttpServletRequestBuilder secure(boolean secure){ public MockHttpServletRequestBuilder secure(boolean secure){
@ -419,7 +385,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
* in ways not built directly into the {@code MockHttpServletRequestBuilder}. * in ways not built directly into the {@code MockHttpServletRequestBuilder}.
* Implementation of this interface can have builder-style methods themselves * Implementation of this interface can have builder-style methods themselves
* and be made accessible through static factory methods. * and be made accessible through static factory methods.
*
* @param postProcessor a post-processor to add * @param postProcessor a post-processor to add
*/ */
public MockHttpServletRequestBuilder with(RequestPostProcessor postProcessor) { public MockHttpServletRequestBuilder with(RequestPostProcessor postProcessor) {
@ -439,7 +404,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
/** /**
* Merges the properties of the "parent" RequestBuilder accepting values * Merges the properties of the "parent" RequestBuilder accepting values
* only if not already set in "this" instance. * only if not already set in "this" instance.
*
* @param parent the parent {@code RequestBuilder} to inherit properties from * @param parent the parent {@code RequestBuilder} to inherit properties from
* @return the result of the merge * @return the result of the merge
*/ */
@ -450,7 +414,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
if (!(parent instanceof MockHttpServletRequestBuilder)) { if (!(parent instanceof MockHttpServletRequestBuilder)) {
throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]"); throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]");
} }
MockHttpServletRequestBuilder parentBuilder = (MockHttpServletRequestBuilder) parent; MockHttpServletRequestBuilder parentBuilder = (MockHttpServletRequestBuilder) parent;
for (String headerName : parentBuilder.headers.keySet()) { for (String headerName : parentBuilder.headers.keySet()) {
@ -462,7 +425,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
if (this.contentType == null) { if (this.contentType == null) {
this.contentType = parentBuilder.contentType; this.contentType = parentBuilder.contentType;
} }
if (this.content == null) { if (this.content == null) {
this.content = parentBuilder.content; this.content = parentBuilder.content;
} }
@ -472,7 +434,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
this.parameters.put(paramName, parentBuilder.parameters.get(paramName)); this.parameters.put(paramName, parentBuilder.parameters.get(paramName));
} }
} }
for (Cookie cookie : parentBuilder.cookies) { for (Cookie cookie : parentBuilder.cookies) {
if (!containsCookie(cookie)) { if (!containsCookie(cookie)) {
this.cookies.add(cookie); this.cookies.add(cookie);
@ -482,18 +443,16 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
if (this.locale == null) { if (this.locale == null) {
this.locale = parentBuilder.locale; this.locale = parentBuilder.locale;
} }
if (this.characterEncoding == null) { if (this.characterEncoding == null) {
this.characterEncoding = parentBuilder.characterEncoding; this.characterEncoding = parentBuilder.characterEncoding;
} }
if (this.principal == null) {
this.principal = parentBuilder.principal;
}
if (this.secure == null) { if (this.secure == null) {
this.secure = parentBuilder.secure; this.secure = parentBuilder.secure;
} }
if (this.principal == null) {
this.principal = parentBuilder.principal;
}
for (String attributeName : parentBuilder.attributes.keySet()) { for (String attributeName : parentBuilder.attributes.keySet()) {
if (!this.attributes.containsKey(attributeName)) { if (!this.attributes.containsKey(attributeName)) {
@ -535,8 +494,8 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
} }
private boolean containsCookie(Cookie cookie) { private boolean containsCookie(Cookie cookie) {
for (Cookie c : this.cookies) { for (Cookie cookieToCheck : this.cookies) {
if (ObjectUtils.nullSafeEquals(c.getName(), cookie.getName())) { if (ObjectUtils.nullSafeEquals(cookieToCheck.getName(), cookie.getName())) {
return true; return true;
} }
} }
@ -547,12 +506,10 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
* Build a {@link MockHttpServletRequest}. * Build a {@link MockHttpServletRequest}.
*/ */
public final MockHttpServletRequest buildRequest(ServletContext servletContext) { public final MockHttpServletRequest buildRequest(ServletContext servletContext) {
MockHttpServletRequest request = createServletRequest(servletContext); MockHttpServletRequest request = createServletRequest(servletContext);
String requestUri = this.uriComponents.getPath(); String requestUri = this.uriComponents.getPath();
request.setRequestURI(requestUri); request.setRequestURI(requestUri);
updatePathRequestProperties(request, requestUri); updatePathRequestProperties(request, requestUri);
if (this.uriComponents.getScheme() != null) { if (this.uriComponents.getScheme() != null) {
@ -566,7 +523,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
} }
request.setMethod(this.method.name()); request.setMethod(this.method.name());
for (String name : this.headers.keySet()) { for (String name : this.headers.keySet()) {
for (Object value : this.headers.get(name)) { for (Object value : this.headers.get(name)) {
request.addHeader(name, value); request.addHeader(name, value);
@ -598,31 +554,26 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
request.setContentType(this.contentType); request.setContentType(this.contentType);
request.setContent(this.content); request.setContent(this.content);
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()])); request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
if (this.locale != null) { if (this.locale != null) {
request.addPreferredLocale(this.locale); request.addPreferredLocale(this.locale);
} }
request.setCharacterEncoding(this.characterEncoding); request.setCharacterEncoding(this.characterEncoding);
request.setUserPrincipal(this.principal);
if (this.secure != null) { if (this.secure != null) {
request.setSecure(this.secure); request.setSecure(this.secure);
} }
request.setUserPrincipal(this.principal);
for (String name : this.attributes.keySet()) { for (String name : this.attributes.keySet()) {
request.setAttribute(name, this.attributes.get(name)); request.setAttribute(name, this.attributes.get(name));
} }
// Set session before session and flash attributes // Set session before session and flash attributes
if (this.session != null) { if (this.session != null) {
request.setSession(this.session); request.setSession(this.session);
} }
for (String name : this.sessionAttributes.keySet()) { for (String name : this.sessionAttributes.keySet()) {
request.getSession().setAttribute(name, this.sessionAttributes.get(name)); request.getSession().setAttribute(name, this.sessionAttributes.get(name));
} }
@ -634,21 +585,22 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
flashMapManager.saveOutputFlashMap(flashMap, request, new MockHttpServletResponse()); flashMapManager.saveOutputFlashMap(flashMap, request, new MockHttpServletResponse());
// Apply post-processors at the very end // Apply post-processors at the very end
for (RequestPostProcessor postProcessor : this.postProcessors) { for (RequestPostProcessor postProcessor : this.postProcessors) {
request = postProcessor.postProcessRequest(request); request = postProcessor.postProcessRequest(request);
Assert.notNull(request, "Post-processor [" + postProcessor.getClass().getName() + "] returned null"); if (request == null) {
throw new IllegalStateException("Post-processor [" + postProcessor.getClass().getName() + "] returned null");
}
} }
return request; return request;
} }
/** /**
* Creates a new {@link MockHttpServletRequest} based on the given * Create a new {@link MockHttpServletRequest} based on the given
* {@link ServletContext}. Can be overridden in sub-classes. * {@link ServletContext}. Can be overridden in subclasses.
*/ */
protected MockHttpServletRequest createServletRequest(ServletContext servletContext) { protected MockHttpServletRequest createServletRequest(ServletContext servletContext) {
return servlet3Present ? createServlet3Request(servletContext) : new MockHttpServletRequest(servletContext); return (servlet3Present ? createServlet3Request(servletContext) : new MockHttpServletRequest(servletContext));
} }
private MockHttpServletRequest createServlet3Request(ServletContext servletContext) { private MockHttpServletRequest createServlet3Request(ServletContext servletContext) {
@ -658,8 +610,8 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
Constructor<?> constructor = clazz.getConstructor(ServletContext.class); Constructor<?> constructor = clazz.getConstructor(ServletContext.class);
return (MockHttpServletRequest) BeanUtils.instantiateClass(constructor, servletContext); return (MockHttpServletRequest) BeanUtils.instantiateClass(constructor, servletContext);
} }
catch (Throwable t) { catch (Throwable ex) {
throw new IllegalStateException("Failed to instantiate MockHttpServletRequest", t); throw new IllegalStateException("Failed to instantiate MockHttpServletRequest", ex);
} }
} }
@ -667,22 +619,16 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
* Update the contextPath, servletPath, and pathInfo of the request. * Update the contextPath, servletPath, and pathInfo of the request.
*/ */
private void updatePathRequestProperties(MockHttpServletRequest request, String requestUri) { private void updatePathRequestProperties(MockHttpServletRequest request, String requestUri) {
Assert.isTrue(requestUri.startsWith(this.contextPath), Assert.isTrue(requestUri.startsWith(this.contextPath),
"requestURI [" + requestUri + "] does not start with contextPath [" + this.contextPath + "]"); "requestURI [" + requestUri + "] does not start with contextPath [" + this.contextPath + "]");
request.setContextPath(this.contextPath); request.setContextPath(this.contextPath);
request.setServletPath(this.servletPath); request.setServletPath(this.servletPath);
if (ValueConstants.DEFAULT_NONE.equals(this.pathInfo)) { if (ValueConstants.DEFAULT_NONE.equals(this.pathInfo)) {
Assert.isTrue(requestUri.startsWith(this.contextPath + this.servletPath), Assert.isTrue(requestUri.startsWith(this.contextPath + this.servletPath),
"Invalid servletPath [" + this.servletPath + "] for requestURI [" + requestUri + "]"); "Invalid servletPath [" + this.servletPath + "] for requestURI [" + requestUri + "]");
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length()); String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
this.pathInfo = (StringUtils.hasText(extraPath)) ? extraPath : null; this.pathInfo = (StringUtils.hasText(extraPath)) ? extraPath : null;
} }
request.setPathInfo(this.pathInfo); request.setPathInfo(this.pathInfo);
} }
@ -694,10 +640,12 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
flashMapManager = wac.getBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, FlashMapManager.class); flashMapManager = wac.getBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, FlashMapManager.class);
} }
catch (IllegalStateException ex) { catch (IllegalStateException ex) {
// ignore
} }
catch (NoSuchBeanDefinitionException ex) { catch (NoSuchBeanDefinitionException ex) {
// ignore
} }
return (flashMapManager != null) ? flashMapManager : new SessionFlashMapManager(); return (flashMapManager != null ? flashMapManager : new SessionFlashMapManager());
} }
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) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -43,13 +43,11 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
/** /**
* Package private constructor. Use static factory methods in * Package-private constructor. Use static factory methods in
* {@link MockMvcRequestBuilders}. * {@link MockMvcRequestBuilders}.
*
* <p>For other ways to initialize a {@code MockMultipartHttpServletRequest}, * <p>For other ways to initialize a {@code MockMultipartHttpServletRequest},
* see {@link #with(RequestPostProcessor)} and the * see {@link #with(RequestPostProcessor)} and the
* {@link RequestPostProcessor} extension point. * {@link RequestPostProcessor} extension point.
*
* @param urlTemplate a URL template; the resulting URL will be encoded * @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables * @param urlVariables zero or more URL variables
*/ */
@ -58,9 +56,9 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
super.contentType(MediaType.MULTIPART_FORM_DATA); super.contentType(MediaType.MULTIPART_FORM_DATA);
} }
/** /**
* Create a new MockMultipartFile with the given content. * Create a new MockMultipartFile with the given content.
*
* @param name the name of the file * @param name the name of the file
* @param content the content of the file * @param content the content of the file
*/ */
@ -71,7 +69,6 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
/** /**
* Add the given MockMultipartFile. * Add the given MockMultipartFile.
*
* @param file the multipart file * @param file the multipart file
*/ */
public MockMultipartHttpServletRequestBuilder file(MockMultipartFile file) { public MockMultipartHttpServletRequestBuilder file(MockMultipartFile file) {
@ -99,8 +96,8 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
@Override @Override
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) { protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
MockMultipartHttpServletRequest request = servlet3Present ? MockMultipartHttpServletRequest request =
createServlet3Request() : new MockMultipartHttpServletRequest(); (servlet3Present ? createServlet3Request() : new MockMultipartHttpServletRequest());
for (MockMultipartFile file : this.files) { for (MockMultipartFile file : this.files) {
request.addFile(file); request.addFile(file);
} }
@ -114,8 +111,8 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
Constructor<?> constructor = clazz.getDeclaredConstructor(); Constructor<?> constructor = clazz.getDeclaredConstructor();
return (MockMultipartHttpServletRequest) BeanUtils.instantiateClass(constructor); return (MockMultipartHttpServletRequest) BeanUtils.instantiateClass(constructor);
} }
catch (Throwable t) { catch (Throwable ex) {
throw new IllegalStateException("Failed to instantiate MockHttpServletRequest", t); throw new IllegalStateException("Failed to instantiate MockMultipartHttpServletRequest", ex);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.test.web.servlet.request; package org.springframework.test.web.servlet.request;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
@ -20,12 +21,12 @@ import org.springframework.mock.web.MockHttpServletRequest;
/** /**
* Extension point for applications or 3rd party libraries that wish to further * Extension point for applications or 3rd party libraries that wish to further
* initialize a {@link MockHttpServletRequest} instance after it has been built * initialize a {@link MockHttpServletRequest} instance after it has been built
* by {@link MockHttpServletRequestBuilder} or its sub-class * by {@link MockHttpServletRequestBuilder} or its subclass
* {@link MockMultipartHttpServletRequestBuilder}. * {@link MockMultipartHttpServletRequestBuilder}.
* *
* <p>Implementations of this interface can be provided to * <p>Implementations of this interface can be provided to
* {@link MockHttpServletRequestBuilder#with(RequestPostProcessor)} at the time * {@link MockHttpServletRequestBuilder#with(RequestPostProcessor)} at the time
* when a request is about to be performed. * when a request is about to be constructed.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Rob Winch * @author Rob Winch
@ -36,9 +37,8 @@ public interface RequestPostProcessor {
/** /**
* Post-process the given {@code MockHttpServletRequest} after its creation * Post-process the given {@code MockHttpServletRequest} after its creation
* and initialization through a {@code MockHttpServletRequestBuilder}. * and initialization through a {@code MockHttpServletRequestBuilder}.
*
* @param request the request to initialize * @param request the request to initialize
* @return the request to use, either the one passed in or a wrapped one; * @return the request to use, either the one passed in or a wrapped one
*/ */
MockHttpServletRequest postProcessRequest(MockHttpServletRequest request); MockHttpServletRequest postProcessRequest(MockHttpServletRequest request);