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.
29
29
import java .util .List ;
30
30
import java .util .Locale ;
31
31
import java .util .Map ;
32
+
32
33
import javax .servlet .RequestDispatcher ;
33
34
import javax .validation .constraints .NotNull ;
34
35
35
- import com .fasterxml .jackson .databind .DeserializationFeature ;
36
- import com .fasterxml .jackson .databind .MapperFeature ;
37
- import com .fasterxml .jackson .databind .ObjectMapper ;
38
- import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
39
36
import org .hamcrest .Matchers ;
37
+
38
+ import org .joda .time .LocalDate ;
39
+
40
40
import org .junit .Before ;
41
41
import org .junit .Test ;
42
42
52
52
import org .springframework .core .io .ClassPathResource ;
53
53
import org .springframework .format .annotation .DateTimeFormat ;
54
54
import org .springframework .format .annotation .DateTimeFormat .ISO ;
55
+ import org .springframework .format .annotation .NumberFormat ;
55
56
import org .springframework .format .support .FormattingConversionServiceFactoryBean ;
56
57
import org .springframework .http .MediaType ;
57
58
import org .springframework .http .converter .HttpMessageConverter ;
132
133
import org .springframework .web .servlet .view .velocity .VelocityViewResolver ;
133
134
import org .springframework .web .util .UrlPathHelper ;
134
135
136
+ import com .fasterxml .jackson .databind .DeserializationFeature ;
137
+ import com .fasterxml .jackson .databind .MapperFeature ;
138
+ import com .fasterxml .jackson .databind .ObjectMapper ;
139
+ import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
140
+
141
+ import static org .hamcrest .CoreMatchers .*;
135
142
import static org .junit .Assert .*;
136
143
137
144
/**
142
149
* @author Jeremy Grelle
143
150
* @author Brian Clozel
144
151
* @author Sebastien Deleuze
152
+ * @author Kazuki Shimizu
153
+ * @author Sam Brannen
145
154
*/
146
155
public class MvcNamespaceTests {
147
156
148
157
public static final String VIEWCONTROLLER_BEAN_NAME = "org.springframework.web.servlet.config.viewControllerHandlerMapping" ;
149
-
158
+
150
159
private GenericWebApplicationContext appContext ;
151
160
152
161
private TestController handler ;
@@ -165,7 +174,7 @@ public void setUp() throws Exception {
165
174
appContext .getServletContext ().setAttribute (attributeName , appContext );
166
175
167
176
handler = new TestController ();
168
- Method method = TestController .class .getMethod ("testBind" , Date .class , TestBean .class , BindingResult .class );
177
+ Method method = TestController .class .getMethod ("testBind" , Date .class , Double . class , TestBean .class , BindingResult .class );
169
178
handlerMethod = new InvocableHandlerMethod (handler , method );
170
179
}
171
180
@@ -211,6 +220,7 @@ public void testDefaultConfig() throws Exception {
211
220
// default web binding initializer behavior test
212
221
request = new MockHttpServletRequest ("GET" , "/" );
213
222
request .addParameter ("date" , "2009-10-31" );
223
+ request .addParameter ("percent" , "99.99%" );
214
224
MockHttpServletResponse response = new MockHttpServletResponse ();
215
225
216
226
HandlerExecutionChain chain = mapping .getHandler (request );
@@ -222,6 +232,8 @@ public void testDefaultConfig() throws Exception {
222
232
223
233
adapter .handle (request , response , handlerMethod );
224
234
assertTrue (handler .recordedValidationError );
235
+ assertEquals (LocalDate .parse ("2009-10-31" ).toDate (), handler .date );
236
+ assertEquals (Double .valueOf (0.9999 ),handler .percent );
225
237
226
238
CompositeUriComponentsContributor uriComponentsContributor = this .appContext .getBean (
227
239
MvcUriComponentsBuilder .MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME ,
@@ -718,22 +730,22 @@ public void testViewResolution() throws Exception {
718
730
assertEquals (TilesViewResolver .class , resolvers .get (2 ).getClass ());
719
731
720
732
resolver = resolvers .get (3 );
721
- FreeMarkerViewResolver freeMarkerViewResolver = (FreeMarkerViewResolver ) resolver ;
733
+ assertThat ( resolver , instanceOf (FreeMarkerViewResolver . class )) ;
722
734
accessor = new DirectFieldAccessor (resolver );
723
735
assertEquals ("freemarker-" , accessor .getPropertyValue ("prefix" ));
724
736
assertEquals (".freemarker" , accessor .getPropertyValue ("suffix" ));
725
737
assertArrayEquals (new String [] {"my*" , "*Report" }, (String []) accessor .getPropertyValue ("viewNames" ));
726
738
assertEquals (1024 , accessor .getPropertyValue ("cacheLimit" ));
727
739
728
740
resolver = resolvers .get (4 );
729
- VelocityViewResolver velocityViewResolver = (VelocityViewResolver ) resolver ;
741
+ assertThat ( resolver , instanceOf (VelocityViewResolver . class )) ;
730
742
accessor = new DirectFieldAccessor (resolver );
731
743
assertEquals ("" , accessor .getPropertyValue ("prefix" ));
732
744
assertEquals (".vm" , accessor .getPropertyValue ("suffix" ));
733
745
assertEquals (0 , accessor .getPropertyValue ("cacheLimit" ));
734
746
735
747
resolver = resolvers .get (5 );
736
- GroovyMarkupViewResolver groovyMarkupViewResolver = (GroovyMarkupViewResolver ) resolver ;
748
+ assertThat ( resolver , instanceOf (GroovyMarkupViewResolver . class )) ;
737
749
accessor = new DirectFieldAccessor (resolver );
738
750
assertEquals ("" , accessor .getPropertyValue ("prefix" ));
739
751
assertEquals (".tpl" , accessor .getPropertyValue ("suffix" ));
@@ -742,7 +754,6 @@ public void testViewResolution() throws Exception {
742
754
assertEquals (InternalResourceViewResolver .class , resolvers .get (6 ).getClass ());
743
755
assertEquals (InternalResourceViewResolver .class , resolvers .get (7 ).getClass ());
744
756
745
-
746
757
TilesConfigurer tilesConfigurer = appContext .getBean (TilesConfigurer .class );
747
758
assertNotNull (tilesConfigurer );
748
759
String [] definitions = {
@@ -841,6 +852,12 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
841
852
public @interface IsoDate {
842
853
}
843
854
855
+ @ NumberFormat (style = NumberFormat .Style .PERCENT )
856
+ @ Target ({ElementType .PARAMETER })
857
+ @ Retention (RetentionPolicy .RUNTIME )
858
+ public @interface PercentNumber {
859
+ }
860
+
844
861
@ Validated (MyGroup .class )
845
862
@ Target ({ElementType .PARAMETER })
846
863
@ Retention (RetentionPolicy .RUNTIME )
@@ -850,10 +867,14 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
850
867
@ Controller
851
868
public static class TestController {
852
869
870
+ private Date date ;
871
+ private Double percent ;
853
872
private boolean recordedValidationError ;
854
873
855
874
@ RequestMapping
856
- public void testBind (@ RequestParam @ IsoDate Date date , @ MyValid TestBean bean , BindingResult result ) {
875
+ public void testBind (@ RequestParam @ IsoDate Date date , @ RequestParam (required = false ) @ PercentNumber Double percent , @ MyValid TestBean bean , BindingResult result ) {
876
+ this .date = date ;
877
+ this .percent = percent ;
857
878
this .recordedValidationError = (result .getErrorCount () == 1 );
858
879
}
859
880
}
@@ -965,5 +986,4 @@ public Collection<String> getCacheNames() {
965
986
}
966
987
}
967
988
968
-
969
989
}
0 commit comments