Search in sources :

Example 1 with StrutsUploadedFile

use of org.apache.struts2.dispatcher.multipart.StrutsUploadedFile in project struts by apache.

the class FileUploadInterceptorTest method testAcceptFileWithMaxSize.

public void testAcceptFileWithMaxSize() throws Exception {
    interceptor.setAllowedTypes("text/plain");
    interceptor.setMaximumSize(new Long(10));
    // when file is not of allowed types
    ValidationAwareSupport validation = new ValidationAwareSupport();
    URL url = ClassLoaderUtil.getResource("log4j2.xml", FileUploadInterceptorTest.class);
    File file = new File(new URI(url.toString()));
    assertTrue("log4j2.xml should be in src/test folder", file.exists());
    boolean notOk = interceptor.acceptFile(action, new StrutsUploadedFile(file), "filename", "text/html", "inputName", validation);
    assertFalse(notOk);
    assertFalse(validation.getFieldErrors().isEmpty());
    assertTrue(validation.hasErrors());
    List errors = (List) validation.getFieldErrors().get("inputName");
    assertEquals(1, errors.size());
    String msg = (String) errors.get(0);
    // the error message should contain at least this test
    assertTrue(msg.startsWith("The file is too large to be uploaded"));
    assertTrue(msg.indexOf("inputName") > 0);
    assertTrue(msg.indexOf("log4j2.xml") > 0);
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) StrutsUploadedFile(org.apache.struts2.dispatcher.multipart.StrutsUploadedFile) List(java.util.List) StrutsUploadedFile(org.apache.struts2.dispatcher.multipart.StrutsUploadedFile) UploadedFile(org.apache.struts2.dispatcher.multipart.UploadedFile) File(java.io.File) URI(java.net.URI) URL(java.net.URL)

Example 2 with StrutsUploadedFile

use of org.apache.struts2.dispatcher.multipart.StrutsUploadedFile in project struts by apache.

the class UploadedFileConverterTest method convertUploadedFileToFile.

@Test
public void convertUploadedFileToFile() throws Exception {
    // given
    UploadedFileConverter ufc = new UploadedFileConverter();
    UploadedFile uploadedFile = new StrutsUploadedFile(tempFile);
    // when
    Object result = ufc.convertValue(context, target, member, propertyName, uploadedFile, File.class);
    // then
    assertThat(result).isInstanceOf(File.class);
    File file = (File) result;
    assertThat(file.length()).isEqualTo(tempFile.length());
    assertThat(file.getAbsolutePath()).isEqualTo(tempFile.getAbsolutePath());
}
Also used : StrutsUploadedFile(org.apache.struts2.dispatcher.multipart.StrutsUploadedFile) UploadedFile(org.apache.struts2.dispatcher.multipart.UploadedFile) StrutsUploadedFile(org.apache.struts2.dispatcher.multipart.StrutsUploadedFile) StrutsUploadedFile(org.apache.struts2.dispatcher.multipart.StrutsUploadedFile) UploadedFile(org.apache.struts2.dispatcher.multipart.UploadedFile) File(java.io.File) Test(org.junit.Test)

Example 3 with StrutsUploadedFile

use of org.apache.struts2.dispatcher.multipart.StrutsUploadedFile in project struts by apache.

the class UploadedFileConverterTest method convertUploadedFileArrayToFile.

@Test
public void convertUploadedFileArrayToFile() throws Exception {
    // given
    UploadedFileConverter ufc = new UploadedFileConverter();
    UploadedFile[] uploadedFile = new UploadedFile[] { new StrutsUploadedFile(tempFile) };
    // when
    Object result = ufc.convertValue(context, target, member, propertyName, uploadedFile, File.class);
    // then
    assertThat(result).isInstanceOf(File.class);
    File file = (File) result;
    assertThat(file.length()).isEqualTo(tempFile.length());
    assertThat(file.getAbsolutePath()).isEqualTo(tempFile.getAbsolutePath());
}
Also used : StrutsUploadedFile(org.apache.struts2.dispatcher.multipart.StrutsUploadedFile) UploadedFile(org.apache.struts2.dispatcher.multipart.UploadedFile) StrutsUploadedFile(org.apache.struts2.dispatcher.multipart.StrutsUploadedFile) StrutsUploadedFile(org.apache.struts2.dispatcher.multipart.StrutsUploadedFile) UploadedFile(org.apache.struts2.dispatcher.multipart.UploadedFile) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)3 StrutsUploadedFile (org.apache.struts2.dispatcher.multipart.StrutsUploadedFile)3 UploadedFile (org.apache.struts2.dispatcher.multipart.UploadedFile)3 Test (org.junit.Test)2 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)1 URI (java.net.URI)1 URL (java.net.URL)1 List (java.util.List)1