2020import static org .junit .Assert .assertNotNull ;
2121
2222import java .text .SimpleDateFormat ;
23- import java .util .ArrayList ;
2423import java .util .Arrays ;
2524import java .util .Collection ;
2625import java .util .Date ;
27- import java .util .List ;
28- import java .util .Map ;
2926
3027import org .junit .Test ;
3128import org .junit .runner .RunWith ;
3633import org .springframework .aop .support .DefaultPointcutAdvisor ;
3734import org .springframework .beans .factory .support .RootBeanDefinition ;
3835import org .springframework .beans .propertyeditors .CustomDateEditor ;
39- import org .springframework .http .converter .HttpMessageConverter ;
40- import org .springframework .http .converter .json .MappingJacksonHttpMessageConverter ;
4136import org .springframework .mock .web .MockHttpServletRequest ;
4237import org .springframework .mock .web .MockHttpServletResponse ;
4338import org .springframework .stereotype .Controller ;
5348import org .springframework .web .bind .annotation .ResponseBody ;
5449import org .springframework .web .context .support .GenericWebApplicationContext ;
5550import org .springframework .web .servlet .HandlerExecutionChain ;
51+ import org .springframework .web .servlet .ModelAndView ;
5652
5753/**
5854 * Test various scenarios for detecting method-level and method parameter annotations depending
@@ -90,13 +86,10 @@ public static Collection<Object[]> handlerTypes() {
9086 }
9187
9288 private RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping ();
93- // private DefaultAnnotationHandlerMapping handlerMapping = new DefaultAnnotationHandlerMapping();
9489
9590 private RequestMappingHandlerAdapter handlerAdapter = new RequestMappingHandlerAdapter ();
96- // AnnotationMethodHandlerAdapter handlerAdapter = new AnnotationMethodHandlerAdapter();
9791
9892 private ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver ();
99- // private AnnotationMethodHandlerExceptionResolver exceptionResolver = new AnnotationMethodHandlerExceptionResolver();
10093
10194 public HandlerMethodAnnotationDetectionTests (Class <?> controllerType , boolean useAutoProxy ) {
10295 GenericWebApplicationContext context = new GenericWebApplicationContext ();
@@ -105,45 +98,39 @@ public HandlerMethodAnnotationDetectionTests(Class<?> controllerType, boolean us
10598 DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator ();
10699 autoProxyCreator .setBeanFactory (context .getBeanFactory ());
107100 context .getBeanFactory ().addBeanPostProcessor (autoProxyCreator );
108- context .getBeanFactory ().registerSingleton ("advsr " , new DefaultPointcutAdvisor (new SimpleTraceInterceptor ()));
101+ context .getBeanFactory ().registerSingleton ("advisor " , new DefaultPointcutAdvisor (new SimpleTraceInterceptor ()));
109102 }
110103 context .refresh ();
111104
112105 handlerMapping .setApplicationContext (context );
113-
114- List <HttpMessageConverter <?>> messageConverters = new ArrayList <HttpMessageConverter <?>>();
115- messageConverters .add (new MappingJacksonHttpMessageConverter ());
116-
117- handlerAdapter .setMessageConverters (messageConverters );
118106 handlerAdapter .afterPropertiesSet ();
119- // handlerAdapter.setMessageConverters(messageConverters.toArray(new HttpMessageConverter<?>[messageConverters.size()]));
120- // handlerAdapter.setApplicationContext(context);
121-
122- exceptionResolver .setMessageConverters (messageConverters );
123107 exceptionResolver .afterPropertiesSet ();
124- // exceptionResolver.setMessageConverters(messageConverters.toArray(new HttpMessageConverter<?>[messageConverters.size()]));
125108 }
126109
127110 @ Test
128111 public void testRequestMappingMethod () throws Exception {
112+ String datePattern = "MM:dd:yyyy" ;
113+ SimpleDateFormat dateFormat = new SimpleDateFormat (datePattern );
114+ String dateA = "11:01:2011" ;
115+ String dateB = "11:02:2011" ;
116+
129117 MockHttpServletRequest request = new MockHttpServletRequest ("POST" , "/path1/path2" );
130- request .setParameter ("datePattern" , "MM:dd:yyyy" );
131- request .addHeader ("dateA" , "11:01:2011" );
132- request .addHeader ("dateB" , "11:02:2011" );
133- request .addHeader ("Accept" , "application/json" );
118+ request .setParameter ("datePattern" , datePattern );
119+ request .addHeader ("header1" , dateA );
120+ request .addHeader ("header2" , dateB );
134121
135122 HandlerExecutionChain chain = handlerMapping .getHandler (request );
136123 assertNotNull (chain );
137124
138- MockHttpServletResponse response = new MockHttpServletResponse ();
139- handlerAdapter .handle (request , response , chain .getHandler ());
140- assertEquals ("application/json" , response .getHeader ("Content-Type" ));
141- assertEquals ("{\" dateA\" :1320105600000,\" dateB\" :1320192000000}" , response .getContentAsString ());
125+ ModelAndView mav = handlerAdapter .handle (request , new MockHttpServletResponse (), chain .getHandler ());
142126
143- response = new MockHttpServletResponse ();
127+ assertEquals (mav .getModel ().get ("attr1" ), dateFormat .parse (dateA ));
128+ assertEquals (mav .getModel ().get ("attr2" ), dateFormat .parse (dateB ));
129+
130+ MockHttpServletResponse response = new MockHttpServletResponse ();
144131 exceptionResolver .resolveException (request , response , chain .getHandler (), new Exception ("failure" ));
145- assertEquals ("application/json " , response .getHeader ("Content-Type" ));
146- assertEquals ("\" failure\" " , response .getContentAsString ());
132+ assertEquals ("text/plain;charset=ISO-8859-1 " , response .getHeader ("Content-Type" ));
133+ assertEquals ("failure" , response .getContentAsString ());
147134 }
148135
149136
@@ -154,21 +141,20 @@ public void testRequestMappingMethod() throws Exception {
154141 static class SimpleController {
155142
156143 @ InitBinder
157- public void initBinder (WebDataBinder dataBinder , @ RequestParam ("datePattern" ) String thePattern ) {
158- CustomDateEditor dateEditor = new CustomDateEditor (new SimpleDateFormat (thePattern ), false );
144+ public void initBinder (WebDataBinder dataBinder , @ RequestParam ("datePattern" ) String pattern ) {
145+ CustomDateEditor dateEditor = new CustomDateEditor (new SimpleDateFormat (pattern ), false );
159146 dataBinder .registerCustomEditor (Date .class , dateEditor );
160147 }
161148
162149 @ ModelAttribute
163- public void initModel (@ RequestHeader ("dateA " ) Date date , Model model ) {
164- model .addAttribute ("dateA " , date );
150+ public void initModel (@ RequestHeader ("header1 " ) Date date , Model model ) {
151+ model .addAttribute ("attr1 " , date );
165152 }
166153
167- @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST , produces ="application/json" )
168- @ ResponseBody
169- public Map <String , Object > handle (@ RequestHeader ("dateB" ) Date date , Model model ) throws Exception {
170- model .addAttribute ("dateB" , date );
171- return model .asMap ();
154+ @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST )
155+ @ ModelAttribute ("attr2" )
156+ public Date handle (@ RequestHeader ("header2" ) Date date ) throws Exception {
157+ return date ;
172158 }
173159
174160 @ ExceptionHandler (Exception .class )
@@ -183,14 +169,14 @@ public String handleException(Exception exception) {
183169 static abstract class MappingAbstractClass {
184170
185171 @ InitBinder
186- public abstract void initBinder (WebDataBinder dataBinder , String thePattern );
172+ public abstract void initBinder (WebDataBinder dataBinder , String pattern );
187173
188174 @ ModelAttribute
189175 public abstract void initModel (Date date , Model model );
190176
191- @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST , produces = "application/json" )
192- @ ResponseBody
193- public abstract Map < String , Object > handle (Date date , Model model ) throws Exception ;
177+ @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST )
178+ @ ModelAttribute ( "attr2" )
179+ public abstract Date handle (Date date , Model model ) throws Exception ;
194180
195181 @ ExceptionHandler (Exception .class )
196182 @ ResponseBody
@@ -204,18 +190,17 @@ static abstract class MappingAbstractClass {
204190 */
205191 static class AbstractClassController extends MappingAbstractClass {
206192
207- public void initBinder (WebDataBinder dataBinder , @ RequestParam ("datePattern" ) String thePattern ) {
208- CustomDateEditor dateEditor = new CustomDateEditor (new SimpleDateFormat (thePattern ), false );
193+ public void initBinder (WebDataBinder dataBinder , @ RequestParam ("datePattern" ) String pattern ) {
194+ CustomDateEditor dateEditor = new CustomDateEditor (new SimpleDateFormat (pattern ), false );
209195 dataBinder .registerCustomEditor (Date .class , dateEditor );
210196 }
211197
212- public void initModel (@ RequestHeader ("dateA " ) Date date , Model model ) {
213- model .addAttribute ("dateA " , date );
198+ public void initModel (@ RequestHeader ("header1 " ) Date date , Model model ) {
199+ model .addAttribute ("attr1 " , date );
214200 }
215201
216- public Map <String , Object > handle (@ RequestHeader ("dateB" ) Date date , Model model ) throws Exception {
217- model .addAttribute ("dateB" , date );
218- return model .asMap ();
202+ public Date handle (@ RequestHeader ("header2" ) Date date , Model model ) throws Exception {
203+ return date ;
219204 }
220205
221206 public String handleException (Exception exception ) {
@@ -231,11 +216,11 @@ static interface MappingInterface {
231216 void initBinder (WebDataBinder dataBinder , @ RequestParam ("datePattern" ) String thePattern );
232217
233218 @ ModelAttribute
234- void initModel (@ RequestHeader ("dateA " ) Date date , Model model );
219+ void initModel (@ RequestHeader ("header1 " ) Date date , Model model );
235220
236- @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST , produces = "application/json" )
237- @ ResponseBody
238- Map < String , Object > handle (@ RequestHeader ("dateB " ) Date date , Model model ) throws Exception ;
221+ @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST )
222+ @ ModelAttribute ( "attr2" )
223+ Date handle (@ RequestHeader ("header2 " ) Date date , Model model ) throws Exception ;
239224
240225 @ ExceptionHandler (Exception .class )
241226 @ ResponseBody
@@ -258,13 +243,12 @@ public void initBinder(WebDataBinder dataBinder, @RequestParam("datePattern") St
258243 dataBinder .registerCustomEditor (Date .class , dateEditor );
259244 }
260245
261- public void initModel (@ RequestHeader ("dateA " ) Date date , Model model ) {
262- model .addAttribute ("dateA " , date );
246+ public void initModel (@ RequestHeader ("header1 " ) Date date , Model model ) {
247+ model .addAttribute ("attr1 " , date );
263248 }
264249
265- public Map <String , Object > handle (@ RequestHeader ("dateB" ) Date date , Model model ) throws Exception {
266- model .addAttribute ("dateB" , date );
267- return model .asMap ();
250+ public Date handle (@ RequestHeader ("header2" ) Date date , Model model ) throws Exception {
251+ return date ;
268252 }
269253
270254 public String handleException (Exception exception ) {
@@ -282,9 +266,9 @@ static abstract class MappingParameterizedAbstractClass<A, B, C> {
282266 @ ModelAttribute
283267 public abstract void initModel (B date , Model model );
284268
285- @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST , produces = "application/json" )
286- @ ResponseBody
287- public abstract Map < String , Object > handle (C date , Model model ) throws Exception ;
269+ @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST )
270+ @ ModelAttribute ( "attr2" )
271+ public abstract Date handle (C date , Model model ) throws Exception ;
288272
289273 @ ExceptionHandler (Exception .class )
290274 @ ResponseBody
@@ -303,13 +287,12 @@ public void initBinder(WebDataBinder dataBinder, @RequestParam("datePattern") St
303287 dataBinder .registerCustomEditor (Date .class , dateEditor );
304288 }
305289
306- public void initModel (@ RequestHeader ("dateA " ) Date date , Model model ) {
307- model .addAttribute ("dateA " , date );
290+ public void initModel (@ RequestHeader ("header1 " ) Date date , Model model ) {
291+ model .addAttribute ("attr1 " , date );
308292 }
309293
310- public Map <String , Object > handle (@ RequestHeader ("dateB" ) Date date , Model model ) throws Exception {
311- model .addAttribute ("dateB" , date );
312- return model .asMap ();
294+ public Date handle (@ RequestHeader ("header2" ) Date date , Model model ) throws Exception {
295+ return date ;
313296 }
314297
315298 public String handleException (Exception exception ) {
@@ -327,9 +310,9 @@ static interface MappingParameterizedInterface<A, B, C> {
327310 @ ModelAttribute
328311 void initModel (B date , Model model );
329312
330- @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST , produces = "application/json" )
331- @ ResponseBody
332- Map < String , Object > handle (C date , Model model ) throws Exception ;
313+ @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST )
314+ @ ModelAttribute ( "attr2" )
315+ Date handle (C date , Model model ) throws Exception ;
333316
334317 @ ExceptionHandler (Exception .class )
335318 @ ResponseBody
@@ -352,15 +335,14 @@ public void initBinder(WebDataBinder dataBinder, @RequestParam("datePattern") St
352335 }
353336
354337 @ ModelAttribute
355- public void initModel (@ RequestHeader ("dateA " ) Date date , Model model ) {
356- model .addAttribute ("dateA " , date );
338+ public void initModel (@ RequestHeader ("header1 " ) Date date , Model model ) {
339+ model .addAttribute ("attr1 " , date );
357340 }
358341
359- @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST , produces ="application/json" )
360- @ ResponseBody
361- public Map <String , Object > handle (@ RequestHeader ("dateB" ) Date date , Model model ) throws Exception {
362- model .addAttribute ("dateB" , date );
363- return model .asMap ();
342+ @ RequestMapping (value ="/path1/path2" , method =RequestMethod .POST )
343+ @ ModelAttribute ("attr2" )
344+ public Date handle (@ RequestHeader ("header2" ) Date date , Model model ) throws Exception {
345+ return date ;
364346 }
365347
366348 @ ExceptionHandler (Exception .class )
@@ -386,15 +368,14 @@ public void initBinder(WebDataBinder dataBinder, @RequestParam("datePattern") St
386368 }
387369
388370 @ ModelAttribute
389- public void initModel (@ RequestHeader ("dateA " ) Date date , Model model ) {
390- model .addAttribute ("dateA " , date );
371+ public void initModel (@ RequestHeader ("header1 " ) Date date , Model model ) {
372+ model .addAttribute ("attr1 " , date );
391373 }
392374
393- @ ResponseBody
394- @ RequestMapping (value ="/path2" , method =RequestMethod .POST , produces ="application/json" )
395- public Map <String , Object > handle (@ RequestHeader ("dateB" ) Date date , Model model ) throws Exception {
396- model .addAttribute ("dateB" , date );
397- return model .asMap ();
375+ @ RequestMapping (value ="/path2" , method =RequestMethod .POST )
376+ @ ModelAttribute ("attr2" )
377+ public Date handle (@ RequestHeader ("header2" ) Date date , Model model ) throws Exception {
378+ return date ;
398379 }
399380
400381 @ ExceptionHandler (Exception .class )
0 commit comments