Map arg resolver backs out if annotations present

Closes gh-21874
This commit is contained in:
Rossen Stoyanchev
2019-03-19 16:41:26 -04:00
parent a2fcf0a821
commit 5a3ff35215
4 changed files with 72 additions and 45 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.method.annotation;
import java.lang.reflect.Method;
import java.util.Map;
import org.junit.Before;
@@ -25,14 +24,18 @@ import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.ResolvableMethod;
import org.springframework.web.method.support.ModelAndViewContainer;
import static org.junit.Assert.*;
/**
* Test fixture with {@link org.springframework.web.method.annotation.MapMethodProcessor}.
* Test fixture with
* {@link org.springframework.web.method.annotation.MapMethodProcessor}.
*
* @author Rossen Stoyanchev
*/
@@ -42,52 +45,59 @@ public class MapMethodProcessorTests {
private ModelAndViewContainer mavContainer;
private MethodParameter paramMap;
private MethodParameter returnParamMap;
private NativeWebRequest webRequest;
private final ResolvableMethod resolvable =
ResolvableMethod.on(getClass()).annotPresent(RequestMapping.class).build();
@Before
public void setUp() throws Exception {
processor = new MapMethodProcessor();
mavContainer = new ModelAndViewContainer();
Method method = getClass().getDeclaredMethod("map", Map.class);
paramMap = new MethodParameter(method, 0);
returnParamMap = new MethodParameter(method, 0);
webRequest = new ServletWebRequest(new MockHttpServletRequest());
this.processor = new MapMethodProcessor();
this.mavContainer = new ModelAndViewContainer();
this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
}
@Test
public void supportsParameter() {
assertTrue(processor.supportsParameter(paramMap));
assertTrue(this.processor.supportsParameter(
this.resolvable.annotNotPresent().arg(Map.class, String.class, Object.class)));
assertFalse(this.processor.supportsParameter(
this.resolvable.annotPresent(RequestBody.class).arg(Map.class, String.class, Object.class)));
}
@Test
public void supportsReturnType() {
assertTrue(processor.supportsReturnType(returnParamMap));
assertTrue(this.processor.supportsReturnType(this.resolvable.returnType()));
}
@Test
public void resolveArgumentValue() throws Exception {
assertSame(mavContainer.getModel(), processor.resolveArgument(paramMap, mavContainer, webRequest, null));
MethodParameter param = this.resolvable.annotNotPresent().arg(Map.class, String.class, Object.class);
assertSame(this.mavContainer.getModel(),
this.processor.resolveArgument(param, this.mavContainer, this.webRequest, null));
}
@Test
public void handleMapReturnValue() throws Exception {
mavContainer.addAttribute("attr1", "value1");
this.mavContainer.addAttribute("attr1", "value1");
Map<String, Object> returnValue = new ModelMap("attr2", "value2");
processor.handleReturnValue(returnValue , returnParamMap, mavContainer, webRequest);
this.processor.handleReturnValue(
returnValue , this.resolvable.returnType(), this.mavContainer, this.webRequest);
assertEquals("value1", mavContainer.getModel().get("attr1"));
assertEquals("value2", mavContainer.getModel().get("attr2"));
}
@SuppressWarnings("unused")
private Map<String, Object> map(Map<String, Object> map) {
@RequestMapping
private Map<String, Object> handle(
Map<String, Object> map,
@RequestBody Map<String, Object> annotMap) {
return null;
}