Search in sources :

Example 1 with AnnotationUser

use of com.opensymphony.xwork2.test.AnnotationUser 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 2 with AnnotationUser

use of com.opensymphony.xwork2.test.AnnotationUser in project struts by apache.

the class AnnotationActionValidatorManagerTest method testActionLevelShortCircuit.

public void testActionLevelShortCircuit() throws Exception {
    List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
    assertEquals(10, validatorList.size());
    AnnotationUser user = new AnnotationUser();
    // all fields will trigger error, but sc of action-level, cause it to not appear
    user.setName(null);
    user.setEmail("rainerh(at)example.com");
    user.setEmail("rainer_h(at)example.com");
    ValidatorContext context = new DummyValidatorContext(user, tpf);
    annotationActionValidatorManager.validate(user, null, context);
    // check field level errors
    // shouldn't have any because action error prevents validation of anything else
    List l = context.getFieldErrors().get("email2");
    assertNull(l);
    // check action errors
    assertTrue(context.hasActionErrors());
    l = (List) context.getActionErrors();
    assertNotNull(l);
    // we only get one, because AnnotationUserMarker-validation.xml action-level validator
    // already sc it   :-)
    assertEquals(1, l.size());
    assertEquals("Email not the same as email2", l.get(0));
}
Also used : List(java.util.List) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Example 3 with AnnotationUser

use of com.opensymphony.xwork2.test.AnnotationUser 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 4 with AnnotationUser

use of com.opensymphony.xwork2.test.AnnotationUser 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 5 with AnnotationUser

use of com.opensymphony.xwork2.test.AnnotationUser in project struts by apache.

the class AnnotationXWorkConverterTest method testStringToCollectionConversion.

// TODO: Fixme... This test does not work with GenericsObjectDeterminer!
public void testStringToCollectionConversion() {
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, Object> stackContext = stack.getContext();
    stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE);
    stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    AnnotationUser user = new AnnotationUser();
    stack.push(user);
    stack.setValue("list", "asdf");
    assertNotNull(user.getList());
    assertEquals(1, user.getList().size());
    assertEquals(String.class, user.getList().get(0).getClass());
    assertEquals("asdf", user.getList().get(0));
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Aggregations

AnnotationUser (com.opensymphony.xwork2.test.AnnotationUser)5 List (java.util.List)4 ValueStack (com.opensymphony.xwork2.util.ValueStack)1