|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2021 the original author or authors. |
|
|
|
* Copyright 2002-2022 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -16,6 +16,9 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.web.servlet.mvc.method.annotation; |
|
|
|
package org.springframework.web.servlet.mvc.method.annotation; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.FilterInputStream; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.io.InputStream; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Arrays; |
|
|
|
@ -82,6 +85,8 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
|
|
|
|
|
|
|
|
private MultipartFile multipartFile2; |
|
|
|
private MultipartFile multipartFile2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private CloseTrackingInputStream trackedStream; |
|
|
|
|
|
|
|
|
|
|
|
private MockMultipartHttpServletRequest multipartRequest; |
|
|
|
private MockMultipartHttpServletRequest multipartRequest; |
|
|
|
|
|
|
|
|
|
|
|
private NativeWebRequest webRequest; |
|
|
|
private NativeWebRequest webRequest; |
|
|
|
@ -115,7 +120,14 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
reset(messageConverter); |
|
|
|
reset(messageConverter); |
|
|
|
|
|
|
|
|
|
|
|
byte[] content = "doesn't matter as long as not empty".getBytes(StandardCharsets.UTF_8); |
|
|
|
byte[] content = "doesn't matter as long as not empty".getBytes(StandardCharsets.UTF_8); |
|
|
|
multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", content); |
|
|
|
multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", content) { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public InputStream getInputStream() throws IOException { |
|
|
|
|
|
|
|
CloseTrackingInputStream in = new CloseTrackingInputStream(super.getInputStream()); |
|
|
|
|
|
|
|
trackedStream = in; |
|
|
|
|
|
|
|
return in; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", content); |
|
|
|
multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", content); |
|
|
|
multipartRequest = new MockMultipartHttpServletRequest(); |
|
|
|
multipartRequest = new MockMultipartHttpServletRequest(); |
|
|
|
multipartRequest.addFile(multipartFile1); |
|
|
|
multipartRequest.addFile(multipartFile1); |
|
|
|
@ -181,8 +193,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void resolveMultipartFileList() throws Exception { |
|
|
|
public void resolveMultipartFileList() throws Exception { |
|
|
|
Object actual = resolver.resolveArgument(paramMultipartFileList, null, webRequest, null); |
|
|
|
Object actual = resolver.resolveArgument(paramMultipartFileList, null, webRequest, null); |
|
|
|
boolean condition = actual instanceof List; |
|
|
|
assertThat(actual instanceof List).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
assertThat(actual).isEqualTo(Arrays.asList(multipartFile1, multipartFile2)); |
|
|
|
assertThat(actual).isEqualTo(Arrays.asList(multipartFile1, multipartFile2)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -190,8 +201,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
public void resolveMultipartFileArray() throws Exception { |
|
|
|
public void resolveMultipartFileArray() throws Exception { |
|
|
|
Object actual = resolver.resolveArgument(paramMultipartFileArray, null, webRequest, null); |
|
|
|
Object actual = resolver.resolveArgument(paramMultipartFileArray, null, webRequest, null); |
|
|
|
assertThat(actual).isNotNull(); |
|
|
|
assertThat(actual).isNotNull(); |
|
|
|
boolean condition = actual instanceof MultipartFile[]; |
|
|
|
assertThat(actual instanceof MultipartFile[]).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
MultipartFile[] parts = (MultipartFile[]) actual; |
|
|
|
MultipartFile[] parts = (MultipartFile[]) actual; |
|
|
|
assertThat(parts.length).isEqualTo(2); |
|
|
|
assertThat(parts.length).isEqualTo(2); |
|
|
|
assertThat(multipartFile1).isEqualTo(parts[0]); |
|
|
|
assertThat(multipartFile1).isEqualTo(parts[0]); |
|
|
|
@ -208,8 +218,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
|
|
|
|
|
|
|
|
Object result = resolver.resolveArgument(paramMultipartFileNotAnnot, null, webRequest, null); |
|
|
|
Object result = resolver.resolveArgument(paramMultipartFileNotAnnot, null, webRequest, null); |
|
|
|
|
|
|
|
|
|
|
|
boolean condition = result instanceof MultipartFile; |
|
|
|
assertThat(result instanceof MultipartFile).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
assertThat(result).as("Invalid result").isEqualTo(expected); |
|
|
|
assertThat(result).as("Invalid result").isEqualTo(expected); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -224,8 +233,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
webRequest = new ServletWebRequest(request); |
|
|
|
webRequest = new ServletWebRequest(request); |
|
|
|
|
|
|
|
|
|
|
|
Object result = resolver.resolveArgument(paramPart, null, webRequest, null); |
|
|
|
Object result = resolver.resolveArgument(paramPart, null, webRequest, null); |
|
|
|
boolean condition = result instanceof Part; |
|
|
|
assertThat(result instanceof Part).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
assertThat(result).as("Invalid result").isEqualTo(expected); |
|
|
|
assertThat(result).as("Invalid result").isEqualTo(expected); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -242,8 +250,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
webRequest = new ServletWebRequest(request); |
|
|
|
webRequest = new ServletWebRequest(request); |
|
|
|
|
|
|
|
|
|
|
|
Object result = resolver.resolveArgument(paramPartList, null, webRequest, null); |
|
|
|
Object result = resolver.resolveArgument(paramPartList, null, webRequest, null); |
|
|
|
boolean condition = result instanceof List; |
|
|
|
assertThat(result instanceof List).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
assertThat(result).isEqualTo(Arrays.asList(part1, part2)); |
|
|
|
assertThat(result).isEqualTo(Arrays.asList(part1, part2)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -260,8 +267,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
webRequest = new ServletWebRequest(request); |
|
|
|
webRequest = new ServletWebRequest(request); |
|
|
|
|
|
|
|
|
|
|
|
Object result = resolver.resolveArgument(paramPartArray, null, webRequest, null); |
|
|
|
Object result = resolver.resolveArgument(paramPartArray, null, webRequest, null); |
|
|
|
boolean condition = result instanceof Part[]; |
|
|
|
assertThat(result instanceof Part[]).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
Part[] parts = (Part[]) result; |
|
|
|
Part[] parts = (Part[]) result; |
|
|
|
assertThat(parts.length).isEqualTo(2); |
|
|
|
assertThat(parts.length).isEqualTo(2); |
|
|
|
assertThat(part1).isEqualTo(parts[0]); |
|
|
|
assertThat(part1).isEqualTo(parts[0]); |
|
|
|
@ -356,8 +362,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected); |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected); |
|
|
|
|
|
|
|
|
|
|
|
actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null); |
|
|
|
actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null); |
|
|
|
boolean condition = actualValue instanceof Optional; |
|
|
|
assertThat(actualValue instanceof Optional).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected); |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -398,8 +403,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected)); |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected)); |
|
|
|
|
|
|
|
|
|
|
|
actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null); |
|
|
|
actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null); |
|
|
|
boolean condition = actualValue instanceof Optional; |
|
|
|
assertThat(actualValue instanceof Optional).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected)); |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -442,8 +446,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected); |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected); |
|
|
|
|
|
|
|
|
|
|
|
actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null); |
|
|
|
actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null); |
|
|
|
boolean condition = actualValue instanceof Optional; |
|
|
|
assertThat(actualValue instanceof Optional).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected); |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -488,8 +491,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected)); |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected)); |
|
|
|
|
|
|
|
|
|
|
|
actualValue = resolver.resolveArgument(optionalPartList, null, webRequest, null); |
|
|
|
actualValue = resolver.resolveArgument(optionalPartList, null, webRequest, null); |
|
|
|
boolean condition = actualValue instanceof Optional; |
|
|
|
assertThat(actualValue instanceof Optional).isTrue(); |
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected)); |
|
|
|
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -571,6 +573,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
Object actualValue = resolver.resolveArgument(parameter, mavContainer, webRequest, new ValidatingBinderFactory()); |
|
|
|
Object actualValue = resolver.resolveArgument(parameter, mavContainer, webRequest, new ValidatingBinderFactory()); |
|
|
|
assertThat(actualValue).as("Invalid argument value").isEqualTo(argValue); |
|
|
|
assertThat(actualValue).as("Invalid argument value").isEqualTo(argValue); |
|
|
|
assertThat(mavContainer.isRequestHandled()).as("The requestHandled flag shouldn't change").isFalse(); |
|
|
|
assertThat(mavContainer.isRequestHandled()).as("The requestHandled flag shouldn't change").isFalse(); |
|
|
|
|
|
|
|
assertThat(trackedStream != null && trackedStream.closed).isTrue(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -590,7 +593,7 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final class ValidatingBinderFactory implements WebDataBinderFactory { |
|
|
|
private static class ValidatingBinderFactory implements WebDataBinderFactory { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public WebDataBinder createBinder(NativeWebRequest webRequest, @Nullable Object target, |
|
|
|
public WebDataBinder createBinder(NativeWebRequest webRequest, @Nullable Object target, |
|
|
|
@ -605,6 +608,21 @@ public class RequestPartMethodArgumentResolverTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class CloseTrackingInputStream extends FilterInputStream { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean closed = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CloseTrackingInputStream(InputStream in) { |
|
|
|
|
|
|
|
super(in); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void close() { |
|
|
|
|
|
|
|
this.closed = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
public void handle( |
|
|
|
public void handle( |
|
|
|
@RequestPart SimpleBean requestPart, |
|
|
|
@RequestPart SimpleBean requestPart, |
|
|
|
|