Polishing (backports from 4.0.1)
This commit is contained in:
parent
1e0bf493cc
commit
354917c53d
|
@ -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");
|
||||
* 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<?>[] getSpecificTargetClasses() {
|
||||
return new Class[] { Environment.class };
|
||||
return new Class<?>[] {Environment.class};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,19 +48,18 @@ public class EnvironmentAccessor implements PropertyAccessor {
|
|||
* environment.
|
||||
*/
|
||||
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.
|
||||
* @return false
|
||||
* Read-only: returns {@code false}.
|
||||
*/
|
||||
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read only. No-op.
|
||||
* Read-only: no-op.
|
||||
*/
|
||||
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
|
||||
}
|
||||
|
|
|
@ -247,10 +247,8 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
|
|||
* 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.
|
||||
* 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
|
||||
* 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.
|
||||
* this setting to at least {@link #CACHE_CONNECTION} or {@link #CACHE_SESSION}
|
||||
* even in conjunction with an external transaction manager.
|
||||
* @see #CACHE_NONE
|
||||
* @see #CACHE_CONNECTION
|
||||
* @see #CACHE_SESSION
|
||||
|
|
|
@ -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");
|
||||
* 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
|
||||
* to the JPA class transformer contract.
|
||||
* <p>It is not required to specify a LoadTimeWeaver: Most providers will be
|
||||
* able to provide a subset of their functionality without class instrumentation
|
||||
* as well, or operate with their VM agent specified on JVM startup.
|
||||
* <p>It is not required to specify a LoadTimeWeaver: Most providers will be able
|
||||
* to provide a subset of their functionality without class instrumentation as well,
|
||||
* 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
|
||||
* InstrumentationLoadTimeWeaver, which requires a Spring-specific (but very general)
|
||||
* 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).
|
||||
* <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,
|
||||
* 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
|
||||
* such a shared LoadTimeWeaver (autodetecting the environment by default).
|
||||
* @see org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver
|
||||
* @see org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver
|
||||
* @see org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader
|
||||
*/
|
||||
public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
|
||||
this.loadTimeWeaver = loadTimeWeaver;
|
||||
|
@ -447,7 +448,7 @@ public class DefaultPersistenceUnitManager
|
|||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
* 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 Arjen Poutsma
|
||||
* @since 3.2
|
||||
*/
|
||||
* @since 3.2
|
||||
*/
|
||||
public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable {
|
||||
|
||||
static final boolean servlet3Present = ClassUtils.hasMethod(ServletRequest.class, "startAsync");
|
||||
|
||||
private final UriComponents uriComponents;
|
||||
|
||||
private final HttpMethod method;
|
||||
|
||||
private final UriComponents uriComponents;
|
||||
|
||||
private final MultiValueMap<String, Object> headers = new LinkedMultiValueMap<String, Object>();
|
||||
|
||||
private String contentType;
|
||||
|
@ -93,10 +94,10 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
private String characterEncoding;
|
||||
|
||||
private Principal principal;
|
||||
|
||||
private Boolean secure;
|
||||
|
||||
private Principal principal;
|
||||
|
||||
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
|
||||
|
||||
private MockHttpSession session;
|
||||
|
@ -111,34 +112,28 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
private String pathInfo = ValueConstants.DEFAULT_NONE;
|
||||
|
||||
private final List<RequestPostProcessor> postProcessors =
|
||||
new ArrayList<RequestPostProcessor>();
|
||||
private final List<RequestPostProcessor> postProcessors = new ArrayList<RequestPostProcessor>();
|
||||
|
||||
|
||||
/**
|
||||
* Package private constructor. To get an instance, use static factory
|
||||
* methods in {@link MockMvcRequestBuilders}.
|
||||
*
|
||||
* <p>Although this class cannot be extended, additional ways to initialize
|
||||
* the {@code MockHttpServletRequest} can be plugged in via
|
||||
* {@link #with(RequestPostProcessor)}.
|
||||
*
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
*/
|
||||
MockHttpServletRequestBuilder(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) {
|
||||
|
||||
Assert.notNull(urlTemplate, "uriTemplate is required");
|
||||
Assert.notNull(httpMethod, "httpMethod is required");
|
||||
|
||||
this.uriComponents = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(urlVariables).encode();
|
||||
Assert.notNull(urlTemplate, "uriTemplate is required");
|
||||
this.method = httpMethod;
|
||||
this.uriComponents = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(urlVariables).encode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a request parameter to the {@link MockHttpServletRequest}.
|
||||
* If called more than once, the new values are added.
|
||||
*
|
||||
* @param name the parameter name
|
||||
* @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.
|
||||
*
|
||||
* @param name the header name
|
||||
* @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.
|
||||
*
|
||||
* @param httpHeaders the headers and values to add
|
||||
*/
|
||||
public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) {
|
||||
|
@ -173,7 +166,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set the 'Content-Type' header of the request.
|
||||
*
|
||||
* @param mediaType the content type
|
||||
*/
|
||||
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).
|
||||
*
|
||||
* @param mediaTypes one or more media types
|
||||
*/
|
||||
public MockHttpServletRequestBuilder accept(MediaType... mediaTypes) {
|
||||
|
@ -196,7 +187,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set the request body.
|
||||
*
|
||||
* @param content the body 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.
|
||||
*
|
||||
* @param content the body 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.
|
||||
*
|
||||
* @param cookies the cookies to add
|
||||
*/
|
||||
public MockHttpServletRequestBuilder cookie(Cookie... cookies) {
|
||||
|
@ -233,7 +221,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set the locale of the request.
|
||||
*
|
||||
* @param locale the locale
|
||||
*/
|
||||
public MockHttpServletRequestBuilder locale(Locale locale) {
|
||||
|
@ -243,7 +230,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set the character encoding of the request.
|
||||
*
|
||||
* @param encoding the character encoding
|
||||
*/
|
||||
public MockHttpServletRequestBuilder characterEncoding(String encoding) {
|
||||
|
@ -253,7 +239,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set a request attribute.
|
||||
*
|
||||
* @param name the attribute name
|
||||
* @param value the attribute value
|
||||
*/
|
||||
|
@ -264,7 +249,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set a session attribute.
|
||||
*
|
||||
* @param name the session attribute name
|
||||
* @param value the session attribute value
|
||||
*/
|
||||
|
@ -275,7 +259,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set session attributes.
|
||||
*
|
||||
* @param sessionAttributes the session attributes
|
||||
*/
|
||||
public MockHttpServletRequestBuilder sessionAttrs(Map<String, Object> sessionAttributes) {
|
||||
|
@ -288,7 +271,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set an "input" flash attribute.
|
||||
*
|
||||
* @param name the flash attribute name
|
||||
* @param value the flash attribute value
|
||||
*/
|
||||
|
@ -299,7 +281,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set flash attributes.
|
||||
*
|
||||
* @param flashAttributes the flash attributes
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* <p>Individual attributes provided via {@link #sessionAttr(String, Object)}
|
||||
* override the content of the session provided here.
|
||||
*
|
||||
* @param session the HTTP session
|
||||
*/
|
||||
public MockHttpServletRequestBuilder session(MockHttpSession session) {
|
||||
|
@ -326,7 +305,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Set the principal of the request.
|
||||
*
|
||||
* @param principal the 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.
|
||||
* The context path, if specified, must match to the start of the request
|
||||
* URI.
|
||||
*
|
||||
* The context path, if specified, must match to the start of the request URI.
|
||||
* <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
|
||||
* on the name under which they're deployed. If specified here, the context
|
||||
* 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) {
|
||||
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
|
||||
* the Servlet is mapped. This is typically a portion of the requestURI
|
||||
* after the context path.
|
||||
*
|
||||
* <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
|
||||
* 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"}.
|
||||
* If specified here, the servletPath 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#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) {
|
||||
if (StringUtils.hasText(servletPath)) {
|
||||
|
@ -384,16 +355,12 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
/**
|
||||
* Specify the portion of the requestURI that represents the pathInfo.
|
||||
*
|
||||
* <p>If left unspecified (recommended), the pathInfo will be automatically
|
||||
* derived by removing the contextPath and the servletPath from the
|
||||
* requestURI and using any remaining part. If specified here, the pathInfo
|
||||
* must start with a "/".
|
||||
*
|
||||
* <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) {
|
||||
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
|
||||
* secure channel, such as HTTPS.
|
||||
*
|
||||
* @param secure whether the request is using a secure channel
|
||||
*/
|
||||
public MockHttpServletRequestBuilder secure(boolean secure){
|
||||
|
@ -419,7 +385,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
* in ways not built directly into the {@code MockHttpServletRequestBuilder}.
|
||||
* Implementation of this interface can have builder-style methods themselves
|
||||
* and be made accessible through static factory methods.
|
||||
*
|
||||
* @param postProcessor a post-processor to add
|
||||
*/
|
||||
public MockHttpServletRequestBuilder with(RequestPostProcessor postProcessor) {
|
||||
|
@ -439,7 +404,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
/**
|
||||
* Merges the properties of the "parent" RequestBuilder accepting values
|
||||
* only if not already set in "this" instance.
|
||||
*
|
||||
* @param parent the parent {@code RequestBuilder} to inherit properties from
|
||||
* @return the result of the merge
|
||||
*/
|
||||
|
@ -450,7 +414,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
if (!(parent instanceof MockHttpServletRequestBuilder)) {
|
||||
throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]");
|
||||
}
|
||||
|
||||
MockHttpServletRequestBuilder parentBuilder = (MockHttpServletRequestBuilder) parent;
|
||||
|
||||
for (String headerName : parentBuilder.headers.keySet()) {
|
||||
|
@ -462,7 +425,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
if (this.contentType == null) {
|
||||
this.contentType = parentBuilder.contentType;
|
||||
}
|
||||
|
||||
if (this.content == null) {
|
||||
this.content = parentBuilder.content;
|
||||
}
|
||||
|
@ -472,7 +434,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
this.parameters.put(paramName, parentBuilder.parameters.get(paramName));
|
||||
}
|
||||
}
|
||||
|
||||
for (Cookie cookie : parentBuilder.cookies) {
|
||||
if (!containsCookie(cookie)) {
|
||||
this.cookies.add(cookie);
|
||||
|
@ -482,18 +443,16 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
if (this.locale == null) {
|
||||
this.locale = parentBuilder.locale;
|
||||
}
|
||||
|
||||
if (this.characterEncoding == null) {
|
||||
this.characterEncoding = parentBuilder.characterEncoding;
|
||||
}
|
||||
|
||||
if (this.principal == null) {
|
||||
this.principal = parentBuilder.principal;
|
||||
}
|
||||
|
||||
if (this.secure == null) {
|
||||
this.secure = parentBuilder.secure;
|
||||
}
|
||||
if (this.principal == null) {
|
||||
this.principal = parentBuilder.principal;
|
||||
}
|
||||
|
||||
for (String attributeName : parentBuilder.attributes.keySet()) {
|
||||
if (!this.attributes.containsKey(attributeName)) {
|
||||
|
@ -535,8 +494,8 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
}
|
||||
|
||||
private boolean containsCookie(Cookie cookie) {
|
||||
for (Cookie c : this.cookies) {
|
||||
if (ObjectUtils.nullSafeEquals(c.getName(), cookie.getName())) {
|
||||
for (Cookie cookieToCheck : this.cookies) {
|
||||
if (ObjectUtils.nullSafeEquals(cookieToCheck.getName(), cookie.getName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -547,12 +506,10 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
* Build a {@link MockHttpServletRequest}.
|
||||
*/
|
||||
public final MockHttpServletRequest buildRequest(ServletContext servletContext) {
|
||||
|
||||
MockHttpServletRequest request = createServletRequest(servletContext);
|
||||
|
||||
String requestUri = this.uriComponents.getPath();
|
||||
request.setRequestURI(requestUri);
|
||||
|
||||
updatePathRequestProperties(request, requestUri);
|
||||
|
||||
if (this.uriComponents.getScheme() != null) {
|
||||
|
@ -566,7 +523,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
}
|
||||
|
||||
request.setMethod(this.method.name());
|
||||
|
||||
for (String name : this.headers.keySet()) {
|
||||
for (Object value : this.headers.get(name)) {
|
||||
request.addHeader(name, value);
|
||||
|
@ -598,31 +554,26 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
|
||||
request.setContentType(this.contentType);
|
||||
request.setContent(this.content);
|
||||
|
||||
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
|
||||
|
||||
if (this.locale != null) {
|
||||
request.addPreferredLocale(this.locale);
|
||||
}
|
||||
|
||||
request.setCharacterEncoding(this.characterEncoding);
|
||||
|
||||
request.setUserPrincipal(this.principal);
|
||||
|
||||
if (this.secure != null) {
|
||||
request.setSecure(this.secure);
|
||||
}
|
||||
request.setUserPrincipal(this.principal);
|
||||
|
||||
for (String name : this.attributes.keySet()) {
|
||||
request.setAttribute(name, this.attributes.get(name));
|
||||
}
|
||||
|
||||
// Set session before session and flash attributes
|
||||
|
||||
if (this.session != null) {
|
||||
request.setSession(this.session);
|
||||
}
|
||||
|
||||
for (String name : this.sessionAttributes.keySet()) {
|
||||
request.getSession().setAttribute(name, this.sessionAttributes.get(name));
|
||||
}
|
||||
|
@ -634,21 +585,22 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
flashMapManager.saveOutputFlashMap(flashMap, request, new MockHttpServletResponse());
|
||||
|
||||
// Apply post-processors at the very end
|
||||
|
||||
for (RequestPostProcessor postProcessor : this.postProcessors) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link MockHttpServletRequest} based on the given
|
||||
* {@link ServletContext}. Can be overridden in sub-classes.
|
||||
* Create a new {@link MockHttpServletRequest} based on the given
|
||||
* {@link ServletContext}. Can be overridden in subclasses.
|
||||
*/
|
||||
protected MockHttpServletRequest createServletRequest(ServletContext servletContext) {
|
||||
return servlet3Present ? createServlet3Request(servletContext) : new MockHttpServletRequest(servletContext);
|
||||
return (servlet3Present ? createServlet3Request(servletContext) : new MockHttpServletRequest(servletContext));
|
||||
}
|
||||
|
||||
private MockHttpServletRequest createServlet3Request(ServletContext servletContext) {
|
||||
|
@ -658,8 +610,8 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|||
Constructor<?> constructor = clazz.getConstructor(ServletContext.class);
|
||||
return (MockHttpServletRequest) BeanUtils.instantiateClass(constructor, servletContext);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
throw new IllegalStateException("Failed to instantiate MockHttpServletRequest", t);
|
||||
catch (Throwable ex) {
|
||||
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.
|
||||
*/
|
||||
private void updatePathRequestProperties(MockHttpServletRequest request, String requestUri) {
|
||||
|
||||
Assert.isTrue(requestUri.startsWith(this.contextPath),
|
||||
"requestURI [" + requestUri + "] does not start with contextPath [" + this.contextPath + "]");
|
||||
|
||||
request.setContextPath(this.contextPath);
|
||||
request.setServletPath(this.servletPath);
|
||||
|
||||
if (ValueConstants.DEFAULT_NONE.equals(this.pathInfo)) {
|
||||
|
||||
Assert.isTrue(requestUri.startsWith(this.contextPath + this.servletPath),
|
||||
"Invalid servletPath [" + this.servletPath + "] for requestURI [" + requestUri + "]");
|
||||
|
||||
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
|
||||
this.pathInfo = (StringUtils.hasText(extraPath)) ? extraPath : null;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// ignore
|
||||
}
|
||||
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) {
|
||||
|
|
|
@ -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");
|
||||
* 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}.
|
||||
*
|
||||
* <p>For other ways to initialize a {@code MockMultipartHttpServletRequest},
|
||||
* see {@link #with(RequestPostProcessor)} and the
|
||||
* {@link RequestPostProcessor} extension point.
|
||||
*
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
*/
|
||||
|
@ -58,9 +56,9 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
|
|||
super.contentType(MediaType.MULTIPART_FORM_DATA);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new MockMultipartFile with the given content.
|
||||
*
|
||||
* @param name the name of the file
|
||||
* @param content the content of the file
|
||||
*/
|
||||
|
@ -71,7 +69,6 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
|
|||
|
||||
/**
|
||||
* Add the given MockMultipartFile.
|
||||
*
|
||||
* @param file the multipart file
|
||||
*/
|
||||
public MockMultipartHttpServletRequestBuilder file(MockMultipartFile file) {
|
||||
|
@ -99,8 +96,8 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
|
|||
|
||||
@Override
|
||||
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
|
||||
MockMultipartHttpServletRequest request = servlet3Present ?
|
||||
createServlet3Request() : new MockMultipartHttpServletRequest();
|
||||
MockMultipartHttpServletRequest request =
|
||||
(servlet3Present ? createServlet3Request() : new MockMultipartHttpServletRequest());
|
||||
for (MockMultipartFile file : this.files) {
|
||||
request.addFile(file);
|
||||
}
|
||||
|
@ -114,8 +111,8 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
|
|||
Constructor<?> constructor = clazz.getDeclaredConstructor();
|
||||
return (MockMultipartHttpServletRequest) BeanUtils.instantiateClass(constructor);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
throw new IllegalStateException("Failed to instantiate MockHttpServletRequest", t);
|
||||
catch (Throwable ex) {
|
||||
throw new IllegalStateException("Failed to instantiate MockMultipartHttpServletRequest", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
* 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
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.web.servlet.request;
|
||||
|
||||
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
|
||||
* 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}.
|
||||
*
|
||||
* <p>Implementations of this interface can be provided to
|
||||
* {@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 Rob Winch
|
||||
|
@ -36,9 +37,8 @@ public interface RequestPostProcessor {
|
|||
/**
|
||||
* Post-process the given {@code MockHttpServletRequest} after its creation
|
||||
* and initialization through a {@code MockHttpServletRequestBuilder}.
|
||||
*
|
||||
* @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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue