Revised imports in tests (org.jetbrains.annotations.NotNull etc)

This commit is contained in:
Juergen Hoeller
2017-02-20 23:00:20 +01:00
parent 370e3d683a
commit 73493bc490
84 changed files with 651 additions and 866 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,18 +16,16 @@
package org.springframework.web.servlet.mvc;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import java.util.Properties;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.web.servlet.support.WebContentGenerator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* @author Rick Evans
@@ -35,17 +33,9 @@ import org.springframework.web.servlet.support.WebContentGenerator;
*/
public class WebContentInterceptorTests {
private MockHttpServletRequest request;
private MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
private MockHttpServletResponse response;
@Before
public void setUp() throws Exception {
request = new MockHttpServletRequest();
request.setMethod(WebContentGenerator.METHOD_GET);
response = new MockHttpServletResponse();
}
private MockHttpServletResponse response = new MockHttpServletResponse();
@Test
@@ -68,14 +58,14 @@ public class WebContentInterceptorTests {
interceptor.setCacheSeconds(10);
interceptor.setCacheMappings(mappings);
// request.setRequestURI("http://localhost:7070/example/adminhandle.vm");
// request.setRequestURI("http://localhost:7070/example/adminhandle.vm");
request.setRequestURI("example/adminhandle.vm");
interceptor.preHandle(request, response, null);
Iterable<String> cacheControlHeaders = response.getHeaders("Cache-Control");
assertThat(cacheControlHeaders, Matchers.emptyIterable());
// request.setRequestURI("http://localhost:7070/example/bingo.html");
// request.setRequestURI("http://localhost:7070/example/bingo.html");
request.setRequestURI("example/bingo.html");
interceptor.preHandle(request, response, null);
@@ -148,7 +138,7 @@ public class WebContentInterceptorTests {
mappings.setProperty("*/*.cache.html", "10"); // was **/*.cache.html
interceptor.setCacheMappings(mappings);
// request.setRequestURI("http://example.org/foo/page.html");
// request.setRequestURI("http://example.org/foo/page.html");
request.setRequestURI("foo/page.html");
interceptor.preHandle(request, response, null);
@@ -159,9 +149,9 @@ public class WebContentInterceptorTests {
Iterable<String> pragmaHeaders = response.getHeaders("Pragma");
assertThat(pragmaHeaders, Matchers.contains("no-cache"));
// request.setRequestURI("http://example.org/page.cache.html");
request = new MockHttpServletRequest("GET", "foo/page.cache.html");
response = new MockHttpServletResponse();
// request.setRequestURI("http://example.org/page.cache.html");
request.setRequestURI("foo/page.cache.html");
interceptor.preHandle(request, response, null);
expiresHeaders = response.getHeaders("Expires");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -84,7 +84,7 @@ public class RequestMappingInfoHandlerMappingTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
TestController testController = new TestController();
this.fooMethod = new HandlerMethod(testController, "foo");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -62,11 +62,13 @@ public abstract class AbstractRequestAttributesArgumentResolverTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
this.webRequest = new ServletWebRequest(request, response);
this.resolver = createResolver();
this.handleMethod = AbstractRequestAttributesArgumentResolverTests.class
.getDeclaredMethod(getHandleMethodName(), Foo.class, Foo.class, Foo.class, Optional.class);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -74,7 +74,7 @@ public class CrossOriginTests {
@Before
public void setUp() {
public void setup() {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
Properties props = new Properties();
props.setProperty("myOrigin", "http://example.com");
@@ -89,6 +89,7 @@ public class CrossOriginTests {
this.request.addHeader(HttpHeaders.ORIGIN, "http://domain.com/");
}
@Test
public void noAnnotationWithoutOrigin() throws Exception {
this.handlerMapping.registerHandler(new MethodLevelController());
@@ -288,6 +289,7 @@ public class CrossOriginTests {
assertNull(this.handlerMapping.getHandler(request));
}
private CorsConfiguration getCorsConfiguration(HandlerExecutionChain chain, boolean isPreFlightRequest) {
if (isPreFlightRequest) {
Object handler = chain.getHandler();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ import static org.junit.Assert.assertTrue;
/**
* Unit tests for {@link DeferredResultMethodReturnValueHandler}.
*
* @author Rossen Stoyanchev
*/
public class DeferredResultReturnValueHandlerTests {
@@ -53,7 +54,7 @@ public class DeferredResultReturnValueHandlerTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
this.handler = new DeferredResultMethodReturnValueHandler();
this.request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
@@ -190,8 +191,6 @@ public class DeferredResultReturnValueHandlerTests {
private CompletableFuture<String> handleCompletableFuture() {
return null;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -110,9 +110,9 @@ public class HttpEntityMethodProcessorMockTests {
private ServletWebRequest webRequest;
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
@SuppressWarnings("unchecked")
public void setup() throws Exception {
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
@@ -259,8 +259,8 @@ public class HttpEntityMethodProcessorMockTests {
verify(stringHttpMessageConverter).write(eq(body), eq(MediaType.TEXT_HTML), isA(HttpOutputMessage.class));
}
@SuppressWarnings("unchecked")
@Test
@SuppressWarnings("unchecked")
public void shouldHandleReturnValueWithResponseBodyAdvice() throws Exception {
servletRequest.addHeader("Accept", "text/*");
servletRequest.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.TEXT_HTML));
@@ -310,7 +310,7 @@ public class HttpEntityMethodProcessorMockTests {
processor.handleReturnValue(returnValue, returnTypeResponseEntityProduces, mavContainer, webRequest);
}
@Test // SPR-9142
@Test // SPR-9142
public void shouldFailHandlingWhenAcceptHeaderIllegal() throws Exception {
ResponseEntity<String> returnValue = new ResponseEntity<>("Body", HttpStatus.ACCEPTED);
servletRequest.addHeader("Accept", "01");
@@ -371,7 +371,7 @@ public class HttpEntityMethodProcessorMockTests {
assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, -1);
}
@Test // SPR-14559
@Test // SPR-14559
public void shouldHandleInvalidIfNoneMatchWithHttp200() throws Exception {
String etagValue = "\"deadb33f8badf00d\"";
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, "unquoted");
@@ -429,7 +429,7 @@ public class HttpEntityMethodProcessorMockTests {
assertConditionalResponse(HttpStatus.OK, null, changedEtagValue, oneMinuteAgo);
}
@Test // SPR-13496
@Test // SPR-13496
public void shouldHandleConditionalRequestIfNoneMatchWildcard() throws Exception {
String wildcardValue = "*";
String etagValue = "\"some-etag\"";
@@ -443,7 +443,7 @@ public class HttpEntityMethodProcessorMockTests {
assertConditionalResponse(HttpStatus.OK, "body", etagValue, -1);
}
@Test // SPR-13626
@Test // SPR-13626
public void shouldHandleGetIfNoneMatchWildcard() throws Exception {
String wildcardValue = "*";
String etagValue = "\"some-etag\"";
@@ -456,7 +456,7 @@ public class HttpEntityMethodProcessorMockTests {
assertConditionalResponse(HttpStatus.OK, "body", etagValue, -1);
}
@Test // SPR-13626
@Test // SPR-13626
public void shouldHandleIfNoneMatchIfMatch() throws Exception {
String etagValue = "\"some-etag\"";
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
@@ -469,7 +469,7 @@ public class HttpEntityMethodProcessorMockTests {
assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, -1);
}
@Test // SPR-13626
@Test // SPR-13626
public void shouldHandleIfNoneMatchIfUnmodifiedSince() throws Exception {
String etagValue = "\"some-etag\"";
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
@@ -498,7 +498,7 @@ public class HttpEntityMethodProcessorMockTests {
assertEquals(200, servletResponse.getStatus());
}
@Test //SPR-14767
@Test //SPR-14767
public void shouldHandleValidatorHeadersInPutResponses() throws Exception {
servletRequest.setMethod("PUT");
String etagValue = "\"some-etag\"";
@@ -510,6 +510,7 @@ public class HttpEntityMethodProcessorMockTests {
assertConditionalResponse(HttpStatus.OK, "body", etagValue, -1);
}
private void initStringMessageConversion(MediaType accepted) {
given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true);
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
@@ -541,6 +542,7 @@ public class HttpEntityMethodProcessorMockTests {
}
}
@SuppressWarnings("unused")
public ResponseEntity<String> handle1(HttpEntity<String> httpEntity, ResponseEntity<String> entity,
int i, RequestEntity<String> requestEntity) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,10 +46,7 @@ import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.support.ModelAndViewContainer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* Test fixture with {@link HttpEntityMethodProcessor} delegating to
@@ -78,7 +75,7 @@ public class HttpEntityMethodProcessorTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
Method method = getClass().getDeclaredMethod("handle", HttpEntity.class, HttpEntity.class);
paramList = new MethodParameter(method, 0);
paramSimpleBean = new MethodParameter(method, 1);
@@ -110,9 +107,7 @@ public class HttpEntityMethodProcessorTests {
assertEquals("Jad", result.getBody().getName());
}
// SPR-12861
@Test
@Test // SPR-12861
public void resolveArgumentWithEmptyBody() throws Exception {
this.servletRequest.setContent(new byte[0]);
this.servletRequest.setContentType("application/json");
@@ -187,9 +182,7 @@ public class HttpEntityMethodProcessorTests {
assertTrue(content.contains("\"type\":\"bar\""));
}
// SPR-13423
@Test
@Test // SPR-13423
public void handleReturnValueCharSequence() throws Exception {
List<HttpMessageConverter<?>>converters = new ArrayList<>();
converters.add(new ByteArrayHttpMessageConverter());
@@ -207,7 +200,6 @@ public class HttpEntityMethodProcessorTests {
}
@SuppressWarnings("unused")
private void handle(HttpEntity<List<SimpleBean>> arg1, HttpEntity<SimpleBean> arg2) {
}
@@ -216,6 +208,7 @@ public class HttpEntityMethodProcessorTests {
return null;
}
@SuppressWarnings("unused")
private static abstract class MyParameterizedController<DTO extends Identifiable> {
@@ -223,10 +216,12 @@ public class HttpEntityMethodProcessorTests {
}
}
@SuppressWarnings("unused")
private static class MySimpleParameterizedController extends MyParameterizedController<SimpleBean> {
}
private interface Identifiable extends Serializable {
Long getId();
@@ -275,6 +270,7 @@ public class HttpEntityMethodProcessorTests {
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
private static class ParentClass {
@@ -296,6 +292,7 @@ public class HttpEntityMethodProcessorTests {
}
}
@JsonTypeName("foo")
private static class Foo extends ParentClass {
@@ -307,6 +304,7 @@ public class HttpEntityMethodProcessorTests {
}
}
@JsonTypeName("bar")
private static class Bar extends ParentClass {
@@ -318,6 +316,7 @@ public class HttpEntityMethodProcessorTests {
}
}
private static class JacksonController {
@RequestMapping

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -47,22 +47,28 @@ public class MatrixVariablesMapMethodArgumentResolverTests {
private MatrixVariableMapMethodArgumentResolver resolver;
private MethodParameter paramString;
private MethodParameter paramMap;
private MethodParameter paramMultivalueMap;
private MethodParameter paramMapForPathVar;
private MethodParameter paramMapWithName;
private ModelAndViewContainer mavContainer;
private ServletWebRequest webRequest;
private MockHttpServletRequest request;
private MethodParameter paramString;
private MethodParameter paramMap;
private MethodParameter paramMultivalueMap;
private MethodParameter paramMapForPathVar;
private MethodParameter paramMapWithName;
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
this.resolver = new MatrixVariableMapMethodArgumentResolver();
this.mavContainer = new ModelAndViewContainer();
this.request = new MockHttpServletRequest();
this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<>();
this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
Method method = getClass().getMethod("handle", String.class,
Map.class, MultiValueMap.class, MultiValueMap.class, Map.class);
@@ -72,15 +78,9 @@ public class MatrixVariablesMapMethodArgumentResolverTests {
this.paramMultivalueMap = new SynthesizingMethodParameter(method, 2);
this.paramMapForPathVar = new SynthesizingMethodParameter(method, 3);
this.paramMapWithName = new SynthesizingMethodParameter(method, 4);
this.mavContainer = new ModelAndViewContainer();
this.request = new MockHttpServletRequest();
this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<>();
this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
}
@Test
public void supportsParameter() {
assertFalse(resolver.supportsParameter(paramString));

View File

@@ -48,36 +48,35 @@ public class MatrixVariablesMethodArgumentResolverTests {
private MatrixVariableMethodArgumentResolver resolver;
private MethodParameter paramString;
private MethodParameter paramColors;
private MethodParameter paramYear;
private ModelAndViewContainer mavContainer;
private ServletWebRequest webRequest;
private MockHttpServletRequest request;
private MethodParameter paramString;
private MethodParameter paramColors;
private MethodParameter paramYear;
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
this.resolver = new MatrixVariableMethodArgumentResolver();
Method method = getClass().getMethod("handle", String.class, List.class, int.class);
this.paramString = new MethodParameter(method, 0);
this.paramColors = new MethodParameter(method, 1);
this.paramYear = new MethodParameter(method, 2);
this.paramColors.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
this.mavContainer = new ModelAndViewContainer();
this.request = new MockHttpServletRequest();
this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<>();
this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
Method method = getClass().getMethod("handle", String.class, List.class, int.class);
this.paramString = new MethodParameter(method, 0);
this.paramColors = new MethodParameter(method, 1);
this.paramColors.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
this.paramYear = new MethodParameter(method, 2);
}
@Test
public void supportsParameter() {
assertFalse(resolver.supportsParameter(paramString));
@@ -87,7 +86,6 @@ public class MatrixVariablesMethodArgumentResolverTests {
@Test
public void resolveArgument() throws Exception {
MultiValueMap<String, String> params = getMatrixVariables("cars");
params.add("colors", "red");
params.add("colors", "green");
@@ -99,7 +97,6 @@ public class MatrixVariablesMethodArgumentResolverTests {
@Test
public void resolveArgumentPathVariable() throws Exception {
getMatrixVariables("cars").add("year", "2006");
getMatrixVariables("bikes").add("year", "2005");
@@ -113,7 +110,6 @@ public class MatrixVariablesMethodArgumentResolverTests {
@Test(expected = ServletRequestBindingException.class)
public void resolveArgumentMultipleMatches() throws Exception {
getMatrixVariables("var1").add("colors", "red");
getMatrixVariables("var2").add("colors", "green");
@@ -127,7 +123,6 @@ public class MatrixVariablesMethodArgumentResolverTests {
@Test
public void resolveArgumentNoMatch() throws Exception {
MultiValueMap<String, String> params = getMatrixVariables("cars");
params.add("anotherYear", "2012");
@@ -137,7 +132,6 @@ public class MatrixVariablesMethodArgumentResolverTests {
@SuppressWarnings("unchecked")
private MultiValueMap<String, String> getMatrixVariables(String pathVarName) {
Map<String, MultiValueMap<String, String>> matrixVariables =
(Map<String, MultiValueMap<String, String>>) this.request.getAttribute(
HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ public class ModelAndViewMethodReturnValueHandlerTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
this.handler = new ModelAndViewMethodReturnValueHandler();
this.mavContainer = new ModelAndViewContainer();
this.webRequest = new ServletWebRequest(new MockHttpServletRequest());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@ public class ModelAndViewResolverMethodReturnValueHandlerTests {
@Before
public void setUp() {
public void setup() {
mavResolvers = new ArrayList<>();
handler = new ModelAndViewResolverMethodReturnValueHandler(mavResolvers);
mavContainer = new ModelAndViewContainer();
@@ -118,7 +118,7 @@ public class ModelAndViewResolverMethodReturnValueHandlerTests {
private static class TestModelAndViewResolver implements ModelAndViewResolver {
private Class<?> returnValueType;
private final Class<?> returnValueType;
public TestModelAndViewResolver(Class<?> returnValueType) {
this.returnValueType = returnValueType;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,10 +16,6 @@
package org.springframework.web.servlet.mvc.method.annotation;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -58,6 +54,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*;
/**
* Unit tests for {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.
*
@@ -73,12 +73,12 @@ public class MvcUriComponentsBuilderTests {
@Before
public void setUp() {
public void setup() {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request));
}
@After
public void tearDown() {
public void reset() {
RequestContextHolder.resetRequestAttributes();
}
@@ -372,15 +372,16 @@ public class MvcUriComponentsBuilderTests {
}
}
@RequestMapping("/people")
interface PersonController {
}
private class PersonControllerImpl implements PersonController {
}
@RequestMapping("/people/{id}/addresses")
private static class PersonsAddressesController {
@@ -390,11 +391,12 @@ public class MvcUriComponentsBuilderTests {
}
}
@RequestMapping({ "/persons", "/people" })
private class InvalidController {
@RequestMapping({"/persons", "/people"})
private class InvalidController {
}
private class UnmappedController {
@RequestMapping
@@ -402,6 +404,7 @@ public class MvcUriComponentsBuilderTests {
}
}
@RequestMapping("/something")
static class ControllerWithMethods {
@@ -439,11 +442,13 @@ public class MvcUriComponentsBuilderTests {
}
}
@RequestMapping("/extended") @SuppressWarnings("WeakerAccess")
static class ExtendedController extends ControllerWithMethods {
@RequestMapping("/extended")
@SuppressWarnings("WeakerAccess")
static class ExtendedController extends ControllerWithMethods {
}
@RequestMapping("/user/{userId}/contacts")
private static class UserContactController {
@@ -453,11 +458,13 @@ public class MvcUriComponentsBuilderTests {
}
}
static abstract class AbstractCrudController<T, ID> {
abstract T get(ID id);
}
private static class PersonCrudController extends AbstractCrudController<Person, Long> {
@RequestMapping(path = "/{id}", method = RequestMethod.GET)
@@ -466,6 +473,7 @@ public class MvcUriComponentsBuilderTests {
}
}
@Controller
private static class MetaAnnotationController {
@@ -473,12 +481,12 @@ public class MvcUriComponentsBuilderTests {
public void handle() {
}
@PostJson(path="/input")
@PostJson(path = "/input")
public void handleInput() {
}
}
@RequestMapping(method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@@ -486,9 +494,11 @@ public class MvcUriComponentsBuilderTests {
@Retention(RetentionPolicy.RUNTIME)
@Documented
private @interface PostJson {
String[] path() default {};
}
@EnableWebMvc
static class WebConfig extends WebMvcConfigurerAdapter {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,32 +43,31 @@ public class PathVariableMapMethodArgumentResolverTests {
private PathVariableMapMethodArgumentResolver resolver;
private MethodParameter paramMap;
private MethodParameter paramNamedMap;
private MethodParameter paramMapNoAnnot;
private ModelAndViewContainer mavContainer;
private ServletWebRequest webRequest;
private MockHttpServletRequest request;
private MethodParameter paramMap;
private MethodParameter paramNamedMap;
private MethodParameter paramMapNoAnnot;
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
resolver = new PathVariableMapMethodArgumentResolver();
mavContainer = new ModelAndViewContainer();
request = new MockHttpServletRequest();
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
Method method = getClass().getMethod("handle", Map.class, Map.class, Map.class);
paramMap = new MethodParameter(method, 0);
paramNamedMap = new MethodParameter(method, 1);
paramMapNoAnnot = new MethodParameter(method, 2);
mavContainer = new ModelAndViewContainer();
request = new MockHttpServletRequest();
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}
@Test
public void supportsParameter() {
assertTrue(resolver.supportsParameter(paramMap));
@@ -103,4 +102,4 @@ public class PathVariableMapMethodArgumentResolverTests {
Map<String, String> mapWithoutAnnotat) {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,34 +52,30 @@ public class PathVariableMethodArgumentResolverTests {
private PathVariableMethodArgumentResolver resolver;
private MethodParameter paramNamedString;
private MethodParameter paramString;
private MethodParameter paramNotRequired;
private MethodParameter paramOptional;
private ModelAndViewContainer mavContainer;
private ServletWebRequest webRequest;
private MockHttpServletRequest request;
private MethodParameter paramNamedString;
private MethodParameter paramString;
private MethodParameter paramNotRequired;
private MethodParameter paramOptional;
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
resolver = new PathVariableMethodArgumentResolver();
mavContainer = new ModelAndViewContainer();
request = new MockHttpServletRequest();
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
paramNamedString = new SynthesizingMethodParameter(method, 0);
paramString = new SynthesizingMethodParameter(method, 1);
paramNotRequired = new SynthesizingMethodParameter(method, 2);
paramOptional = new SynthesizingMethodParameter(method, 3);
mavContainer = new ModelAndViewContainer();
request = new MockHttpServletRequest();
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -123,7 +123,7 @@ public class RequestPartIntegrationTests {
}
@Before
public void setUp() {
public void setup() {
ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter();
emptyBodyConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -65,13 +65,18 @@ import static org.mockito.BDDMockito.*;
*/
public class RequestPartMethodArgumentResolverTests {
private RequestPartMethodArgumentResolver resolver;
private HttpMessageConverter<SimpleBean> messageConverter;
private RequestPartMethodArgumentResolver resolver;
private MultipartFile multipartFile1;
private MultipartFile multipartFile2;
private MockMultipartHttpServletRequest multipartRequest;
private NativeWebRequest webRequest;
private MethodParameter paramRequestPart;
private MethodParameter paramNamedRequestPart;
private MethodParameter paramValidRequestPart;
@@ -90,16 +95,26 @@ public class RequestPartMethodArgumentResolverTests {
private MethodParameter optionalPartList;
private MethodParameter optionalRequestPart;
private NativeWebRequest webRequest;
private MockMultipartHttpServletRequest multipartRequest;
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
@SuppressWarnings("unchecked")
public void setup() throws Exception {
messageConverter = mock(HttpMessageConverter.class);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
resolver = new RequestPartMethodArgumentResolver(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
reset(messageConverter);
byte[] content = "doesn't matter as long as not empty".getBytes(StandardCharsets.UTF_8);
multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", content);
multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", content);
multipartRequest = new MockMultipartHttpServletRequest();
multipartRequest.addFile(multipartFile1);
multipartRequest.addFile(multipartFile2);
multipartRequest.addFile(new MockMultipartFile("otherPart", "", "text/plain", content));
webRequest = new ServletWebRequest(multipartRequest, new MockHttpServletResponse());
Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
paramRequestPart = new SynthesizingMethodParameter(method, 0);
paramRequestPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
paramNamedRequestPart = new SynthesizingMethodParameter(method, 1);
@@ -124,22 +139,6 @@ public class RequestPartMethodArgumentResolverTests {
optionalPartList = new SynthesizingMethodParameter(method, 15);
optionalPartList.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
optionalRequestPart = new SynthesizingMethodParameter(method, 16);
messageConverter = mock(HttpMessageConverter.class);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
resolver = new RequestPartMethodArgumentResolver(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
reset(messageConverter);
byte[] content = "doesn't matter as long as not empty".getBytes(StandardCharsets.UTF_8);
multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", content);
multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", content);
multipartRequest = new MockMultipartHttpServletRequest();
multipartRequest.addFile(multipartFile1);
multipartRequest.addFile(multipartFile2);
multipartRequest.addFile(new MockMultipartFile("otherPart", "", "text/plain", content));
webRequest = new ServletWebRequest(multipartRequest, new MockHttpServletResponse());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -66,12 +66,20 @@ import static org.mockito.BDDMockito.*;
*/
public class RequestResponseBodyMethodProcessorMockTests {
private RequestResponseBodyMethodProcessor processor;
private HttpMessageConverter<String> stringMessageConverter;
private HttpMessageConverter<Resource> resourceMessageConverter;
private RequestResponseBodyMethodProcessor processor;
private ModelAndViewContainer mavContainer;
private MockHttpServletRequest servletRequest;
private MockHttpServletResponse servletResponse;
private NativeWebRequest webRequest;
private MethodParameter paramRequestBodyString;
private MethodParameter paramInt;
private MethodParameter paramValidBean;
@@ -82,18 +90,10 @@ public class RequestResponseBodyMethodProcessorMockTests {
private MethodParameter returnTypeStringProduces;
private MethodParameter returnTypeResource;
private ModelAndViewContainer mavContainer;
private NativeWebRequest webRequest;
private MockHttpServletRequest servletRequest;
private MockHttpServletResponse servletResponse;
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
@SuppressWarnings("unchecked")
public void setup() throws Exception {
stringMessageConverter = mock(HttpMessageConverter.class);
given(stringMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
@@ -102,6 +102,12 @@ public class RequestResponseBodyMethodProcessorMockTests {
processor = new RequestResponseBodyMethodProcessor(Arrays.asList(stringMessageConverter, resourceMessageConverter));
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);
paramInt = new MethodParameter(methodHandle1, 1);
@@ -112,13 +118,6 @@ public class RequestResponseBodyMethodProcessorMockTests {
returnTypeInt = new MethodParameter(getClass().getMethod("handle5"), -1);
returnTypeStringProduces = new MethodParameter(getClass().getMethod("handle6"), -1);
returnTypeResource = new MethodParameter(getClass().getMethod("handle7"), -1);
mavContainer = new ModelAndViewContainer();
servletRequest = new MockHttpServletRequest();
servletRequest.setMethod("POST");
servletResponse = new MockHttpServletResponse();
webRequest = new ServletWebRequest(servletRequest, servletResponse);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -82,45 +82,42 @@ import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class RequestResponseBodyMethodProcessorTests {
private ModelAndViewContainer container;
private MockHttpServletRequest servletRequest;
private MockHttpServletResponse servletResponse;
private NativeWebRequest request;
private ValidatingBinderFactory factory;
private MethodParameter paramGenericList;
private MethodParameter paramSimpleBean;
private MethodParameter paramMultiValueMap;
private MethodParameter paramString;
private MethodParameter returnTypeString;
private ModelAndViewContainer container;
private NativeWebRequest request;
private MockHttpServletRequest servletRequest;
private MockHttpServletResponse servletResponse;
private ValidatingBinderFactory factory;
@Before
public void setUp() throws Exception {
Method method = getClass().getDeclaredMethod("handle", List.class, SimpleBean.class,
MultiValueMap.class, String.class);
public void setup() throws Exception {
container = new ModelAndViewContainer();
servletRequest = new MockHttpServletRequest();
servletRequest.setMethod("POST");
servletResponse = new MockHttpServletResponse();
request = new ServletWebRequest(servletRequest, servletResponse);
this.factory = new ValidatingBinderFactory();
Method method = getClass().getDeclaredMethod("handle",
List.class, SimpleBean.class, MultiValueMap.class, String.class);
paramGenericList = new MethodParameter(method, 0);
paramSimpleBean = new MethodParameter(method, 1);
paramMultiValueMap = new MethodParameter(method, 2);
paramString = new MethodParameter(method, 3);
returnTypeString = new MethodParameter(method, -1);
container = new ModelAndViewContainer();
servletRequest = new MockHttpServletRequest();
servletRequest.setMethod("POST");
servletResponse = new MockHttpServletResponse();
request = new ServletWebRequest(servletRequest, servletResponse);
this.factory = new ValidatingBinderFactory();
}
@Test
public void resolveArgumentParameterizedType() throws Exception {
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,10 +15,6 @@
*/
package org.springframework.web.servlet.mvc.method.annotation;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.web.servlet.mvc.method.annotation.SseEmitter.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
@@ -43,9 +39,12 @@ import org.springframework.web.context.request.async.StandardServletAsyncWebRequ
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.method.support.ModelAndViewContainer;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for ResponseBodyEmitterReturnValueHandler.
*
* @author Rossen Stoyanchev
*/
public class ResponseBodyEmitterReturnValueHandlerTests {
@@ -60,8 +59,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
List<HttpMessageConverter<?>> converters = Arrays.asList(
new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter());
@@ -75,6 +73,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
this.request.setAsyncSupported(true);
}
@Test
public void supportsReturnType() throws Exception {
assertTrue(this.handler.supportsReturnType(returnType("handle")));
@@ -158,7 +157,8 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
bean2.setId(2L);
bean2.setName("John");
emitter.send(event().comment("a test").name("update").id("1").reconnectTime(5000L).data(bean1).data(bean2));
emitter.send(SseEmitter.event().
comment("a test").name("update").id("1").reconnectTime(5000L).data(bean1).data(bean2));
assertEquals(":a test\n" +
"event:update\n" +
@@ -193,6 +193,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
assertEquals(Collections.singletonList("bar"), this.response.getHeaders("foo"));
}
private void handleReturnValue(Object returnValue, MethodParameter returnType) throws Exception {
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
this.handler.handleReturnValue(returnValue, returnType, mavContainer, this.webRequest);
@@ -204,7 +205,6 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
}
@SuppressWarnings("unused")
private static class TestController {
@@ -235,9 +235,9 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
private ResponseEntity handleRawResponseEntity() {
return null;
}
}
private static class SimpleBean {
private Long id;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,25 +41,23 @@ public class ServletCookieValueMethodArgumentResolverTests {
private ServletCookieValueMethodArgumentResolver resolver;
private MethodParameter cookieParameter;
private MethodParameter cookieStringParameter;
private MockHttpServletRequest request;
private ServletWebRequest webRequest;
private MockHttpServletRequest request;
private MethodParameter cookieParameter;
private MethodParameter cookieStringParameter;
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
resolver = new ServletCookieValueMethodArgumentResolver(null);
request = new MockHttpServletRequest();
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
Method method = getClass().getMethod("params", Cookie.class, String.class);
cookieParameter = new SynthesizingMethodParameter(method, 0);
cookieStringParameter = new SynthesizingMethodParameter(method, 1);
request = new MockHttpServletRequest();
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -48,40 +48,36 @@ public class ServletModelAttributeMethodProcessorTests {
private ServletModelAttributeMethodProcessor processor;
private MethodParameter testBeanModelAttr;
private MethodParameter testBeanWithoutStringConstructorModelAttr;
private MethodParameter testBeanWithOptionalModelAttr;
private WebDataBinderFactory binderFactory;
private ModelAndViewContainer mavContainer;
private NativeWebRequest webRequest;
private MockHttpServletRequest request;
private WebDataBinderFactory binderFactory;
private NativeWebRequest webRequest;
private MethodParameter testBeanModelAttr;
private MethodParameter testBeanWithoutStringConstructorModelAttr;
private MethodParameter testBeanWithOptionalModelAttr;
@Before
public void setUp() throws Exception {
this.processor = new ServletModelAttributeMethodProcessor(false);
Method method = getClass().getDeclaredMethod("modelAttribute",
TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);
this.testBeanModelAttr = new MethodParameter(method, 0);
this.testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
this.testBeanWithOptionalModelAttr = new MethodParameter(method, 2);
public void setup() throws Exception {
processor = new ServletModelAttributeMethodProcessor(false);
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
binderFactory = new ServletRequestDataBinderFactory(null, initializer);
this.binderFactory = new ServletRequestDataBinderFactory(null, initializer);
this.mavContainer = new ModelAndViewContainer();
mavContainer = new ModelAndViewContainer();
request = new MockHttpServletRequest();
webRequest = new ServletWebRequest(request);
this.request = new MockHttpServletRequest();
this.webRequest = new ServletWebRequest(request);
Method method = getClass().getDeclaredMethod("modelAttribute",
TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);
testBeanModelAttr = new MethodParameter(method, 0);
testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
testBeanWithOptionalModelAttr = new MethodParameter(method, 2);
}
@@ -89,13 +85,11 @@ public class ServletModelAttributeMethodProcessorTests {
public void createAttributeUriTemplateVar() throws Exception {
Map<String, String> uriTemplateVars = new HashMap<>();
uriTemplateVars.put("testBean1", "Patty");
this.request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
// Type conversion from "Patty" to TestBean via TestBean(String) constructor
TestBean testBean =
(TestBean) this.processor.resolveArgument(
this.testBeanModelAttr, this.mavContainer, this.webRequest, this.binderFactory);
TestBean testBean = (TestBean) processor.resolveArgument(
testBeanModelAttr, mavContainer, webRequest, binderFactory);
assertEquals("Patty", testBean.getName());
}
@@ -106,9 +100,8 @@ public class ServletModelAttributeMethodProcessorTests {
uriTemplateVars.put("testBean2", "Patty");
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
TestBeanWithoutStringConstructor testBean =
(TestBeanWithoutStringConstructor) this.processor.resolveArgument(
this.testBeanWithoutStringConstructorModelAttr, this.mavContainer, this.webRequest, this.binderFactory);
TestBeanWithoutStringConstructor testBean = (TestBeanWithoutStringConstructor) processor.resolveArgument(
testBeanWithoutStringConstructorModelAttr, mavContainer, webRequest, binderFactory);
assertNotNull(testBean);
}
@@ -117,89 +110,81 @@ public class ServletModelAttributeMethodProcessorTests {
public void createAttributeUriTemplateVarWithOptional() throws Exception {
Map<String, String> uriTemplateVars = new HashMap<>();
uriTemplateVars.put("testBean3", "Patty");
this.request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
// Type conversion from "Patty" to TestBean via TestBean(String) constructor
Optional<TestBean> testBean =
(Optional<TestBean>) this.processor.resolveArgument(
this.testBeanWithOptionalModelAttr, this.mavContainer, this.webRequest, this.binderFactory);
Optional<TestBean> testBean = (Optional<TestBean>) processor.resolveArgument(
testBeanWithOptionalModelAttr, mavContainer, webRequest, binderFactory);
assertEquals("Patty", testBean.get().getName());
}
@Test
public void createAttributeRequestParameter() throws Exception {
this.request.addParameter("testBean1", "Patty");
request.addParameter("testBean1", "Patty");
// Type conversion from "Patty" to TestBean via TestBean(String) constructor
TestBean testBean =
(TestBean) this.processor.resolveArgument(
this.testBeanModelAttr, this.mavContainer, this.webRequest, this.binderFactory);
TestBean testBean = (TestBean) processor.resolveArgument(
testBeanModelAttr, mavContainer, webRequest, binderFactory);
assertEquals("Patty", testBean.getName());
}
@Test
public void createAttributeRequestParameterCannotConvert() throws Exception {
this.request.addParameter("testBean2", "Patty");
request.addParameter("testBean2", "Patty");
TestBeanWithoutStringConstructor testBean =
(TestBeanWithoutStringConstructor) this.processor.resolveArgument(
this.testBeanWithoutStringConstructorModelAttr, this.mavContainer, this.webRequest, this.binderFactory);
TestBeanWithoutStringConstructor testBean = (TestBeanWithoutStringConstructor) processor.resolveArgument(
testBeanWithoutStringConstructorModelAttr, mavContainer, webRequest, binderFactory);
assertNotNull(testBean);
}
@Test
public void createAttributeRequestParameterWithOptional() throws Exception {
this.request.addParameter("testBean3", "Patty");
request.addParameter("testBean3", "Patty");
Optional<TestBean> testBean =
(Optional<TestBean>) this.processor.resolveArgument(
this.testBeanWithOptionalModelAttr, this.mavContainer, this.webRequest, this.binderFactory);
Optional<TestBean> testBean = (Optional<TestBean>) processor.resolveArgument(
testBeanWithOptionalModelAttr, mavContainer, webRequest, binderFactory);
assertEquals("Patty", testBean.get().getName());
}
@Test
public void attributesAsNullValues() throws Exception {
this.request.addParameter("name", "Patty");
request.addParameter("name", "Patty");
this.mavContainer.getModel().put("testBean1", null);
this.mavContainer.getModel().put("testBean2", null);
this.mavContainer.getModel().put("testBean3", null);
mavContainer.getModel().put("testBean1", null);
mavContainer.getModel().put("testBean2", null);
mavContainer.getModel().put("testBean3", null);
assertNull(this.processor.resolveArgument(
this.testBeanModelAttr, this.mavContainer, this.webRequest, this.binderFactory));
assertNull(processor.resolveArgument(
testBeanModelAttr, mavContainer, webRequest, binderFactory));
assertNull(this.processor.resolveArgument(
this.testBeanWithoutStringConstructorModelAttr, this.mavContainer, this.webRequest, this.binderFactory));
assertNull(processor.resolveArgument(
testBeanWithoutStringConstructorModelAttr, mavContainer, webRequest, binderFactory));
Optional<TestBean> testBean =
(Optional<TestBean>) this.processor.resolveArgument(
this.testBeanWithOptionalModelAttr, this.mavContainer, this.webRequest, this.binderFactory);
Optional<TestBean> testBean = (Optional<TestBean>) processor.resolveArgument(
testBeanWithOptionalModelAttr, mavContainer, webRequest, binderFactory);
assertFalse(testBean.isPresent());
}
@Test
public void attributesAsOptionalEmpty() throws Exception {
this.request.addParameter("name", "Patty");
request.addParameter("name", "Patty");
this.mavContainer.getModel().put("testBean1", Optional.empty());
this.mavContainer.getModel().put("testBean2", Optional.empty());
this.mavContainer.getModel().put("testBean3", Optional.empty());
mavContainer.getModel().put("testBean1", Optional.empty());
mavContainer.getModel().put("testBean2", Optional.empty());
mavContainer.getModel().put("testBean3", Optional.empty());
assertNull(this.processor.resolveArgument(
this.testBeanModelAttr, this.mavContainer, this.webRequest, this.binderFactory));
assertNull(processor.resolveArgument(
testBeanModelAttr, mavContainer, webRequest, binderFactory));
assertNull(this.processor.resolveArgument(
this.testBeanWithoutStringConstructorModelAttr, this.mavContainer, this.webRequest, this.binderFactory));
assertNull(processor.resolveArgument(
testBeanWithoutStringConstructorModelAttr, mavContainer, webRequest, binderFactory));
Optional<TestBean> testBean =
(Optional<TestBean>) this.processor.resolveArgument(
this.testBeanWithOptionalModelAttr, this.mavContainer, this.webRequest, this.binderFactory);
Optional<TestBean> testBean =(Optional<TestBean>) processor.resolveArgument(
testBeanWithOptionalModelAttr, mavContainer, webRequest, binderFactory);
assertFalse(testBean.isPresent());
}
@@ -219,7 +204,6 @@ public class ServletModelAttributeMethodProcessorTests {
public TestBeanWithoutStringConstructor(int i) {
}
}
}

View File

@@ -41,22 +41,23 @@ public class ServletResponseMethodArgumentResolverTests {
private ServletResponseMethodArgumentResolver resolver;
private Method method;
private ModelAndViewContainer mavContainer;
private ServletWebRequest webRequest;
private MockHttpServletResponse servletResponse;
private ServletWebRequest webRequest;
private Method method;
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
resolver = new ServletResponseMethodArgumentResolver();
method = getClass().getMethod("supportedParams", ServletResponse.class, OutputStream.class, Writer.class);
servletResponse = new MockHttpServletResponse();
mavContainer = new ModelAndViewContainer();
servletResponse = new MockHttpServletResponse();
webRequest = new ServletWebRequest(new MockHttpServletRequest(), servletResponse);
method = getClass().getMethod("supportedParams", ServletResponse.class, OutputStream.class, Writer.class);
}

View File

@@ -62,7 +62,7 @@ public class StreamingResponseBodyReturnValueHandlerTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
this.handler = new StreamingResponseBodyReturnValueHandler();
this.mavContainer = new ModelAndViewContainer();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,27 +39,28 @@ public class UriComponentsBuilderMethodArgumentResolverTests {
private UriComponentsBuilderMethodArgumentResolver resolver;
private MethodParameter builderParam;
private MethodParameter servletBuilderParam;
private MethodParameter intParam;
private ServletWebRequest webRequest;
private MockHttpServletRequest servletRequest;
private MethodParameter builderParam;
private MethodParameter servletBuilderParam;
private MethodParameter intParam;
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
this.resolver = new UriComponentsBuilderMethodArgumentResolver();
this.servletRequest = new MockHttpServletRequest();
this.webRequest = new ServletWebRequest(this.servletRequest);
Method method = this.getClass().getDeclaredMethod("handle", UriComponentsBuilder.class, ServletUriComponentsBuilder.class, int.class);
this.builderParam = new MethodParameter(method, 0);
this.servletBuilderParam = new MethodParameter(method, 1);
this.intParam = new MethodParameter(method, 2);
this.servletRequest = new MockHttpServletRequest();
this.webRequest = new ServletWebRequest(this.servletRequest);
}
@Test
public void supportsParameter() throws Exception {
assertTrue(this.resolver.supportsParameter(this.builderParam));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,13 +46,15 @@ public class ViewMethodReturnValueHandlerTests {
private ServletWebRequest webRequest;
@Before
public void setUp() {
public void setup() {
this.handler = new ViewMethodReturnValueHandler();
this.mavContainer = new ModelAndViewContainer();
this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
}
@Test
public void supportsReturnType() throws Exception {
assertTrue(this.handler.supportsReturnType(createReturnValueParam("view")));
@@ -83,6 +85,7 @@ public class ViewMethodReturnValueHandlerTests {
return new MethodParameter(method, -1);
}
View view() {
return null;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,13 +45,15 @@ public class ViewNameMethodReturnValueHandlerTests {
@Before
public void setUp() throws NoSuchMethodException {
public void setup() throws NoSuchMethodException {
this.handler = new ViewNameMethodReturnValueHandler();
this.mavContainer = new ModelAndViewContainer();
this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
this.param = new MethodParameter(getClass().getDeclaredMethod("viewName"), -1);
}
@Test
public void supportsReturnType() throws Exception {
assertTrue(this.handler.supportsReturnType(this.param));
@@ -98,4 +100,4 @@ public class ViewNameMethodReturnValueHandlerTests {
return null;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,21 +52,19 @@ import static org.junit.Assert.*;
*/
public class DefaultHandlerExceptionResolverTests {
private DefaultHandlerExceptionResolver exceptionResolver;
private final DefaultHandlerExceptionResolver exceptionResolver = new DefaultHandlerExceptionResolver();
private MockHttpServletRequest request;
private final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
private final MockHttpServletResponse response = new MockHttpServletResponse();
private MockHttpServletResponse response;
@Before
public void setUp() {
exceptionResolver = new DefaultHandlerExceptionResolver();
public void setup() {
exceptionResolver.setWarnLogCategory(exceptionResolver.getClass().getName());
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
request.setMethod("GET");
}
@Test
public void handleHttpRequestMethodNotSupported() {
HttpRequestMethodNotSupportedException ex =
@@ -207,7 +205,7 @@ public class DefaultHandlerExceptionResolverTests {
assertSame(ex, request.getAttribute("javax.servlet.error.exception"));
}
@Test // SPR-14669
@Test // SPR-14669
public void handleAsyncRequestTimeoutException() throws Exception {
Exception ex = new AsyncRequestTimeoutException();
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
package org.springframework.web.servlet.mvc.support;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
@@ -38,19 +37,11 @@ import static org.junit.Assert.*;
*/
public class ParameterizableViewControllerTests {
private ParameterizableViewController controller;
private final ParameterizableViewController controller = new ParameterizableViewController();
private MockHttpServletRequest request;
private final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
private MockHttpServletResponse response;
@Before
public void setUp() throws Exception {
this.controller = new ParameterizableViewController();
this.request = new MockHttpServletRequest("GET", "/");
this.response = new MockHttpServletResponse();
}
private final MockHttpServletResponse response = new MockHttpServletResponse();
@Test