Search in sources :

Example 11 with ValidationAwareSupport

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));
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) List(java.util.List) Equidae(com.opensymphony.xwork2.test.Equidae) Map(java.util.Map)

Example 12 with ValidationAwareSupport

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());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport)

Example 13 with ValidationAwareSupport

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());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport)

Example 14 with ValidationAwareSupport

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());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport)

Example 15 with ValidationAwareSupport

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

Aggregations

ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)15 ValueStack (com.opensymphony.xwork2.util.ValueStack)4 List (java.util.List)4 Equidae (com.opensymphony.xwork2.test.Equidae)3 Map (java.util.Map)2 ActionProxy (com.opensymphony.xwork2.ActionProxy)1 ActionProxyFactory (com.opensymphony.xwork2.ActionProxyFactory)1 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)1 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)1 ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)1 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)1 File (java.io.File)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ApplicationMap (org.apache.struts2.dispatcher.ApplicationMap)1 Dispatcher (org.apache.struts2.dispatcher.Dispatcher)1