Search in sources :

Example 1 with ValidationException

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

the class VisitorFieldValidator method validateObject.

private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException {
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(o);
    ValidatorContext validatorContext;
    if (appendPrefix) {
        ValidatorContext parent = getValidatorContext();
        validatorContext = new AppendingValidatorContext(parent, createTextProvider(o, parent), fieldName, getMessage(o));
    } else {
        ValidatorContext parent = getValidatorContext();
        CompositeTextProvider textProvider = createTextProvider(o, parent);
        validatorContext = new DelegatingValidatorContext(parent, textProvider, parent);
    }
    actionValidatorManager.validate(o, visitorContext, validatorContext);
    stack.pop();
}
Also used : DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) ValueStack(com.opensymphony.xwork2.util.ValueStack) DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) CompositeTextProvider(com.opensymphony.xwork2.CompositeTextProvider)

Example 2 with ValidationException

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

the class SimpleActionValidationTest method testMessageKeyIsReturnedIfNoOtherDefault.

public void testMessageKeyIsReturnedIfNoOtherDefault() throws ValidationException {
    Validator validator = new ValidatorSupport() {

        public void validate(Object object) throws ValidationException {
            addActionError(object);
        }
    };
    validator.setValueStack(ActionContext.getContext().getValueStack());
    String messageKey = "does.not.exist";
    validator.setMessageKey(messageKey);
    SimpleAction action = new SimpleAction();
    container.inject(action);
    ValidatorContext validatorContext = new DelegatingValidatorContext(action, container.getInstance(TextProviderFactory.class));
    validator.setValidatorContext(validatorContext);
    validator.validate(this);
    assertTrue(validatorContext.hasActionErrors());
    Collection<String> errors = validatorContext.getActionErrors();
    assertEquals(1, errors.size());
    assertEquals(messageKey, errors.toArray()[0]);
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) SimpleAction(com.opensymphony.xwork2.SimpleAction) ValidatorSupport(com.opensymphony.xwork2.validator.validators.ValidatorSupport)

Example 3 with ValidationException

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

the class AnnotationActionValidatorManagerTest method testSkipUserMarkerActionLevelShortCircuit.

public void testSkipUserMarkerActionLevelShortCircuit() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
    assertEquals(10, validatorList.size());
    try {
        AnnotationUser user = new AnnotationUser();
        user.setName("Mark");
        user.setEmail("bad_email");
        user.setEmail2("bad_email");
        ValidatorContext context = new DummyValidatorContext(user, tpf);
        annotationActionValidatorManager.validate(user, null, context);
        assertTrue(context.hasFieldErrors());
        // check field errors
        List<String> l = context.getFieldErrors().get("email");
        assertNotNull(l);
        assertEquals(1, l.size());
        assertEquals("Not a valid e-mail.", l.get(0));
        l = context.getFieldErrors().get("email2");
        assertNotNull(l);
        assertEquals(2, l.size());
        assertEquals("Not a valid e-mail2.", l.get(0));
        assertEquals("Email2 not from the right company.", l.get(1));
        // check action errors
        assertTrue(context.hasActionErrors());
        l = (List<String>) context.getActionErrors();
        assertNotNull(l);
        // both expression test failed see AnnotationUser-validation.xml
        assertEquals(2, l.size());
        assertEquals("Email does not start with mark", l.get(0));
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : List(java.util.List) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Example 4 with ValidationException

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

the class AnnotationActionValidatorManagerTest method testDefaultMessageInterpolation.

public void testDefaultMessageInterpolation() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotatedTestBean.class, "beanMessageBundle");
    assertEquals(3, validatorList.size());
    try {
        AnnotatedTestBean bean = new AnnotatedTestBean();
        bean.setName("foo");
        bean.setCount(99);
        ValidatorContext context = new DummyValidatorContext(bean, tpf);
        annotationActionValidatorManager.validate(bean, "beanMessageBundle", context);
        assertTrue(context.hasErrors());
        assertTrue(context.hasFieldErrors());
        List<String> l = context.getFieldErrors().get("count");
        assertNotNull(l);
        assertEquals(1, l.size());
        assertEquals("Smaller Invalid Count: 99", l.get(0));
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : AnnotatedTestBean(com.opensymphony.xwork2.AnnotatedTestBean) List(java.util.List)

Example 5 with ValidationException

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

the class AnnotationActionValidatorManagerTest method testMessageInterpolation.

public void testMessageInterpolation() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotatedTestBean.class, "beanMessageBundle");
    assertEquals(3, validatorList.size());
    try {
        AnnotatedTestBean bean = new AnnotatedTestBean();
        bean.setName("foo");
        bean.setCount(150);
        ValidatorContext context = new DummyValidatorContext(bean, tpf);
        annotationActionValidatorManager.validate(bean, "beanMessageBundle", context);
        assertTrue(context.hasErrors());
        assertTrue(context.hasFieldErrors());
        List<String> l = context.getFieldErrors().get("count");
        assertNotNull(l);
        assertEquals(1, l.size());
        assertEquals("Count must be between 1 and 100, current value is 150.", l.get(0));
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : AnnotatedTestBean(com.opensymphony.xwork2.AnnotatedTestBean) List(java.util.List)

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