1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
37
37
import com .fasterxml .jackson .databind .ObjectMapper ;
38
38
import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
39
39
import org .hamcrest .Matchers ;
40
+ import org .joda .time .LocalDate ;
40
41
import org .junit .Before ;
41
42
import org .junit .Test ;
42
43
52
53
import org .springframework .core .io .ClassPathResource ;
53
54
import org .springframework .format .annotation .DateTimeFormat ;
54
55
import org .springframework .format .annotation .DateTimeFormat .ISO ;
56
+ import org .springframework .format .annotation .NumberFormat ;
55
57
import org .springframework .format .support .FormattingConversionServiceFactoryBean ;
56
58
import org .springframework .http .MediaType ;
57
59
import org .springframework .http .converter .HttpMessageConverter ;
@@ -165,7 +167,7 @@ public void setUp() throws Exception {
165
167
appContext .getServletContext ().setAttribute (attributeName , appContext );
166
168
167
169
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 );
169
171
handlerMethod = new InvocableHandlerMethod (handler , method );
170
172
}
171
173
@@ -211,6 +213,7 @@ public void testDefaultConfig() throws Exception {
211
213
// default web binding initializer behavior test
212
214
request = new MockHttpServletRequest ("GET" , "/" );
213
215
request .addParameter ("date" , "2009-10-31" );
216
+ request .addParameter ("percent" , "99.99%" );
214
217
MockHttpServletResponse response = new MockHttpServletResponse ();
215
218
216
219
HandlerExecutionChain chain = mapping .getHandler (request );
@@ -222,6 +225,8 @@ public void testDefaultConfig() throws Exception {
222
225
223
226
adapter .handle (request , response , handlerMethod );
224
227
assertTrue (handler .recordedValidationError );
228
+ assertEquals (LocalDate .parse ("2009-10-31" ).toDate (), handler .date );
229
+ assertEquals (Double .valueOf (0.9999 ),handler .percent );
225
230
226
231
CompositeUriComponentsContributor uriComponentsContributor = this .appContext .getBean (
227
232
MvcUriComponentsBuilder .MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME ,
@@ -841,6 +846,12 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
841
846
public @interface IsoDate {
842
847
}
843
848
849
+ @ NumberFormat (style = NumberFormat .Style .PERCENT )
850
+ @ Target ({ElementType .PARAMETER })
851
+ @ Retention (RetentionPolicy .RUNTIME )
852
+ public @interface PercentNumber {
853
+ }
854
+
844
855
@ Validated (MyGroup .class )
845
856
@ Target ({ElementType .PARAMETER })
846
857
@ Retention (RetentionPolicy .RUNTIME )
@@ -850,10 +861,14 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
850
861
@ Controller
851
862
public static class TestController {
852
863
864
+ private Date date ;
865
+ private Double percent ;
853
866
private boolean recordedValidationError ;
854
867
855
868
@ 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 ;
857
872
this .recordedValidationError = (result .getErrorCount () == 1 );
858
873
}
859
874
}
0 commit comments