Search in sources :

Example 76 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class RequestParamMapMethodArgumentResolverTests method resolveMapOfMultipartFile.

@Test
@SuppressWarnings("unchecked")
public void resolveMapOfMultipartFile() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("mfile", "Hello World".getBytes());
    MultipartFile expected2 = new MockMultipartFile("other", "Hello World 3".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(Map.class, String.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof Map;
    assertThat(condition).isTrue();
    Map<String, MultipartFile> resultMap = (Map<String, MultipartFile>) result;
    assertThat(resultMap.size()).isEqualTo(2);
    assertThat(resultMap.get("mfile")).isEqualTo(expected1);
    assertThat(resultMap.get("other")).isEqualTo(expected2);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Example 77 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class ByteArrayMultipartFileEditorTests method setValueAsMultipartFileWithBadBytes.

@Test
public void setValueAsMultipartFileWithBadBytes() throws Exception {
    MultipartFile file = mock(MultipartFile.class);
    given(file.getBytes()).willThrow(new IOException());
    assertThatIllegalArgumentException().isThrownBy(() -> editor.setValue(file));
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 78 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class RequestPartServletServerHttpRequestTests method getBodyWithWrappedRequest.

// SPR-13317
@Test
public void getBodyWithWrappedRequest() throws Exception {
    byte[] bytes = "content".getBytes("UTF-8");
    MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
    this.mockRequest.addFile(part);
    HttpServletRequest wrapped = new HttpServletRequestWrapper(this.mockRequest);
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(wrapped, "part");
    byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
    assertThat(result).isEqualTo(bytes);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 79 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method supportsParameter.

@Test
public void supportsParameter() {
    resolver = new RequestParamMethodArgumentResolver(null, true);
    MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
    assertThat(resolver.supportsParameter(param)).isFalse();
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isFalse();
    param = this.testMethod.annot(requestParam()).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    resolver = new RequestParamMethodArgumentResolver(null, false);
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isFalse();
    param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isFalse();
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Optional(java.util.Optional) MockPart(org.springframework.web.testfixture.servlet.MockPart) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestPart) Part(jakarta.servlet.http.Part) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 80 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileArrayMissing.

@Test
public void resolveMultipartFileArrayMissing() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertThatExceptionOfType(MissingServletRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(param, null, webRequest, null));
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)272 File (java.io.File)151 IOException (java.io.IOException)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)71 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)53 Test (org.junit.Test)41 FileInputStream (java.io.FileInputStream)37 ArrayList (java.util.ArrayList)30 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)28 Test (org.junit.jupiter.api.Test)27 InputStream (java.io.InputStream)25 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)24 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 FileOutputStream (java.io.FileOutputStream)18 Date (java.util.Date)18 List (java.util.List)18 PostMapping (org.springframework.web.bind.annotation.PostMapping)17 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)17 Path (java.nio.file.Path)15