Search in sources :

Example 16 with MultipartFile

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

the class ByteArrayMultipartFileEditorTests method setValueAsMultipartFileWithBadBytes.

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

Example 17 with MultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileNotAnnot.

@Test
public void resolveMultipartFileNotAnnot() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof MultipartFile);
    assertEquals("Invalid result", expected, 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) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 18 with MultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFile.

@Test
public void resolveMultipartFile() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
    request.addFile(expected);
    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);
    assertEquals("Invalid result", expected, 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) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 19 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);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    resolver = new RequestParamMethodArgumentResolver(null, false);
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Optional(java.util.Optional) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) Map(java.util.Map) Test(org.junit.Test)

Example 20 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());
    assertArrayEquals(bytes, result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) 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