Merge branch '3.2.x'
* 3.2.x: (28 commits) Hide 'doc' changes from jdiff reports Document @Bean 'lite' mode vs @Configuration Final preparations for 3.2.2 Remove Tiles 3 configuration method Polishing Extracted buildRequestAttributes template method from FrameworkServlet Added "beforeExistingAdvisors" flag to AbstractAdvisingBeanPostProcessor Minor refinements along the way of researching static CGLIB callbacks Compare Kind references before checking log levels Polish Javadoc in RequestAttributes Fix copy-n-paste errors in NativeWebRequest Fix issue with restoring included attributes Add additional test for daylight savings glitch Document context hierarchy support in the TCF Fix test for daylight savings glitch Make the methodParameter field of HandlerMethod final Disable AsyncTests in spring-test-mvc Reformat the testing chapter Document context hierarchy support in the TCF Document context hierarchy support in the TCF ...
This commit is contained in:
@@ -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.
|
||||
@@ -28,7 +28,6 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -36,6 +35,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
@@ -810,13 +810,13 @@ public class DispatcherServlet extends FrameworkServlet {
|
||||
return context.getAutowireCapableBeanFactory().createBean(clazz);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Exposes the DispatcherServlet-specific request attributes and delegates to {@link #doDispatch}
|
||||
* for the actual dispatching.
|
||||
*/
|
||||
@Override
|
||||
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
String requestUri = urlPathHelper.getRequestUri(request);
|
||||
String resumed = WebAsyncUtils.getAsyncManager(request).hasConcurrentResult() ? " resumed" : "";
|
||||
@@ -1268,6 +1268,7 @@ public class DispatcherServlet extends FrameworkServlet {
|
||||
* @param request current HTTP request
|
||||
* @param attributesSnapshot the snapshot of the request attributes before the include
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void restoreAttributesAfterInclude(HttpServletRequest request, Map<?,?> attributesSnapshot) {
|
||||
logger.debug("Restoring snapshot of request attributes after include");
|
||||
|
||||
@@ -1282,6 +1283,9 @@ public class DispatcherServlet extends FrameworkServlet {
|
||||
}
|
||||
}
|
||||
|
||||
// Add attributes that may have been removed
|
||||
attrsToCheck.addAll((Set<String>) attributesSnapshot.keySet());
|
||||
|
||||
// Iterate over the attributes to check, restoring the original value
|
||||
// or removing the attribute, respectively, if appropriate.
|
||||
for (String attrName : attrsToCheck) {
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.security.Principal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -52,7 +51,6 @@ import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.context.request.async.CallableProcessingInterceptor;
|
||||
import org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter;
|
||||
import org.springframework.web.context.request.async.WebAsyncManager;
|
||||
import org.springframework.web.context.request.async.WebAsyncUtils;
|
||||
@@ -785,6 +783,18 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
||||
// For subclasses: do nothing by default.
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the WebApplicationContext of this servlet.
|
||||
* @see org.springframework.context.ConfigurableApplicationContext#close()
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
getServletContext().log("Destroying Spring FrameworkServlet '" + getServletName() + "'");
|
||||
if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
|
||||
((ConfigurableApplicationContext) this.webApplicationContext).close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Override the parent class implementation in order to intercept PATCH
|
||||
@@ -873,7 +883,7 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
||||
super.doOptions(request, new HttpServletResponseWrapper(response) {
|
||||
@Override
|
||||
public void setHeader(String name, String value) {
|
||||
if("Allow".equals(name)) {
|
||||
if ("Allow".equals(name)) {
|
||||
value = (StringUtils.hasLength(value) ? value + ", " : "") + RequestMethod.PATCH.name();
|
||||
}
|
||||
super.setHeader(name, value);
|
||||
@@ -915,15 +925,12 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
||||
LocaleContext localeContext = buildLocaleContext(request);
|
||||
|
||||
RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
|
||||
ServletRequestAttributes requestAttributes = null;
|
||||
if (previousAttributes == null || (previousAttributes instanceof ServletRequestAttributes)) {
|
||||
requestAttributes = new ServletRequestAttributes(request);
|
||||
}
|
||||
|
||||
initContextHolders(request, localeContext, requestAttributes);
|
||||
ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
|
||||
|
||||
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
|
||||
asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), getRequestBindingInterceptor(request));
|
||||
asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
|
||||
|
||||
initContextHolders(request, localeContext, requestAttributes);
|
||||
|
||||
try {
|
||||
doService(request, response);
|
||||
@@ -950,27 +957,18 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
||||
if (logger.isDebugEnabled()) {
|
||||
if (failureCause != null) {
|
||||
this.logger.debug("Could not complete request", failureCause);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (asyncManager.isConcurrentHandlingStarted()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Leaving response open for concurrent processing");
|
||||
}
|
||||
logger.debug("Leaving response open for concurrent processing");
|
||||
}
|
||||
else {
|
||||
this.logger.debug("Successfully completed request");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.publishEvents) {
|
||||
// Whether or not we succeeded, publish an event.
|
||||
long processingTime = System.currentTimeMillis() - startTime;
|
||||
this.webApplicationContext.publishEvent(
|
||||
new ServletRequestHandledEvent(this,
|
||||
request.getRequestURI(), request.getRemoteAddr(),
|
||||
request.getMethod(), getServletConfig().getServletName(),
|
||||
WebUtils.getSessionId(request), getUsernameForRequest(request),
|
||||
processingTime, failureCause));
|
||||
}
|
||||
|
||||
publishRequestHandledEvent(request, startTime, failureCause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -978,18 +976,43 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
||||
* Build a LocaleContext for the given request, exposing the request's
|
||||
* primary locale as current locale.
|
||||
* @param request current HTTP request
|
||||
* @return the corresponding LocaleContext
|
||||
* @return the corresponding LocaleContext, or {@code null} if none to bind
|
||||
* @see LocaleContextHolder#setLocaleContext
|
||||
*/
|
||||
protected LocaleContext buildLocaleContext(HttpServletRequest request) {
|
||||
return new SimpleLocaleContext(request.getLocale());
|
||||
}
|
||||
|
||||
private void initContextHolders(HttpServletRequest request,
|
||||
LocaleContext localeContext, RequestAttributes attributes) {
|
||||
/**
|
||||
* Build ServletRequestAttributes for the given request (potentially also
|
||||
* holding a reference to the response), taking pre-bound attributes
|
||||
* (and their type) into consideration.
|
||||
* @param request current HTTP request
|
||||
* @param response current HTTP response
|
||||
* @param previousAttributes pre-bound RequestAttributes instance, if any
|
||||
* @return the ServletRequestAttributes to bind, or {@code null} to preserve
|
||||
* the previously bound instance (or not binding any, if none bound before)
|
||||
* @see RequestContextHolder#setRequestAttributes
|
||||
*/
|
||||
protected ServletRequestAttributes buildRequestAttributes(
|
||||
HttpServletRequest request, HttpServletResponse response, RequestAttributes previousAttributes) {
|
||||
|
||||
LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable);
|
||||
if (attributes != null) {
|
||||
RequestContextHolder.setRequestAttributes(attributes, this.threadContextInheritable);
|
||||
if (previousAttributes == null || previousAttributes instanceof ServletRequestAttributes) {
|
||||
return new ServletRequestAttributes(request);
|
||||
}
|
||||
else {
|
||||
return null; // preserve the pre-bound RequestAttributes instance
|
||||
}
|
||||
}
|
||||
|
||||
private void initContextHolders(
|
||||
HttpServletRequest request, LocaleContext localeContext, RequestAttributes requestAttributes) {
|
||||
|
||||
if (localeContext != null) {
|
||||
LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable);
|
||||
}
|
||||
if (requestAttributes != null) {
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
|
||||
}
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Bound request context to thread: " + request);
|
||||
@@ -1006,17 +1029,17 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
||||
}
|
||||
}
|
||||
|
||||
private CallableProcessingInterceptor getRequestBindingInterceptor(final HttpServletRequest request) {
|
||||
return new CallableProcessingInterceptorAdapter() {
|
||||
@Override
|
||||
public <T> void preProcess(NativeWebRequest webRequest, Callable<T> task) {
|
||||
initContextHolders(request, buildLocaleContext(request), new ServletRequestAttributes(request));
|
||||
}
|
||||
@Override
|
||||
public <T> void postProcess(NativeWebRequest webRequest, Callable<T> task, Object concurrentResult) {
|
||||
resetContextHolders(request, null, null);
|
||||
}
|
||||
};
|
||||
private void publishRequestHandledEvent(HttpServletRequest request, long startTime, Throwable failureCause) {
|
||||
if (this.publishEvents) {
|
||||
// Whether or not we succeeded, publish an event.
|
||||
long processingTime = System.currentTimeMillis() - startTime;
|
||||
this.webApplicationContext.publishEvent(
|
||||
new ServletRequestHandledEvent(this,
|
||||
request.getRequestURI(), request.getRemoteAddr(),
|
||||
request.getMethod(), getServletConfig().getServletName(),
|
||||
WebUtils.getSessionId(request), getUsernameForRequest(request),
|
||||
processingTime, failureCause));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1032,6 +1055,7 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
||||
return (userPrincipal != null ? userPrincipal.getName() : null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Subclasses must implement this method to do the work of request handling,
|
||||
* receiving a centralized callback for GET, POST, PUT and DELETE.
|
||||
@@ -1049,19 +1073,6 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
||||
throws Exception;
|
||||
|
||||
|
||||
/**
|
||||
* Close the WebApplicationContext of this servlet.
|
||||
* @see org.springframework.context.ConfigurableApplicationContext#close()
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
getServletContext().log("Destroying Spring FrameworkServlet '" + getServletName() + "'");
|
||||
if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
|
||||
((ConfigurableApplicationContext) this.webApplicationContext).close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ApplicationListener endpoint that receives events from this servlet's WebApplicationContext
|
||||
* only, delegating to {@code onApplicationEvent} on the FrameworkServlet instance.
|
||||
@@ -1073,4 +1084,28 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* CallableProcessingInterceptor implementation that initializes and resets
|
||||
* FrameworkServlet's context holders, i.e. LocaleContextHolder and RequestContextHolder.
|
||||
*/
|
||||
private class RequestBindingInterceptor extends CallableProcessingInterceptorAdapter {
|
||||
|
||||
@Override
|
||||
public <T> void preProcess(NativeWebRequest webRequest, Callable<T> task) {
|
||||
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
||||
if (request != null) {
|
||||
HttpServletResponse response = webRequest.getNativeRequest(HttpServletResponse.class);
|
||||
initContextHolders(request, buildLocaleContext(request), buildRequestAttributes(request, response, null));
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public <T> void postProcess(NativeWebRequest webRequest, Callable<T> task, Object concurrentResult) {
|
||||
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
||||
if (request != null) {
|
||||
resetContextHolders(request, null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.servlet.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
@@ -89,7 +90,7 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
|
||||
}
|
||||
|
||||
ManagedList<String> locations = new ManagedList<String>();
|
||||
locations.addAll(StringUtils.commaDelimitedListToSet(locationAttr));
|
||||
locations.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(locationAttr)));
|
||||
|
||||
RootBeanDefinition resourceHandlerDef = new RootBeanDefinition(ResourceHttpRequestHandler.class);
|
||||
resourceHandlerDef.setSource(source);
|
||||
|
||||
@@ -61,8 +61,7 @@ import org.springframework.web.util.WebUtils;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
|
||||
@@ -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.
|
||||
@@ -16,17 +16,14 @@
|
||||
|
||||
package org.springframework.web.servlet;
|
||||
|
||||
import static org.easymock.EasyMock.createStrictMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* A test fixture with HandlerExecutionChain and mock handler interceptors.
|
||||
*
|
||||
@@ -56,9 +53,9 @@ public class HandlerExecutionChainTests {
|
||||
this.handler = new Object();
|
||||
this.chain = new HandlerExecutionChain(this.handler);
|
||||
|
||||
this.interceptor1 = createStrictMock(AsyncHandlerInterceptor.class);
|
||||
this.interceptor2 = createStrictMock(AsyncHandlerInterceptor.class);
|
||||
this.interceptor3 = createStrictMock(AsyncHandlerInterceptor.class);
|
||||
this.interceptor1 = mock(AsyncHandlerInterceptor.class);
|
||||
this.interceptor2 = mock(AsyncHandlerInterceptor.class);
|
||||
this.interceptor3 = mock(AsyncHandlerInterceptor.class);
|
||||
|
||||
this.chain.addInterceptor(this.interceptor1);
|
||||
this.chain.addInterceptor(this.interceptor2);
|
||||
@@ -69,79 +66,60 @@ public class HandlerExecutionChainTests {
|
||||
public void successScenario() throws Exception {
|
||||
ModelAndView mav = new ModelAndView();
|
||||
|
||||
expect(this.interceptor1.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
expect(this.interceptor2.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
expect(this.interceptor3.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
|
||||
this.interceptor1.postHandle(this.request, this.response, this.handler, mav);
|
||||
this.interceptor2.postHandle(this.request, this.response, this.handler, mav);
|
||||
this.interceptor3.postHandle(this.request, this.response, this.handler, mav);
|
||||
|
||||
this.interceptor3.afterCompletion(this.request, this.response, this.handler, null);
|
||||
this.interceptor2.afterCompletion(this.request, this.response, this.handler, null);
|
||||
this.interceptor1.afterCompletion(this.request, this.response, this.handler, null);
|
||||
|
||||
replay(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
given(this.interceptor3.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
|
||||
this.chain.applyPreHandle(request, response);
|
||||
this.chain.applyPostHandle(request, response, mav);
|
||||
this.chain.triggerAfterCompletion(this.request, this.response, null);
|
||||
|
||||
verify(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
verify(this.interceptor1).postHandle(this.request, this.response, this.handler, mav);
|
||||
verify(this.interceptor2).postHandle(this.request, this.response, this.handler, mav);
|
||||
verify(this.interceptor3).postHandle(this.request, this.response, this.handler, mav);
|
||||
|
||||
verify(this.interceptor3).afterCompletion(this.request, this.response, this.handler, null);
|
||||
verify(this.interceptor2).afterCompletion(this.request, this.response, this.handler, null);
|
||||
verify(this.interceptor1).afterCompletion(this.request, this.response, this.handler, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void successAsyncScenario() throws Exception {
|
||||
expect(this.interceptor1.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
expect(this.interceptor2.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
expect(this.interceptor3.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
|
||||
this.interceptor1.afterConcurrentHandlingStarted(request, response, this.handler);
|
||||
this.interceptor2.afterConcurrentHandlingStarted(request, response, this.handler);
|
||||
this.interceptor3.afterConcurrentHandlingStarted(request, response, this.handler);
|
||||
|
||||
replay(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
given(this.interceptor3.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
|
||||
this.chain.applyPreHandle(request, response);
|
||||
this.chain.applyAfterConcurrentHandlingStarted(request, response);
|
||||
this.chain.triggerAfterCompletion(this.request, this.response, null);
|
||||
|
||||
verify(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
verify(this.interceptor1).afterConcurrentHandlingStarted(request, response, this.handler);
|
||||
verify(this.interceptor2).afterConcurrentHandlingStarted(request, response, this.handler);
|
||||
verify(this.interceptor3).afterConcurrentHandlingStarted(request, response, this.handler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void earlyExitInPreHandle() throws Exception {
|
||||
expect(this.interceptor1.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
expect(this.interceptor2.preHandle(this.request, this.response, this.handler)).andReturn(false);
|
||||
|
||||
this.interceptor1.afterCompletion(this.request, this.response, this.handler, null);
|
||||
|
||||
replay(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willReturn(false);
|
||||
|
||||
this.chain.applyPreHandle(request, response);
|
||||
|
||||
verify(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
verify(this.interceptor1).afterCompletion(this.request, this.response, this.handler, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionBeforePreHandle() throws Exception {
|
||||
replay(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
|
||||
this.chain.triggerAfterCompletion(this.request, this.response, null);
|
||||
|
||||
verify(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
verifyZeroInteractions(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionDuringPreHandle() throws Exception {
|
||||
Exception ex = new Exception("");
|
||||
|
||||
expect(this.interceptor1.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
expect(this.interceptor2.preHandle(this.request, this.response, this.handler)).andThrow(ex);
|
||||
|
||||
this.interceptor1.afterCompletion(this.request, this.response, this.handler, ex);
|
||||
|
||||
replay(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willThrow(ex);
|
||||
|
||||
try {
|
||||
this.chain.applyPreHandle(request, response);
|
||||
@@ -151,27 +129,24 @@ public class HandlerExecutionChainTests {
|
||||
}
|
||||
this.chain.triggerAfterCompletion(this.request, this.response, ex);
|
||||
|
||||
verify(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
verify(this.interceptor1).afterCompletion(this.request, this.response, this.handler, ex);
|
||||
verify(this.interceptor3, never()).preHandle(this.request, this.response, this.handler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionAfterPreHandle() throws Exception {
|
||||
Exception ex = new Exception("");
|
||||
|
||||
expect(this.interceptor1.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
expect(this.interceptor2.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
expect(this.interceptor3.preHandle(this.request, this.response, this.handler)).andReturn(true);
|
||||
|
||||
this.interceptor3.afterCompletion(this.request, this.response, this.handler, ex);
|
||||
this.interceptor2.afterCompletion(this.request, this.response, this.handler, ex);
|
||||
this.interceptor1.afterCompletion(this.request, this.response, this.handler, ex);
|
||||
|
||||
replay(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
given(this.interceptor3.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
|
||||
this.chain.applyPreHandle(request, response);
|
||||
this.chain.triggerAfterCompletion(this.request, this.response, ex);
|
||||
|
||||
verify(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
verify(this.interceptor3).afterCompletion(this.request, this.response, this.handler, ex);
|
||||
verify(this.interceptor2).afterCompletion(this.request, this.response, this.handler, ex);
|
||||
verify(this.interceptor1).afterCompletion(this.request, this.response, this.handler, 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.
|
||||
@@ -16,23 +16,17 @@
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import static org.easymock.EasyMock.capture;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.Capture;
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.format.support.FormattingConversionService;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
@@ -48,6 +42,9 @@ import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExc
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* A test fixture for {@link DelegatingWebMvcConfiguration} tests.
|
||||
*
|
||||
@@ -57,46 +54,59 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
|
||||
private DelegatingWebMvcConfiguration delegatingConfig;
|
||||
|
||||
@Mock
|
||||
private WebMvcConfigurer webMvcConfigurer;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<List<HttpMessageConverter<?>>> converters;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<ContentNegotiationConfigurer> contentNegotiationConfigurer;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<FormattingConversionService> conversionService;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<List<HandlerMethodArgumentResolver>> resolvers;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<List<HandlerMethodReturnValueHandler>> handlers;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<AsyncSupportConfigurer> asyncConfigurer;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<List<HandlerExceptionResolver>> exceptionResolvers;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
webMvcConfigurer = EasyMock.createMock(WebMvcConfigurer.class);
|
||||
MockitoAnnotations.initMocks(this);
|
||||
delegatingConfig = new DelegatingWebMvcConfiguration();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestMappingHandlerAdapter() throws Exception {
|
||||
Capture<List<HttpMessageConverter<?>>> converters = new Capture<List<HttpMessageConverter<?>>>();
|
||||
Capture<ContentNegotiationConfigurer> contentNegotiationConfigurer = new Capture<ContentNegotiationConfigurer>();
|
||||
Capture<FormattingConversionService> conversionService = new Capture<FormattingConversionService>();
|
||||
Capture<List<HandlerMethodArgumentResolver>> resolvers = new Capture<List<HandlerMethodArgumentResolver>>();
|
||||
Capture<List<HandlerMethodReturnValueHandler>> handlers = new Capture<List<HandlerMethodReturnValueHandler>>();
|
||||
Capture<AsyncSupportConfigurer> asyncConfigurer = new Capture<AsyncSupportConfigurer>();
|
||||
|
||||
webMvcConfigurer.configureMessageConverters(capture(converters));
|
||||
webMvcConfigurer.configureContentNegotiation(capture(contentNegotiationConfigurer));
|
||||
expect(webMvcConfigurer.getValidator()).andReturn(null);
|
||||
expect(webMvcConfigurer.getMessageCodesResolver()).andReturn(null);
|
||||
webMvcConfigurer.addFormatters(capture(conversionService));
|
||||
webMvcConfigurer.addArgumentResolvers(capture(resolvers));
|
||||
webMvcConfigurer.addReturnValueHandlers(capture(handlers));
|
||||
webMvcConfigurer.configureAsyncSupport(capture(asyncConfigurer));
|
||||
replay(webMvcConfigurer);
|
||||
|
||||
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
|
||||
RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter();
|
||||
|
||||
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
|
||||
assertSame(conversionService.getValue(), initializer.getConversionService());
|
||||
ConversionService initializerConversionService = initializer.getConversionService();
|
||||
assertTrue(initializer.getValidator() instanceof LocalValidatorFactoryBean);
|
||||
|
||||
verify(webMvcConfigurer).configureMessageConverters(converters.capture());
|
||||
verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
|
||||
verify(webMvcConfigurer).addFormatters(conversionService.capture());
|
||||
verify(webMvcConfigurer).addArgumentResolvers(resolvers.capture());
|
||||
verify(webMvcConfigurer).addReturnValueHandlers(handlers.capture());
|
||||
verify(webMvcConfigurer).configureAsyncSupport(asyncConfigurer.capture());
|
||||
|
||||
assertSame(conversionService.getValue(), initializerConversionService);
|
||||
assertEquals(0, resolvers.getValue().size());
|
||||
assertEquals(0, handlers.getValue().size());
|
||||
assertEquals(converters.getValue(), adapter.getMessageConverters());
|
||||
assertNotNull(asyncConfigurer);
|
||||
|
||||
verify(webMvcConfigurer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -117,47 +127,39 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void getCustomValidator() {
|
||||
expect(webMvcConfigurer.getValidator()).andReturn(new LocalValidatorFactoryBean());
|
||||
replay(webMvcConfigurer);
|
||||
given(webMvcConfigurer.getValidator()).willReturn(new LocalValidatorFactoryBean());
|
||||
|
||||
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
|
||||
delegatingConfig.mvcValidator();
|
||||
|
||||
verify(webMvcConfigurer);
|
||||
verify(webMvcConfigurer).getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCustomMessageCodesResolver() {
|
||||
expect(webMvcConfigurer.getMessageCodesResolver()).andReturn(new DefaultMessageCodesResolver());
|
||||
replay(webMvcConfigurer);
|
||||
given(webMvcConfigurer.getMessageCodesResolver()).willReturn(new DefaultMessageCodesResolver());
|
||||
|
||||
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
|
||||
delegatingConfig.getMessageCodesResolver();
|
||||
|
||||
verify(webMvcConfigurer);
|
||||
verify(webMvcConfigurer).getMessageCodesResolver();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlerExceptionResolver() throws Exception {
|
||||
Capture<List<HttpMessageConverter<?>>> converters = new Capture<List<HttpMessageConverter<?>>>();
|
||||
Capture<List<HandlerExceptionResolver>> exceptionResolvers = new Capture<List<HandlerExceptionResolver>>();
|
||||
Capture<ContentNegotiationConfigurer> contentNegotiationConfigurer = new Capture<ContentNegotiationConfigurer>();
|
||||
|
||||
webMvcConfigurer.configureMessageConverters(capture(converters));
|
||||
webMvcConfigurer.configureContentNegotiation(capture(contentNegotiationConfigurer));
|
||||
webMvcConfigurer.configureHandlerExceptionResolvers(capture(exceptionResolvers));
|
||||
replay(webMvcConfigurer);
|
||||
|
||||
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
|
||||
delegatingConfig.handlerExceptionResolver();
|
||||
|
||||
verify(webMvcConfigurer).configureMessageConverters(converters.capture());
|
||||
verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
|
||||
verify(webMvcConfigurer).configureHandlerExceptionResolvers(exceptionResolvers.capture());
|
||||
|
||||
assertEquals(3, exceptionResolvers.getValue().size());
|
||||
assertTrue(exceptionResolvers.getValue().get(0) instanceof ExceptionHandlerExceptionResolver);
|
||||
assertTrue(exceptionResolvers.getValue().get(1) instanceof ResponseStatusExceptionResolver);
|
||||
assertTrue(exceptionResolvers.getValue().get(2) instanceof DefaultHandlerExceptionResolver);
|
||||
assertTrue(converters.getValue().size() > 0);
|
||||
|
||||
verify(webMvcConfigurer);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.
|
||||
@@ -28,7 +28,6 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
@@ -36,6 +35,8 @@ import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
@@ -74,43 +75,31 @@ public class ControllerTests extends TestCase {
|
||||
private void doTestServletForwardingController(ServletForwardingController sfc, boolean include)
|
||||
throws Exception {
|
||||
|
||||
MockControl requestControl = MockControl.createControl(HttpServletRequest.class);
|
||||
HttpServletRequest request = (HttpServletRequest) requestControl.getMock();
|
||||
MockControl responseControl = MockControl.createControl(HttpServletResponse.class);
|
||||
HttpServletResponse response = (HttpServletResponse) responseControl.getMock();
|
||||
MockControl contextControl = MockControl.createControl(ServletContext.class);
|
||||
ServletContext context = (ServletContext) contextControl.getMock();
|
||||
MockControl dispatcherControl = MockControl.createControl(RequestDispatcher.class);
|
||||
RequestDispatcher dispatcher = (RequestDispatcher) dispatcherControl.getMock();
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
ServletContext context = mock(ServletContext.class);
|
||||
RequestDispatcher dispatcher = mock(RequestDispatcher.class);
|
||||
|
||||
request.getMethod();
|
||||
requestControl.setReturnValue("GET", 1);
|
||||
context.getNamedDispatcher("action");
|
||||
contextControl.setReturnValue(dispatcher, 1);
|
||||
given(request.getMethod()).willReturn("GET");
|
||||
given(context.getNamedDispatcher("action")).willReturn(dispatcher);
|
||||
if (include) {
|
||||
request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
|
||||
requestControl.setReturnValue("somePath", 1);
|
||||
dispatcher.include(request, response);
|
||||
dispatcherControl.setVoidCallable(1);
|
||||
given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somePath");
|
||||
}
|
||||
else {
|
||||
request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
|
||||
requestControl.setReturnValue(null, 1);
|
||||
dispatcher.forward(request, response);
|
||||
dispatcherControl.setVoidCallable(1);
|
||||
given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
|
||||
}
|
||||
requestControl.replay();
|
||||
contextControl.replay();
|
||||
dispatcherControl.replay();
|
||||
|
||||
StaticWebApplicationContext sac = new StaticWebApplicationContext();
|
||||
sac.setServletContext(context);
|
||||
sfc.setApplicationContext(sac);
|
||||
assertNull(sfc.handleRequest(request, response));
|
||||
|
||||
requestControl.verify();
|
||||
contextControl.verify();
|
||||
dispatcherControl.verify();
|
||||
if (include) {
|
||||
verify(dispatcher).include(request, response);
|
||||
}
|
||||
else {
|
||||
verify(dispatcher).forward(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
public void testServletWrappingController() throws Exception {
|
||||
|
||||
@@ -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.
|
||||
@@ -15,27 +15,14 @@
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
import static org.easymock.EasyMock.capture;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.eq;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.isA;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.reset;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.springframework.web.servlet.HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.easymock.Capture;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -52,7 +39,12 @@ import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.springframework.web.servlet.HandlerMapping.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link HttpEntityMethodProcessor} delegating to a mock
|
||||
@@ -88,9 +80,8 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
messageConverter = createMock(HttpMessageConverter.class);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
replay(messageConverter);
|
||||
messageConverter = mock(HttpMessageConverter.class);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
|
||||
processor = new HttpEntityMethodProcessor(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
|
||||
reset(messageConverter);
|
||||
@@ -135,16 +126,14 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||
|
||||
String body = "Foo";
|
||||
expect(messageConverter.canRead(String.class, contentType)).andReturn(true);
|
||||
expect(messageConverter.read(eq(String.class), isA(HttpInputMessage.class))).andReturn(body);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canRead(String.class, contentType)).willReturn(true);
|
||||
given(messageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body);
|
||||
|
||||
Object result = processor.resolveArgument(paramHttpEntity, mavContainer, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof HttpEntity);
|
||||
assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled());
|
||||
assertEquals("Invalid argument", body, ((HttpEntity<?>) result).getBody());
|
||||
verify(messageConverter);
|
||||
}
|
||||
|
||||
@Test(expected = HttpMediaTypeNotSupportedException.class)
|
||||
@@ -152,9 +141,8 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Arrays.asList(contentType));
|
||||
expect(messageConverter.canRead(String.class, contentType)).andReturn(false);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Arrays.asList(contentType));
|
||||
given(messageConverter.canRead(String.class, contentType)).willReturn(false);
|
||||
|
||||
processor.resolveArgument(paramHttpEntity, mavContainer, webRequest, null);
|
||||
|
||||
@@ -175,16 +163,14 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
MediaType accepted = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Accept", accepted.toString());
|
||||
|
||||
expect(messageConverter.canWrite(String.class, null)).andReturn(true);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
expect(messageConverter.canWrite(String.class, accepted)).andReturn(true);
|
||||
messageConverter.write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, null)).willReturn(true);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
given(messageConverter.canWrite(String.class, accepted)).willReturn(true);
|
||||
|
||||
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
|
||||
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
verify(messageConverter);
|
||||
verify(messageConverter).write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -195,14 +181,12 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
servletRequest.addHeader("Accept", "text/*");
|
||||
servletRequest.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.TEXT_HTML));
|
||||
|
||||
expect(messageConverter.canWrite(String.class, MediaType.TEXT_HTML)).andReturn(true);
|
||||
messageConverter.write(eq(body), eq(MediaType.TEXT_HTML), isA(HttpOutputMessage.class));
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, MediaType.TEXT_HTML)).willReturn(true);
|
||||
|
||||
processor.handleReturnValue(returnValue, returnTypeResponseEntityProduces, mavContainer, webRequest);
|
||||
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
verify(messageConverter);
|
||||
verify(messageConverter).write(eq(body), eq(MediaType.TEXT_HTML), isA(HttpOutputMessage.class));
|
||||
}
|
||||
|
||||
@Test(expected = HttpMediaTypeNotAcceptableException.class)
|
||||
@@ -213,10 +197,9 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
MediaType accepted = MediaType.APPLICATION_ATOM_XML;
|
||||
servletRequest.addHeader("Accept", accepted.toString());
|
||||
|
||||
expect(messageConverter.canWrite(String.class, null)).andReturn(true);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Arrays.asList(MediaType.TEXT_PLAIN));
|
||||
expect(messageConverter.canWrite(String.class, accepted)).andReturn(false);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, null)).willReturn(true);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Arrays.asList(MediaType.TEXT_PLAIN));
|
||||
given(messageConverter.canWrite(String.class, accepted)).willReturn(false);
|
||||
|
||||
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
|
||||
|
||||
@@ -231,10 +214,9 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
MediaType accepted = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Accept", accepted.toString());
|
||||
|
||||
expect(messageConverter.canWrite(String.class, null)).andReturn(true);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
expect(messageConverter.canWrite(String.class, accepted)).andReturn(false);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, null)).willReturn(true);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
given(messageConverter.canWrite(String.class, accepted)).willReturn(false);
|
||||
|
||||
processor.handleReturnValue(returnValue, returnTypeResponseEntityProduces, mavContainer, webRequest);
|
||||
|
||||
@@ -270,18 +252,16 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
responseHeaders.set("header", "headerValue");
|
||||
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.ACCEPTED);
|
||||
|
||||
Capture<HttpOutputMessage> outputMessage = new Capture<HttpOutputMessage>();
|
||||
expect(messageConverter.canWrite(String.class, null)).andReturn(true);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
expect(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).andReturn(true);
|
||||
messageConverter.write(eq("body"), eq(MediaType.TEXT_PLAIN), capture(outputMessage));
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, null)).willReturn(true);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
|
||||
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
|
||||
|
||||
ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class);
|
||||
verify(messageConverter).write(eq("body"), eq(MediaType.TEXT_PLAIN), outputMessage.capture());
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
assertEquals("headerValue", outputMessage.getValue().getHeaders().get("header").get(0));
|
||||
verify(messageConverter);
|
||||
}
|
||||
|
||||
public ResponseEntity<String> handle1(HttpEntity<String> httpEntity, ResponseEntity<String> responseEntity, int i) {
|
||||
@@ -302,4 +282,4 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -16,20 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.eq;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.isA;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.reset;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
@@ -52,19 +38,21 @@ import org.springframework.mock.web.test.MockMultipartFile;
|
||||
import org.springframework.mock.web.test.MockMultipartHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockPart;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
||||
import org.springframework.web.multipart.support.RequestPartServletServerHttpRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link RequestPartMethodArgumentResolver} and mock {@link HttpMessageConverter}.
|
||||
@@ -116,9 +104,8 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
paramServlet30Part.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
|
||||
paramRequestParamAnnot = new MethodParameter(method, 8);
|
||||
|
||||
messageConverter = createMock(HttpMessageConverter.class);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
replay(messageConverter);
|
||||
messageConverter = mock(HttpMessageConverter.class);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
|
||||
resolver = new RequestPartMethodArgumentResolver(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
|
||||
reset(messageConverter);
|
||||
@@ -246,17 +233,14 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
private void testResolveArgument(SimpleBean argValue, MethodParameter parameter) throws IOException, Exception {
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
|
||||
expect(messageConverter.canRead(SimpleBean.class, contentType)).andReturn(true);
|
||||
expect(messageConverter.read(eq(SimpleBean.class), isA(RequestPartServletServerHttpRequest.class))).andReturn(argValue);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canRead(SimpleBean.class, contentType)).willReturn(true);
|
||||
given(messageConverter.read(eq(SimpleBean.class), isA(RequestPartServletServerHttpRequest.class))).willReturn(argValue);
|
||||
|
||||
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
|
||||
Object actualValue = resolver.resolveArgument(parameter, mavContainer, webRequest, new ValidatingBinderFactory());
|
||||
|
||||
assertEquals("Invalid argument value", argValue, actualValue);
|
||||
assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled());
|
||||
|
||||
verify(messageConverter);
|
||||
}
|
||||
|
||||
private static class SimpleBean {
|
||||
|
||||
@@ -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.
|
||||
@@ -16,20 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.eq;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.isA;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.reset;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
@@ -61,6 +47,9 @@ import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link RequestResponseBodyMethodProcessor} delegating to a
|
||||
* mock HttpMessageConverter.
|
||||
@@ -95,12 +84,10 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
messageConverter = createMock(HttpMessageConverter.class);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
replay(messageConverter);
|
||||
messageConverter = mock(HttpMessageConverter.class);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
|
||||
processor = new RequestResponseBodyMethodProcessor(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
|
||||
reset(messageConverter);
|
||||
|
||||
Method methodHandle1 = getClass().getMethod("handle1", String.class, Integer.TYPE);
|
||||
paramRequestBodyString = new MethodParameter(methodHandle1, 0);
|
||||
@@ -138,15 +125,13 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
String body = "Foo";
|
||||
servletRequest.setContent(body.getBytes());
|
||||
|
||||
expect(messageConverter.canRead(String.class, contentType)).andReturn(true);
|
||||
expect(messageConverter.read(eq(String.class), isA(HttpInputMessage.class))).andReturn(body);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canRead(String.class, contentType)).willReturn(true);
|
||||
given(messageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body);
|
||||
|
||||
Object result = processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, new ValidatingBinderFactory());
|
||||
|
||||
assertEquals("Invalid argument", body, result);
|
||||
assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled());
|
||||
verify(messageConverter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -172,16 +157,13 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
servletRequest.setContent(new byte[] {});
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
HttpMessageConverter<SimpleBean> beanConverter = createMock(HttpMessageConverter.class);
|
||||
expect(beanConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
expect(beanConverter.canRead(SimpleBean.class, contentType)).andReturn(true);
|
||||
expect(beanConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).andReturn(simpleBean);
|
||||
replay(beanConverter);
|
||||
HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class);
|
||||
given(beanConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
given(beanConverter.canRead(SimpleBean.class, contentType)).willReturn(true);
|
||||
given(beanConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean);
|
||||
|
||||
processor = new RequestResponseBodyMethodProcessor(Collections.<HttpMessageConverter<?>>singletonList(beanConverter));
|
||||
processor.resolveArgument(paramValidBean, mavContainer, webRequest, new ValidatingBinderFactory());
|
||||
|
||||
verify(beanConverter);
|
||||
}
|
||||
|
||||
@Test(expected = HttpMediaTypeNotSupportedException.class)
|
||||
@@ -189,23 +171,20 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||
|
||||
expect(messageConverter.canRead(String.class, contentType)).andReturn(false);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canRead(String.class, contentType)).willReturn(false);
|
||||
|
||||
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentNoContentType() throws Exception {
|
||||
expect(messageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).andReturn(false);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false);
|
||||
try {
|
||||
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (HttpMediaTypeNotSupportedException ex) {
|
||||
}
|
||||
verify(messageConverter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -223,16 +202,14 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
servletRequest.addHeader("Accept", accepted.toString());
|
||||
|
||||
String body = "Foo";
|
||||
expect(messageConverter.canWrite(String.class, null)).andReturn(true);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
expect(messageConverter.canWrite(String.class, accepted)).andReturn(true);
|
||||
messageConverter.write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, null)).willReturn(true);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
given(messageConverter.canWrite(String.class, accepted)).willReturn(true);
|
||||
|
||||
processor.handleReturnValue(body, returnTypeString, mavContainer, webRequest);
|
||||
|
||||
assertTrue("The requestHandled flag wasn't set", mavContainer.isRequestHandled());
|
||||
verify(messageConverter);
|
||||
verify(messageConverter).write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -242,14 +219,12 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
servletRequest.addHeader("Accept", "text/*");
|
||||
servletRequest.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.TEXT_HTML));
|
||||
|
||||
expect(messageConverter.canWrite(String.class, MediaType.TEXT_HTML)).andReturn(true);
|
||||
messageConverter.write(eq(body), eq(MediaType.TEXT_HTML), isA(HttpOutputMessage.class));
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, MediaType.TEXT_HTML)).willReturn(true);
|
||||
|
||||
processor.handleReturnValue(body, returnTypeStringProduces, mavContainer, webRequest);
|
||||
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
verify(messageConverter);
|
||||
verify(messageConverter).write(eq(body), eq(MediaType.TEXT_HTML), isA(HttpOutputMessage.class));
|
||||
}
|
||||
|
||||
|
||||
@@ -258,10 +233,9 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
MediaType accepted = MediaType.APPLICATION_ATOM_XML;
|
||||
servletRequest.addHeader("Accept", accepted.toString());
|
||||
|
||||
expect(messageConverter.canWrite(String.class, null)).andReturn(true);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Arrays.asList(MediaType.TEXT_PLAIN));
|
||||
expect(messageConverter.canWrite(String.class, accepted)).andReturn(false);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, null)).willReturn(true);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Arrays.asList(MediaType.TEXT_PLAIN));
|
||||
given(messageConverter.canWrite(String.class, accepted)).willReturn(false);
|
||||
|
||||
processor.handleReturnValue("Foo", returnTypeString, mavContainer, webRequest);
|
||||
}
|
||||
@@ -271,10 +245,9 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
MediaType accepted = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Accept", accepted.toString());
|
||||
|
||||
expect(messageConverter.canWrite(String.class, null)).andReturn(true);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
expect(messageConverter.canWrite(String.class, accepted)).andReturn(false);
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, null)).willReturn(true);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
given(messageConverter.canWrite(String.class, accepted)).willReturn(false);
|
||||
|
||||
processor.handleReturnValue("Foo", returnTypeStringProduces, mavContainer, webRequest);
|
||||
}
|
||||
@@ -289,16 +262,14 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
|
||||
servletRequest.addHeader("Accept", accepted);
|
||||
|
||||
expect(messageConverter.canWrite(String.class, null)).andReturn(true);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(supported);
|
||||
expect(messageConverter.canWrite(String.class, accepted)).andReturn(true);
|
||||
messageConverter.write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
|
||||
replay(messageConverter);
|
||||
given(messageConverter.canWrite(String.class, null)).willReturn(true);
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(supported);
|
||||
given(messageConverter.canWrite(String.class, accepted)).willReturn(true);
|
||||
|
||||
processor.handleReturnValue(body, returnTypeStringProduces, mavContainer, webRequest);
|
||||
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
verify(messageConverter);
|
||||
verify(messageConverter).write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
|
||||
}
|
||||
|
||||
|
||||
@@ -348,4 +319,4 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.tags.form;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
@@ -32,14 +30,16 @@ import org.springframework.mock.web.test.MockPageContext;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.support.RequestDataValueProcessorWrapper;
|
||||
import org.springframework.web.servlet.support.JspAwareRequestContext;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
import org.springframework.web.servlet.support.RequestDataValueProcessor;
|
||||
import org.springframework.web.servlet.support.RequestDataValueProcessorWrapper;
|
||||
import org.springframework.web.servlet.tags.AbstractTagTests;
|
||||
import org.springframework.web.servlet.tags.RequestContextAwareTag;
|
||||
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
@@ -102,7 +102,7 @@ public abstract class AbstractHtmlElementTagTests extends AbstractTagTests {
|
||||
}
|
||||
|
||||
protected RequestDataValueProcessor getMockRequestDataValueProcessor() {
|
||||
RequestDataValueProcessor mockProcessor = createMock(RequestDataValueProcessor.class);
|
||||
RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
|
||||
ServletRequest request = getPageContext().getRequest();
|
||||
StaticWebApplicationContext wac = (StaticWebApplicationContext) RequestContextUtils.getWebApplicationContext(request);
|
||||
wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.tags.form;
|
||||
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.servlet.jsp.PageContext;
|
||||
@@ -28,6 +24,8 @@ import javax.servlet.jsp.tagext.Tag;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.web.servlet.support.RequestDataValueProcessor;
|
||||
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Rick Evans
|
||||
@@ -328,9 +326,8 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
|
||||
public void testRequestDataValueProcessorHooks() throws Exception {
|
||||
String action = "/my/form?foo=bar";
|
||||
RequestDataValueProcessor processor = getMockRequestDataValueProcessor();
|
||||
expect(processor.processAction(this.request, action)).andReturn(action);
|
||||
expect(processor.getExtraHiddenFields(this.request)).andReturn(Collections.singletonMap("key", "value"));
|
||||
replay(processor);
|
||||
given(processor.processAction(this.request, action)).willReturn(action);
|
||||
given(processor.getExtraHiddenFields(this.request)).willReturn(Collections.singletonMap("key", "value"));
|
||||
|
||||
this.tag.doStartTag();
|
||||
this.tag.doEndTag();
|
||||
@@ -341,8 +338,6 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
|
||||
assertEquals("<input type=\"hidden\" name=\"key\" value=\"value\" />", getInputTag(output));
|
||||
assertFormTagOpened(output);
|
||||
assertFormTagClosed(output);
|
||||
|
||||
verify(processor);
|
||||
}
|
||||
|
||||
private String getFormTag(String output) {
|
||||
|
||||
@@ -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.
|
||||
@@ -15,11 +15,6 @@
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expectLastCall;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@@ -40,6 +35,8 @@ import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.View;
|
||||
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Tests for AbstractView. Not called AbstractViewTests as
|
||||
* would otherwise be excluded by Ant build script wildcard.
|
||||
@@ -49,11 +46,8 @@ import org.springframework.web.servlet.View;
|
||||
public class BaseViewTests extends TestCase {
|
||||
|
||||
public void testRenderWithoutStaticAttributes() throws Exception {
|
||||
|
||||
WebApplicationContext wac = createMock(WebApplicationContext.class);
|
||||
wac.getServletContext();
|
||||
expectLastCall().andReturn(new MockServletContext());
|
||||
replay(wac);
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
@@ -72,17 +66,14 @@ public class BaseViewTests extends TestCase {
|
||||
checkContainsAll(model, tv.model);
|
||||
|
||||
assertTrue(tv.inited);
|
||||
verify(wac);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test attribute passing, NOT CSV parsing.
|
||||
*/
|
||||
public void testRenderWithStaticAttributesNoCollision() throws Exception {
|
||||
WebApplicationContext wac = createMock(WebApplicationContext.class);
|
||||
wac.getServletContext();
|
||||
expectLastCall().andReturn(new MockServletContext());
|
||||
replay(wac);
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
@@ -104,14 +95,11 @@ public class BaseViewTests extends TestCase {
|
||||
checkContainsAll(p, tv.model);
|
||||
|
||||
assertTrue(tv.inited);
|
||||
verify(wac);
|
||||
}
|
||||
|
||||
public void testPathVarsOverrideStaticAttributes() throws Exception {
|
||||
WebApplicationContext wac = createMock(WebApplicationContext.class);
|
||||
wac.getServletContext();
|
||||
expectLastCall().andReturn(new MockServletContext());
|
||||
replay(wac);
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
@@ -138,14 +126,11 @@ public class BaseViewTests extends TestCase {
|
||||
assertTrue(tv.model.get("something").equals("else"));
|
||||
|
||||
assertTrue(tv.inited);
|
||||
verify(wac);
|
||||
}
|
||||
|
||||
public void testDynamicModelOverridesStaticAttributesIfCollision() throws Exception {
|
||||
WebApplicationContext wac = createMock(WebApplicationContext.class);
|
||||
wac.getServletContext();
|
||||
expectLastCall().andReturn(new MockServletContext());
|
||||
replay(wac);
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
@@ -169,14 +154,11 @@ public class BaseViewTests extends TestCase {
|
||||
assertTrue(tv.model.get("something").equals("else"));
|
||||
|
||||
assertTrue(tv.inited);
|
||||
verify(wac);
|
||||
}
|
||||
|
||||
public void testDynamicModelOverridesPathVariables() throws Exception {
|
||||
WebApplicationContext wac = createMock(WebApplicationContext.class);
|
||||
wac.getServletContext();
|
||||
expectLastCall().andReturn(new MockServletContext());
|
||||
replay(wac);
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
TestView tv = new TestView(wac);
|
||||
tv.setApplicationContext(wac);
|
||||
@@ -202,7 +184,6 @@ public class BaseViewTests extends TestCase {
|
||||
assertTrue(tv.model.get("something").equals("else"));
|
||||
|
||||
assertTrue(tv.inited);
|
||||
verify(wac);
|
||||
}
|
||||
|
||||
public void testIgnoresNullAttributes() {
|
||||
|
||||
@@ -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.
|
||||
@@ -16,15 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.view;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@@ -53,6 +44,9 @@ import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@@ -92,25 +86,21 @@ public class ContentNegotiatingViewResolverTests {
|
||||
public void resolveViewNameWithPathExtension() throws Exception {
|
||||
request.setRequestURI("/test.xls");
|
||||
|
||||
ViewResolver viewResolverMock = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
View viewMock = createMock("application_xls", View.class);
|
||||
View viewMock = mock(View.class, "application_xls");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(null);
|
||||
expect(viewResolverMock.resolveViewName(viewName + ".xls", locale)).andReturn(viewMock);
|
||||
expect(viewMock.getContentType()).andReturn("application/vnd.ms-excel").anyTimes();
|
||||
|
||||
replay(viewResolverMock, viewMock);
|
||||
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
|
||||
given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
|
||||
given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertSame("Invalid view", viewMock, result);
|
||||
|
||||
verify(viewResolverMock, viewMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -123,23 +113,20 @@ public class ContentNegotiatingViewResolverTests {
|
||||
manager.addFileExtensionResolvers(extensionsResolver);
|
||||
viewResolver.setContentNegotiationManager(manager);
|
||||
|
||||
ViewResolver viewResolverMock = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
View viewMock = createMock("application_xls", View.class);
|
||||
View viewMock = mock(View.class, "application_xls");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(null);
|
||||
expect(viewResolverMock.resolveViewName(viewName + ".xls", locale)).andReturn(viewMock);
|
||||
expect(viewMock.getContentType()).andReturn("application/vnd.ms-excel").anyTimes();
|
||||
|
||||
replay(viewResolverMock, viewMock);
|
||||
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
|
||||
given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
|
||||
given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertSame("Invalid view", viewMock, result);
|
||||
verify(viewResolverMock, viewMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -159,26 +146,21 @@ public class ContentNegotiatingViewResolverTests {
|
||||
ParameterContentNegotiationStrategy paramStrategy = new ParameterContentNegotiationStrategy(mapping);
|
||||
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(paramStrategy));
|
||||
|
||||
ViewResolver viewResolverMock = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
View viewMock = createMock("application_xls", View.class);
|
||||
View viewMock = mock(View.class, "application_xls");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(null);
|
||||
expect(viewResolverMock.resolveViewName(viewName + ".xls", locale)).andReturn(viewMock);
|
||||
expect(viewMock.getContentType()).andReturn("application/vnd.ms-excel").anyTimes();
|
||||
|
||||
replay(viewResolverMock, viewMock);
|
||||
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
|
||||
given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
|
||||
given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertSame("Invalid view", viewMock, result);
|
||||
|
||||
verify(viewResolverMock, viewMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -189,58 +171,49 @@ public class ContentNegotiatingViewResolverTests {
|
||||
FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(mediaType);
|
||||
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));
|
||||
|
||||
ViewResolver viewResolverMock1 = createMock("viewResolver1", ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = createMock("viewResolver2", ViewResolver.class);
|
||||
ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
|
||||
ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
View viewMock1 = createMock("application_xml", View.class);
|
||||
View viewMock2 = createMock("text_html", View.class);
|
||||
View viewMock1 = mock(View.class, "application_xml");
|
||||
View viewMock2 = mock(View.class, "text_html");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
|
||||
expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
|
||||
expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
|
||||
expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
|
||||
|
||||
replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
|
||||
given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
|
||||
given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
|
||||
given(viewMock1.getContentType()).willReturn("application/xml");
|
||||
given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertSame("Invalid view", viewMock1, result);
|
||||
|
||||
verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveViewNameAcceptHeader() throws Exception {
|
||||
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||
|
||||
ViewResolver viewResolverMock1 = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock1 = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
View viewMock1 = createMock("application_xml", View.class);
|
||||
View viewMock2 = createMock("text_html", View.class);
|
||||
View viewMock1 = mock(View.class, "application_xml");
|
||||
View viewMock2 = mock(View.class, "text_html");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
|
||||
expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
|
||||
expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
|
||||
expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
|
||||
|
||||
replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
|
||||
given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
|
||||
given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
|
||||
given(viewMock1.getContentType()).willReturn("application/xml");
|
||||
given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertSame("Invalid view", viewMock2, result);
|
||||
|
||||
verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
|
||||
}
|
||||
|
||||
// SPR-9160
|
||||
@@ -251,26 +224,23 @@ public class ContentNegotiatingViewResolverTests {
|
||||
|
||||
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(new HeaderContentNegotiationStrategy()));
|
||||
|
||||
ViewResolver htmlViewResolver = createMock(ViewResolver.class);
|
||||
ViewResolver jsonViewResolver = createMock(ViewResolver.class);
|
||||
ViewResolver htmlViewResolver = mock(ViewResolver.class);
|
||||
ViewResolver jsonViewResolver = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Arrays.asList(htmlViewResolver, jsonViewResolver));
|
||||
|
||||
View htmlView = createMock("text_html", View.class);
|
||||
View jsonViewMock = createMock("application_json", View.class);
|
||||
View htmlView = mock(View.class, "text_html");
|
||||
View jsonViewMock = mock(View.class, "application_json");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(htmlViewResolver.resolveViewName(viewName, locale)).andReturn(htmlView);
|
||||
expect(jsonViewResolver.resolveViewName(viewName, locale)).andReturn(jsonViewMock);
|
||||
expect(htmlView.getContentType()).andReturn("text/html").anyTimes();
|
||||
expect(jsonViewMock.getContentType()).andReturn("application/json").anyTimes();
|
||||
replay(htmlViewResolver, jsonViewResolver, htmlView, jsonViewMock);
|
||||
given(htmlViewResolver.resolveViewName(viewName, locale)).willReturn(htmlView);
|
||||
given(jsonViewResolver.resolveViewName(viewName, locale)).willReturn(jsonViewMock);
|
||||
given(htmlView.getContentType()).willReturn("text/html");
|
||||
given(jsonViewMock.getContentType()).willReturn("application/json");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertSame("Invalid view", jsonViewMock, result);
|
||||
|
||||
verify(htmlViewResolver, jsonViewResolver, htmlView, jsonViewMock);
|
||||
}
|
||||
|
||||
// SPR-9807
|
||||
@@ -279,40 +249,36 @@ public class ContentNegotiatingViewResolverTests {
|
||||
public void resolveViewNameAcceptHeaderWithSuffix() throws Exception {
|
||||
request.addHeader("Accept", "application/vnd.example-v2+xml");
|
||||
|
||||
ViewResolver viewResolverMock = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
View viewMock = createMock("application_xml", View.class);
|
||||
View viewMock = mock(View.class, "application_xml");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(viewMock);
|
||||
expect(viewMock.getContentType()).andReturn("application/*+xml").anyTimes();
|
||||
|
||||
replay(viewResolverMock, viewMock);
|
||||
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
|
||||
given(viewMock.getContentType()).willReturn("application/*+xml");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
|
||||
assertSame("Invalid view", viewMock, result);
|
||||
assertEquals(new MediaType("application", "vnd.example-v2+xml"), request.getAttribute(View.SELECTED_CONTENT_TYPE));
|
||||
|
||||
verify(viewResolverMock, viewMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveViewNameAcceptHeaderDefaultView() throws Exception {
|
||||
request.addHeader("Accept", "application/json");
|
||||
|
||||
ViewResolver viewResolverMock1 = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock1 = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
|
||||
|
||||
View viewMock1 = createMock("application_xml", View.class);
|
||||
View viewMock2 = createMock("text_html", View.class);
|
||||
View viewMock3 = createMock("application_json", View.class);
|
||||
View viewMock1 = mock(View.class, "application_xml");
|
||||
View viewMock2 = mock(View.class, "text_html");
|
||||
View viewMock3 = mock(View.class, "application_json");
|
||||
|
||||
List<View> defaultViews = new ArrayList<View>();
|
||||
defaultViews.add(viewMock3);
|
||||
@@ -323,49 +289,41 @@ public class ContentNegotiatingViewResolverTests {
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
|
||||
expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
|
||||
expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
|
||||
expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
|
||||
expect(viewMock3.getContentType()).andReturn("application/json").anyTimes();
|
||||
|
||||
replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
|
||||
given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
|
||||
given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
|
||||
given(viewMock1.getContentType()).willReturn("application/xml");
|
||||
given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
|
||||
given(viewMock3.getContentType()).willReturn("application/json");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertSame("Invalid view", viewMock3, result);
|
||||
|
||||
verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveViewNameFilename() throws Exception {
|
||||
request.setRequestURI("/test.html");
|
||||
|
||||
ViewResolver viewResolverMock1 = createMock("viewResolver1", ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = createMock("viewResolver2", ViewResolver.class);
|
||||
ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
|
||||
ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
View viewMock1 = createMock("application_xml", View.class);
|
||||
View viewMock2 = createMock("text_html", View.class);
|
||||
View viewMock1 = mock(View.class, "application_xml");
|
||||
View viewMock2 = mock(View.class, "text_html");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
|
||||
expect(viewResolverMock1.resolveViewName(viewName + ".html", locale)).andReturn(null);
|
||||
expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(null);
|
||||
expect(viewResolverMock2.resolveViewName(viewName + ".html", locale)).andReturn(viewMock2);
|
||||
expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
|
||||
expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
|
||||
|
||||
replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
|
||||
given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
|
||||
given(viewResolverMock1.resolveViewName(viewName + ".html", locale)).willReturn(null);
|
||||
given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(null);
|
||||
given(viewResolverMock2.resolveViewName(viewName + ".html", locale)).willReturn(viewMock2);
|
||||
given(viewMock1.getContentType()).willReturn("application/xml");
|
||||
given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertSame("Invalid view", viewMock2, result);
|
||||
|
||||
verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -376,13 +334,13 @@ public class ContentNegotiatingViewResolverTests {
|
||||
PathExtensionContentNegotiationStrategy pathStrategy = new PathExtensionContentNegotiationStrategy(mapping);
|
||||
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));
|
||||
|
||||
ViewResolver viewResolverMock1 = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock1 = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
|
||||
|
||||
View viewMock1 = createMock("application_xml", View.class);
|
||||
View viewMock2 = createMock("text_html", View.class);
|
||||
View viewMock3 = createMock("application_json", View.class);
|
||||
View viewMock1 = mock(View.class, "application_xml");
|
||||
View viewMock2 = mock(View.class, "text_html");
|
||||
View viewMock3 = mock(View.class, "application_json");
|
||||
|
||||
List<View> defaultViews = new ArrayList<View>();
|
||||
defaultViews.add(viewMock3);
|
||||
@@ -393,45 +351,37 @@ public class ContentNegotiatingViewResolverTests {
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
|
||||
expect(viewResolverMock1.resolveViewName(viewName + ".json", locale)).andReturn(null);
|
||||
expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
|
||||
expect(viewResolverMock2.resolveViewName(viewName + ".json", locale)).andReturn(null);
|
||||
expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
|
||||
expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
|
||||
expect(viewMock3.getContentType()).andReturn("application/json").anyTimes();
|
||||
|
||||
replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
|
||||
given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
|
||||
given(viewResolverMock1.resolveViewName(viewName + ".json", locale)).willReturn(null);
|
||||
given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
|
||||
given(viewResolverMock2.resolveViewName(viewName + ".json", locale)).willReturn(null);
|
||||
given(viewMock1.getContentType()).willReturn("application/xml");
|
||||
given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
|
||||
given(viewMock3.getContentType()).willReturn("application/json");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertSame("Invalid view", viewMock3, result);
|
||||
|
||||
verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveViewContentTypeNull() throws Exception {
|
||||
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||
|
||||
ViewResolver viewResolverMock = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
View viewMock = createMock("application_xml", View.class);
|
||||
View viewMock = mock(View.class, "application_xml");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(viewMock);
|
||||
expect(viewMock.getContentType()).andReturn(null).anyTimes();
|
||||
|
||||
replay(viewResolverMock, viewMock);
|
||||
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
|
||||
given(viewMock.getContentType()).willReturn(null);
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertNull("Invalid view", result);
|
||||
|
||||
verify(viewResolverMock, viewMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -445,11 +395,11 @@ public class ContentNegotiatingViewResolverTests {
|
||||
|
||||
UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
|
||||
urlViewResolver.setApplicationContext(webAppContext);
|
||||
ViewResolver xmlViewResolver = createMock(ViewResolver.class);
|
||||
ViewResolver xmlViewResolver = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));
|
||||
|
||||
View xmlView = createMock("application_xml", View.class);
|
||||
View jsonView = createMock("application_json", View.class);
|
||||
View xmlView = mock(View.class, "application_xml");
|
||||
View jsonView = mock(View.class, "application_json");
|
||||
viewResolver.setDefaultViews(Arrays.asList(jsonView));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
@@ -457,40 +407,32 @@ public class ContentNegotiatingViewResolverTests {
|
||||
String viewName = "redirect:anotherTest";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(xmlViewResolver.resolveViewName(viewName, locale)).andReturn(xmlView);
|
||||
expect(jsonView.getContentType()).andReturn("application/json").anyTimes();
|
||||
|
||||
replay(xmlViewResolver, xmlView, jsonView);
|
||||
given(xmlViewResolver.resolveViewName(viewName, locale)).willReturn(xmlView);
|
||||
given(jsonView.getContentType()).willReturn("application/json");
|
||||
|
||||
View actualView = viewResolver.resolveViewName(viewName, locale);
|
||||
assertEquals("Invalid view", RedirectView.class, actualView.getClass());
|
||||
|
||||
verify(xmlViewResolver, xmlView, jsonView);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveViewNoMatch() throws Exception {
|
||||
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");
|
||||
|
||||
ViewResolver viewResolverMock = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
View viewMock = createMock("application_xml", View.class);
|
||||
View viewMock = mock(View.class, "application_xml");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(viewMock);
|
||||
expect(viewMock.getContentType()).andReturn("application/pdf").anyTimes();
|
||||
|
||||
replay(viewResolverMock, viewMock);
|
||||
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
|
||||
given(viewMock.getContentType()).willReturn("application/pdf");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertNull("Invalid view", result);
|
||||
|
||||
verify(viewResolverMock, viewMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -498,28 +440,24 @@ public class ContentNegotiatingViewResolverTests {
|
||||
viewResolver.setUseNotAcceptableStatusCode(true);
|
||||
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");
|
||||
|
||||
ViewResolver viewResolverMock = createMock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
View viewMock = createMock("application_xml", View.class);
|
||||
View viewMock = mock(View.class, "application_xml");
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(viewMock);
|
||||
expect(viewMock.getContentType()).andReturn("application/pdf").anyTimes();
|
||||
|
||||
replay(viewResolverMock, viewMock);
|
||||
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
|
||||
given(viewMock.getContentType()).willReturn("application/pdf");
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertNotNull("Invalid view", result);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
result.render(null, request, response);
|
||||
assertEquals("Invalid status code set", 406, response.getStatus());
|
||||
|
||||
verify(viewResolverMock, viewMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.
|
||||
@@ -16,11 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.view;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expectLastCall;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
@@ -36,6 +31,8 @@ import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
@@ -150,19 +147,9 @@ public class InternalResourceViewTests extends TestCase {
|
||||
|
||||
String url = "forward-to";
|
||||
|
||||
HttpServletRequest request = createMock(HttpServletRequest.class);
|
||||
request.getAttribute(View.PATH_VARIABLES);
|
||||
expectLastCall().andReturn(null);
|
||||
Set<String> keys = model.keySet();
|
||||
for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
|
||||
String key = iter.next();
|
||||
request.setAttribute(key, model.get(key));
|
||||
expectLastCall().times(1);
|
||||
}
|
||||
|
||||
request.getRequestDispatcher(url);
|
||||
expectLastCall().andReturn(new MockRequestDispatcher(url));
|
||||
replay(request);
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
|
||||
given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
|
||||
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
InternalResourceView v = new InternalResourceView();
|
||||
@@ -172,7 +159,12 @@ public class InternalResourceViewTests extends TestCase {
|
||||
// Can now try multiple tests
|
||||
v.render(model, request, response);
|
||||
assertEquals(url, response.getIncludedUrl());
|
||||
verify(request);
|
||||
|
||||
Set<String> keys = model.keySet();
|
||||
for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
|
||||
String key = iter.next();
|
||||
verify(request).setAttribute(key, model.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
public void testIncludeOnAttribute() throws Exception {
|
||||
@@ -183,21 +175,11 @@ public class InternalResourceViewTests extends TestCase {
|
||||
|
||||
String url = "forward-to";
|
||||
|
||||
HttpServletRequest request = createMock(HttpServletRequest.class);
|
||||
request.getAttribute(View.PATH_VARIABLES);
|
||||
expectLastCall().andReturn(null);
|
||||
Set<String> keys = model.keySet();
|
||||
for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
|
||||
String key = iter.next();
|
||||
request.setAttribute(key, model.get(key));
|
||||
expectLastCall().times(1);
|
||||
}
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
|
||||
|
||||
request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
|
||||
expectLastCall().andReturn("somepath");
|
||||
request.getRequestDispatcher(url);
|
||||
expectLastCall().andReturn(new MockRequestDispatcher(url));
|
||||
replay(request);
|
||||
given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somepath");
|
||||
given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
|
||||
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
InternalResourceView v = new InternalResourceView();
|
||||
@@ -206,7 +188,12 @@ public class InternalResourceViewTests extends TestCase {
|
||||
// Can now try multiple tests
|
||||
v.render(model, request, response);
|
||||
assertEquals(url, response.getIncludedUrl());
|
||||
verify(request);
|
||||
|
||||
Set<String> keys = model.keySet();
|
||||
for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
|
||||
String key = iter.next();
|
||||
verify(request).setAttribute(key, model.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
public void testIncludeOnCommitted() throws Exception {
|
||||
@@ -217,21 +204,11 @@ public class InternalResourceViewTests extends TestCase {
|
||||
|
||||
String url = "forward-to";
|
||||
|
||||
HttpServletRequest request = createMock(HttpServletRequest.class);
|
||||
request.getAttribute(View.PATH_VARIABLES);
|
||||
expectLastCall().andReturn(null);
|
||||
Set<String> keys = model.keySet();
|
||||
for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
|
||||
String key = iter.next();
|
||||
request.setAttribute(key, model.get(key));
|
||||
expectLastCall().times(1);
|
||||
}
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
|
||||
|
||||
request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
|
||||
expectLastCall().andReturn(null);
|
||||
request.getRequestDispatcher(url);
|
||||
expectLastCall().andReturn(new MockRequestDispatcher(url));
|
||||
replay(request);
|
||||
given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
|
||||
given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
|
||||
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
response.setCommitted(true);
|
||||
@@ -241,7 +218,12 @@ public class InternalResourceViewTests extends TestCase {
|
||||
// Can now try multiple tests
|
||||
v.render(model, request, response);
|
||||
assertEquals(url, response.getIncludedUrl());
|
||||
verify(request);
|
||||
|
||||
Set<String> keys = model.keySet();
|
||||
for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
|
||||
String key = iter.next();
|
||||
verify(request).setAttribute(key, model.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,14 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.view;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.createNiceMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -34,13 +26,12 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.Test;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.context.ContextLoader;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
@@ -53,6 +44,9 @@ import org.springframework.web.servlet.support.RequestDataValueProcessorWrapper;
|
||||
import org.springframework.web.servlet.support.SessionFlashMapManager;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Tests for redirect view, and query string construction.
|
||||
* Doesn't test URL encoding, although it does check that it's called.
|
||||
@@ -156,7 +150,7 @@ public class RedirectViewTests {
|
||||
wac.setServletContext(new MockServletContext());
|
||||
wac.refresh();
|
||||
|
||||
RequestDataValueProcessor mockProcessor = createMock(RequestDataValueProcessor.class);
|
||||
RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
|
||||
wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
|
||||
|
||||
RedirectView rv = new RedirectView();
|
||||
@@ -167,12 +161,11 @@ public class RedirectViewTests {
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
EasyMock.expect(mockProcessor.processUrl(request, "/path")).andReturn("/path?key=123");
|
||||
EasyMock.replay(mockProcessor);
|
||||
given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
|
||||
|
||||
rv.render(new ModelMap(), request, response);
|
||||
|
||||
EasyMock.verify(mockProcessor);
|
||||
verify(mockProcessor).processUrl(request, "/path");
|
||||
}
|
||||
|
||||
|
||||
@@ -186,7 +179,7 @@ public class RedirectViewTests {
|
||||
contextLoader.initWebApplicationContext(servletContext);
|
||||
|
||||
try {
|
||||
RequestDataValueProcessor mockProcessor = createMock(RequestDataValueProcessor.class);
|
||||
RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
|
||||
wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
|
||||
|
||||
RedirectView rv = new RedirectView();
|
||||
@@ -195,12 +188,11 @@ public class RedirectViewTests {
|
||||
MockHttpServletRequest request = createRequest();
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
EasyMock.expect(mockProcessor.processUrl(request, "/path")).andReturn("/path?key=123");
|
||||
EasyMock.replay(mockProcessor);
|
||||
given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
|
||||
|
||||
rv.render(new ModelMap(), request, response);
|
||||
|
||||
EasyMock.verify(mockProcessor);
|
||||
verify(mockProcessor).processUrl(request, "/path");
|
||||
}
|
||||
finally {
|
||||
contextLoader.closeWebApplicationContext(servletContext);
|
||||
@@ -363,32 +355,28 @@ public class RedirectViewTests {
|
||||
rv.setContextRelative(contextRelative);
|
||||
rv.setExposeModelAttributes(exposeModelAttributes);
|
||||
|
||||
HttpServletRequest request = createNiceMock("request", HttpServletRequest.class);
|
||||
HttpServletRequest request = mock(HttpServletRequest.class, "request");
|
||||
if (exposeModelAttributes) {
|
||||
expect(request.getCharacterEncoding()).andReturn(WebUtils.DEFAULT_CHARACTER_ENCODING);
|
||||
given(request.getCharacterEncoding()).willReturn(WebUtils.DEFAULT_CHARACTER_ENCODING);
|
||||
}
|
||||
if (contextRelative) {
|
||||
expectedUrlForEncoding = "/context" + expectedUrlForEncoding;
|
||||
expect(request.getContextPath()).andReturn("/context");
|
||||
given(request.getContextPath()).willReturn("/context");
|
||||
}
|
||||
|
||||
expect(request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE)).andReturn(new FlashMap());
|
||||
given(request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE)).willReturn(new FlashMap());
|
||||
|
||||
FlashMapManager flashMapManager = new SessionFlashMapManager();
|
||||
expect(request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE)).andReturn(flashMapManager);
|
||||
given(request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE)).willReturn(flashMapManager);
|
||||
|
||||
HttpServletResponse response = createMock("response", HttpServletResponse.class);
|
||||
expect(response.encodeRedirectURL(expectedUrlForEncoding)).andReturn(expectedUrlForEncoding);
|
||||
HttpServletResponse response = mock(HttpServletResponse.class, "response");
|
||||
given(response.encodeRedirectURL(expectedUrlForEncoding)).willReturn(expectedUrlForEncoding);
|
||||
response.sendRedirect(expectedUrlForEncoding);
|
||||
|
||||
replay(request, response);
|
||||
|
||||
rv.render(map, request, response);
|
||||
if (exposeModelAttributes) {
|
||||
assertTrue("queryProperties() should have been called.", rv.queryPropertiesCalled);
|
||||
}
|
||||
|
||||
verify(request, response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -23,16 +23,10 @@ import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import freemarker.ext.servlet.AllHttpScopesHashModel;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import org.easymock.MockControl;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
@@ -46,6 +40,14 @@ import org.springframework.web.servlet.view.AbstractView;
|
||||
import org.springframework.web.servlet.view.InternalResourceView;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
|
||||
import freemarker.ext.servlet.AllHttpScopesHashModel;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 14.03.2004
|
||||
@@ -56,15 +58,9 @@ public class FreeMarkerViewTests {
|
||||
public void testNoFreeMarkerConfig() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
|
||||
MockControl wmc = MockControl.createControl(WebApplicationContext.class);
|
||||
WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
|
||||
wac.getBeansOfType(FreeMarkerConfig.class, true, false);
|
||||
wmc.setReturnValue(new HashMap());
|
||||
wac.getParentBeanFactory();
|
||||
wmc.setReturnValue(null);
|
||||
wac.getServletContext();
|
||||
wmc.setReturnValue(new MockServletContext());
|
||||
wmc.replay();
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(new HashMap<String, FreeMarkerConfig>());
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
fv.setUrl("anythingButNull");
|
||||
try {
|
||||
@@ -76,8 +72,6 @@ public class FreeMarkerViewTests {
|
||||
// Check there's a helpful error message
|
||||
assertTrue(ex.getMessage().indexOf("FreeMarkerConfig") != -1);
|
||||
}
|
||||
|
||||
wmc.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,21 +91,15 @@ public class FreeMarkerViewTests {
|
||||
public void testValidTemplateName() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
|
||||
MockControl wmc = MockControl.createNiceControl(WebApplicationContext.class);
|
||||
WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
MockServletContext sc = new MockServletContext();
|
||||
|
||||
wac.getBeansOfType(FreeMarkerConfig.class, true, false);
|
||||
Map configs = new HashMap();
|
||||
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
|
||||
configurer.setConfiguration(new TestConfiguration());
|
||||
configs.put("configurer", configurer);
|
||||
wmc.setReturnValue(configs);
|
||||
wac.getParentBeanFactory();
|
||||
wmc.setReturnValue(null);
|
||||
wac.getServletContext();
|
||||
wmc.setReturnValue(sc, 2);
|
||||
wmc.replay();
|
||||
given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
|
||||
given(wac.getServletContext()).willReturn(sc);
|
||||
|
||||
fv.setUrl("templateName");
|
||||
fv.setApplicationContext(wac);
|
||||
@@ -126,7 +114,6 @@ public class FreeMarkerViewTests {
|
||||
model.put("myattr", "myvalue");
|
||||
fv.render(model, request, response);
|
||||
|
||||
wmc.verify();
|
||||
assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
|
||||
}
|
||||
|
||||
@@ -134,21 +121,15 @@ public class FreeMarkerViewTests {
|
||||
public void testKeepExistingContentType() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
|
||||
MockControl wmc = MockControl.createNiceControl(WebApplicationContext.class);
|
||||
WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
MockServletContext sc = new MockServletContext();
|
||||
|
||||
wac.getBeansOfType(FreeMarkerConfig.class, true, false);
|
||||
Map configs = new HashMap();
|
||||
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
|
||||
configurer.setConfiguration(new TestConfiguration());
|
||||
configs.put("configurer", configurer);
|
||||
wmc.setReturnValue(configs);
|
||||
wac.getParentBeanFactory();
|
||||
wmc.setReturnValue(null);
|
||||
wac.getServletContext();
|
||||
wmc.setReturnValue(sc, 2);
|
||||
wmc.replay();
|
||||
given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
|
||||
given(wac.getServletContext()).willReturn(sc);
|
||||
|
||||
fv.setUrl("templateName");
|
||||
fv.setApplicationContext(wac);
|
||||
@@ -164,7 +145,6 @@ public class FreeMarkerViewTests {
|
||||
model.put("myattr", "myvalue");
|
||||
fv.render(model, request, response);
|
||||
|
||||
wmc.verify();
|
||||
assertEquals("myContentType", response.getContentType());
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -32,15 +32,16 @@ import net.sf.jasperreports.engine.JRExporterParameter;
|
||||
import net.sf.jasperreports.engine.JasperReport;
|
||||
import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider;
|
||||
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
|
||||
import org.easymock.MockControl;
|
||||
import org.junit.Ignore;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.ui.jasperreports.PersonBean;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
@@ -372,11 +373,8 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
|
||||
}
|
||||
|
||||
private DataSource getMockJdbcDataSource() throws SQLException {
|
||||
MockControl ctl = MockControl.createControl(DataSource.class);
|
||||
DataSource ds = (DataSource) ctl.getMock();
|
||||
ds.getConnection();
|
||||
ctl.setThrowable(new SQLException());
|
||||
ctl.replay();
|
||||
DataSource ds = mock(DataSource.class);
|
||||
given(ds.getConnection()).willThrow(new SQLException());
|
||||
return ds;
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -16,13 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.view.json;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@@ -56,6 +49,9 @@ import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
|
||||
import com.fasterxml.jackson.databind.ser.SerializerFactory;
|
||||
import com.fasterxml.jackson.databind.ser.Serializers;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Jeremy Grelle
|
||||
* @author Arjen Poutsma
|
||||
@@ -93,7 +89,7 @@ public class MappingJackson2JsonViewTests {
|
||||
public void renderSimpleMap() throws Exception {
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("bindingResult", createMock("binding_result", BindingResult.class));
|
||||
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
|
||||
model.put("foo", "bar");
|
||||
|
||||
view.setUpdateContentLength(true);
|
||||
@@ -133,7 +129,7 @@ public class MappingJackson2JsonViewTests {
|
||||
view.setDisableCaching(false);
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("bindingResult", createMock("binding_result", BindingResult.class));
|
||||
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
|
||||
model.put("foo", "bar");
|
||||
|
||||
view.render(model, request, response);
|
||||
@@ -154,7 +150,7 @@ public class MappingJackson2JsonViewTests {
|
||||
|
||||
Object bean = new TestBeanSimple();
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("bindingResult", createMock("binding_result", BindingResult.class));
|
||||
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
|
||||
model.put("foo", bean);
|
||||
|
||||
view.setUpdateContentLength(true);
|
||||
|
||||
@@ -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.
|
||||
@@ -16,13 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.view.json;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@@ -48,6 +41,9 @@ import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Jeremy Grelle
|
||||
* @author Arjen Poutsma
|
||||
@@ -84,7 +80,7 @@ public class MappingJacksonJsonViewTests {
|
||||
public void renderSimpleMap() throws Exception {
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("bindingResult", createMock("binding_result", BindingResult.class));
|
||||
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
|
||||
model.put("foo", "bar");
|
||||
|
||||
view.setUpdateContentLength(true);
|
||||
@@ -108,7 +104,7 @@ public class MappingJacksonJsonViewTests {
|
||||
view.setDisableCaching(false);
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("bindingResult", createMock("binding_result", BindingResult.class));
|
||||
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
|
||||
model.put("foo", "bar");
|
||||
|
||||
view.render(model, request, response);
|
||||
@@ -129,7 +125,7 @@ public class MappingJacksonJsonViewTests {
|
||||
|
||||
Object bean = new TestBeanSimple();
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("bindingResult", createMock("binding_result", BindingResult.class));
|
||||
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
|
||||
model.put("foo", bean);
|
||||
|
||||
view.setUpdateContentLength(true);
|
||||
|
||||
@@ -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.
|
||||
@@ -16,15 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.view.velocity;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expectLastCall;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
@@ -53,6 +44,9 @@ import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
import org.springframework.web.servlet.support.RequestDataValueProcessor;
|
||||
import org.springframework.web.servlet.view.AbstractView;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
@@ -63,12 +57,8 @@ public class VelocityViewTests {
|
||||
@Test
|
||||
public void testNoVelocityConfig() throws Exception {
|
||||
VelocityView vv = new VelocityView();
|
||||
WebApplicationContext wac = createMock(WebApplicationContext.class);
|
||||
wac.getBeansOfType(VelocityConfig.class, true, false);
|
||||
expectLastCall().andReturn(new HashMap<String, Object>());
|
||||
wac.getParentBeanFactory();
|
||||
expectLastCall().andReturn(null);
|
||||
replay(wac);
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(new HashMap<String, VelocityConfig>());
|
||||
|
||||
vv.setUrl("anythingButNull");
|
||||
try {
|
||||
@@ -79,8 +69,6 @@ public class VelocityViewTests {
|
||||
// Check there's a helpful error message
|
||||
assertTrue(ex.getMessage().contains("VelocityConfig"));
|
||||
}
|
||||
|
||||
verify(wac);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -131,7 +119,7 @@ public class VelocityViewTests {
|
||||
|
||||
final String templateName = "test.vm";
|
||||
|
||||
WebApplicationContext wac = createMock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
@@ -142,17 +130,12 @@ public class VelocityViewTests {
|
||||
return new TestVelocityEngine(templateName, expectedTemplate);
|
||||
}
|
||||
};
|
||||
wac.getBeansOfType(VelocityConfig.class, true, false);
|
||||
Map<String, Object> configurers = new HashMap<String, Object>();
|
||||
Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>();
|
||||
configurers.put("velocityConfigurer", vc);
|
||||
expectLastCall().andReturn(configurers);
|
||||
wac.getParentBeanFactory();
|
||||
expectLastCall().andReturn(null);
|
||||
wac.getServletContext();
|
||||
expectLastCall().andReturn(sc).times(3);
|
||||
wac.getBean("requestDataValueProcessor", RequestDataValueProcessor.class);
|
||||
expectLastCall().andReturn(null);
|
||||
replay(wac);
|
||||
given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers);
|
||||
given(wac.getServletContext()).willReturn(sc);
|
||||
given(wac.getBean("requestDataValueProcessor",
|
||||
RequestDataValueProcessor.class)).willReturn(null);
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
final HttpServletResponse expectedResponse = new MockHttpServletResponse();
|
||||
@@ -182,15 +165,13 @@ public class VelocityViewTests {
|
||||
assertNotNull(mergeTemplateFailureException);
|
||||
assertEquals(ex, mergeTemplateFailureException);
|
||||
}
|
||||
|
||||
verify(wac);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeepExistingContentType() throws Exception {
|
||||
final String templateName = "test.vm";
|
||||
|
||||
WebApplicationContext wac = createMock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
@@ -201,17 +182,12 @@ public class VelocityViewTests {
|
||||
return new TestVelocityEngine(templateName, expectedTemplate);
|
||||
}
|
||||
};
|
||||
wac.getBeansOfType(VelocityConfig.class, true, false);
|
||||
Map<String, Object> configurers = new HashMap<String, Object>();
|
||||
Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>();
|
||||
configurers.put("velocityConfigurer", vc);
|
||||
expectLastCall().andReturn(configurers);
|
||||
wac.getParentBeanFactory();
|
||||
expectLastCall().andReturn(null);
|
||||
wac.getServletContext();
|
||||
expectLastCall().andReturn(sc).times(3);
|
||||
wac.getBean("requestDataValueProcessor", RequestDataValueProcessor.class);
|
||||
expectLastCall().andReturn(null);
|
||||
replay(wac);
|
||||
given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers);
|
||||
given(wac.getServletContext()).willReturn(sc);
|
||||
given(wac.getBean("requestDataValueProcessor",
|
||||
RequestDataValueProcessor.class)).willReturn(null);
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
final HttpServletResponse expectedResponse = new MockHttpServletResponse();
|
||||
@@ -233,7 +209,6 @@ public class VelocityViewTests {
|
||||
vv.setApplicationContext(wac);
|
||||
vv.render(new HashMap<String, Object>(), request, expectedResponse);
|
||||
|
||||
verify(wac);
|
||||
assertEquals("myContentType", expectedResponse.getContentType());
|
||||
}
|
||||
|
||||
@@ -241,11 +216,8 @@ public class VelocityViewTests {
|
||||
public void testExposeHelpers() throws Exception {
|
||||
final String templateName = "test.vm";
|
||||
|
||||
WebApplicationContext wac = createMock(WebApplicationContext.class);
|
||||
wac.getParentBeanFactory();
|
||||
expectLastCall().andReturn(null);
|
||||
wac.getServletContext();
|
||||
expectLastCall().andReturn(new MockServletContext());
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
final Template expectedTemplate = new Template();
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@@ -254,22 +226,16 @@ public class VelocityViewTests {
|
||||
return new TestVelocityEngine(templateName, expectedTemplate);
|
||||
}
|
||||
};
|
||||
wac.getBeansOfType(VelocityConfig.class, true, false);
|
||||
Map<String, Object> configurers = new HashMap<String, Object>();
|
||||
Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>();
|
||||
configurers.put("velocityConfigurer", vc);
|
||||
expectLastCall().andReturn(configurers);
|
||||
replay(wac);
|
||||
given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers);
|
||||
|
||||
|
||||
// let it ask for locale
|
||||
HttpServletRequest req = createMock(HttpServletRequest.class);
|
||||
req.getAttribute(View.PATH_VARIABLES);
|
||||
expectLastCall().andReturn(null);
|
||||
req.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE);
|
||||
expectLastCall().andReturn(new AcceptHeaderLocaleResolver());
|
||||
req.getLocale();
|
||||
expectLastCall().andReturn(Locale.CANADA);
|
||||
replay(req);
|
||||
HttpServletRequest req = mock(HttpServletRequest.class);
|
||||
given(req.getAttribute(View.PATH_VARIABLES)).willReturn(null);
|
||||
given(req.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE)).willReturn(new AcceptHeaderLocaleResolver());
|
||||
given(req.getLocale()).willReturn(Locale.CANADA);
|
||||
|
||||
final HttpServletResponse expectedResponse = new MockHttpServletResponse();
|
||||
|
||||
@@ -308,8 +274,6 @@ public class VelocityViewTests {
|
||||
|
||||
vv.render(new HashMap<String, Object>(), req, expectedResponse);
|
||||
|
||||
verify(wac);
|
||||
verify(req);
|
||||
assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, expectedResponse.getContentType());
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -18,18 +18,19 @@ package org.springframework.web.servlet.view.xml;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@@ -41,7 +42,7 @@ public class MarshallingViewTests {
|
||||
|
||||
@Before
|
||||
public void createView() throws Exception {
|
||||
marshallerMock = createMock(Marshaller.class);
|
||||
marshallerMock = mock(Marshaller.class);
|
||||
view = new MarshallingView(marshallerMock);
|
||||
}
|
||||
|
||||
@@ -71,14 +72,12 @@ public class MarshallingViewTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
expect(marshallerMock.supports(Object.class)).andReturn(true);
|
||||
given(marshallerMock.supports(Object.class)).willReturn(true);
|
||||
marshallerMock.marshal(eq(toBeMarshalled), isA(StreamResult.class));
|
||||
|
||||
replay(marshallerMock);
|
||||
view.render(model, request, response);
|
||||
assertEquals("Invalid content type", "application/xml", response.getContentType());
|
||||
assertEquals("Invalid content length", 0, response.getContentLength());
|
||||
verify(marshallerMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -92,7 +91,6 @@ public class MarshallingViewTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
replay(marshallerMock);
|
||||
try {
|
||||
view.render(model, request, response);
|
||||
fail("ServletException expected");
|
||||
@@ -101,7 +99,6 @@ public class MarshallingViewTests {
|
||||
// expected
|
||||
}
|
||||
assertEquals("Invalid content length", 0, response.getContentLength());
|
||||
verify(marshallerMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -113,7 +110,6 @@ public class MarshallingViewTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
replay(marshallerMock);
|
||||
try {
|
||||
view.render(model, request, response);
|
||||
fail("ServletException expected");
|
||||
@@ -122,7 +118,6 @@ public class MarshallingViewTests {
|
||||
// expected
|
||||
}
|
||||
assertEquals("Invalid content length", 0, response.getContentLength());
|
||||
verify(marshallerMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -136,9 +131,8 @@ public class MarshallingViewTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
expect(marshallerMock.supports(Object.class)).andReturn(false);
|
||||
given(marshallerMock.supports(Object.class)).willReturn(false);
|
||||
|
||||
replay(marshallerMock);
|
||||
try {
|
||||
view.render(model, request, response);
|
||||
fail("ServletException expected");
|
||||
@@ -146,7 +140,6 @@ public class MarshallingViewTests {
|
||||
catch (ServletException ex) {
|
||||
// expected
|
||||
}
|
||||
verify(marshallerMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -159,14 +152,12 @@ public class MarshallingViewTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
expect(marshallerMock.supports(Object.class)).andReturn(true);
|
||||
marshallerMock.marshal(eq(toBeMarshalled), isA(StreamResult.class));
|
||||
given(marshallerMock.supports(Object.class)).willReturn(true);
|
||||
|
||||
replay(marshallerMock);
|
||||
view.render(model, request, response);
|
||||
assertEquals("Invalid content type", "application/xml", response.getContentType());
|
||||
assertEquals("Invalid content length", 0, response.getContentLength());
|
||||
verify(marshallerMock);
|
||||
verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -179,9 +170,8 @@ public class MarshallingViewTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
expect(marshallerMock.supports(Object.class)).andReturn(false);
|
||||
given(marshallerMock.supports(Object.class)).willReturn(false);
|
||||
|
||||
replay(marshallerMock);
|
||||
try {
|
||||
view.render(model, request, response);
|
||||
fail("ServletException expected");
|
||||
@@ -189,7 +179,6 @@ public class MarshallingViewTests {
|
||||
catch (ServletException ex) {
|
||||
// expected
|
||||
}
|
||||
verify(marshallerMock);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user