30
30
import java .util .Collections ;
31
31
import java .util .Date ;
32
32
import java .util .HashSet ;
33
+ import java .util .Iterator ;
33
34
import java .util .LinkedList ;
34
35
import java .util .List ;
35
36
import java .util .Locale ;
36
37
import java .util .Map ;
37
38
import java .util .Set ;
38
- import java .util .Iterator ;
39
39
import javax .servlet .ServletConfig ;
40
40
import javax .servlet .ServletContext ;
41
41
import javax .servlet .ServletException ;
52
52
import org .springframework .aop .framework .autoproxy .DefaultAdvisorAutoProxyCreator ;
53
53
import org .springframework .aop .interceptor .SimpleTraceInterceptor ;
54
54
import org .springframework .aop .support .DefaultPointcutAdvisor ;
55
+ import org .springframework .beans .BeansException ;
55
56
import org .springframework .beans .DerivedTestBean ;
56
57
import org .springframework .beans .ITestBean ;
57
58
import org .springframework .beans .TestBean ;
58
- import org .springframework .beans .BeansException ;
59
59
import org .springframework .beans .factory .annotation .Autowired ;
60
60
import org .springframework .beans .factory .annotation .Value ;
61
61
import org .springframework .beans .factory .config .PropertyPlaceholderConfigurer ;
62
62
import org .springframework .beans .factory .support .RootBeanDefinition ;
63
63
import org .springframework .beans .propertyeditors .CustomDateEditor ;
64
64
import org .springframework .context .annotation .AnnotationConfigUtils ;
65
65
import org .springframework .core .MethodParameter ;
66
+ import org .springframework .http .HttpHeaders ;
66
67
import org .springframework .http .HttpInputMessage ;
67
68
import org .springframework .http .HttpOutputMessage ;
68
69
import org .springframework .http .HttpStatus ;
69
70
import org .springframework .http .MediaType ;
70
- import org .springframework .http .HttpHeaders ;
71
+ import org .springframework .http .converter . ByteArrayHttpMessageConverter ;
71
72
import org .springframework .http .converter .HttpMessageConverter ;
72
73
import org .springframework .http .converter .HttpMessageNotReadableException ;
73
74
import org .springframework .http .converter .HttpMessageNotWritableException ;
75
+ import org .springframework .http .converter .StringHttpMessageConverter ;
74
76
import org .springframework .mock .web .MockHttpServletRequest ;
75
77
import org .springframework .mock .web .MockHttpServletResponse ;
76
78
import org .springframework .mock .web .MockServletConfig ;
79
81
import org .springframework .ui .ExtendedModelMap ;
80
82
import org .springframework .ui .Model ;
81
83
import org .springframework .ui .ModelMap ;
84
+ import org .springframework .util .MultiValueMap ;
82
85
import org .springframework .util .SerializationTestUtils ;
83
86
import org .springframework .util .StringUtils ;
84
- import org .springframework .util .MultiValueMap ;
85
87
import org .springframework .validation .BindingResult ;
86
88
import org .springframework .validation .Errors ;
87
89
import org .springframework .validation .FieldError ;
@@ -445,7 +447,8 @@ protected WebApplicationContext createWebApplicationContext(WebApplicationContex
445
447
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator ();
446
448
autoProxyCreator .setBeanFactory (wac .getBeanFactory ());
447
449
wac .getBeanFactory ().addBeanPostProcessor (autoProxyCreator );
448
- wac .getBeanFactory ().registerSingleton ("advisor" , new DefaultPointcutAdvisor (new SimpleTraceInterceptor ()));
450
+ wac .getBeanFactory ()
451
+ .registerSingleton ("advisor" , new DefaultPointcutAdvisor (new SimpleTraceInterceptor ()));
449
452
wac .refresh ();
450
453
return wac ;
451
454
}
@@ -639,8 +642,8 @@ protected WebApplicationContext createWebApplicationContext(WebApplicationContex
639
642
servlet .service (request , response );
640
643
assertEquals ("mySurpriseView" , response .getContentAsString ());
641
644
642
- MyParameterDispatchingController deserialized = (MyParameterDispatchingController )
643
- SerializationTestUtils .serializeAndDeserialize (servlet .getWebApplicationContext ().getBean ("controller" ));
645
+ MyParameterDispatchingController deserialized = (MyParameterDispatchingController ) SerializationTestUtils
646
+ .serializeAndDeserialize (servlet .getWebApplicationContext ().getBean ("controller" ));
644
647
assertNotNull (deserialized .request );
645
648
assertNotNull (deserialized .session );
646
649
}
@@ -947,7 +950,23 @@ public void requestBodyResponseBody() throws ServletException, IOException {
947
950
948
951
@ Test
949
952
public void responseBodyNoAcceptableMediaType () throws ServletException , IOException {
950
- initServlet (RequestBodyController .class );
953
+ @ SuppressWarnings ("serial" ) DispatcherServlet servlet = new DispatcherServlet () {
954
+ @ Override
955
+ protected WebApplicationContext createWebApplicationContext (WebApplicationContext parent ) {
956
+ GenericWebApplicationContext wac = new GenericWebApplicationContext ();
957
+ wac .registerBeanDefinition ("controller" , new RootBeanDefinition (RequestBodyController .class ));
958
+ RootBeanDefinition converterDef = new RootBeanDefinition (StringHttpMessageConverter .class );
959
+ converterDef .getPropertyValues ().add ("supportedMediaTypes" , new MediaType ("text" , "plain" ));
960
+ RootBeanDefinition adapterDef = new RootBeanDefinition (AnnotationMethodHandlerAdapter .class );
961
+ StringHttpMessageConverter converter = new StringHttpMessageConverter ();
962
+ converter .setSupportedMediaTypes (Collections .singletonList (new MediaType ("text" , "plain" )));
963
+ adapterDef .getPropertyValues ().add ("messageConverters" , converter );
964
+ wac .registerBeanDefinition ("handlerAdapter" , adapterDef );
965
+ wac .refresh ();
966
+ return wac ;
967
+ }
968
+ };
969
+ servlet .init (new MockServletConfig ());
951
970
952
971
MockHttpServletRequest request = new MockHttpServletRequest ("PUT" , "/something" );
953
972
String requestBody = "Hello World" ;
@@ -975,7 +994,19 @@ public void responseBodyWildCardMediaType() throws ServletException, IOException
975
994
976
995
@ Test
977
996
public void unsupportedRequestBody () throws ServletException , IOException {
978
- initServlet (RequestBodyController .class );
997
+ @ SuppressWarnings ("serial" ) DispatcherServlet servlet = new DispatcherServlet () {
998
+ @ Override
999
+ protected WebApplicationContext createWebApplicationContext (WebApplicationContext parent ) {
1000
+ GenericWebApplicationContext wac = new GenericWebApplicationContext ();
1001
+ wac .registerBeanDefinition ("controller" , new RootBeanDefinition (RequestBodyController .class ));
1002
+ RootBeanDefinition adapterDef = new RootBeanDefinition (AnnotationMethodHandlerAdapter .class );
1003
+ adapterDef .getPropertyValues ().add ("messageConverters" , new ByteArrayHttpMessageConverter ());
1004
+ wac .registerBeanDefinition ("handlerAdapter" , adapterDef );
1005
+ wac .refresh ();
1006
+ return wac ;
1007
+ }
1008
+ };
1009
+ servlet .init (new MockServletConfig ());
979
1010
980
1011
MockHttpServletRequest request = new MockHttpServletRequest ("PUT" , "/something" );
981
1012
String requestBody = "Hello World" ;
@@ -1061,11 +1092,9 @@ public void mavResolver() throws ServletException, IOException {
1061
1092
@ Override
1062
1093
protected WebApplicationContext createWebApplicationContext (WebApplicationContext parent ) {
1063
1094
GenericWebApplicationContext wac = new GenericWebApplicationContext ();
1064
- wac .registerBeanDefinition ("controller" ,
1065
- new RootBeanDefinition (ModelAndViewResolverController .class ));
1095
+ wac .registerBeanDefinition ("controller" , new RootBeanDefinition (ModelAndViewResolverController .class ));
1066
1096
RootBeanDefinition adapterDef = new RootBeanDefinition (AnnotationMethodHandlerAdapter .class );
1067
- adapterDef .getPropertyValues ()
1068
- .add ("customModelAndViewResolver" , new MyModelAndViewResolver ());
1097
+ adapterDef .getPropertyValues ().add ("customModelAndViewResolver" , new MyModelAndViewResolver ());
1069
1098
wac .registerBeanDefinition ("handlerAdapter" , adapterDef );
1070
1099
wac .refresh ();
1071
1100
return wac ;
@@ -1086,7 +1115,7 @@ public void bindingCookieValue() throws ServletException, IOException {
1086
1115
1087
1116
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/test" );
1088
1117
request .setCookies (new Cookie ("date" , "2008-11-18" ));
1089
- MockHttpServletResponse response = new MockHttpServletResponse ();
1118
+ MockHttpServletResponse response = new MockHttpServletResponse ();
1090
1119
servlet .service (request , response );
1091
1120
assertEquals ("test-108" , response .getContentAsString ());
1092
1121
}
@@ -1096,7 +1125,7 @@ public void ambiguousParams() throws ServletException, IOException {
1096
1125
initServlet (AmbiguousParamsController .class );
1097
1126
1098
1127
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/test" );
1099
- MockHttpServletResponse response = new MockHttpServletResponse ();
1128
+ MockHttpServletResponse response = new MockHttpServletResponse ();
1100
1129
servlet .service (request , response );
1101
1130
assertEquals ("noParams" , response .getContentAsString ());
1102
1131
@@ -1116,7 +1145,6 @@ public void bridgeMethods() throws Exception {
1116
1145
servlet .service (request , response );
1117
1146
}
1118
1147
1119
-
1120
1148
@ Test
1121
1149
public void requestParamMap () throws Exception {
1122
1150
initServlet (RequestParamMapController .class );
@@ -1229,11 +1257,9 @@ protected WebApplicationContext createWebApplicationContext(WebApplicationContex
1229
1257
assertEquals ("create" , response .getContentAsString ());
1230
1258
}
1231
1259
1232
-
1233
-
1234
1260
/*
1235
- * Controllers
1236
- */
1261
+ * Controllers
1262
+ */
1237
1263
1238
1264
@ RequestMapping ("/myPath.do" )
1239
1265
private static class MyController extends AbstractController {
@@ -1470,7 +1496,8 @@ private static class MyCommandProvidingFormController<T, TB, TB2> extends MyForm
1470
1496
@ SuppressWarnings ("unused" )
1471
1497
@ ModelAttribute ("myCommand" )
1472
1498
private ValidTestBean createTestBean (@ RequestParam T defaultName ,
1473
- Map <String , Object > model , @ RequestParam Date date ) {
1499
+ Map <String , Object > model ,
1500
+ @ RequestParam Date date ) {
1474
1501
model .put ("myKey" , "myOriginalValue" );
1475
1502
ValidTestBean tb = new ValidTestBean ();
1476
1503
tb .setName (defaultName .getClass ().getSimpleName () + ":" + defaultName .toString ());
@@ -1740,9 +1767,10 @@ public void render(Map model, HttpServletRequest request, HttpServletResponse re
1740
1767
}
1741
1768
List <TestBean > testBeans = (List <TestBean >) model .get ("testBeanList" );
1742
1769
if (errors .hasFieldErrors ("age" )) {
1743
- response .getWriter ().write (viewName + "-" + tb .getName () + "-" +
1744
- errors .getFieldError ("age" ).getCode () + "-" + testBeans .get (0 ).getName () + "-" +
1745
- model .get ("myKey" ) + (model .containsKey ("yourKey" ) ? "-" + model .get ("yourKey" ) : "" ));
1770
+ response .getWriter ()
1771
+ .write (viewName + "-" + tb .getName () + "-" + errors .getFieldError ("age" ).getCode () +
1772
+ "-" + testBeans .get (0 ).getName () + "-" + model .get ("myKey" ) +
1773
+ (model .containsKey ("yourKey" ) ? "-" + model .get ("yourKey" ) : "" ));
1746
1774
}
1747
1775
else {
1748
1776
response .getWriter ().write (viewName + "-" + tb .getName () + "-" + tb .getAge () + "-" +
@@ -1760,6 +1788,7 @@ public View resolveViewName(String viewName, Locale locale) throws Exception {
1760
1788
public String getContentType () {
1761
1789
return null ;
1762
1790
}
1791
+
1763
1792
public void render (Map <String , ?> model , HttpServletRequest request , HttpServletResponse response ) {
1764
1793
request .getSession ().setAttribute ("model" , model );
1765
1794
}
@@ -1787,6 +1816,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp, @RequestPara
1787
1816
@ Retention (RetentionPolicy .RUNTIME )
1788
1817
@ Controller
1789
1818
public @interface MyControllerAnnotation {
1819
+
1790
1820
}
1791
1821
1792
1822
@ MyControllerAnnotation
@@ -1835,8 +1865,8 @@ public static class DefaultExpressionValueParamController {
1835
1865
@ RequestMapping ("/myPath.do" )
1836
1866
public void myHandle (@ RequestParam (value = "id" , defaultValue = "${myKey}" ) String id ,
1837
1867
@ RequestHeader (defaultValue = "#{systemProperties.myHeader}" ) String header ,
1838
- @ Value ("#{request.contextPath}" ) String contextPath , HttpServletResponse response )
1839
- throws IOException {
1868
+ @ Value ("#{request.contextPath}" ) String contextPath ,
1869
+ HttpServletResponse response ) throws IOException {
1840
1870
response .getWriter ().write (String .valueOf (id ) + "-" + String .valueOf (header ) + "-" + contextPath );
1841
1871
}
1842
1872
}
@@ -1873,7 +1903,6 @@ public void get() {
1873
1903
}
1874
1904
}
1875
1905
1876
-
1877
1906
@ Controller
1878
1907
public static class PathOrderingController {
1879
1908
@@ -1888,7 +1917,6 @@ public void method2(Writer writer) throws IOException {
1888
1917
}
1889
1918
}
1890
1919
1891
-
1892
1920
@ Controller
1893
1921
public static class RequestBodyController {
1894
1922
@@ -1901,7 +1929,11 @@ public String handle(@RequestBody String body) throws IOException {
1901
1929
1902
1930
public static class MyMessageConverter implements HttpMessageConverter {
1903
1931
1904
- public boolean supports (Class clazz ) {
1932
+ public boolean canRead (Class clazz , MediaType mediaType ) {
1933
+ return true ;
1934
+ }
1935
+
1936
+ public boolean canWrite (Class clazz , MediaType mediaType ) {
1905
1937
return true ;
1906
1938
}
1907
1939
@@ -1914,7 +1946,7 @@ public Object read(Class clazz, HttpInputMessage inputMessage)
1914
1946
throw new HttpMessageNotReadableException ("Could not read" );
1915
1947
}
1916
1948
1917
- public void write (Object o , HttpOutputMessage outputMessage )
1949
+ public void write (Object o , MediaType contentType , HttpOutputMessage outputMessage )
1918
1950
throws IOException , HttpMessageNotWritableException {
1919
1951
throw new UnsupportedOperationException ("Not implemented" );
1920
1952
}
@@ -1955,8 +1987,11 @@ public MySpecialArg handle() {
1955
1987
1956
1988
public static class MyModelAndViewResolver implements ModelAndViewResolver {
1957
1989
1958
- public ModelAndView resolveModelAndView (Method handlerMethod , Class handlerType ,
1959
- Object returnValue , ExtendedModelMap implicitModel , NativeWebRequest webRequest ) {
1990
+ public ModelAndView resolveModelAndView (Method handlerMethod ,
1991
+ Class handlerType ,
1992
+ Object returnValue ,
1993
+ ExtendedModelMap implicitModel ,
1994
+ NativeWebRequest webRequest ) {
1960
1995
if (returnValue instanceof MySpecialArg ) {
1961
1996
return new ModelAndView (new View () {
1962
1997
public String getContentType () {
@@ -2002,8 +2037,7 @@ public void initBinder(WebDataBinder binder) {
2002
2037
}
2003
2038
2004
2039
@ RequestMapping (method = RequestMethod .GET )
2005
- public void handle (@ CookieValue ("date" ) Date date , Writer writer )
2006
- throws IOException {
2040
+ public void handle (@ CookieValue ("date" ) Date date , Writer writer ) throws IOException {
2007
2041
assertEquals ("Invalid path variable value" , new Date (108 , 10 , 18 ), date );
2008
2042
writer .write ("test-" + date .getYear ());
2009
2043
}
@@ -2043,7 +2077,8 @@ public void map(@RequestParam Map<String, String> params, Writer writer) throws
2043
2077
}
2044
2078
2045
2079
@ RequestMapping ("/multiValueMap" )
2046
- public void multiValueMap (@ RequestParam MultiValueMap <String , String > params , Writer writer ) throws IOException {
2080
+ public void multiValueMap (@ RequestParam MultiValueMap <String , String > params , Writer writer )
2081
+ throws IOException {
2047
2082
for (Iterator <Map .Entry <String , List <String >>> it1 = params .entrySet ().iterator (); it1 .hasNext ();) {
2048
2083
Map .Entry <String , List <String >> entry = it1 .next ();
2049
2084
writer .write (entry .getKey () + "=[" );
@@ -2078,7 +2113,8 @@ public void map(@RequestHeader Map<String, String> headers, Writer writer) throw
2078
2113
}
2079
2114
2080
2115
@ RequestMapping ("/multiValueMap" )
2081
- public void multiValueMap (@ RequestHeader MultiValueMap <String , String > headers , Writer writer ) throws IOException {
2116
+ public void multiValueMap (@ RequestHeader MultiValueMap <String , String > headers , Writer writer )
2117
+ throws IOException {
2082
2118
for (Iterator <Map .Entry <String , List <String >>> it1 = headers .entrySet ().iterator (); it1 .hasNext ();) {
2083
2119
Map .Entry <String , List <String >> entry = it1 .next ();
2084
2120
writer .write (entry .getKey () + "=[" );
@@ -2104,5 +2140,5 @@ public void httpHeaders(@RequestHeader HttpHeaders headers, Writer writer) throw
2104
2140
2105
2141
}
2106
2142
2107
-
2143
+
2108
2144
}
0 commit comments