From c211c98b407aa5876ac1a2952d16761e9702cfb0 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 22 Aug 2013 14:54:02 -0400 Subject: [PATCH] Support placeholder values in Spring MVC Test Issue: SPR-10825 --- .../setup/StandaloneMockMvcBuilder.java | 70 +++++++++++++++---- .../setup/StandaloneMockMvcBuilderTests.java | 64 +++++++++++++++++ 2 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 spring-test-mvc/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java index dd098bd4eea..d1032762b90 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java @@ -19,9 +19,12 @@ package org.springframework.test.web.servlet.setup; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.InitializingBean; import org.springframework.format.support.DefaultFormattingConversionService; @@ -30,6 +33,9 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.mock.web.MockServletContext; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; +import org.springframework.util.PropertyPlaceholderHelper; +import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; +import org.springframework.util.StringValueResolver; import org.springframework.validation.Validator; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.context.WebApplicationContext; @@ -110,6 +116,8 @@ public class StandaloneMockMvcBuilder extends DefaultMockMvcBuilder placeHolderValues = new HashMap(); + /** * Protected constructor. Not intended for direct instantiation. @@ -282,10 +290,23 @@ public class StandaloneMockMvcBuilder extends DefaultMockMvcBuilder values) { + this.helper = new PropertyPlaceholderHelper("${", "}", ":", false); + this.resolver = new PlaceholderResolver() { + @Override + public String resolvePlaceholder(String placeholderName) { + return values.get(placeholderName); + } + }; + } + + @Override + public String resolveStringValue(String strVal) throws BeansException { + return this.helper.replacePlaceholders(strVal, this.resolver); + } + } + /** A {@link ViewResolver} that always returns same View */ private static class StaticViewResolver implements ViewResolver { diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java new file mode 100644 index 00000000000..065770f2f37 --- /dev/null +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.web.servlet.setup; + +import org.junit.Test; +import org.springframework.mock.web.test.MockHttpServletRequest; +import org.springframework.mock.web.test.MockServletContext; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.HandlerExecutionChain; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; + +import static org.junit.Assert.*; + + +/** + * @author Rossen Stoyanchev + */ +public class StandaloneMockMvcBuilderTests { + + + // SPR-10825 + + @Test + public void placeHoldersInRequestMapping() throws Exception { + + StubWebApplicationContext cxt = new StubWebApplicationContext(new MockServletContext()); + StandaloneMockMvcBuilder builder = new StandaloneMockMvcBuilder(new PlaceholderController()); + builder.addPlaceHolderValues("sys.login.ajax", "/foo"); + builder.initWebAppContext(cxt); + + RequestMappingHandlerMapping hm = cxt.getBean(RequestMappingHandlerMapping.class); + + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); + HandlerExecutionChain chain = hm.getHandler(request); + + assertNotNull(chain); + assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName()); + } + + + @Controller + private static class PlaceholderController { + + @RequestMapping(value = "${sys.login.ajax}") + private void handleWithPlaceholders() { } + } + +}