use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.
the class StringValidatorTest method testRequiredString.
public void testRequiredString() throws Exception {
Equidae equidae = new Equidae();
// everything should fail
equidae.setHorse("");
ActionContext.getContext().getValueStack().push(equidae);
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
Map fieldErrors = context.getFieldErrors();
assertTrue(fieldErrors.containsKey("horse"));
assertEquals(2, ((List) fieldErrors.get("horse")).size());
// trim = false should fail
equidae.setHorse(" ");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
assertTrue(fieldErrors.containsKey("horse"));
List errors = (List) fieldErrors.get("horse");
assertEquals(1, errors.size());
assertEquals("trim", (String) errors.get(0));
}
use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.
the class FileUploadInterceptorTest method testAcceptFileWithWildcardContent.
public void testAcceptFileWithWildcardContent() throws Exception {
interceptor.setAllowedTypes("text/*");
ValidationAwareSupport validation = new ValidationAwareSupport();
boolean ok = interceptor.acceptFile(action, EMPTY_FILE, "filename.txt", "text/plain", "inputName", validation);
assertTrue(ok);
assertTrue(validation.getFieldErrors().isEmpty());
assertFalse(validation.hasErrors());
interceptor.setAllowedTypes("text/h*");
validation = new ValidationAwareSupport();
boolean notOk = interceptor.acceptFile(action, EMPTY_FILE, "filename.html", "text/plain", "inputName", validation);
assertFalse(notOk);
assertFalse(validation.getFieldErrors().isEmpty());
assertTrue(validation.hasErrors());
}
use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.
the class FileUploadInterceptorTest method testAcceptFileWithoutEmptyTypes.
public void testAcceptFileWithoutEmptyTypes() throws Exception {
interceptor.setAllowedTypes("text/plain");
// when file is of allowed types
ValidationAwareSupport validation = new ValidationAwareSupport();
boolean ok = interceptor.acceptFile(action, EMPTY_FILE, "filename.txt", "text/plain", "inputName", validation);
assertTrue(ok);
assertTrue(validation.getFieldErrors().isEmpty());
assertFalse(validation.hasErrors());
// when file is not of allowed types
validation = new ValidationAwareSupport();
boolean notOk = interceptor.acceptFile(action, EMPTY_FILE, "filename.html", "text/html", "inputName", validation);
assertFalse(notOk);
assertFalse(validation.getFieldErrors().isEmpty());
assertTrue(validation.hasErrors());
}
use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.
the class FileUploadInterceptorTest method testAcceptFileWithoutEmptyExtensions.
public void testAcceptFileWithoutEmptyExtensions() throws Exception {
interceptor.setAllowedExtensions(".txt");
// when file is of allowed extensions
ValidationAwareSupport validation = new ValidationAwareSupport();
boolean ok = interceptor.acceptFile(action, EMPTY_FILE, "filename.txt", "text/plain", "inputName", validation);
assertTrue(ok);
assertTrue(validation.getFieldErrors().isEmpty());
assertFalse(validation.hasErrors());
// when file is not of allowed extensions
validation = new ValidationAwareSupport();
boolean notOk = interceptor.acceptFile(action, EMPTY_FILE, "filename.html", "text/html", "inputName", validation);
assertFalse(notOk);
assertFalse(validation.getFieldErrors().isEmpty());
assertTrue(validation.hasErrors());
// test with multiple extensions
interceptor.setAllowedExtensions(".txt,.lol");
validation = new ValidationAwareSupport();
ok = interceptor.acceptFile(action, EMPTY_FILE, "filename.lol", "text/plain", "inputName", validation);
assertTrue(ok);
assertTrue(validation.getFieldErrors().isEmpty());
assertFalse(validation.hasErrors());
}
use of com.opensymphony.xwork2.ValidationAwareSupport 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);
}
Aggregations