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);
}
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());
}
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());
}
Aggregations