UiFrameworkUtil { public static Object[] determineControllerMethodParameters(Object controller, Method method, Map<Class<?>, Object> argumentsByType, ConversionService conversionService, ApplicationContext applicationContext) throws RequestValidationException { Class<?>[] types = method.getParameterTypes(); int numParams = types.length; Object[] ret = new Object[numParams]; for (int i = 0; i < numParams; ++i) { Object result = determineArgumentValue(controller, argumentsByType, new MethodParameter(method, i), conversionService, applicationContext); if (result instanceof BindingResult) { ret[i] = ((BindingResult) result).getTarget(); if ((i + 1 < numParams) && Errors.class.isAssignableFrom(types[i + 1])) { ret[i + 1] = result; i += 1; } } else { ret[i] = result; } } return ret; }  static Object executeControllerMethod(Object controller, String httpRequestMethod, Map<Class<?>, Object> possibleArguments,
                                                 ConversionService conversionService, ApplicationContext applicationContext); static Object invokeMethodWithArguments(Object target, Method method, Map<Class<?>, Object> possibleArguments,
                                                   ConversionService conversionService, ApplicationContext applicationContext); static Object[] determineControllerMethodParameters(Object controller, Method method, Map<Class<?>, Object> argumentsByType,
                                                               ConversionService conversionService, ApplicationContext applicationContext); static Object determineArgumentValue(Object controller, Map<Class<?>, Object> valuesByType, MethodParameter methodParam,
                                                ConversionService conversionService, ApplicationContext applicationContext); @SuppressWarnings("unchecked") static T findCause(Throwable t, Class<T> lookFor); static String getMessage(MessageSourceResolvable msg); static boolean checkAndSetDevelopmentModeForProvider(String key, Object provider); static DateFormat getDateFormat(AdministrationService administrationService, Locale locale); static DateFormat getDateTimeFormat(AdministrationService administrationService, Locale locale); static DateFormat getTimeFormat(AdministrationService administrationService, Locale locale);  }
