Search in sources :

Example 41 with MultipartFile

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

the class RequestParamMethodArgumentResolverTests method isMultipartRequestHttpPut.

// SPR-9079
@Test
public void isMultipartRequestHttpPut() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileList", "Hello World".getBytes());
    request.addFile(expected);
    request.setMethod("PUT");
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object actual = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(actual instanceof List);
    assertEquals(expected, ((List<?>) actual).get(0));
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 42 with MultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileArray.

@Test
public void resolveMultipartFileArray() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("mfilearray", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("mfilearray", "Hello World 2".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof MultipartFile[]);
    MultipartFile[] parts = (MultipartFile[]) result;
    assertEquals(2, parts.length);
    assertEquals(parts[0], expected1);
    assertEquals(parts[1], expected2);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 43 with MultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileListNotannot.

@Test
public void resolveMultipartFileListNotannot() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("multipartFileList", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("multipartFileList", "Hello World 2".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof List);
    assertEquals(Arrays.asList(expected1, expected2), result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 44 with MultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileList.

@Test
public void resolveMultipartFileList() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("mfilelist", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("mfilelist", "Hello World 2".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof List);
    assertEquals(Arrays.asList(expected1, expected2), result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 45 with MultipartFile

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

the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileArgument.

@Test
public void resolveOptionalMultipartFileArgument() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("optionalMultipartFile", "Hello World".getBytes());
    request.addFile(expected);
    request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", expected, ((Optional) actualValue).get());
    actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", expected, ((Optional) actualValue).get());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) Optional(java.util.Optional) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)76 File (java.io.File)26 IOException (java.io.IOException)23 Test (org.junit.Test)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)14 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)12 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)11 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 MethodParameter (org.springframework.core.MethodParameter)8 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)7 ApiOperation (io.swagger.annotations.ApiOperation)6 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)6 RequestParam (org.springframework.web.bind.annotation.RequestParam)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileOutputStream (java.io.FileOutputStream)5 LinkedList (java.util.LinkedList)5 ActivitiException (org.activiti.engine.ActivitiException)5