/** * Return a {@link ResolvableType} for the specified {@link Method} return type. * @param method the source for the method return type * @return a {@link ResolvableType} for the specified method return * @see #forMethodReturnType(Method, Class) */ public static ResolvableType forMethodReturnType(Method method) { Assert.notNull(method, "Method must not be null"); return forMethodParameter(new MethodParameter(method, -1)); }
private void assertAnnotationFoundOnMethod(Class<? extends Annotation> annotationType, String methodName) throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(new MethodParameter(getClass().getMethod(methodName), -1)); assertNotNull("Should have found @" + annotationType.getSimpleName() + " on " + methodName + ".", typeDescriptor.getAnnotation(annotationType)); }
@Test(expected = IllegalArgumentException.class) public void handleReturnValueWithInvalidReturnType() throws Exception { Method method = getClass().getDeclaredMethod("handleAndReturnOutputStream"); MethodParameter returnType = new MethodParameter(method, -1); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(new ArrayList<>()); processor.writeWithMessageConverters(new ByteArrayOutputStream(), returnType, this.request); }
@Test public void toTextMessageWithReturnTypeAndNoJsonView() throws JMSException, NoSuchMethodException { Method method = this.getClass().getDeclaredMethod("none"); MethodParameter returnType = new MethodParameter(method, -1); testToTextMessageWithReturnType(returnType); verify(sessionMock).createTextMessage("{\"name\":\"test\",\"description\":\"lengthy description\"}"); }
@Test public void nestedTooManyLevels() throws Exception { TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test4", List.class), 0), 3); assertNull(t1); }
@Test public void supportsParameter() throws Exception { assertTrue(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 0))); assertFalse(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, -1))); }
@Test public void nestedMethodParameterType2Levels() throws Exception { TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test2", List.class), 0), 2); assertEquals(String.class, t1.getType()); }
@Test public void nestedMethodParameterTypeMap() throws Exception { TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test3", Map.class), 0), 1); assertEquals(String.class, t1.getType()); }
@Test public void nestedNotParameterized() throws Exception { TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test6", List.class), 0), 1); assertEquals(List.class,t1.getType()); assertEquals("java.util.List<?>", t1.toString()); TypeDescriptor t2 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test6", List.class), 0), 2); assertNull(t2); }
@Test public void supportsParameter() { List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>(); resolvers.add(new RequestParamMethodArgumentResolver(false)); resolvers.add(new RequestHeaderMethodArgumentResolver(null)); resolvers.add(new RequestParamMethodArgumentResolver(true)); Method method = ClassUtils.getMethod(this.getClass(), "handleRequest", String.class, String.class, String.class); CompositeUriComponentsContributor contributor = new CompositeUriComponentsContributor(resolvers); assertTrue(contributor.supportsParameter(new MethodParameter(method, 0))); assertTrue(contributor.supportsParameter(new MethodParameter(method, 1))); assertFalse(contributor.supportsParameter(new MethodParameter(method, 2))); }
@Test public void resolveWithConversion() throws Exception { Message<String> message = MessageBuilder.withPayload("test").build(); MethodParameter parameter = new MethodParameter(this.method, 1); when(this.converter.fromMessage(message, Integer.class)).thenReturn(4); @SuppressWarnings("unchecked") Message<Integer> actual = (Message<Integer>) this.resolver.resolveArgument(parameter, message); assertNotNull(actual); assertSame(message.getHeaders(), actual.getHeaders()); assertEquals(new Integer(4), actual.getPayload()); }
@Test // SPR-16252 public void fromMessageToList() throws Exception { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); String payload = "[1, 2, 3, 4, 5, 6, 7, 8, 9]"; Message<?> message = MessageBuilder.withPayload(payload.getBytes(StandardCharsets.UTF_8)).build(); Method method = getClass().getDeclaredMethod("handleList", List.class); MethodParameter param = new MethodParameter(method, 0); Object actual = converter.fromMessage(message, List.class, param); assertNotNull(actual); assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), actual); }
@Before public void setup() { this.body = "body"; this.contentType = MediaType.TEXT_PLAIN; this.converterType = StringHttpMessageConverter.class; this.paramType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), 0); this.returnType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), -1); this.request = new ServletServerHttpRequest(new MockHttpServletRequest()); this.response = new ServletServerHttpResponse(new MockHttpServletResponse()); }
@Test @SuppressWarnings("unchecked") public void convertObjectToOptional() { Method method = ClassUtils.getMethod(TestEntity.class, "handleOptionalValue", Optional.class); MethodParameter parameter = new MethodParameter(method, 0); TypeDescriptor descriptor = new TypeDescriptor(parameter); Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor); assertEquals(Optional.class, actual.getClass()); assertEquals(Arrays.asList(1, 2, 3), ((Optional<List<Integer>>) actual).get()); }
@Test public void resolveWithMessageSubclassAndPayloadWildcard() throws Exception { ErrorMessage message = new ErrorMessage(new UnsupportedOperationException()); MethodParameter parameter = new MethodParameter(this.method, 0); assertTrue(this.resolver.supportsParameter(parameter)); assertSame(message, this.resolver.resolveArgument(parameter, message)); }
@Before public void setUp() throws Exception { adaptee = mock(WebArgumentResolver.class); adapter = new TestWebArgumentResolverAdapter(adaptee); parameter = new MethodParameter(getClass().getMethod("handle", Integer.TYPE), 0); webRequest = new ServletWebRequest(new MockHttpServletRequest()); // Expose request to the current thread (for SpEL expressions) RequestContextHolder.setRequestAttributes(webRequest); }
@Test public void inputStream() throws Exception { MethodParameter inputStreamParameter = new MethodParameter(method, 5); assertTrue("InputStream not supported", resolver.supportsParameter(inputStreamParameter)); Object result = resolver.resolveArgument(inputStreamParameter, null, webRequest, null); assertSame("Invalid result", webRequest.getRequest().getInputStream(), result); }
@Test public void parameterAnnotated() throws Exception { TypeDescriptor t1 = new TypeDescriptor(new MethodParameter(getClass().getMethod("testAnnotatedMethod", String.class), 0)); assertEquals(String.class, t1.getType()); assertEquals(1, t1.getAnnotations().length); assertNotNull(t1.getAnnotation(ParameterAnnotation.class)); assertTrue(t1.hasAnnotation(ParameterAnnotation.class)); assertEquals(123, t1.getAnnotation(ParameterAnnotation.class).value()); }
@Test public void resolveWithConversionEmptyPayload() throws Exception { Message<String> message = MessageBuilder.withPayload("").build(); MethodParameter parameter = new MethodParameter(this.method, 1); assertTrue(this.resolver.supportsParameter(parameter)); thrown.expect(MessageConversionException.class); thrown.expectMessage("payload is empty"); thrown.expectMessage(Integer.class.getName()); thrown.expectMessage(String.class.getName()); this.resolver.resolveArgument(parameter, message); }
@Test public void zoneIdFromResolver() throws Exception { TimeZone timeZone = TimeZone.getTimeZone("America/New_York"); servletRequest.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(Locale.US, timeZone)); MethodParameter zoneIdParameter = new MethodParameter(method, 9); assertTrue("ZoneId not supported", resolver.supportsParameter(zoneIdParameter)); Object result = resolver.resolveArgument(zoneIdParameter, null, webRequest, null); assertEquals("Invalid result", timeZone.toZoneId(), result); }