From b20397e4d2ea58481aee0f515a9f450e679b6d97 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Thu, 12 Feb 2009 22:47:21 +0000 Subject: [PATCH] custom converter tests --- .../webflow/mvc/view/AbstractMvcView.java | 3 ++ .../webflow/mvc/view/MvcViewTests.java | 42 ++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/AbstractMvcView.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/AbstractMvcView.java index 9b73ed39..911a3bcf 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/AbstractMvcView.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/AbstractMvcView.java @@ -41,6 +41,7 @@ import org.springframework.binding.mapping.impl.DefaultMapping; import org.springframework.binding.message.MessageBuilder; import org.springframework.binding.message.MessageResolver; import org.springframework.core.style.ToStringCreator; +import org.springframework.util.Assert; import org.springframework.validation.BindingResult; import org.springframework.web.util.WebUtils; import org.springframework.webflow.core.collection.AttributeMap; @@ -399,6 +400,8 @@ public abstract class AbstractMvcView implements View { DefaultMapping mapping = new DefaultMapping(source, target); mapping.setRequired(binding.getRequired()); if (binding.getConverter() != null) { + Assert.notNull(conversionService, + "A ConversionService must be configured to use resolve custom converters to use during binding"); ConversionExecutor conversionExecutor = conversionService.getConversionExecutor(binding.getConverter(), String.class, target.getValueType(model)); mapping.setTypeConverter(conversionExecutor); diff --git a/spring-webflow/src/test/java/org/springframework/webflow/mvc/view/MvcViewTests.java b/spring-webflow/src/test/java/org/springframework/webflow/mvc/view/MvcViewTests.java index a87d538f..d1451493 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/mvc/view/MvcViewTests.java +++ b/spring-webflow/src/test/java/org/springframework/webflow/mvc/view/MvcViewTests.java @@ -3,7 +3,6 @@ package org.springframework.webflow.mvc.view; import java.security.Principal; import java.util.Calendar; import java.util.Date; -import java.util.HashSet; import java.util.List; import java.util.Map; @@ -12,6 +11,7 @@ import javax.servlet.http.HttpServletResponse; import junit.framework.TestCase; +import org.springframework.binding.convert.converters.StringToDate; import org.springframework.binding.convert.service.DefaultConversionService; import org.springframework.binding.expression.support.StaticExpression; import org.springframework.binding.validation.ValidationContext; @@ -266,10 +266,10 @@ public class MvcViewTests extends TestCase { assertEquals(null, bindBean.getBeanProperty().getName()); } - public void testResumeEventModelBindingFieldMarker() throws Exception { + public void testResumeEventModelBindingCustomConverter() throws Exception { MockRequestContext context = new MockRequestContext(); context.putRequestParameter("_eventId", "submit"); - context.putRequestParameter("_booleanProperty", "whatever"); + context.putRequestParameter("dateProperty", "01-01-2007"); BindBean bindBean = new BindBean(); StaticExpression modelObject = new StaticExpression(bindBean); modelObject.setExpressionString("bindBean"); @@ -282,8 +282,40 @@ public class MvcViewTests extends TestCase { org.springframework.web.servlet.View mvcView = new MockView(); AbstractMvcView view = new MockMvcView(mvcView, context); view.setExpressionParser(DefaultExpressionParserFactory.getExpressionParser()); - HashSet allowedBindFields = new HashSet(); - allowedBindFields.add("booleanProperty"); + DefaultConversionService conversionService = new DefaultConversionService(); + StringToDate stringToDate = new StringToDate(); + stringToDate.setPattern("MM-dd-yyyy"); + conversionService.addConverter("customDateConverter", stringToDate); + view.setConversionService(conversionService); + BinderConfiguration binderConfiguration = new BinderConfiguration(); + binderConfiguration.addBinding(new Binding("dateProperty", "customDateConverter", true)); + view.setBinderConfiguration(binderConfiguration); + view.processUserEvent(); + assertTrue(view.hasFlowEvent()); + assertEquals("submit", view.getFlowEvent().getId()); + Calendar cal = Calendar.getInstance(); + cal.clear(); + cal.set(Calendar.YEAR, 2007); + assertEquals(cal.getTime(), bindBean.getDateProperty()); + } + + public void testResumeEventModelBindingFieldMarker() throws Exception { + MockRequestContext context = new MockRequestContext(); + context.putRequestParameter("_eventId", "submit"); + context.putRequestParameter("_booleanProperty", "whatever"); + BindBean bindBean = new BindBean(); + bindBean.setBooleanProperty(true); + StaticExpression modelObject = new StaticExpression(bindBean); + modelObject.setExpressionString("bindBean"); + context.getCurrentState().getAttributes().put("model", modelObject); + context.getFlowScope().put("bindBean", bindBean); + context.getMockExternalContext().setNativeContext(new MockServletContext()); + context.getMockExternalContext().setNativeRequest(new MockHttpServletRequest()); + context.getMockExternalContext().setNativeResponse(new MockHttpServletResponse()); + context.getMockFlowExecutionContext().setKey(new MockFlowExecutionKey("c1v1")); + org.springframework.web.servlet.View mvcView = new MockView(); + AbstractMvcView view = new MockMvcView(mvcView, context); + view.setExpressionParser(DefaultExpressionParserFactory.getExpressionParser()); view.processUserEvent(); assertEquals(false, bindBean.getBooleanProperty()); }