@Test(expected = ServletRequestBindingException.class) public void notFound() throws Exception { resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null); }
@Test public void resolveDefaultValueFromSystemProperty() throws Exception { System.setProperty("systemProperty", "bar"); try { Object result = resolver.resolveArgument(paramSystemProperty, null, webRequest, null); assertTrue(result instanceof String); assertEquals("bar", result); } finally { System.clearProperty("systemProperty"); } }
@Test public void resolveNameFromSystemPropertyThroughExpression() throws Exception { String expected = "foo"; servletRequest.addHeader("bar", expected); System.setProperty("systemProperty", "bar"); try { Object result = resolver.resolveArgument(paramResolvedNameWithExpression, null, webRequest, null); assertTrue(result instanceof String); assertEquals(expected, result); } finally { System.clearProperty("systemProperty"); } }
@Test public void resolveNameFromSystemPropertyThroughPlaceholder() throws Exception { String expected = "foo"; servletRequest.addHeader("bar", expected); System.setProperty("systemProperty", "bar"); try { Object result = resolver.resolveArgument(paramResolvedNameWithPlaceholder, null, webRequest, null); assertTrue(result instanceof String); assertEquals(expected, result); } finally { System.clearProperty("systemProperty"); } }
@Test public void resolveDefaultValue() throws Exception { Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null); assertTrue(result instanceof String); assertEquals("bar", result); }
@Test public void resolveStringArrayArgument() throws Exception { String[] expected = new String[] {"foo", "bar"}; servletRequest.addHeader("name", expected); Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null); assertTrue(result instanceof String[]); assertArrayEquals(expected, (String[]) result); }
@Test public void resolveStringArgument() throws Exception { String expected = "foo"; servletRequest.addHeader("name", expected); Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null); assertTrue(result instanceof String); assertEquals(expected, result); }
@Test public void resolveDefaultValueFromRequest() throws Exception { servletRequest.setContextPath("/bar"); Object result = resolver.resolveArgument(paramContextPath, null, webRequest, null); assertTrue(result instanceof String); assertEquals("/bar", result); }
@Test @SuppressWarnings("deprecation") public void dateConversion() throws Exception { String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100"; servletRequest.addHeader("name", rfc1123val); ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer(); bindingInitializer.setConversionService(new DefaultFormattingConversionService()); Object result = resolver.resolveArgument(paramDate, null, webRequest, new DefaultDataBinderFactory(bindingInitializer)); assertTrue(result instanceof Date); assertEquals(new Date(rfc1123val), result); }
@Test public void instantConversion() throws Exception { String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100"; servletRequest.addHeader("name", rfc1123val); ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer(); bindingInitializer.setConversionService(new DefaultFormattingConversionService()); Object result = resolver.resolveArgument(paramInstant, null, webRequest, new DefaultDataBinderFactory(bindingInitializer)); assertTrue(result instanceof Instant); assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result); }