11/*
2- * Copyright 2002-2014 the original author or authors.
2+ * Copyright 2002-2015 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
3737import com .fasterxml .jackson .databind .ObjectMapper ;
3838import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
3939import org .hamcrest .Matchers ;
40+ import org .joda .time .LocalDate ;
4041import org .junit .Before ;
4142import org .junit .Test ;
4243
5253import org .springframework .core .io .ClassPathResource ;
5354import org .springframework .format .annotation .DateTimeFormat ;
5455import org .springframework .format .annotation .DateTimeFormat .ISO ;
56+ import org .springframework .format .annotation .NumberFormat ;
5557import org .springframework .format .support .FormattingConversionServiceFactoryBean ;
5658import org .springframework .http .MediaType ;
5759import org .springframework .http .converter .HttpMessageConverter ;
@@ -165,7 +167,7 @@ public void setUp() throws Exception {
165167 appContext .getServletContext ().setAttribute (attributeName , appContext );
166168
167169 handler = new TestController ();
168- Method method = TestController .class .getMethod ("testBind" , Date .class , TestBean .class , BindingResult .class );
170+ Method method = TestController .class .getMethod ("testBind" , Date .class , Double . class , TestBean .class , BindingResult .class );
169171 handlerMethod = new InvocableHandlerMethod (handler , method );
170172 }
171173
@@ -211,6 +213,7 @@ public void testDefaultConfig() throws Exception {
211213 // default web binding initializer behavior test
212214 request = new MockHttpServletRequest ("GET" , "/" );
213215 request .addParameter ("date" , "2009-10-31" );
216+ request .addParameter ("percent" , "99.99%" );
214217 MockHttpServletResponse response = new MockHttpServletResponse ();
215218
216219 HandlerExecutionChain chain = mapping .getHandler (request );
@@ -222,6 +225,8 @@ public void testDefaultConfig() throws Exception {
222225
223226 adapter .handle (request , response , handlerMethod );
224227 assertTrue (handler .recordedValidationError );
228+ assertEquals (LocalDate .parse ("2009-10-31" ).toDate (), handler .date );
229+ assertEquals (Double .valueOf (0.9999 ),handler .percent );
225230
226231 CompositeUriComponentsContributor uriComponentsContributor = this .appContext .getBean (
227232 MvcUriComponentsBuilder .MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME ,
@@ -841,6 +846,12 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
841846 public @interface IsoDate {
842847 }
843848
849+ @ NumberFormat (style = NumberFormat .Style .PERCENT )
850+ @ Target ({ElementType .PARAMETER })
851+ @ Retention (RetentionPolicy .RUNTIME )
852+ public @interface PercentNumber {
853+ }
854+
844855 @ Validated (MyGroup .class )
845856 @ Target ({ElementType .PARAMETER })
846857 @ Retention (RetentionPolicy .RUNTIME )
@@ -850,10 +861,14 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
850861 @ Controller
851862 public static class TestController {
852863
864+ private Date date ;
865+ private Double percent ;
853866 private boolean recordedValidationError ;
854867
855868 @ RequestMapping
856- public void testBind (@ RequestParam @ IsoDate Date date , @ MyValid TestBean bean , BindingResult result ) {
869+ public void testBind (@ RequestParam @ IsoDate Date date , @ RequestParam (required = false ) @ PercentNumber Double percent , @ MyValid TestBean bean , BindingResult result ) {
870+ this .date = date ;
871+ this .percent = percent ;
857872 this .recordedValidationError = (result .getErrorCount () == 1 );
858873 }
859874 }
0 commit comments