Search in sources :

Example 6 with ValidationException

use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.

the class AnnotationActionValidatorManagerTest method testShortCircuitNoErrors.

public void testShortCircuitNoErrors() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
    assertEquals(10, validatorList.size());
    try {
        AnnotationUser user = new AnnotationUser();
        user.setName("Mark");
        user.setEmail("mark@mycompany.com");
        user.setEmail2("mark@mycompany.com");
        ValidatorContext context = new DummyValidatorContext(user, tpf);
        annotationActionValidatorManager.validate(user, null, context);
        assertFalse(context.hasErrors());
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : List(java.util.List) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Example 7 with ValidationException

use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.

the class ValidatorSupportTest method testConditionalParseExpression.

public void testConditionalParseExpression() {
    OgnlValueStack stack = (OgnlValueStack) container.getInstance(ValueStackFactory.class).createValueStack();
    stack.getContext().put("something", "somevalue");
    ActionContext.of(stack.getContext()).withContainer(container).bind();
    ValidatorSupport validator = new ValidatorSupport() {

        public void validate(Object object) throws ValidationException {
        }
    };
    validator.setValueStack(ActionContext.getContext().getValueStack());
    String result1 = validator.parse("${#something}", String.class).toString();
    assertEquals(result1, "somevalue");
}
Also used : OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack)

Example 8 with ValidationException

use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.

the class AnnotationActionValidatorManagerTest method testSkipAllActionLevelShortCircuit2.

public void testSkipAllActionLevelShortCircuit2() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
    assertEquals(10, validatorList.size());
    try {
        AnnotationUser user = new AnnotationUser();
        user.setName("Mark");
        // * mark both email to starts with mark to get pass the action-level validator,
        // so we could concentrate on testing the field-level validators (AnnotationUser-validation.xml)
        // * make both email the same to pass the action-level validator at
        // AnnotationUserMarker-validation.xml
        user.setEmail("mark_bad_email_for_field_val@foo.com");
        user.setEmail2("mark_bad_email_for_field_val@foo.com");
        ValidatorContext context = new DummyValidatorContext(user, tpf);
        annotationActionValidatorManager.validate(user, null, context);
        assertTrue(context.hasFieldErrors());
        // check field errors
        // we have an error in this field level, email does not ends with mycompany.com
        List l = context.getFieldErrors().get("email");
        assertNotNull(l);
        // because email-field-val is short-circuit
        assertEquals(1, l.size());
        assertEquals("Email not from the right company.", l.get(0));
        // check action errors
        l = (List) context.getActionErrors();
        assertFalse(context.hasActionErrors());
        assertEquals(0, l.size());
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : List(java.util.List) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Example 9 with ValidationException

use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.

the class AnnotationActionValidatorManagerTest method testGetValidatorsForGivenMethodNameWithoutReloading.

public void testGetValidatorsForGivenMethodNameWithoutReloading() throws ValidationException {
    FileManager fileManager = container.getInstance(FileManagerFactory.class).getFileManager();
    List validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias, "execute");
    // disable configuration reload/devmode
    fileManager.setReloadingConfigs(false);
    // 17 in the class level + 0 in the alias
    assertEquals(12, validatorList.size());
    validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias, "execute");
    // expect same number of validators
    assertEquals(12, validatorList.size());
}
Also used : FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) List(java.util.List) SimpleAnnotationAction(com.opensymphony.xwork2.SimpleAnnotationAction) FileManager(com.opensymphony.xwork2.FileManager)

Example 10 with ValidationException

use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.

the class URLValidatorTest method testValidUrlCaseInsensitive.

public void testValidUrlCaseInsensitive() throws Exception {
    // given
    final Map<String, Object> fieldErrors = new HashMap<>();
    URLValidator validator = new URLValidator() {

        @Override
        public String getFieldName() {
            return "url";
        }

        @Override
        protected Object getFieldValue(String name, Object object) throws ValidationException {
            return object;
        }

        @Override
        protected void addFieldError(String propertyName, Object object) {
            fieldErrors.put(propertyName, object);
        }
    };
    // when
    validator.validate("http://localhost:8080/myapp");
    // then
    assertTrue(fieldErrors.isEmpty());
    // when
    validator.validate("http://LOCALHOST:8080/MYAPP");
    // then
    assertTrue(fieldErrors.isEmpty());
    // when
    validator.validate("http://www.appache.org/TEST");
    // then
    assertTrue(fieldErrors.isEmpty());
}
Also used : HashMap(java.util.HashMap) URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator)

Aggregations

List (java.util.List)6 AnnotationUser (com.opensymphony.xwork2.test.AnnotationUser)3 ValueStack (com.opensymphony.xwork2.util.ValueStack)3 AnnotatedTestBean (com.opensymphony.xwork2.AnnotatedTestBean)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 Mock (com.mockobjects.dynamic.Mock)1 ActionContext (com.opensymphony.xwork2.ActionContext)1 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)1 CompositeTextProvider (com.opensymphony.xwork2.CompositeTextProvider)1 FileManager (com.opensymphony.xwork2.FileManager)1 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 SimpleAnnotationAction (com.opensymphony.xwork2.SimpleAnnotationAction)1 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)1 PreResultListener (com.opensymphony.xwork2.interceptor.PreResultListener)1 OgnlValueStack (com.opensymphony.xwork2.ognl.OgnlValueStack)1 DelegatingValidatorContext (com.opensymphony.xwork2.validator.DelegatingValidatorContext)1 ValidationException (com.opensymphony.xwork2.validator.ValidationException)1 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)1 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)1