Migrate to Mockito.mock(T...) where feasible
This commit is contained in:
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
@@ -30,47 +29,28 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
/**
|
||||
* A test fixture with HandlerExecutionChain and mock handler interceptors.
|
||||
* Tests for {@link HandlerExecutionChain} with mock handler interceptors.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class HandlerExecutionChainTests {
|
||||
class HandlerExecutionChainTests {
|
||||
|
||||
private HandlerExecutionChain chain;
|
||||
private Object handler = new Object();
|
||||
|
||||
private Object handler;
|
||||
private AsyncHandlerInterceptor interceptor1 = mock();
|
||||
private AsyncHandlerInterceptor interceptor2 = mock();
|
||||
private AsyncHandlerInterceptor interceptor3 = mock();
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
private HandlerExecutionChain chain = new HandlerExecutionChain(handler, interceptor1, interceptor2, interceptor3);
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
private MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
private AsyncHandlerInterceptor interceptor1;
|
||||
|
||||
private AsyncHandlerInterceptor interceptor2;
|
||||
|
||||
private AsyncHandlerInterceptor interceptor3;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
this.request = new MockHttpServletRequest();
|
||||
this.response= new MockHttpServletResponse() ;
|
||||
|
||||
this.handler = new Object();
|
||||
this.chain = new HandlerExecutionChain(this.handler);
|
||||
|
||||
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);
|
||||
this.chain.addInterceptor(this.interceptor3);
|
||||
}
|
||||
private MockHttpServletResponse response = new MockHttpServletResponse() ;
|
||||
|
||||
|
||||
@Test
|
||||
public void successScenario() throws Exception {
|
||||
void successScenario() throws Exception {
|
||||
ModelAndView mav = new ModelAndView();
|
||||
|
||||
given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
@@ -91,7 +71,7 @@ public class HandlerExecutionChainTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void successAsyncScenario() throws Exception {
|
||||
void successAsyncScenario() throws Exception {
|
||||
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);
|
||||
@@ -106,7 +86,7 @@ public class HandlerExecutionChainTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void earlyExitInPreHandle() throws Exception {
|
||||
void earlyExitInPreHandle() throws Exception {
|
||||
given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willReturn(false);
|
||||
|
||||
@@ -116,13 +96,13 @@ public class HandlerExecutionChainTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionBeforePreHandle() throws Exception {
|
||||
void exceptionBeforePreHandle() throws Exception {
|
||||
this.chain.triggerAfterCompletion(this.request, this.response, null);
|
||||
verifyNoInteractions(this.interceptor1, this.interceptor2, this.interceptor3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionDuringPreHandle() throws Exception {
|
||||
void exceptionDuringPreHandle() throws Exception {
|
||||
Exception ex = new Exception("");
|
||||
|
||||
given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
@@ -141,7 +121,7 @@ public class HandlerExecutionChainTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionAfterPreHandle() throws Exception {
|
||||
void exceptionAfterPreHandle() throws Exception {
|
||||
Exception ex = new Exception("");
|
||||
|
||||
given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true);
|
||||
|
||||
@@ -120,7 +120,7 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void configureMessageConverters() {
|
||||
HttpMessageConverter<?> customConverter = mock(HttpMessageConverter.class);
|
||||
HttpMessageConverter<?> customConverter = mock();
|
||||
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
|
||||
WebMvcConfigurer configurer = new WebMvcConfigurer() {
|
||||
@Override
|
||||
@@ -206,8 +206,8 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void configurePathMatcher() {
|
||||
PathMatcher pathMatcher = mock(PathMatcher.class);
|
||||
UrlPathHelper pathHelper = mock(UrlPathHelper.class);
|
||||
PathMatcher pathMatcher = mock();
|
||||
UrlPathHelper pathHelper = mock();
|
||||
|
||||
WebMvcConfigurer configurer = new WebMvcConfigurer() {
|
||||
@Override
|
||||
@@ -275,8 +275,8 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
@Test
|
||||
public void configurePathPatternParser() {
|
||||
PathPatternParser patternParser = new PathPatternParser();
|
||||
PathMatcher pathMatcher = mock(PathMatcher.class);
|
||||
UrlPathHelper pathHelper = mock(UrlPathHelper.class);
|
||||
PathMatcher pathMatcher = mock();
|
||||
UrlPathHelper pathHelper = mock();
|
||||
|
||||
WebMvcConfigurer configurer = new WebMvcConfigurer() {
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -42,6 +41,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test fixture with a {@link InterceptorRegistry}, two {@link HandlerInterceptor}s and two
|
||||
@@ -122,7 +122,7 @@ public class InterceptorRegistryTests {
|
||||
|
||||
@Test
|
||||
public void addInterceptorsWithCustomPathMatcher() {
|
||||
PathMatcher pathMatcher = Mockito.mock(PathMatcher.class);
|
||||
PathMatcher pathMatcher = mock();
|
||||
this.registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").pathMatcher(pathMatcher);
|
||||
|
||||
MappedInterceptor mappedInterceptor = (MappedInterceptor) this.registry.getInterceptors().get(0);
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -47,6 +46,7 @@ import org.springframework.web.testfixture.servlet.MockServletContext;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ResourceHandlerRegistry}.
|
||||
@@ -132,8 +132,8 @@ public class ResourceHandlerRegistryTests {
|
||||
|
||||
@Test
|
||||
public void resourceChain() {
|
||||
ResourceResolver mockResolver = Mockito.mock(ResourceResolver.class);
|
||||
ResourceTransformer mockTransformer = Mockito.mock(ResourceTransformer.class);
|
||||
ResourceResolver mockResolver = mock();
|
||||
ResourceTransformer mockTransformer = mock();
|
||||
this.registration.resourceChain(true).addResolver(mockResolver).addTransformer(mockTransformer);
|
||||
|
||||
ResourceHttpRequestHandler handler = getHandler("/resources/**");
|
||||
@@ -190,11 +190,11 @@ public class ResourceHandlerRegistryTests {
|
||||
|
||||
@Test
|
||||
public void resourceChainWithOverrides() {
|
||||
CachingResourceResolver cachingResolver = Mockito.mock(CachingResourceResolver.class);
|
||||
VersionResourceResolver versionResolver = Mockito.mock(VersionResourceResolver.class);
|
||||
WebJarsResourceResolver webjarsResolver = Mockito.mock(WebJarsResourceResolver.class);
|
||||
CachingResourceResolver cachingResolver = mock();
|
||||
VersionResourceResolver versionResolver = mock();
|
||||
WebJarsResourceResolver webjarsResolver = mock();
|
||||
PathResourceResolver pathResourceResolver = new PathResourceResolver();
|
||||
CachingResourceTransformer cachingTransformer = Mockito.mock(CachingResourceTransformer.class);
|
||||
CachingResourceTransformer cachingTransformer = mock();
|
||||
CssLinkResourceTransformer cssLinkTransformer = new CssLinkResourceTransformer();
|
||||
|
||||
this.registration.setCachePeriod(3600)
|
||||
|
||||
@@ -487,7 +487,7 @@ public class WebMvcConfigurationSupportExtensionTests {
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Principal getUser() {
|
||||
return mock(Principal.class);
|
||||
return mock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ public class RouterFunctionsTests {
|
||||
public void routeMatch() {
|
||||
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
|
||||
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
RequestPredicate requestPredicate = mock();
|
||||
given(requestPredicate.test(request)).willReturn(true);
|
||||
|
||||
RouterFunction<ServerResponse>
|
||||
@@ -57,7 +57,7 @@ public class RouterFunctionsTests {
|
||||
public void routeNoMatch() {
|
||||
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
|
||||
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
RequestPredicate requestPredicate = mock();
|
||||
given(requestPredicate.test(request)).willReturn(false);
|
||||
|
||||
RouterFunction<ServerResponse> result = RouterFunctions.route(requestPredicate, handlerFunction);
|
||||
@@ -72,7 +72,7 @@ public class RouterFunctionsTests {
|
||||
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
|
||||
RouterFunction<ServerResponse> routerFunction = request -> Optional.of(handlerFunction);
|
||||
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
RequestPredicate requestPredicate = mock();
|
||||
given(requestPredicate.nest(request)).willReturn(Optional.of(request));
|
||||
|
||||
RouterFunction<ServerResponse> result = RouterFunctions.nest(requestPredicate, routerFunction);
|
||||
@@ -88,7 +88,7 @@ public class RouterFunctionsTests {
|
||||
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
|
||||
RouterFunction<ServerResponse> routerFunction = request -> Optional.of(handlerFunction);
|
||||
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
RequestPredicate requestPredicate = mock();
|
||||
given(requestPredicate.nest(request)).willReturn(Optional.empty());
|
||||
|
||||
RouterFunction<ServerResponse> result = RouterFunctions.nest(requestPredicate, routerFunction);
|
||||
|
||||
@@ -48,11 +48,10 @@ class HandlerMappingTests {
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void orderedInterceptors(Function<String, MockHttpServletRequest> requestFactory, TestHandlerMapping mapping) throws Exception {
|
||||
|
||||
MappedInterceptor i1 = new MappedInterceptor(new String[] {"/**"}, mock(HandlerInterceptor.class));
|
||||
HandlerInterceptor i2 = mock(HandlerInterceptor.class);
|
||||
HandlerInterceptor i2 = mock();
|
||||
MappedInterceptor i3 = new MappedInterceptor(new String[] {"/**"}, mock(HandlerInterceptor.class));
|
||||
HandlerInterceptor i4 = mock(HandlerInterceptor.class);
|
||||
HandlerInterceptor i4 = mock();
|
||||
|
||||
mapping.setInterceptors(i1, i2, i3, i4);
|
||||
mapping.setApplicationContext(new StaticWebApplicationContext());
|
||||
|
||||
@@ -28,7 +28,6 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
|
||||
@@ -109,30 +108,27 @@ class MappedInterceptorTests {
|
||||
|
||||
@Test
|
||||
void preHandle() throws Exception {
|
||||
HandlerInterceptor delegate = mock(HandlerInterceptor.class);
|
||||
HandlerInterceptor delegate = mock();
|
||||
|
||||
new MappedInterceptor(null, delegate).preHandle(
|
||||
mock(HttpServletRequest.class), mock(HttpServletResponse.class), null);
|
||||
new MappedInterceptor(null, delegate).preHandle(mock(), mock(), null);
|
||||
|
||||
then(delegate).should().preHandle(any(HttpServletRequest.class), any(HttpServletResponse.class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void postHandle() throws Exception {
|
||||
HandlerInterceptor delegate = mock(HandlerInterceptor.class);
|
||||
HandlerInterceptor delegate = mock();
|
||||
|
||||
new MappedInterceptor(null, delegate).postHandle(
|
||||
mock(HttpServletRequest.class), mock(HttpServletResponse.class), null, mock(ModelAndView.class));
|
||||
new MappedInterceptor(null, delegate).postHandle(mock(), mock(), null, mock());
|
||||
|
||||
then(delegate).should().postHandle(any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void afterCompletion() throws Exception {
|
||||
HandlerInterceptor delegate = mock(HandlerInterceptor.class);
|
||||
HandlerInterceptor delegate = mock();
|
||||
|
||||
new MappedInterceptor(null, delegate).afterCompletion(
|
||||
mock(HttpServletRequest.class), mock(HttpServletResponse.class), null, mock(Exception.class));
|
||||
new MappedInterceptor(null, delegate).afterCompletion(mock(), mock(), null, mock());
|
||||
|
||||
then(delegate).should().afterCompletion(any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@@ -81,10 +81,10 @@ public class ControllerTests {
|
||||
private void doTestServletForwardingController(ServletForwardingController sfc, boolean include)
|
||||
throws Exception {
|
||||
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
ServletContext context = mock(ServletContext.class);
|
||||
RequestDispatcher dispatcher = mock(RequestDispatcher.class);
|
||||
HttpServletRequest request = mock();
|
||||
HttpServletResponse response = mock();
|
||||
ServletContext context = mock();
|
||||
RequestDispatcher dispatcher = mock();
|
||||
|
||||
given(request.getMethod()).willReturn("GET");
|
||||
given(context.getNamedDispatcher("action")).willReturn(dispatcher);
|
||||
|
||||
@@ -121,7 +121,7 @@ public abstract class AbstractRequestAttributesArgumentResolverTests {
|
||||
public void resolveOptional() throws Exception {
|
||||
WebDataBinder dataBinder = new WebRequestDataBinder(null);
|
||||
dataBinder.setConversionService(new DefaultConversionService());
|
||||
WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
|
||||
WebDataBinderFactory factory = mock();
|
||||
given(factory.createBinder(this.webRequest, null, "foo")).willReturn(dataBinder);
|
||||
|
||||
MethodParameter param = initMethodParameter(3);
|
||||
|
||||
@@ -96,20 +96,24 @@ import static org.springframework.web.servlet.HandlerMapping.PRODUCIBLE_MEDIA_TY
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class HttpEntityMethodProcessorMockTests {
|
||||
class HttpEntityMethodProcessorMockTests {
|
||||
|
||||
private static final ZoneId GMT = ZoneId.of("GMT");
|
||||
|
||||
|
||||
private HttpEntityMethodProcessor processor;
|
||||
|
||||
private HttpMessageConverter<String> stringHttpMessageConverter;
|
||||
@SuppressWarnings("unchecked")
|
||||
private HttpMessageConverter<String> stringHttpMessageConverter = mock();
|
||||
|
||||
private HttpMessageConverter<Resource> resourceMessageConverter;
|
||||
@SuppressWarnings("unchecked")
|
||||
private HttpMessageConverter<Resource> resourceMessageConverter = mock();
|
||||
|
||||
private HttpMessageConverter<Object> resourceRegionMessageConverter;
|
||||
@SuppressWarnings("unchecked")
|
||||
private HttpMessageConverter<Object> resourceRegionMessageConverter = mock();
|
||||
|
||||
private HttpMessageConverter<Object> jsonMessageConverter;
|
||||
@SuppressWarnings("unchecked")
|
||||
private HttpMessageConverter<Object> jsonMessageConverter = mock();
|
||||
|
||||
private MethodParameter paramHttpEntity;
|
||||
|
||||
@@ -135,31 +139,25 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
|
||||
private MethodParameter returnTypeProblemDetail;
|
||||
|
||||
private ModelAndViewContainer mavContainer;
|
||||
private ModelAndViewContainer mavContainer = new ModelAndViewContainer();
|
||||
|
||||
private MockHttpServletRequest servletRequest;
|
||||
private MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/foo");
|
||||
|
||||
private MockHttpServletResponse servletResponse;
|
||||
private MockHttpServletResponse servletResponse = new MockHttpServletResponse();
|
||||
|
||||
private ServletWebRequest webRequest;
|
||||
private ServletWebRequest webRequest = new ServletWebRequest(servletRequest, servletResponse);
|
||||
|
||||
|
||||
@BeforeEach
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setup() throws Exception {
|
||||
|
||||
stringHttpMessageConverter = mock(HttpMessageConverter.class);
|
||||
void setup() throws Exception {
|
||||
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
|
||||
|
||||
resourceMessageConverter = mock(HttpMessageConverter.class);
|
||||
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
|
||||
given(resourceMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
|
||||
|
||||
resourceRegionMessageConverter = mock(HttpMessageConverter.class);
|
||||
given(resourceRegionMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
|
||||
given(resourceRegionMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
|
||||
|
||||
jsonMessageConverter = mock(HttpMessageConverter.class);
|
||||
given(jsonMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.APPLICATION_PROBLEM_JSON));
|
||||
given(jsonMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.APPLICATION_PROBLEM_JSON));
|
||||
|
||||
@@ -181,16 +179,11 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
returnTypeResponseEntityResource = new MethodParameter(getClass().getMethod("handle5"), -1);
|
||||
returnTypeErrorResponse = new MethodParameter(getClass().getMethod("handle6"), -1);
|
||||
returnTypeProblemDetail = new MethodParameter(getClass().getMethod("handle7"), -1);
|
||||
|
||||
mavContainer = new ModelAndViewContainer();
|
||||
servletRequest = new MockHttpServletRequest("GET", "/foo");
|
||||
servletResponse = new MockHttpServletResponse();
|
||||
webRequest = new ServletWebRequest(servletRequest, servletResponse);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void supportsParameter() {
|
||||
void supportsParameter() {
|
||||
assertThat(processor.supportsParameter(paramHttpEntity)).as("HttpEntity parameter not supported").isTrue();
|
||||
assertThat(processor.supportsParameter(paramRequestEntity)).as("RequestEntity parameter not supported").isTrue();
|
||||
assertThat(processor.supportsParameter(paramResponseEntity)).as("ResponseEntity parameter supported").isFalse();
|
||||
@@ -198,7 +191,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsReturnType() {
|
||||
void supportsReturnType() {
|
||||
assertThat(processor.supportsReturnType(returnTypeResponseEntity)).as("ResponseEntity return type not supported").isTrue();
|
||||
assertThat(processor.supportsReturnType(returnTypeHttpEntity)).as("HttpEntity return type not supported").isTrue();
|
||||
assertThat(processor.supportsReturnType(returnTypeHttpEntitySubclass)).as("Custom HttpEntity subclass not supported").isTrue();
|
||||
@@ -209,7 +202,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldResolveHttpEntityArgument() throws Exception {
|
||||
void shouldResolveHttpEntityArgument() throws Exception {
|
||||
String body = "Foo";
|
||||
|
||||
MediaType contentType = TEXT_PLAIN;
|
||||
@@ -227,7 +220,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldResolveRequestEntityArgument() throws Exception {
|
||||
void shouldResolveRequestEntityArgument() throws Exception {
|
||||
String body = "Foo";
|
||||
|
||||
MediaType contentType = TEXT_PLAIN;
|
||||
@@ -254,7 +247,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailResolvingWhenConverterCannotRead() throws Exception {
|
||||
void shouldFailResolvingWhenConverterCannotRead() throws Exception {
|
||||
MediaType contentType = TEXT_PLAIN;
|
||||
servletRequest.setMethod("POST");
|
||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||
@@ -267,7 +260,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailResolvingWhenContentTypeNotSupported() throws Exception {
|
||||
void shouldFailResolvingWhenContentTypeNotSupported() throws Exception {
|
||||
servletRequest.setMethod("POST");
|
||||
servletRequest.setContent("some content".getBytes(StandardCharsets.UTF_8));
|
||||
assertThatExceptionOfType(HttpMediaTypeNotSupportedException.class).isThrownBy(() ->
|
||||
@@ -275,7 +268,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleReturnValue() throws Exception {
|
||||
void shouldHandleReturnValue() throws Exception {
|
||||
String body = "Foo";
|
||||
ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
|
||||
MediaType accepted = TEXT_PLAIN;
|
||||
@@ -289,7 +282,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleErrorResponse() throws Exception {
|
||||
void shouldHandleErrorResponse() throws Exception {
|
||||
ErrorResponseException ex = new ErrorResponseException(HttpStatus.BAD_REQUEST);
|
||||
ex.getHeaders().add("foo", "bar");
|
||||
servletRequest.addHeader("Accept", APPLICATION_PROBLEM_JSON_VALUE);
|
||||
@@ -307,7 +300,6 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
.as("Instance was not set to the request path")
|
||||
.isEqualTo(servletRequest.getRequestURI());
|
||||
|
||||
|
||||
// But if instance is set, it should be respected
|
||||
ex.getBody().setInstance(URI.create("/something/else"));
|
||||
processor.handleReturnValue(ex, returnTypeProblemDetail, mavContainer, webRequest);
|
||||
@@ -320,7 +312,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleProblemDetail() throws Exception {
|
||||
void shouldHandleProblemDetail() throws Exception {
|
||||
ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
|
||||
servletRequest.addHeader("Accept", APPLICATION_PROBLEM_JSON_VALUE);
|
||||
given(jsonMessageConverter.canWrite(ProblemDetail.class, APPLICATION_PROBLEM_JSON)).willReturn(true);
|
||||
@@ -350,7 +342,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleReturnValueWithProducibleMediaType() throws Exception {
|
||||
void shouldHandleReturnValueWithProducibleMediaType() throws Exception {
|
||||
String body = "Foo";
|
||||
ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
|
||||
servletRequest.addHeader("Accept", "text/*");
|
||||
@@ -365,11 +357,11 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void shouldHandleReturnValueWithResponseBodyAdvice() throws Exception {
|
||||
void shouldHandleReturnValueWithResponseBodyAdvice() throws Exception {
|
||||
servletRequest.addHeader("Accept", "text/*");
|
||||
servletRequest.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.TEXT_HTML));
|
||||
ResponseEntity<String> returnValue = new ResponseEntity<>(HttpStatus.OK);
|
||||
ResponseBodyAdvice<String> advice = mock(ResponseBodyAdvice.class);
|
||||
ResponseBodyAdvice<String> advice = mock();
|
||||
given(advice.supports(any(), any())).willReturn(true);
|
||||
given(advice.beforeBodyWrite(any(), any(), any(), any(), any(), any())).willReturn("Foo");
|
||||
|
||||
@@ -386,7 +378,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailHandlingWhenContentTypeNotSupported() throws Exception {
|
||||
void shouldFailHandlingWhenContentTypeNotSupported() throws Exception {
|
||||
String body = "Foo";
|
||||
ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
|
||||
MediaType accepted = MediaType.APPLICATION_ATOM_XML;
|
||||
@@ -401,7 +393,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // gh-23205
|
||||
public void shouldFailWithServerErrorIfContentTypeFromResponseEntity() {
|
||||
void shouldFailWithServerErrorIfContentTypeFromResponseEntity() {
|
||||
ResponseEntity<String> returnValue = ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body("<foo/>");
|
||||
@@ -417,7 +409,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // gh-23287
|
||||
public void shouldFailWithServerErrorIfContentTypeFromProducibleAttribute() {
|
||||
void shouldFailWithServerErrorIfContentTypeFromProducibleAttribute() {
|
||||
Set<MediaType> mediaTypes = Collections.singleton(MediaType.APPLICATION_XML);
|
||||
servletRequest.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
|
||||
|
||||
@@ -434,7 +426,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailHandlingWhenConverterCannotWrite() throws Exception {
|
||||
void shouldFailHandlingWhenConverterCannotWrite() throws Exception {
|
||||
String body = "Foo";
|
||||
ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
|
||||
MediaType accepted = TEXT_PLAIN;
|
||||
@@ -450,7 +442,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // SPR-9142
|
||||
public void shouldFailHandlingWhenAcceptHeaderIllegal() throws Exception {
|
||||
void shouldFailHandlingWhenAcceptHeaderIllegal() throws Exception {
|
||||
ResponseEntity<String> returnValue = new ResponseEntity<>("Body", HttpStatus.ACCEPTED);
|
||||
servletRequest.addHeader("Accept", "01");
|
||||
|
||||
@@ -459,7 +451,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleResponseHeaderNoBody() throws Exception {
|
||||
void shouldHandleResponseHeaderNoBody() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("headerName", "headerValue");
|
||||
ResponseEntity<String> returnValue = new ResponseEntity<>(headers, HttpStatus.ACCEPTED);
|
||||
@@ -471,7 +463,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleResponseHeaderAndBody() throws Exception {
|
||||
void shouldHandleResponseHeaderAndBody() throws Exception {
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.set("header", "headerValue");
|
||||
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.ACCEPTED);
|
||||
@@ -486,7 +478,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleLastModifiedWithHttp304() throws Exception {
|
||||
void shouldHandleLastModifiedWithHttp304() throws Exception {
|
||||
long currentTime = new Date().getTime();
|
||||
long oneMinuteAgo = currentTime - (1000 * 60);
|
||||
ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT);
|
||||
@@ -500,7 +492,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleEtagWithHttp304() throws Exception {
|
||||
void handleEtagWithHttp304() throws Exception {
|
||||
String etagValue = "\"deadb33f8badf00d\"";
|
||||
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
|
||||
ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body");
|
||||
@@ -512,8 +504,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleEtagWithHttp304AndEtagFilterHasNoImpact() throws Exception {
|
||||
|
||||
void handleEtagWithHttp304AndEtagFilterHasNoImpact() throws Exception {
|
||||
String eTagValue = "\"deadb33f8badf00d\"";
|
||||
|
||||
FilterChain chain = (req, res) -> {
|
||||
@@ -534,7 +525,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // SPR-14559
|
||||
public void shouldHandleInvalidIfNoneMatchWithHttp200() throws Exception {
|
||||
void shouldHandleInvalidIfNoneMatchWithHttp200() throws Exception {
|
||||
String etagValue = "\"deadb33f8badf00d\"";
|
||||
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, "unquoted");
|
||||
ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body");
|
||||
@@ -546,7 +537,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleETagAndLastModifiedWithHttp304() throws Exception {
|
||||
void shouldHandleETagAndLastModifiedWithHttp304() throws Exception {
|
||||
long currentTime = new Date().getTime();
|
||||
long oneMinuteAgo = currentTime - (1000 * 60);
|
||||
String etagValue = "\"deadb33f8badf00d\"";
|
||||
@@ -563,7 +554,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNotModifiedResponse() throws Exception {
|
||||
void shouldHandleNotModifiedResponse() throws Exception {
|
||||
long currentTime = new Date().getTime();
|
||||
long oneMinuteAgo = currentTime - (1000 * 60);
|
||||
String etagValue = "\"deadb33f8badf00d\"";
|
||||
@@ -577,7 +568,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleChangedETagAndLastModified() throws Exception {
|
||||
void shouldHandleChangedETagAndLastModified() throws Exception {
|
||||
long currentTime = new Date().getTime();
|
||||
long oneMinuteAgo = currentTime - (1000 * 60);
|
||||
String etagValue = "\"deadb33f8badf00d\"";
|
||||
@@ -595,7 +586,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // SPR-13496
|
||||
public void shouldHandleConditionalRequestIfNoneMatchWildcard() throws Exception {
|
||||
void shouldHandleConditionalRequestIfNoneMatchWildcard() throws Exception {
|
||||
String wildcardValue = "*";
|
||||
String etagValue = "\"some-etag\"";
|
||||
servletRequest.setMethod("POST");
|
||||
@@ -609,7 +600,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // SPR-13626
|
||||
public void shouldHandleGetIfNoneMatchWildcard() throws Exception {
|
||||
void shouldHandleGetIfNoneMatchWildcard() throws Exception {
|
||||
String wildcardValue = "*";
|
||||
String etagValue = "\"some-etag\"";
|
||||
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, wildcardValue);
|
||||
@@ -622,7 +613,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // SPR-13626
|
||||
public void shouldHandleIfNoneMatchIfMatch() throws Exception {
|
||||
void shouldHandleIfNoneMatchIfMatch() throws Exception {
|
||||
String etagValue = "\"some-etag\"";
|
||||
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
|
||||
servletRequest.addHeader(HttpHeaders.IF_MATCH, "ifmatch");
|
||||
@@ -635,7 +626,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // SPR-13626
|
||||
public void shouldHandleIfNoneMatchIfUnmodifiedSince() throws Exception {
|
||||
void shouldHandleIfNoneMatchIfUnmodifiedSince() throws Exception {
|
||||
String etagValue = "\"some-etag\"";
|
||||
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
|
||||
ZonedDateTime dateTime = ofEpochMilli(new Date().getTime()).atZone(GMT);
|
||||
@@ -649,7 +640,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleResource() throws Exception {
|
||||
void shouldHandleResource() throws Exception {
|
||||
ResponseEntity<Resource> returnValue = ResponseEntity
|
||||
.ok(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
@@ -665,7 +656,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleResourceByteRange() throws Exception {
|
||||
void shouldHandleResourceByteRange() throws Exception {
|
||||
ResponseEntity<Resource> returnValue = ResponseEntity
|
||||
.ok(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)));
|
||||
servletRequest.addHeader("Range", "bytes=0-5");
|
||||
@@ -682,7 +673,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleReturnTypeResourceIllegalByteRange() throws Exception {
|
||||
void handleReturnTypeResourceIllegalByteRange() throws Exception {
|
||||
ResponseEntity<Resource> returnValue = ResponseEntity
|
||||
.ok(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)));
|
||||
servletRequest.addHeader("Range", "illegal");
|
||||
@@ -698,7 +689,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test //SPR-16754
|
||||
public void disableRangeSupportForStreamingResponses() throws Exception {
|
||||
void disableRangeSupportForStreamingResponses() throws Exception {
|
||||
InputStream is = new ByteArrayInputStream("Content".getBytes(StandardCharsets.UTF_8));
|
||||
InputStreamResource resource = new InputStreamResource(is, "test");
|
||||
ResponseEntity<Resource> returnValue = ResponseEntity.ok(resource);
|
||||
@@ -715,7 +706,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test //SPR-16921
|
||||
public void disableRangeSupportIfContentRangePresent() throws Exception {
|
||||
void disableRangeSupportIfContentRangePresent() throws Exception {
|
||||
ResponseEntity<Resource> returnValue = ResponseEntity
|
||||
.status(HttpStatus.PARTIAL_CONTENT)
|
||||
.header(HttpHeaders.RANGE, "bytes=0-5")
|
||||
@@ -731,7 +722,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test //SPR-14767
|
||||
public void shouldHandleValidatorHeadersInputResponses() throws Exception {
|
||||
void shouldHandleValidatorHeadersInputResponses() throws Exception {
|
||||
servletRequest.setMethod("PUT");
|
||||
String etagValue = "\"some-etag\"";
|
||||
ResponseEntity<String> returnValue = ResponseEntity.ok().header(HttpHeaders.ETAG, etagValue).body("body");
|
||||
@@ -743,7 +734,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotFailPreconditionForPutRequests() throws Exception {
|
||||
void shouldNotFailPreconditionForPutRequests() throws Exception {
|
||||
servletRequest.setMethod("PUT");
|
||||
ZonedDateTime dateTime = ofEpochMilli(new Date().getTime()).atZone(GMT);
|
||||
servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime));
|
||||
@@ -758,7 +749,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void varyHeader() throws Exception {
|
||||
void varyHeader() throws Exception {
|
||||
String[] entityValues = {"Accept-Language", "User-Agent"};
|
||||
String[] existingValues = {};
|
||||
String[] expected = {"Accept-Language, User-Agent"};
|
||||
@@ -766,7 +757,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void varyHeaderWithExistingWildcard() throws Exception {
|
||||
void varyHeaderWithExistingWildcard() throws Exception {
|
||||
String[] entityValues = {"Accept-Language"};
|
||||
String[] existingValues = {"*"};
|
||||
String[] expected = {"*"};
|
||||
@@ -774,7 +765,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void varyHeaderWithExistingCommaValues() throws Exception {
|
||||
void varyHeaderWithExistingCommaValues() throws Exception {
|
||||
String[] entityValues = {"Accept-Language", "User-Agent"};
|
||||
String[] existingValues = {"Accept-Encoding", "Accept-Language"};
|
||||
String[] expected = {"Accept-Encoding", "Accept-Language", "User-Agent"};
|
||||
@@ -782,7 +773,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void varyHeaderWithExistingCommaSeparatedValues() throws Exception {
|
||||
void varyHeaderWithExistingCommaSeparatedValues() throws Exception {
|
||||
String[] entityValues = {"Accept-Language", "User-Agent"};
|
||||
String[] existingValues = {"Accept-Encoding, Accept-Language"};
|
||||
String[] expected = {"Accept-Encoding, Accept-Language", "User-Agent"};
|
||||
@@ -790,7 +781,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleReturnValueVaryHeader() throws Exception {
|
||||
void handleReturnValueVaryHeader() throws Exception {
|
||||
String[] entityValues = {"Accept-Language", "User-Agent"};
|
||||
String[] existingValues = {"Accept-Encoding, Accept-Language"};
|
||||
String[] expected = {"Accept-Encoding, Accept-Language", "User-Agent"};
|
||||
|
||||
@@ -363,7 +363,7 @@ public class RequestMappingHandlerMappingTests {
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Principal getUser() {
|
||||
return mock(Principal.class);
|
||||
return mock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -70,14 +70,15 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.reset;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link RequestPartMethodArgumentResolver} and mock {@link HttpMessageConverter}.
|
||||
* Tests for {@link RequestPartMethodArgumentResolver} with a mock {@link HttpMessageConverter}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class RequestPartMethodArgumentResolverTests {
|
||||
class RequestPartMethodArgumentResolverTests {
|
||||
|
||||
private HttpMessageConverter<SimpleBean> messageConverter;
|
||||
@SuppressWarnings("unchecked")
|
||||
private HttpMessageConverter<SimpleBean> messageConverter = mock();
|
||||
|
||||
private RequestPartMethodArgumentResolver resolver;
|
||||
|
||||
@@ -112,8 +113,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
|
||||
@BeforeEach
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setup() throws Exception {
|
||||
messageConverter = mock(HttpMessageConverter.class);
|
||||
void setup() throws Exception {
|
||||
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
|
||||
resolver = new RequestPartMethodArgumentResolver(Collections.singletonList(messageConverter));
|
||||
@@ -164,7 +164,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void supportsParameter() {
|
||||
void supportsParameter() {
|
||||
assertThat(resolver.supportsParameter(paramRequestPart)).isTrue();
|
||||
assertThat(resolver.supportsParameter(paramNamedRequestPart)).isTrue();
|
||||
assertThat(resolver.supportsParameter(paramValidRequestPart)).isTrue();
|
||||
@@ -185,20 +185,20 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMultipartFile() throws Exception {
|
||||
void resolveMultipartFile() throws Exception {
|
||||
Object actual = resolver.resolveArgument(paramMultipartFile, null, webRequest, null);
|
||||
assertThat(actual).isSameAs(multipartFile1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMultipartFileList() throws Exception {
|
||||
void resolveMultipartFileList() throws Exception {
|
||||
Object actual = resolver.resolveArgument(paramMultipartFileList, null, webRequest, null);
|
||||
assertThat(actual instanceof List).isTrue();
|
||||
assertThat(actual).isEqualTo(Arrays.asList(multipartFile1, multipartFile2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMultipartFileArray() throws Exception {
|
||||
void resolveMultipartFileArray() throws Exception {
|
||||
Object actual = resolver.resolveArgument(paramMultipartFileArray, null, webRequest, null);
|
||||
assertThat(actual).isNotNull();
|
||||
assertThat(actual instanceof MultipartFile[]).isTrue();
|
||||
@@ -209,7 +209,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMultipartFileNotAnnotArgument() throws Exception {
|
||||
void resolveMultipartFileNotAnnotArgument() throws Exception {
|
||||
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
|
||||
MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes());
|
||||
request.addFile(expected);
|
||||
@@ -223,7 +223,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePartArgument() throws Exception {
|
||||
void resolvePartArgument() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
@@ -238,7 +238,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePartListArgument() throws Exception {
|
||||
void resolvePartListArgument() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
@@ -255,7 +255,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePartArrayArgument() throws Exception {
|
||||
void resolvePartArrayArgument() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
@@ -275,22 +275,22 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveRequestPart() throws Exception {
|
||||
void resolveRequestPart() throws Exception {
|
||||
testResolveArgument(new SimpleBean("foo"), paramRequestPart);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveNamedRequestPart() throws Exception {
|
||||
void resolveNamedRequestPart() throws Exception {
|
||||
testResolveArgument(new SimpleBean("foo"), paramNamedRequestPart);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveNamedRequestPartNotPresent() throws Exception {
|
||||
void resolveNamedRequestPartNotPresent() throws Exception {
|
||||
testResolveArgument(null, paramNamedRequestPart);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveRequestPartNotValid() throws Exception {
|
||||
void resolveRequestPartNotValid() throws Exception {
|
||||
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
|
||||
testResolveArgument(new SimpleBean(null), paramValidRequestPart))
|
||||
.satisfies(ex -> {
|
||||
@@ -302,24 +302,24 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveRequestPartValid() throws Exception {
|
||||
void resolveRequestPartValid() throws Exception {
|
||||
testResolveArgument(new SimpleBean("foo"), paramValidRequestPart);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveRequestPartRequired() throws Exception {
|
||||
void resolveRequestPartRequired() throws Exception {
|
||||
assertThatExceptionOfType(MissingServletRequestPartException.class).isThrownBy(() ->
|
||||
testResolveArgument(null, paramValidRequestPart))
|
||||
.satisfies(ex -> assertThat(ex.getRequestPartName()).isEqualTo("requestPart"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveRequestPartNotRequired() throws Exception {
|
||||
void resolveRequestPartNotRequired() throws Exception {
|
||||
testResolveArgument(new SimpleBean("foo"), paramValidRequestPart);
|
||||
}
|
||||
|
||||
@Test // gh-26501
|
||||
public void resolveRequestPartWithoutContentType() throws Exception {
|
||||
void resolveRequestPartWithoutContentType() throws Exception {
|
||||
MockMultipartHttpServletRequest servletRequest = new MockMultipartHttpServletRequest();
|
||||
servletRequest.addPart(new MockPart("requestPartString", "part value".getBytes(StandardCharsets.UTF_8)));
|
||||
ServletWebRequest webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
|
||||
@@ -335,21 +335,21 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isMultipartRequest() throws Exception {
|
||||
void isMultipartRequest() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
assertThatExceptionOfType(MultipartException.class).isThrownBy(() ->
|
||||
resolver.resolveArgument(paramMultipartFile, new ModelAndViewContainer(), new ServletWebRequest(request), null));
|
||||
}
|
||||
|
||||
@Test // SPR-9079
|
||||
public void isMultipartRequestPut() throws Exception {
|
||||
void isMultipartRequestPut() throws Exception {
|
||||
this.multipartRequest.setMethod("PUT");
|
||||
Object actualValue = resolver.resolveArgument(paramMultipartFile, null, webRequest, null);
|
||||
assertThat(actualValue).isSameAs(multipartFile1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalMultipartFileArgument() throws Exception {
|
||||
void resolveOptionalMultipartFileArgument() throws Exception {
|
||||
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
|
||||
MultipartFile expected = new MockMultipartFile("optionalMultipartFile", "Hello World".getBytes());
|
||||
request.addFile(expected);
|
||||
@@ -367,7 +367,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalMultipartFileArgumentNotPresent() throws Exception {
|
||||
void resolveOptionalMultipartFileArgumentNotPresent() throws Exception {
|
||||
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
@@ -379,7 +379,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalMultipartFileArgumentWithoutMultipartRequest() throws Exception {
|
||||
void resolveOptionalMultipartFileArgumentWithoutMultipartRequest() throws Exception {
|
||||
webRequest = new ServletWebRequest(new MockHttpServletRequest());
|
||||
|
||||
Object actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
|
||||
@@ -390,7 +390,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalMultipartFileList() throws Exception {
|
||||
void resolveOptionalMultipartFileList() throws Exception {
|
||||
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
|
||||
MultipartFile expected = new MockMultipartFile("requestPart", "Hello World".getBytes());
|
||||
request.addFile(expected);
|
||||
@@ -408,7 +408,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalMultipartFileListNotPresent() throws Exception {
|
||||
void resolveOptionalMultipartFileListNotPresent() throws Exception {
|
||||
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
@@ -420,7 +420,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalMultipartFileListWithoutMultipartRequest() throws Exception {
|
||||
void resolveOptionalMultipartFileListWithoutMultipartRequest() throws Exception {
|
||||
webRequest = new ServletWebRequest(new MockHttpServletRequest());
|
||||
|
||||
Object actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
|
||||
@@ -431,7 +431,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalPartArgument() throws Exception {
|
||||
void resolveOptionalPartArgument() throws Exception {
|
||||
MockPart expected = new MockPart("optionalPart", "Hello World".getBytes());
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
@@ -451,7 +451,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalPartArgumentNotPresent() throws Exception {
|
||||
void resolveOptionalPartArgumentNotPresent() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
@@ -465,7 +465,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalPartArgumentWithoutMultipartRequest() throws Exception {
|
||||
void resolveOptionalPartArgumentWithoutMultipartRequest() throws Exception {
|
||||
webRequest = new ServletWebRequest(new MockHttpServletRequest());
|
||||
|
||||
Object actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null);
|
||||
@@ -476,7 +476,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalPartList() throws Exception {
|
||||
void resolveOptionalPartList() throws Exception {
|
||||
MockPart expected = new MockPart("requestPart", "Hello World".getBytes());
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
@@ -496,7 +496,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalPartListNotPresent() throws Exception {
|
||||
void resolveOptionalPartListNotPresent() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
@@ -510,7 +510,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalPartListWithoutMultipartRequest() throws Exception {
|
||||
void resolveOptionalPartListWithoutMultipartRequest() throws Exception {
|
||||
webRequest = new ServletWebRequest(new MockHttpServletRequest());
|
||||
|
||||
Object actualValue = resolver.resolveArgument(optionalPartList, null, webRequest, null);
|
||||
@@ -521,7 +521,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalRequestPart() throws Exception {
|
||||
void resolveOptionalRequestPart() throws Exception {
|
||||
SimpleBean simpleBean = new SimpleBean("foo");
|
||||
given(messageConverter.canRead(SimpleBean.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
given(messageConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean);
|
||||
@@ -539,7 +539,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalRequestPartNotPresent() throws Exception {
|
||||
void resolveOptionalRequestPartNotPresent() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
@@ -553,7 +553,7 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalRequestPartWithoutMultipartRequest() throws Exception {
|
||||
void resolveOptionalRequestPartWithoutMultipartRequest() throws Exception {
|
||||
webRequest = new ServletWebRequest(new MockHttpServletRequest());
|
||||
|
||||
Object actualValue = resolver.resolveArgument(optionalRequestPart, null, webRequest, null);
|
||||
|
||||
@@ -21,9 +21,7 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
@@ -46,6 +44,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.same;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RequestResponseBodyAdviceChain}.
|
||||
@@ -53,38 +52,26 @@ import static org.mockito.BDDMockito.given;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.2
|
||||
*/
|
||||
public class RequestResponseBodyAdviceChainTests {
|
||||
class RequestResponseBodyAdviceChainTests {
|
||||
|
||||
private String body;
|
||||
private String body = "body";
|
||||
|
||||
private MediaType contentType;
|
||||
private MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
|
||||
private Class<? extends HttpMessageConverter<?>> converterType;
|
||||
private Class<? extends HttpMessageConverter<?>> converterType = StringHttpMessageConverter.class;
|
||||
|
||||
private MethodParameter paramType;
|
||||
private MethodParameter returnType;
|
||||
private MethodParameter paramType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), 0);
|
||||
private MethodParameter returnType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), -1);
|
||||
|
||||
private ServerHttpRequest request;
|
||||
private ServerHttpResponse response;
|
||||
private ServerHttpRequest request = new ServletServerHttpRequest(new MockHttpServletRequest());
|
||||
private ServerHttpResponse response = new ServletServerHttpResponse(new MockHttpServletResponse());
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
this.body = "body";
|
||||
this.contentType = MediaType.TEXT_PLAIN;
|
||||
this.converterType = StringHttpMessageConverter.class;
|
||||
this.paramType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), 0);
|
||||
this.returnType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), -1);
|
||||
this.request = new ServletServerHttpRequest(new MockHttpServletRequest());
|
||||
this.response = new ServletServerHttpResponse(new MockHttpServletResponse());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void requestBodyAdvice() throws IOException {
|
||||
RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class);
|
||||
ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
void requestBodyAdvice() throws IOException {
|
||||
RequestBodyAdvice requestAdvice = mock();
|
||||
ResponseBodyAdvice<String> responseAdvice = mock();
|
||||
List<Object> advice = Arrays.asList(requestAdvice, responseAdvice);
|
||||
RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice);
|
||||
|
||||
@@ -103,11 +90,11 @@ public class RequestResponseBodyAdviceChainTests {
|
||||
String.class, this.converterType)).isEqualTo(modified);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void responseBodyAdvice() {
|
||||
RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class);
|
||||
ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
void responseBodyAdvice() {
|
||||
RequestBodyAdvice requestAdvice = mock();
|
||||
ResponseBodyAdvice<String> responseAdvice = mock();
|
||||
List<Object> advice = Arrays.asList(requestAdvice, responseAdvice);
|
||||
RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice);
|
||||
|
||||
@@ -123,7 +110,7 @@ public class RequestResponseBodyAdviceChainTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void controllerAdvice() {
|
||||
void controllerAdvice() {
|
||||
Object adviceBean = new ControllerAdviceBean(new MyControllerAdvice());
|
||||
RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Collections.singletonList(adviceBean));
|
||||
|
||||
@@ -134,7 +121,7 @@ public class RequestResponseBodyAdviceChainTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void controllerAdviceNotApplicable() {
|
||||
void controllerAdviceNotApplicable() {
|
||||
Object adviceBean = new ControllerAdviceBean(new TargetedControllerAdvice());
|
||||
RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Collections.singletonList(adviceBean));
|
||||
|
||||
|
||||
@@ -78,23 +78,26 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class RequestResponseBodyMethodProcessorMockTests {
|
||||
class RequestResponseBodyMethodProcessorMockTests {
|
||||
|
||||
private HttpMessageConverter<String> stringMessageConverter;
|
||||
@SuppressWarnings("unchecked")
|
||||
private HttpMessageConverter<String> stringMessageConverter = mock();
|
||||
|
||||
private HttpMessageConverter<Resource> resourceMessageConverter;
|
||||
@SuppressWarnings("unchecked")
|
||||
private HttpMessageConverter<Resource> resourceMessageConverter = mock();
|
||||
|
||||
private HttpMessageConverter<Object> resourceRegionMessageConverter;
|
||||
@SuppressWarnings("unchecked")
|
||||
private HttpMessageConverter<Object> resourceRegionMessageConverter = mock();
|
||||
|
||||
private RequestResponseBodyMethodProcessor processor;
|
||||
|
||||
private ModelAndViewContainer mavContainer;
|
||||
private ModelAndViewContainer mavContainer = new ModelAndViewContainer();
|
||||
|
||||
private MockHttpServletRequest servletRequest;
|
||||
private MockHttpServletRequest servletRequest = new MockHttpServletRequest();
|
||||
|
||||
private MockHttpServletResponse servletResponse;
|
||||
private MockHttpServletResponse servletResponse = new MockHttpServletResponse();
|
||||
|
||||
private NativeWebRequest webRequest;
|
||||
private NativeWebRequest webRequest = new ServletWebRequest(servletRequest, servletResponse);
|
||||
|
||||
private MethodParameter paramRequestBodyString;
|
||||
private MethodParameter paramInt;
|
||||
@@ -109,25 +112,18 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
|
||||
@BeforeEach
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setup() throws Exception {
|
||||
stringMessageConverter = mock(HttpMessageConverter.class);
|
||||
void setup() throws Exception {
|
||||
given(stringMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
given(stringMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
resourceMessageConverter = mock(HttpMessageConverter.class);
|
||||
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
|
||||
given(resourceMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
|
||||
resourceRegionMessageConverter = mock(HttpMessageConverter.class);
|
||||
given(resourceRegionMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
|
||||
given(resourceRegionMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
|
||||
|
||||
processor = new RequestResponseBodyMethodProcessor(
|
||||
Arrays.asList(stringMessageConverter, resourceMessageConverter, resourceRegionMessageConverter));
|
||||
|
||||
mavContainer = new ModelAndViewContainer();
|
||||
servletRequest = new MockHttpServletRequest();
|
||||
servletRequest.setMethod("POST");
|
||||
servletResponse = new MockHttpServletResponse();
|
||||
webRequest = new ServletWebRequest(servletRequest, servletResponse);
|
||||
|
||||
Method methodHandle1 = getClass().getMethod("handle1", String.class, Integer.TYPE);
|
||||
paramRequestBodyString = new MethodParameter(methodHandle1, 0);
|
||||
@@ -142,19 +138,19 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsParameter() {
|
||||
void supportsParameter() {
|
||||
assertThat(processor.supportsParameter(paramRequestBodyString)).as("RequestBody parameter not supported").isTrue();
|
||||
assertThat(processor.supportsParameter(paramInt)).as("non-RequestBody parameter supported").isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsReturnType() {
|
||||
void supportsReturnType() {
|
||||
assertThat(processor.supportsReturnType(returnTypeString)).as("ResponseBody return type not supported").isTrue();
|
||||
assertThat(processor.supportsReturnType(returnTypeInt)).as("non-ResponseBody return type supported").isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgument() throws Exception {
|
||||
void resolveArgument() throws Exception {
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||
|
||||
@@ -172,7 +168,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentNotValid() throws Exception {
|
||||
void resolveArgumentNotValid() throws Exception {
|
||||
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
|
||||
testResolveArgumentWithValidation(new SimpleBean(null)))
|
||||
.satisfies(ex -> {
|
||||
@@ -184,7 +180,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentValid() throws Exception {
|
||||
void resolveArgumentValid() throws Exception {
|
||||
testResolveArgumentWithValidation(new SimpleBean("name"));
|
||||
}
|
||||
|
||||
@@ -194,7 +190,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class);
|
||||
HttpMessageConverter<SimpleBean> beanConverter = mock();
|
||||
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);
|
||||
@@ -204,7 +200,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentCannotRead() throws Exception {
|
||||
void resolveArgumentCannotRead() throws Exception {
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8));
|
||||
@@ -216,7 +212,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentNoContentType() throws Exception {
|
||||
void resolveArgumentNoContentType() throws Exception {
|
||||
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8));
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false);
|
||||
assertThatExceptionOfType(HttpMediaTypeNotSupportedException.class).isThrownBy(() ->
|
||||
@@ -224,7 +220,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentInvalidContentType() throws Exception {
|
||||
void resolveArgumentInvalidContentType() throws Exception {
|
||||
this.servletRequest.setContentType("bad");
|
||||
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8));
|
||||
assertThatExceptionOfType(HttpMediaTypeNotSupportedException.class).isThrownBy(() ->
|
||||
@@ -232,7 +228,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // SPR-9942
|
||||
public void resolveArgumentRequiredNoContent() throws Exception {
|
||||
void resolveArgumentRequiredNoContent() throws Exception {
|
||||
servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE);
|
||||
servletRequest.setContent(new byte[0]);
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
@@ -243,7 +239,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentNotGetRequests() throws Exception {
|
||||
void resolveArgumentNotGetRequests() throws Exception {
|
||||
servletRequest.setMethod("GET");
|
||||
servletRequest.setContent(new byte[0]);
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false);
|
||||
@@ -252,7 +248,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentNotRequiredWithContent() throws Exception {
|
||||
void resolveArgumentNotRequiredWithContent() throws Exception {
|
||||
servletRequest.setContentType("text/plain");
|
||||
servletRequest.setContent("body".getBytes());
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
@@ -262,7 +258,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentNotRequiredNoContent() throws Exception {
|
||||
void resolveArgumentNotRequiredNoContent() throws Exception {
|
||||
servletRequest.setContentType("text/plain");
|
||||
servletRequest.setContent(new byte[0]);
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
@@ -271,7 +267,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // SPR-13417
|
||||
public void resolveArgumentNotRequiredNoContentNoContentType() throws Exception {
|
||||
void resolveArgumentNotRequiredNoContentNoContentType() throws Exception {
|
||||
servletRequest.setContent(new byte[0]);
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false);
|
||||
@@ -280,7 +276,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentOptionalWithContent() throws Exception {
|
||||
void resolveArgumentOptionalWithContent() throws Exception {
|
||||
servletRequest.setContentType("text/plain");
|
||||
servletRequest.setContent("body".getBytes());
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
@@ -290,7 +286,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentOptionalNoContent() throws Exception {
|
||||
void resolveArgumentOptionalNoContent() throws Exception {
|
||||
servletRequest.setContentType("text/plain");
|
||||
servletRequest.setContent(new byte[0]);
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
@@ -298,7 +294,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentOptionalNoContentNoContentType() throws Exception {
|
||||
void resolveArgumentOptionalNoContentNoContentType() throws Exception {
|
||||
servletRequest.setContent(new byte[0]);
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false);
|
||||
@@ -307,7 +303,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleReturnValue() throws Exception {
|
||||
void handleReturnValue() throws Exception {
|
||||
MediaType accepted = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Accept", accepted.toString());
|
||||
|
||||
@@ -323,7 +319,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleReturnValueProduces() throws Exception {
|
||||
void handleReturnValueProduces() throws Exception {
|
||||
String body = "Foo";
|
||||
|
||||
servletRequest.addHeader("Accept", "text/*");
|
||||
@@ -340,7 +336,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void handleReturnValueNotAcceptable() throws Exception {
|
||||
void handleReturnValueNotAcceptable() throws Exception {
|
||||
MediaType accepted = MediaType.APPLICATION_ATOM_XML;
|
||||
servletRequest.addHeader("Accept", accepted.toString());
|
||||
|
||||
@@ -353,7 +349,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleReturnValueNotAcceptableProduces() throws Exception {
|
||||
void handleReturnValueNotAcceptableProduces() throws Exception {
|
||||
MediaType accepted = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Accept", accepted.toString());
|
||||
|
||||
@@ -366,7 +362,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleReturnTypeResource() throws Exception {
|
||||
void handleReturnTypeResource() throws Exception {
|
||||
Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true);
|
||||
@@ -382,7 +378,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test // SPR-9841
|
||||
public void handleReturnValueMediaTypeSuffix() throws Exception {
|
||||
void handleReturnValueMediaTypeSuffix() throws Exception {
|
||||
String body = "Foo";
|
||||
MediaType accepted = MediaType.APPLICATION_XHTML_XML;
|
||||
List<MediaType> supported = Collections.singletonList(MediaType.valueOf("application/*+xml"));
|
||||
@@ -400,7 +396,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleReturnTypeResourceByteRange() throws Exception {
|
||||
void handleReturnTypeResourceByteRange() throws Exception {
|
||||
Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8));
|
||||
servletRequest.addHeader("Range", "bytes=0-5");
|
||||
|
||||
@@ -416,7 +412,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleReturnTypeResourceIllegalByteRange() throws Exception {
|
||||
void handleReturnTypeResourceIllegalByteRange() throws Exception {
|
||||
Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8));
|
||||
servletRequest.addHeader("Range", "illegal");
|
||||
|
||||
|
||||
@@ -35,7 +35,6 @@ import reactor.core.scheduler.Schedulers;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
@@ -57,33 +56,26 @@ import static org.springframework.core.ResolvableType.forClassWithGenerics;
|
||||
import static org.springframework.web.testfixture.method.ResolvableMethod.on;
|
||||
|
||||
/**
|
||||
* Unit tests for ResponseBodyEmitterReturnValueHandler.
|
||||
* Unit tests for {@link ResponseBodyEmitterReturnValueHandler}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
|
||||
private ResponseBodyEmitterReturnValueHandler handler;
|
||||
private ResponseBodyEmitterReturnValueHandler handler =
|
||||
new ResponseBodyEmitterReturnValueHandler(List.of(new MappingJackson2HttpMessageConverter()));
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
private MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
private MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
private NativeWebRequest webRequest;
|
||||
private NativeWebRequest webRequest = new ServletWebRequest(this.request, this.response);
|
||||
|
||||
private final ModelAndViewContainer mavContainer = new ModelAndViewContainer();
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws Exception {
|
||||
|
||||
List<HttpMessageConverter<?>> converters =
|
||||
Collections.singletonList(new MappingJackson2HttpMessageConverter());
|
||||
|
||||
this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
|
||||
this.request = new MockHttpServletRequest();
|
||||
this.response = new MockHttpServletResponse();
|
||||
this.webRequest = new ServletWebRequest(this.request, this.response);
|
||||
|
||||
void setup() throws Exception {
|
||||
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
|
||||
WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
|
||||
this.request.setAsyncSupported(true);
|
||||
@@ -91,8 +83,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void supportsReturnTypes() {
|
||||
|
||||
void supportsReturnTypes() {
|
||||
assertThat(this.handler.supportsReturnType(
|
||||
on(TestController.class).resolveReturnType(ResponseBodyEmitter.class))).isTrue();
|
||||
|
||||
@@ -111,8 +102,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotSupportReturnTypes() {
|
||||
|
||||
void doesNotSupportReturnTypes() {
|
||||
assertThat(this.handler.supportsReturnType(
|
||||
on(TestController.class).resolveReturnType(ResponseEntity.class, String.class))).isFalse();
|
||||
|
||||
@@ -125,7 +115,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseBodyEmitter() throws Exception {
|
||||
void responseBodyEmitter() throws Exception {
|
||||
MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
|
||||
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
|
||||
this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);
|
||||
@@ -161,14 +151,13 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseBodyEmitterWithTimeoutValue() throws Exception {
|
||||
|
||||
AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
|
||||
void responseBodyEmitterWithTimeoutValue() throws Exception {
|
||||
AsyncWebRequest asyncWebRequest = mock();
|
||||
WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);
|
||||
|
||||
ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
|
||||
emitter.onTimeout(mock(Runnable.class));
|
||||
emitter.onCompletion(mock(Runnable.class));
|
||||
emitter.onTimeout(mock());
|
||||
emitter.onCompletion(mock());
|
||||
|
||||
MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
|
||||
this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);
|
||||
@@ -179,16 +168,15 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
verify(asyncWebRequest).startAsync();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void responseBodyEmitterWithErrorValue() throws Exception {
|
||||
|
||||
AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
void responseBodyEmitterWithErrorValue() throws Exception {
|
||||
AsyncWebRequest asyncWebRequest = mock();
|
||||
WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);
|
||||
|
||||
ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
|
||||
emitter.onError(mock(Consumer.class));
|
||||
emitter.onCompletion(mock(Runnable.class));
|
||||
emitter.onError(mock());
|
||||
emitter.onCompletion(mock());
|
||||
|
||||
MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
|
||||
this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);
|
||||
@@ -199,7 +187,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sseEmitter() throws Exception {
|
||||
void sseEmitter() throws Exception {
|
||||
MethodParameter type = on(TestController.class).resolveReturnType(SseEmitter.class);
|
||||
SseEmitter emitter = new SseEmitter();
|
||||
this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);
|
||||
@@ -231,8 +219,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseBodyFlux() throws Exception {
|
||||
|
||||
void responseBodyFlux() throws Exception {
|
||||
this.request.addHeader("Accept", "text/event-stream");
|
||||
|
||||
MethodParameter type = on(TestController.class).resolveReturnType(Flux.class, String.class);
|
||||
@@ -245,10 +232,9 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
assertThat(this.response.getContentAsString()).isEqualTo("data:foo\n\ndata:bar\n\ndata:baz\n\n");
|
||||
}
|
||||
|
||||
@SuppressWarnings({"try","unused"})
|
||||
@Test
|
||||
public void responseBodyFluxWithThreadLocal() throws Exception {
|
||||
|
||||
@SuppressWarnings({"try","unused"})
|
||||
void responseBodyFluxWithThreadLocal() throws Exception {
|
||||
this.request.addHeader("Accept", "text/event-stream");
|
||||
|
||||
ThreadLocal<String> threadLocal = new ThreadLocal<>();
|
||||
@@ -286,8 +272,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test // gh-21972
|
||||
public void responseBodyFluxWithError() throws Exception {
|
||||
|
||||
void responseBodyFluxWithError() throws Exception {
|
||||
this.request.addHeader("Accept", "text/event-stream");
|
||||
|
||||
IllegalStateException ex = new IllegalStateException("wah wah");
|
||||
@@ -302,7 +287,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseEntitySse() throws Exception {
|
||||
void responseEntitySse() throws Exception {
|
||||
MethodParameter type = on(TestController.class).resolveReturnType(ResponseEntity.class, SseEmitter.class);
|
||||
SseEmitter emitter = new SseEmitter();
|
||||
ResponseEntity<SseEmitter> entity = ResponseEntity.ok().header("foo", "bar").body(emitter);
|
||||
@@ -316,7 +301,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseEntitySseNoContent() throws Exception {
|
||||
void responseEntitySseNoContent() throws Exception {
|
||||
MethodParameter type = on(TestController.class).resolveReturnType(ResponseEntity.class, SseEmitter.class);
|
||||
ResponseEntity<?> entity = ResponseEntity.noContent().header("foo", "bar").build();
|
||||
this.handler.handleReturnValue(entity, type, this.mavContainer, this.webRequest);
|
||||
@@ -327,8 +312,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseEntityFlux() throws Exception {
|
||||
|
||||
void responseEntityFlux() throws Exception {
|
||||
ResponseEntity<Flux<String>> entity = ResponseEntity.ok().body(Flux.just("foo", "bar", "baz"));
|
||||
ResolvableType bodyType = forClassWithGenerics(Flux.class, String.class);
|
||||
MethodParameter type = on(TestController.class).resolveReturnType(ResponseEntity.class, bodyType);
|
||||
@@ -342,8 +326,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test // SPR-17076
|
||||
public void responseEntityFluxWithCustomHeader() throws Exception {
|
||||
|
||||
void responseEntityFluxWithCustomHeader() throws Exception {
|
||||
Sinks.Many<SimpleBean> sink = Sinks.many().unicast().onBackpressureBuffer();
|
||||
ResponseEntity<Flux<SimpleBean>> entity = ResponseEntity.ok().header("x-foo", "bar").body(sink.asFlux());
|
||||
ResolvableType bodyType = forClassWithGenerics(Flux.class, SimpleBean.class);
|
||||
|
||||
@@ -155,7 +155,7 @@ public class ResponseBodyEmitterTests {
|
||||
|
||||
@Test
|
||||
public void onTimeoutBeforeHandlerInitialized() throws Exception {
|
||||
Runnable runnable = mock(Runnable.class);
|
||||
Runnable runnable = mock();
|
||||
this.emitter.onTimeout(runnable);
|
||||
this.emitter.initialize(this.handler);
|
||||
|
||||
@@ -176,7 +176,7 @@ public class ResponseBodyEmitterTests {
|
||||
verify(this.handler).onTimeout(captor.capture());
|
||||
verify(this.handler).onCompletion(any());
|
||||
|
||||
Runnable runnable = mock(Runnable.class);
|
||||
Runnable runnable = mock();
|
||||
this.emitter.onTimeout(runnable);
|
||||
|
||||
assertThat(captor.getValue()).isNotNull();
|
||||
@@ -186,7 +186,7 @@ public class ResponseBodyEmitterTests {
|
||||
|
||||
@Test
|
||||
public void onCompletionBeforeHandlerInitialized() throws Exception {
|
||||
Runnable runnable = mock(Runnable.class);
|
||||
Runnable runnable = mock();
|
||||
this.emitter.onCompletion(runnable);
|
||||
this.emitter.initialize(this.handler);
|
||||
|
||||
@@ -207,7 +207,7 @@ public class ResponseBodyEmitterTests {
|
||||
verify(this.handler).onTimeout(any());
|
||||
verify(this.handler).onCompletion(captor.capture());
|
||||
|
||||
Runnable runnable = mock(Runnable.class);
|
||||
Runnable runnable = mock();
|
||||
this.emitter.onCompletion(runnable);
|
||||
|
||||
assertThat(captor.getValue()).isNotNull();
|
||||
|
||||
@@ -33,7 +33,6 @@ import jakarta.servlet.http.HttpSession;
|
||||
import jakarta.servlet.http.PushBuilder;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -48,6 +47,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpSession;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -242,7 +242,7 @@ public class ServletRequestMethodArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void pushBuilder() throws Exception {
|
||||
final PushBuilder pushBuilder = Mockito.mock(PushBuilder.class);
|
||||
final PushBuilder pushBuilder = mock();
|
||||
servletRequest = new MockHttpServletRequest("GET", "") {
|
||||
@Override
|
||||
public PushBuilder newPushBuilder() {
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
@@ -33,6 +32,7 @@ import org.springframework.web.servlet.resource.GzipSupport.GzippedFiles;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Unit tests for
|
||||
@@ -76,7 +76,7 @@ public class CachingResourceResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveResourceInternalFromCache() {
|
||||
Resource expected = Mockito.mock(Resource.class);
|
||||
Resource expected = mock();
|
||||
this.cache.put(resourceKey("bar.css"), expected);
|
||||
Resource actual = this.chain.resolveResource(null, "bar.css", this.locations);
|
||||
|
||||
@@ -157,8 +157,8 @@ public class CachingResourceResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveResourceMatchingEncoding() {
|
||||
Resource resource = Mockito.mock(Resource.class);
|
||||
Resource gzipped = Mockito.mock(Resource.class);
|
||||
Resource resource = mock();
|
||||
Resource gzipped = mock();
|
||||
this.cache.put(resourceKey("bar.css"), resource);
|
||||
this.cache.put(resourceKey("bar.css+encoding=gzip"), gzipped);
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.Map;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -33,6 +32,9 @@ import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CssLinkResourceTransformer}.
|
||||
@@ -111,7 +113,7 @@ class CssLinkResourceTransformerTests {
|
||||
this.request = new MockHttpServletRequest("GET", "/static/external.css");
|
||||
|
||||
List<ResourceTransformer> transformers = Collections.singletonList(new CssLinkResourceTransformer());
|
||||
ResourceResolverChain mockChain = Mockito.mock(DefaultResourceResolverChain.class);
|
||||
ResourceResolverChain mockChain = mock();
|
||||
ResourceTransformerChain chain = new DefaultResourceTransformerChain(mockChain, transformers);
|
||||
|
||||
Resource resource = getResource("external.css");
|
||||
@@ -125,9 +127,9 @@ class CssLinkResourceTransformerTests {
|
||||
assertThat(result).isEqualToNormalizingNewlines(expected);
|
||||
|
||||
List<Resource> locations = List.of(resource);
|
||||
Mockito.verify(mockChain, Mockito.never()).resolveUrlPath("https://example.org/fonts/css", locations);
|
||||
Mockito.verify(mockChain, Mockito.never()).resolveUrlPath("file:///home/spring/image.png", locations);
|
||||
Mockito.verify(mockChain, Mockito.never()).resolveUrlPath("//example.org/style.css", locations);
|
||||
verify(mockChain, never()).resolveUrlPath("https://example.org/fonts/css", locations);
|
||||
verify(mockChain, never()).resolveUrlPath("file:///home/spring/image.png", locations);
|
||||
verify(mockChain, never()).resolveUrlPath("//example.org/style.css", locations);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -336,10 +336,10 @@ class ResourceHttpRequestHandlerTests {
|
||||
void testInvalidPath() throws Exception {
|
||||
// Use mock ResourceResolver: i.e. we're only testing upfront validations...
|
||||
|
||||
Resource resource = mock(Resource.class);
|
||||
Resource resource = mock();
|
||||
given(resource.getFilename()).willThrow(new AssertionError("Resource should not be resolved"));
|
||||
given(resource.getInputStream()).willThrow(new AssertionError("Resource should not be resolved"));
|
||||
ResourceResolver resolver = mock(ResourceResolver.class);
|
||||
ResourceResolver resolver = mock();
|
||||
given(resolver.resolveResource(any(), any(), any(), any())).willReturn(resource);
|
||||
|
||||
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
|
||||
|
||||
@@ -176,7 +176,7 @@ public class ResourceUrlProviderTests {
|
||||
@Test // SPR-16296
|
||||
void getForLookupPathShouldNotFailIfPathContainsDoubleSlashes() {
|
||||
// given
|
||||
ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
|
||||
ResourceResolver mockResourceResolver = mock();
|
||||
given(mockResourceResolver.resolveUrlPath(any(), any(), any())).willReturn("some-path");
|
||||
|
||||
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
|
||||
|
||||
@@ -16,13 +16,11 @@
|
||||
|
||||
package org.springframework.web.servlet.resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -42,30 +40,21 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Brian Clozel
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class VersionResourceResolverTests {
|
||||
class VersionResourceResolverTests {
|
||||
|
||||
private List<Resource> locations;
|
||||
private List<Resource> locations = List.of(
|
||||
new ClassPathResource("test/", getClass()),
|
||||
new ClassPathResource("testalternatepath/", getClass()));
|
||||
|
||||
private VersionResourceResolver resolver;
|
||||
private VersionResourceResolver resolver = new VersionResourceResolver();
|
||||
|
||||
private ResourceResolverChain chain;
|
||||
private ResourceResolverChain chain = mock();
|
||||
|
||||
private VersionStrategy versionStrategy;
|
||||
private VersionStrategy versionStrategy = mock();
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
this.locations = new ArrayList<>();
|
||||
this.locations.add(new ClassPathResource("test/", getClass()));
|
||||
this.locations.add(new ClassPathResource("testalternatepath/", getClass()));
|
||||
|
||||
this.resolver = new VersionResourceResolver();
|
||||
this.chain = mock(ResourceResolverChain.class);
|
||||
this.versionStrategy = mock(VersionStrategy.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceExisting() throws Exception {
|
||||
void resolveResourceExisting() throws Exception {
|
||||
String file = "bar.css";
|
||||
Resource expected = new ClassPathResource("test/" + file, getClass());
|
||||
given(this.chain.resolveResource(null, file, this.locations)).willReturn(expected);
|
||||
@@ -78,7 +67,7 @@ public class VersionResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceNoVersionStrategy() throws Exception {
|
||||
void resolveResourceNoVersionStrategy() throws Exception {
|
||||
String file = "missing.css";
|
||||
given(this.chain.resolveResource(null, file, this.locations)).willReturn(null);
|
||||
|
||||
@@ -89,7 +78,7 @@ public class VersionResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceNoVersionInPath() throws Exception {
|
||||
void resolveResourceNoVersionInPath() throws Exception {
|
||||
String file = "bar.css";
|
||||
given(this.chain.resolveResource(null, file, this.locations)).willReturn(null);
|
||||
given(this.versionStrategy.extractVersion(file)).willReturn("");
|
||||
@@ -102,7 +91,7 @@ public class VersionResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceNoResourceAfterVersionRemoved() throws Exception {
|
||||
void resolveResourceNoResourceAfterVersionRemoved() throws Exception {
|
||||
String versionFile = "bar-version.css";
|
||||
String version = "version";
|
||||
String file = "bar.css";
|
||||
@@ -118,7 +107,7 @@ public class VersionResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceVersionDoesNotMatch() throws Exception {
|
||||
void resolveResourceVersionDoesNotMatch() throws Exception {
|
||||
String versionFile = "bar-version.css";
|
||||
String version = "version";
|
||||
String file = "bar.css";
|
||||
@@ -136,7 +125,7 @@ public class VersionResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceSuccess() throws Exception {
|
||||
void resolveResourceSuccess() throws Exception {
|
||||
String versionFile = "bar-version.css";
|
||||
String version = "version";
|
||||
String file = "bar.css";
|
||||
@@ -158,10 +147,10 @@ public class VersionResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getStrategyForPath() throws Exception {
|
||||
void getStrategyForPath() throws Exception {
|
||||
Map<String, VersionStrategy> strategies = new HashMap<>();
|
||||
VersionStrategy jsStrategy = mock(VersionStrategy.class);
|
||||
VersionStrategy catchAllStrategy = mock(VersionStrategy.class);
|
||||
VersionStrategy jsStrategy = mock();
|
||||
VersionStrategy catchAllStrategy = mock();
|
||||
strategies.put("/**", catchAllStrategy);
|
||||
strategies.put("/**/*.js", jsStrategy);
|
||||
this.resolver.setStrategyMap(strategies);
|
||||
@@ -174,8 +163,7 @@ public class VersionResourceResolverTests {
|
||||
|
||||
// SPR-13883
|
||||
@Test
|
||||
public void shouldConfigureFixedPrefixAutomatically() throws Exception {
|
||||
|
||||
void shouldConfigureFixedPrefixAutomatically() throws Exception {
|
||||
this.resolver.addFixedVersionStrategy("fixedversion", "/js/**", "/css/**", "/fixedversion/css/**");
|
||||
|
||||
assertThat(this.resolver.getStrategyMap()).hasSize(4);
|
||||
@@ -186,11 +174,10 @@ public class VersionResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test // SPR-15372
|
||||
public void resolveUrlPathNoVersionStrategy() throws Exception {
|
||||
void resolveUrlPathNoVersionStrategy() throws Exception {
|
||||
given(this.chain.resolveUrlPath("/foo.css", this.locations)).willReturn("/foo.css");
|
||||
String resolved = this.resolver.resolveUrlPathInternal("/foo.css", this.locations, this.chain);
|
||||
assertThat(resolved).isEqualTo("/foo.css");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -16,11 +16,9 @@
|
||||
|
||||
package org.springframework.web.servlet.resource;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -35,34 +33,25 @@ import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Unit tests for
|
||||
* {@link org.springframework.web.servlet.resource.WebJarsResourceResolver}.
|
||||
* Unit tests for {@link WebJarsResourceResolver}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class WebJarsResourceResolverTests {
|
||||
class WebJarsResourceResolverTests {
|
||||
|
||||
private List<Resource> locations;
|
||||
private List<Resource> locations = List.of(new ClassPathResource("/META-INF/resources/webjars"));
|
||||
|
||||
private WebJarsResourceResolver resolver;
|
||||
// for this to work, an actual WebJar must be on the test classpath
|
||||
private WebJarsResourceResolver resolver = new WebJarsResourceResolver();
|
||||
|
||||
private ResourceResolverChain chain;
|
||||
private ResourceResolverChain chain = mock();
|
||||
|
||||
private HttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
// for this to work, an actual WebJar must be on the test classpath
|
||||
this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars"));
|
||||
this.resolver = new WebJarsResourceResolver();
|
||||
this.chain = mock(ResourceResolverChain.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveUrlExisting() {
|
||||
this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass()));
|
||||
void resolveUrlExisting() {
|
||||
String file = "/foo/2.3/foo.txt";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(file);
|
||||
|
||||
@@ -73,8 +62,7 @@ public class WebJarsResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveUrlExistingNotInJarFile() {
|
||||
this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass()));
|
||||
void resolveUrlExistingNotInJarFile() {
|
||||
String file = "foo/foo.txt";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null);
|
||||
|
||||
@@ -86,7 +74,7 @@ public class WebJarsResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveUrlWebJarResource() {
|
||||
void resolveUrlWebJarResource() {
|
||||
String file = "underscorejs/underscore.js";
|
||||
String expected = "underscorejs/1.8.3/underscore.js";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null);
|
||||
@@ -100,7 +88,7 @@ public class WebJarsResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveUrlWebJarResourceNotFound() {
|
||||
void resolveUrlWebJarResourceNotFound() {
|
||||
String file = "something/something.js";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null);
|
||||
|
||||
@@ -112,9 +100,8 @@ public class WebJarsResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceExisting() {
|
||||
Resource expected = mock(Resource.class);
|
||||
this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass()));
|
||||
void resolveResourceExisting() {
|
||||
Resource expected = mock();
|
||||
String file = "foo/2.3/foo.txt";
|
||||
given(this.chain.resolveResource(this.request, file, this.locations)).willReturn(expected);
|
||||
|
||||
@@ -125,7 +112,7 @@ public class WebJarsResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceNotFound() {
|
||||
void resolveResourceNotFound() {
|
||||
String file = "something/something.js";
|
||||
given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null);
|
||||
|
||||
@@ -137,11 +124,10 @@ public class WebJarsResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceWebJar() {
|
||||
Resource expected = mock(Resource.class);
|
||||
void resolveResourceWebJar() {
|
||||
Resource expected = mock();
|
||||
String file = "underscorejs/underscore.js";
|
||||
String expectedPath = "underscorejs/1.8.3/underscore.js";
|
||||
this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass()));
|
||||
given(this.chain.resolveResource(this.request, expectedPath, this.locations)).willReturn(expected);
|
||||
|
||||
Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain);
|
||||
|
||||
@@ -104,7 +104,7 @@ public abstract class AbstractHtmlElementTagTests extends AbstractTagTests {
|
||||
}
|
||||
|
||||
protected RequestDataValueProcessor getMockRequestDataValueProcessor() {
|
||||
RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
|
||||
RequestDataValueProcessor mockProcessor = mock();
|
||||
HttpServletRequest request = (HttpServletRequest) getPageContext().getRequest();
|
||||
WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request);
|
||||
wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
|
||||
|
||||
@@ -51,7 +51,7 @@ public class BaseViewTests {
|
||||
|
||||
@Test
|
||||
public void renderWithoutStaticAttributes() throws Exception {
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock();
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -77,7 +77,7 @@ public class BaseViewTests {
|
||||
*/
|
||||
@Test
|
||||
public void renderWithStaticAttributesNoCollision() throws Exception {
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock();
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -103,7 +103,7 @@ public class BaseViewTests {
|
||||
|
||||
@Test
|
||||
public void pathVarsOverrideStaticAttributes() throws Exception {
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock();
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -133,7 +133,7 @@ public class BaseViewTests {
|
||||
|
||||
@Test
|
||||
public void dynamicModelOverridesStaticAttributesIfCollision() throws Exception {
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock();
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -161,7 +161,7 @@ public class BaseViewTests {
|
||||
|
||||
@Test
|
||||
public void dynamicModelOverridesPathVariables() throws Exception {
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock();
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
TestView tv = new TestView(wac);
|
||||
|
||||
@@ -93,7 +93,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
new ParameterContentNegotiationStrategy(
|
||||
Collections.singletonMap("xls", MediaType.parseMediaType(mediaType))));
|
||||
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock();
|
||||
viewResolver.setContentNegotiationManager(manager);
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
viewResolver.afterPropertiesSet();
|
||||
@@ -121,7 +121,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
manager.addFileExtensionResolvers(extensionsResolver);
|
||||
viewResolver.setContentNegotiationManager(manager);
|
||||
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock();
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
View viewMock = mock(View.class, "application_xls");
|
||||
@@ -141,7 +141,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
public void resolveViewNameWithInvalidAcceptHeader() throws Exception {
|
||||
request.addHeader("Accept", "application");
|
||||
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock();
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
@@ -157,7 +157,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
ParameterContentNegotiationStrategy paramStrategy = new ParameterContentNegotiationStrategy(mapping);
|
||||
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(paramStrategy));
|
||||
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock();
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
@@ -206,8 +206,8 @@ public class ContentNegotiatingViewResolverTests {
|
||||
public void resolveViewNameAcceptHeader() throws Exception {
|
||||
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||
|
||||
ViewResolver viewResolverMock1 = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock1 = mock();
|
||||
ViewResolver viewResolverMock2 = mock();
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
@@ -235,8 +235,8 @@ public class ContentNegotiatingViewResolverTests {
|
||||
|
||||
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(new HeaderContentNegotiationStrategy()));
|
||||
|
||||
ViewResolver htmlViewResolver = mock(ViewResolver.class);
|
||||
ViewResolver jsonViewResolver = mock(ViewResolver.class);
|
||||
ViewResolver htmlViewResolver = mock();
|
||||
ViewResolver jsonViewResolver = mock();
|
||||
viewResolver.setViewResolvers(Arrays.asList(htmlViewResolver, jsonViewResolver));
|
||||
|
||||
View htmlView = mock(View.class, "text_html");
|
||||
@@ -260,7 +260,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
public void resolveViewNameAcceptHeaderWithSuffix() throws Exception {
|
||||
request.addHeader("Accept", "application/vnd.example-v2+xml");
|
||||
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock();
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
@@ -283,8 +283,8 @@ public class ContentNegotiatingViewResolverTests {
|
||||
public void resolveViewNameAcceptHeaderDefaultView() throws Exception {
|
||||
request.addHeader("Accept", "application/json");
|
||||
|
||||
ViewResolver viewResolverMock1 = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock1 = mock();
|
||||
ViewResolver viewResolverMock2 = mock();
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
|
||||
|
||||
View viewMock1 = mock(View.class, "application_xml");
|
||||
@@ -352,8 +352,8 @@ public class ContentNegotiatingViewResolverTests {
|
||||
new org.springframework.web.accept.PathExtensionContentNegotiationStrategy(mapping);
|
||||
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));
|
||||
|
||||
ViewResolver viewResolverMock1 = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock2 = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock1 = mock();
|
||||
ViewResolver viewResolverMock2 = mock();
|
||||
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
|
||||
|
||||
View viewMock1 = mock(View.class, "application_xml");
|
||||
@@ -385,7 +385,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
public void resolveViewContentTypeNull() throws Exception {
|
||||
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock();
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
@@ -413,7 +413,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
|
||||
UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
|
||||
urlViewResolver.setApplicationContext(webAppContext);
|
||||
ViewResolver xmlViewResolver = mock(ViewResolver.class);
|
||||
ViewResolver xmlViewResolver = mock();
|
||||
viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));
|
||||
|
||||
View xmlView = mock(View.class, "application_xml");
|
||||
@@ -436,7 +436,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
public void resolveViewNoMatch() throws Exception {
|
||||
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");
|
||||
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock();
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
@@ -458,7 +458,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
viewResolver.setUseNotAcceptableStatusCode(true);
|
||||
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");
|
||||
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock();
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
@@ -505,7 +505,7 @@ public class ContentNegotiatingViewResolverTests {
|
||||
public void resolveQualityValue() throws Exception {
|
||||
request.addHeader("Accept", "text/html;q=0.9");
|
||||
|
||||
ViewResolver viewResolverMock = mock(ViewResolver.class);
|
||||
ViewResolver viewResolverMock = mock();
|
||||
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
|
||||
|
||||
viewResolver.afterPropertiesSet();
|
||||
|
||||
@@ -53,7 +53,7 @@ public class InternalResourceViewTests {
|
||||
|
||||
private static final String url = "forward-to";
|
||||
|
||||
private final HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
private final HttpServletRequest request = mock();
|
||||
|
||||
private final MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
|
||||
@@ -161,7 +161,7 @@ public class RedirectViewTests {
|
||||
wac.setServletContext(new MockServletContext());
|
||||
wac.refresh();
|
||||
|
||||
RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
|
||||
RequestDataValueProcessor mockProcessor = mock();
|
||||
wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
|
||||
|
||||
RedirectView rv = new RedirectView();
|
||||
@@ -186,7 +186,7 @@ public class RedirectViewTests {
|
||||
contextLoader.initWebApplicationContext(servletContext);
|
||||
|
||||
try {
|
||||
RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
|
||||
RequestDataValueProcessor mockProcessor = mock();
|
||||
wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
|
||||
|
||||
RedirectView rv = new RedirectView();
|
||||
|
||||
@@ -61,7 +61,7 @@ public class FreeMarkerViewTests {
|
||||
public void noFreeMarkerConfig() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock();
|
||||
given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(new HashMap<>());
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
@@ -85,7 +85,7 @@ public class FreeMarkerViewTests {
|
||||
public void validTemplateName() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock();
|
||||
MockServletContext sc = new MockServletContext();
|
||||
|
||||
Map<String, FreeMarkerConfig> configs = new HashMap<>();
|
||||
@@ -115,7 +115,7 @@ public class FreeMarkerViewTests {
|
||||
public void keepExistingContentType() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
WebApplicationContext wac = mock();
|
||||
MockServletContext sc = new MockServletContext();
|
||||
|
||||
Map<String, FreeMarkerConfig> configs = new HashMap<>();
|
||||
|
||||
@@ -48,37 +48,35 @@ import static org.mockito.Mockito.mock;
|
||||
/**
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class GroovyMarkupViewTests {
|
||||
class GroovyMarkupViewTests {
|
||||
|
||||
private static final String RESOURCE_LOADER_PATH = "classpath*:org/springframework/web/servlet/view/groovy/";
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
private WebApplicationContext webAppContext = mock();
|
||||
|
||||
private ServletContext servletContext;
|
||||
private ServletContext servletContext = new MockServletContext();
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
void setup() {
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void missingGroovyMarkupConfig() throws Exception {
|
||||
void missingGroovyMarkupConfig() throws Exception {
|
||||
GroovyMarkupView view = new GroovyMarkupView();
|
||||
given(this.webAppContext.getBeansOfType(GroovyMarkupConfig.class, true, false))
|
||||
.willReturn(new HashMap<>());
|
||||
|
||||
view.setUrl("sampleView");
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() ->
|
||||
view.setApplicationContext(this.webAppContext))
|
||||
assertThatExceptionOfType(ApplicationContextException.class)
|
||||
.isThrownBy(() -> view.setApplicationContext(this.webAppContext))
|
||||
.withMessageContaining("GroovyMarkupConfig");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTemplateEngine() throws Exception {
|
||||
void customTemplateEngine() throws Exception {
|
||||
GroovyMarkupView view = new GroovyMarkupView();
|
||||
view.setTemplateEngine(new TestTemplateEngine());
|
||||
view.setApplicationContext(this.webAppContext);
|
||||
@@ -90,7 +88,7 @@ public class GroovyMarkupViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectTemplateEngine() throws Exception {
|
||||
void detectTemplateEngine() throws Exception {
|
||||
GroovyMarkupView view = new GroovyMarkupView();
|
||||
view.setTemplateEngine(new TestTemplateEngine());
|
||||
view.setApplicationContext(this.webAppContext);
|
||||
@@ -102,41 +100,39 @@ public class GroovyMarkupViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkResource() throws Exception {
|
||||
void checkResource() throws Exception {
|
||||
GroovyMarkupView view = createViewWithUrl("test.tpl");
|
||||
assertThat(view.checkResource(Locale.US)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkMissingResource() throws Exception {
|
||||
void checkMissingResource() throws Exception {
|
||||
GroovyMarkupView view = createViewWithUrl("missing.tpl");
|
||||
assertThat(view.checkResource(Locale.US)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkI18nResource() throws Exception {
|
||||
void checkI18nResource() throws Exception {
|
||||
GroovyMarkupView view = createViewWithUrl("i18n.tpl");
|
||||
assertThat(view.checkResource(Locale.FRENCH)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkI18nResourceMissingLocale() throws Exception {
|
||||
void checkI18nResourceMissingLocale() throws Exception {
|
||||
GroovyMarkupView view = createViewWithUrl("i18n.tpl");
|
||||
assertThat(view.checkResource(Locale.CHINESE)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderMarkupTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("name", "Spring");
|
||||
void renderMarkupTemplate() throws Exception {
|
||||
Map<String, Object> model = Map.of("name", "Spring");
|
||||
MockHttpServletResponse response = renderViewWithModel("test.tpl", model, Locale.US);
|
||||
assertThat(response.getContentAsString()).contains("<h1>Hello Spring</h1>");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderI18nTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("name", "Spring");
|
||||
void renderI18nTemplate() throws Exception {
|
||||
Map<String, Object> model = Map.of("name", "Spring");
|
||||
MockHttpServletResponse response = renderViewWithModel("i18n.tpl", model, Locale.FRANCE);
|
||||
assertThat(response.getContentAsString()).isEqualTo("<p>Bonjour Spring</p>");
|
||||
|
||||
@@ -148,14 +144,14 @@ public class GroovyMarkupViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderLayoutTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
void renderLayoutTemplate() throws Exception {
|
||||
Map<String, Object> model = Map.of();
|
||||
MockHttpServletResponse response = renderViewWithModel("content.tpl", model, Locale.US);
|
||||
assertThat(response.getContentAsString()).isEqualTo("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>");
|
||||
}
|
||||
|
||||
|
||||
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String,
|
||||
private static MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String,
|
||||
Object> model, Locale locale) throws Exception {
|
||||
|
||||
GroovyMarkupView view = createViewWithUrl(viewUrl);
|
||||
@@ -167,10 +163,8 @@ public class GroovyMarkupViewTests {
|
||||
return response;
|
||||
}
|
||||
|
||||
private GroovyMarkupView createViewWithUrl(String viewUrl) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(GroovyMarkupConfiguration.class);
|
||||
ctx.refresh();
|
||||
private static GroovyMarkupView createViewWithUrl(String viewUrl) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(GroovyMarkupConfiguration.class);
|
||||
|
||||
GroovyMarkupView view = new GroovyMarkupView();
|
||||
view.setUrl(viewUrl);
|
||||
@@ -180,9 +174,9 @@ public class GroovyMarkupViewTests {
|
||||
}
|
||||
|
||||
|
||||
public class TestTemplateEngine extends MarkupTemplateEngine {
|
||||
static class TestTemplateEngine extends MarkupTemplateEngine {
|
||||
|
||||
public TestTemplateEngine() {
|
||||
TestTemplateEngine() {
|
||||
super(new TemplateConfiguration());
|
||||
}
|
||||
|
||||
@@ -197,7 +191,7 @@ public class GroovyMarkupViewTests {
|
||||
static class GroovyMarkupConfiguration {
|
||||
|
||||
@Bean
|
||||
public GroovyMarkupConfig groovyMarkupConfigurer() {
|
||||
GroovyMarkupConfig groovyMarkupConfigurer() {
|
||||
GroovyMarkupConfigurer configurer = new GroovyMarkupConfigurer();
|
||||
configurer.setResourceLoaderPath(RESOURCE_LOADER_PATH);
|
||||
return configurer;
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import jakarta.servlet.ServletContext;
|
||||
@@ -24,6 +23,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -39,44 +39,43 @@ import static org.mockito.Mockito.mock;
|
||||
* Unit tests for ERB templates running on JRuby.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@Disabled("JRuby not compatible with JDK 9 yet")
|
||||
public class JRubyScriptTemplateTests {
|
||||
class JRubyScriptTemplateTests {
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
private WebApplicationContext webAppContext = mock();
|
||||
|
||||
private ServletContext servletContext;
|
||||
private ServletContext servletContext = new MockServletContext();
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
void setup() {
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("title", "Layout example");
|
||||
model.put("body", "This is the body");
|
||||
void renderTemplate() throws Exception {
|
||||
Map<String, Object> model = Map.of(
|
||||
"title", "Layout example",
|
||||
"body", "This is the body"
|
||||
);
|
||||
String url = "org/springframework/web/servlet/view/script/jruby/template.erb";
|
||||
MockHttpServletResponse response = render(url, model);
|
||||
assertThat(response.getContentAsString()).isEqualTo("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>");
|
||||
assertThat(response.getContentAsString())
|
||||
.isEqualTo("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>");
|
||||
}
|
||||
|
||||
private MockHttpServletResponse render(String viewUrl, Map<String, Object> model) throws Exception {
|
||||
private static MockHttpServletResponse render(String viewUrl, Map<String, Object> model) throws Exception {
|
||||
ScriptTemplateView view = createViewWithUrl(viewUrl);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
view.renderMergedOutputModel(model, request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ScriptTemplatingConfiguration.class);
|
||||
ctx.refresh();
|
||||
private static ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
|
||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(ScriptTemplatingConfiguration.class);
|
||||
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
view.setApplicationContext(ctx);
|
||||
@@ -90,7 +89,7 @@ public class JRubyScriptTemplateTests {
|
||||
static class ScriptTemplatingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer jRubyConfigurer() {
|
||||
ScriptTemplateConfigurer jRubyConfigurer() {
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setScripts("org/springframework/web/servlet/view/script/jruby/render.rb");
|
||||
configurer.setEngineName("jruby");
|
||||
|
||||
@@ -16,13 +16,13 @@
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import jakarta.servlet.ServletContext;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -38,32 +38,33 @@ import static org.mockito.Mockito.mock;
|
||||
* Unit tests for String templates running on Jython.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class JythonScriptTemplateTests {
|
||||
class JythonScriptTemplateTests {
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
private WebApplicationContext webAppContext = mock();
|
||||
|
||||
private ServletContext servletContext;
|
||||
private ServletContext servletContext = new MockServletContext();
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
void setup() {
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("title", "Layout example");
|
||||
model.put("body", "This is the body");
|
||||
void renderTemplate() throws Exception {
|
||||
Map<String, Object> model = Map.of(
|
||||
"title", "Layout example",
|
||||
"body", "This is the body"
|
||||
);
|
||||
String url = "org/springframework/web/servlet/view/script/jython/template.html";
|
||||
MockHttpServletResponse response = render(url, model);
|
||||
assertThat(response.getContentAsString()).isEqualTo("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>");
|
||||
assertThat(response.getContentAsString())
|
||||
.isEqualTo("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>");
|
||||
}
|
||||
|
||||
private MockHttpServletResponse render(String viewUrl, Map<String, Object> model) throws Exception {
|
||||
private static MockHttpServletResponse render(String viewUrl, Map<String, Object> model) throws Exception {
|
||||
ScriptTemplateView view = createViewWithUrl(viewUrl);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -71,10 +72,8 @@ public class JythonScriptTemplateTests {
|
||||
return response;
|
||||
}
|
||||
|
||||
private ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ScriptTemplatingConfiguration.class);
|
||||
ctx.refresh();
|
||||
private static ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
|
||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(ScriptTemplatingConfiguration.class);
|
||||
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
view.setApplicationContext(ctx);
|
||||
@@ -88,7 +87,7 @@ public class JythonScriptTemplateTests {
|
||||
static class ScriptTemplatingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer jythonConfigurer() {
|
||||
ScriptTemplateConfigurer jythonConfigurer() {
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setScripts("org/springframework/web/servlet/view/script/jython/render.py");
|
||||
configurer.setEngineName("jython");
|
||||
|
||||
@@ -42,24 +42,23 @@ import static org.mockito.Mockito.mock;
|
||||
* Unit tests for Kotlin script templates running on Kotlin JSR-223 support.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@DisabledOnJre(value = JRE.JAVA_19, disabledReason = "Kotlin doesn't support Java 19 yet")
|
||||
public class KotlinScriptTemplateTests {
|
||||
class KotlinScriptTemplateTests {
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
private WebApplicationContext webAppContext = mock();
|
||||
|
||||
private ServletContext servletContext;
|
||||
private ServletContext servletContext = new MockServletContext();
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
void setup() {
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplateWithFrenchLocale() throws Exception {
|
||||
void renderTemplateWithFrenchLocale() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("foo", "Foo");
|
||||
String url = "org/springframework/web/servlet/view/script/kotlin/template.kts";
|
||||
@@ -68,7 +67,7 @@ public class KotlinScriptTemplateTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplateWithEnglishLocale() throws Exception {
|
||||
void renderTemplateWithEnglishLocale() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("foo", "Foo");
|
||||
String url = "org/springframework/web/servlet/view/script/kotlin/template.kts";
|
||||
@@ -77,7 +76,7 @@ public class KotlinScriptTemplateTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplateWithoutRenderFunction() throws Exception {
|
||||
void renderTemplateWithoutRenderFunction() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("header", "<html><body>");
|
||||
model.put("hello", "Hello");
|
||||
@@ -89,7 +88,7 @@ public class KotlinScriptTemplateTests {
|
||||
}
|
||||
|
||||
|
||||
private MockHttpServletResponse render(String viewUrl, Map<String, Object> model,
|
||||
private static MockHttpServletResponse render(String viewUrl, Map<String, Object> model,
|
||||
Locale locale, Class<?> configuration) throws Exception {
|
||||
|
||||
ScriptTemplateView view = createViewWithUrl(viewUrl, configuration);
|
||||
@@ -100,10 +99,8 @@ public class KotlinScriptTemplateTests {
|
||||
return response;
|
||||
}
|
||||
|
||||
private ScriptTemplateView createViewWithUrl(String viewUrl, Class<?> configuration) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(configuration);
|
||||
ctx.refresh();
|
||||
private static ScriptTemplateView createViewWithUrl(String viewUrl, Class<?> configuration) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(configuration);
|
||||
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
view.setApplicationContext(ctx);
|
||||
@@ -117,7 +114,7 @@ public class KotlinScriptTemplateTests {
|
||||
static class ScriptTemplatingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer kotlinScriptConfigurer() {
|
||||
ScriptTemplateConfigurer kotlinScriptConfigurer() {
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setEngineName("kotlin");
|
||||
configurer.setScripts("org/springframework/web/servlet/view/script/kotlin/render.kts");
|
||||
@@ -126,7 +123,7 @@ public class KotlinScriptTemplateTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("org/springframework/web/servlet/view/script/messages");
|
||||
return messageSource;
|
||||
@@ -137,7 +134,7 @@ public class KotlinScriptTemplateTests {
|
||||
static class ScriptTemplatingConfigurationWithoutRenderFunction {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer kotlinScriptConfigurer() {
|
||||
ScriptTemplateConfigurer kotlinScriptConfigurer() {
|
||||
return new ScriptTemplateConfigurer("kotlin");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import javax.xml.namespace.QName;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import jakarta.xml.bind.JAXBElement;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.oxm.Marshaller;
|
||||
@@ -45,37 +44,30 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Arjen Poutsma
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class MarshallingViewTests {
|
||||
class MarshallingViewTests {
|
||||
|
||||
private Marshaller marshallerMock;
|
||||
private Marshaller marshallerMock = mock();
|
||||
|
||||
private MarshallingView view;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void createView() throws Exception {
|
||||
marshallerMock = mock(Marshaller.class);
|
||||
view = new MarshallingView(marshallerMock);
|
||||
}
|
||||
private MarshallingView view = new MarshallingView(marshallerMock);
|
||||
|
||||
|
||||
@Test
|
||||
public void getContentType() {
|
||||
void getContentType() {
|
||||
assertThat(view.getContentType()).as("Invalid content type").isEqualTo("application/xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isExposePathVars() {
|
||||
void isExposePathVars() {
|
||||
assertThat(view.isExposePathVariables()).as("Must not expose path variables").isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isExposePathVarsDefaultConstructor() {
|
||||
void isExposePathVarsDefaultConstructor() {
|
||||
assertThat(new MarshallingView().isExposePathVariables()).as("Must not expose path variables").isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderModelKey() throws Exception {
|
||||
void renderModelKey() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
view.setModelKey(modelKey);
|
||||
@@ -94,7 +86,7 @@ public class MarshallingViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderModelKeyWithJaxbElement() throws Exception {
|
||||
void renderModelKeyWithJaxbElement() throws Exception {
|
||||
String toBeMarshalled = "value";
|
||||
String modelKey = "key";
|
||||
view.setModelKey(modelKey);
|
||||
@@ -113,7 +105,7 @@ public class MarshallingViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderInvalidModelKey() throws Exception {
|
||||
void renderInvalidModelKey() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
view.setModelKey("invalidKey");
|
||||
@@ -130,7 +122,7 @@ public class MarshallingViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderNullModelValue() throws Exception {
|
||||
void renderNullModelValue() throws Exception {
|
||||
String modelKey = "key";
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put(modelKey, null);
|
||||
@@ -145,7 +137,7 @@ public class MarshallingViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderModelKeyUnsupported() throws Exception {
|
||||
void renderModelKeyUnsupported() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
view.setModelKey(modelKey);
|
||||
@@ -162,7 +154,7 @@ public class MarshallingViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderNoModelKey() throws Exception {
|
||||
void renderNoModelKey() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
@@ -180,7 +172,7 @@ public class MarshallingViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderNoModelKeyAndBindingResultFirst() throws Exception {
|
||||
void renderNoModelKeyAndBindingResultFirst() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
Map<String, Object> model = new LinkedHashMap<>();
|
||||
@@ -200,7 +192,7 @@ public class MarshallingViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenderUnsupportedModel() throws Exception {
|
||||
void testRenderUnsupportedModel() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user