private HandlerResult handle(RequestMappingHandlerAdapter adapter, Object controller, String methodName) throws Exception { Method method = controller.getClass().getMethod(methodName); HandlerMethod handlerMethod = new HandlerMethod(controller, method); return adapter.handle(this.exchange, handlerMethod).block(Duration.ZERO); }
@Before public void setup() throws Exception { TestController testController = new TestController(); this.fooMethod = new HandlerMethod(testController, "foo"); this.fooParamMethod = new HandlerMethod(testController, "fooParam"); this.barMethod = new HandlerMethod(testController, "bar"); this.emptyMethod = new HandlerMethod(testController, "empty"); this.handlerMapping = new TestRequestMappingInfoHandlerMapping(); this.handlerMapping.registerHandler(testController); this.handlerMapping.setRemoveSemicolonContent(false); }
private HandlerResult getHandlerResult(Object controller, Method method) { HandlerMethod handlerMethod = new HandlerMethod(controller, method); return new HandlerResult(handlerMethod, null, handlerMethod.getReturnType()); }
@Test public void printHandlerMethod() throws Exception { HandlerMethod handlerMethod = new HandlerMethod(this, "handle"); this.mvcResult.setHandler(handlerMethod); this.handler.handle(mvcResult); assertValue("Handler", "Type", this.getClass().getName()); assertValue("Handler", "Method", handlerMethod); }
@Test // SPR-13631 public void defaultCharset() throws Exception { Method method = JacksonController.class.getMethod("defaultCharset"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); Object returnValue = new JacksonController().defaultCharset(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); assertEquals("UTF-8", this.servletResponse.getCharacterEncoding()); }
@Test public void resolveNoExceptionHandlerForException() throws NoSuchMethodException { Exception npe = new NullPointerException(); HandlerMethod handlerMethod = new HandlerMethod(new IoExceptionController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, npe); assertNull("NPE should not have been handled", mav); }
@Test // SPR-9964 public void parameterizedMethodArgument() throws Exception { Method method = AbstractParameterizedController.class.getMethod("handleDto", Identifiable.class); HandlerMethod handlerMethod = new HandlerMethod(new ConcreteParameterizedController(), method); MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; SimpleBean simpleBean = resolveValue(methodParam, "{\"name\" : \"Jad\"}"); assertEquals("Jad", simpleBean.getName()); }
@Test public void resolveExceptionGlobalHandler() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalAccessException ex = new IllegalAccessException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("AnotherTestExceptionResolver: IllegalAccessException", this.response.getContentAsString()); }
@Test // SPR-12605 public void resolveExceptionWithHandlerMethodArg() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); ArrayIndexOutOfBoundsException ex = new ArrayIndexOutOfBoundsException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("HandlerMethod: handle", this.response.getContentAsString()); }
@Test public void resolveExceptionGlobalHandlerOrdered() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("TestExceptionResolver: IllegalStateException", this.response.getContentAsString()); }
@Test public void resolveExceptionControllerAdviceHandler() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyControllerAdviceConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("BasePackageTestExceptionResolver: IllegalStateException", this.response.getContentAsString()); }
@Test // SPR-16496 public void resolveExceptionControllerAdviceAgainstProxy() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyControllerAdviceConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(); HandlerMethod handlerMethod = new HandlerMethod(new ProxyFactory(new ResponseBodyController()).getProxy(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("BasePackageTestExceptionResolver: IllegalStateException", this.response.getContentAsString()); }
@Test public void resolveExceptionModelAndView() throws NoSuchMethodException { IllegalArgumentException ex = new IllegalArgumentException("Bad argument"); HandlerMethod handlerMethod = new HandlerMethod(new ModelAndViewController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertFalse(mav.isEmpty()); assertEquals("errorView", mav.getViewName()); assertEquals("Bad argument", mav.getModel().get("detail")); }
@Test public void resolveExceptionResponseWriter() throws Exception { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseWriterController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertTrue(mav.isEmpty()); assertEquals("IllegalArgumentException", this.response.getContentAsString()); }
@Test public void resolveExceptionResponseBody() throws UnsupportedEncodingException, NoSuchMethodException { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertTrue(mav.isEmpty()); assertEquals("IllegalArgumentException", this.response.getContentAsString()); }
@Test public void getNameExplicit() { Method method = ClassUtils.getMethod(TestController.class, "handle"); HandlerMethod handlerMethod = new HandlerMethod(new TestController(), method); RequestMappingInfo rmi = new RequestMappingInfo("foo", null, null, null, null, null, null, null); HandlerMethodMappingNamingStrategy<RequestMappingInfo> strategy = new RequestMappingInfoHandlerMethodMappingNamingStrategy(); assertEquals("foo", strategy.getName(handlerMethod, rmi)); }
@Test public void getNameConvention() { Method method = ClassUtils.getMethod(TestController.class, "handle"); HandlerMethod handlerMethod = new HandlerMethod(new TestController(), method); RequestMappingInfo rmi = new RequestMappingInfo(null, null, null, null, null, null, null, null); HandlerMethodMappingNamingStrategy<RequestMappingInfo> strategy = new RequestMappingInfoHandlerMethodMappingNamingStrategy(); assertEquals("TC#handle", strategy.getName(handlerMethod, rmi)); }
@Test // SPR-14651 public void resolveRedirectAttributesAtArgument() throws Exception { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new RedirectAttributesController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertEquals("redirect:/", mav.getViewName()); FlashMap flashMap = (FlashMap) this.request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE); assertNotNull("output FlashMap should exist", flashMap); assertEquals("IllegalArgumentException", flashMap.get("exceptionClassName")); }
@Test // SPR-13546 public void resolveExceptionModelAtArgument() throws Exception { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new ModelArgumentController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertEquals(1, mav.getModelMap().size()); assertEquals("IllegalArgumentException", mav.getModelMap().get("exceptionClassName")); }
@Test public void unregisterMapping() throws Exception { String key = "foo"; HandlerMethod handlerMethod = new HandlerMethod(this.handler, this.method1); this.mapping.registerMapping(key, this.handler, this.method1); assertNotNull(this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key))); this.mapping.unregisterMapping(key); assertNull(mapping.getHandlerInternal(new MockHttpServletRequest("GET", key))); assertNull(this.mapping.getMappingRegistry().getMappingsByUrl(key)); assertNull(this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(this.method1.getName())); assertNull(this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod)); }