@Test public void test_determineControllerMethodParameters_requestParamCollection() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.addParameter("properties", new String[] { "name", "description" }); req.addParameter("number", "5"); Map<Class<?>, Object> argumentsByType = new HashMap<Class<?>, Object>(); argumentsByType.put(HttpServletRequest.class, req); Method method = MockController.class.getMethod("controller", String[].class, Integer.class); Object[] temp = UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); String[] props = (String[]) temp[0]; Assert.assertNotNull(props); Assert.assertEquals(2, props.length); Assert.assertEquals("name", props[0]); Assert.assertEquals("description", props[1]); Integer number = (Integer) temp[1]; Assert.assertNotNull(number); Assert.assertEquals(Integer.valueOf(5), number); }
@Test public void test_determineControllerMethodParameters_requestParamList() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.addParameter("numbers", new String[] { "1", "2", "3" }); Map<Class<?>, Object> argumentsByType = new HashMap<Class<?>, Object>(); argumentsByType.put(HttpServletRequest.class, req); Method method = MockController.class.getMethod("integerList", List.class); Object[] temp = UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); @SuppressWarnings("unchecked") List<Integer> list = (List<Integer>) temp[0]; Assert.assertNotNull(list); Assert.assertEquals(3, list.size()); for (int i = 0; i < 3; ++i) Assert.assertEquals(Integer.valueOf(i + 1), list.get(i)); }
@Test public void test_determineControllerMethodParameters_requestParamRequired() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); Map<Class<?>, Object> argumentsByType = new HashMap<Class<?>, Object>(); argumentsByType.put(HttpServletRequest.class, req); Method method = MockController.class.getMethod("controller", String[].class, Integer.class); try { UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); Assert.fail("Should have caught that a required parameter was missing"); } catch (MissingRequiredParameterException ex) { } req.setParameter("properties", "name"); try { UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); } catch (MissingRequiredParameterException ex) { Assert.fail("Should not have required the second parameter"); } req.setParameter("number", ""); Object[] temp = UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); Assert.assertNull(temp[1]); req.setParameter("number", new String[] { "", "" }); temp = UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); Assert.assertNull(temp[1]); }
@Test public void test_determineControllerMethodParameters_requestParamDefault() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); Map<Class<?>, Object> argumentsByType = new HashMap<Class<?>, Object>(); argumentsByType.put(HttpServletRequest.class, req); Method method = MockController.class.getMethod("withDefault", int.class); UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); }
@Test public void test_determineControllerMethodParameters_fromCookie() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.setCookies(new Cookie("number", "3")); PageRequest pr = new PageRequest(null, null, req, null, null); Map<Class<?>, Object> argumentsByType = new HashMap<Class<?>, Object>(); argumentsByType.put(HttpServletRequest.class, req); argumentsByType.put(PageRequest.class, pr); Method method = MockController.class.getMethod("fromCookie", Integer.class); Object[] vals = UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); assertThat(vals[0], is((Object) 3)); }
@Test public void test_determineControllerMethodParameters_methodParam() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.addParameter("name", "Testing"); Map<Class<?>, Object> argumentsByType = new HashMap<Class<?>, Object>(); argumentsByType.put(HttpServletRequest.class, req); Method method = MockController.class.getMethod("fromMethod", MockDomainObject.class); Object[] temp = UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); MockDomainObject bound = (MockDomainObject) temp[0]; Assert.assertNotNull(bound); Assert.assertEquals("Testing", bound.getName()); }
@Test public void test_determineControllerMethodParameters_methodParamSubclass() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.addParameter("name", "Testing"); Map<Class<?>, Object> argumentsByType = new HashMap<Class<?>, Object>(); argumentsByType.put(HttpServletRequest.class, req); Method method = MockController.class.getMethod("fromMethodSubclass", MockDomainObject.class); Object[] temp = UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); MockDomainObject bound = (MockDomainObject) temp[0]; Assert.assertNotNull(bound); Assert.assertEquals("Testing", bound.getName()); Assert.assertEquals(MockDomainSubclass.class, bound.getClass()); }
@Test public void test_determineControllerMethodParameters_bindCollection() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.addParameter("helper.name", "Helper"); req.addParameter("helper.numbers", new String[] { "1", "2", "3" }); Map<Class<?>, Object> argumentsByType = new HashMap<Class<?>, Object>(); argumentsByType.put(HttpServletRequest.class, req); Method method = MockController.class.getMethod("action", MockDomainObject.class); Object[] temp = UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); MockDomainObject bound = (MockDomainObject) temp[0]; Assert.assertNotNull(bound); Assert.assertEquals("Helper", bound.getName()); Assert.assertNotNull(bound.getNumbers()); Assert.assertEquals(3, bound.getNumbers().size()); Assert.assertTrue(bound.getNumbers().contains(1)); Assert.assertTrue(bound.getNumbers().contains(2)); Assert.assertTrue(bound.getNumbers().contains(3)); }
@Test public void test_determineControllerMethodParameters_bindMap() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.addParameter("helper.map['foo']", "123"); req.addParameter("helper.map[bar]", "456"); Map<Class<?>, Object> argumentsByType = new HashMap<Class<?>, Object>(); argumentsByType.put(HttpServletRequest.class, req); Method method = MockController.class.getMethod("action", MockDomainObject.class); Object[] temp = UiFrameworkUtil.determineControllerMethodParameters(controller, method, argumentsByType, conversionService, applicationContext); MockDomainObject bound = (MockDomainObject) temp[0]; Assert.assertNotNull(bound); Assert.assertNotNull(bound.getMap()); Assert.assertEquals(2, bound.getMap().size()); Assert.assertEquals(Integer.valueOf(123), bound.getMap().get("foo")); Assert.assertEquals(Integer.valueOf(456), bound.getMap().get("bar")); }