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 GenericValidatorContext(user); annotationActionValidatorManager.validate(user, null, context); // check field level errors // shouldn't have any because action error prevents validation of anything else List l = (List) 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)); }
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("*****@*****.**"); user.setEmail2("*****@*****.**"); ValidatorContext context = new GenericValidatorContext(user); annotationActionValidatorManager.validate(user, null, context); assertFalse(context.hasErrors()); } catch (ValidationException ex) { ex.printStackTrace(); fail("Validation error: " + ex.getMessage()); } }
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("*****@*****.**"); user.setEmail2("*****@*****.**"); ValidatorContext context = new GenericValidatorContext(user); 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 = (List) context.getFieldErrors().get("email"); assertNotNull(l); assertEquals(1, l.size()); // because email-field-val is short-circuit 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()); } }
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 GenericValidatorContext(user); 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); assertEquals(2, l.size()); // both expression test failed see AnnotationUser-validation.xml assertEquals("Email does not start with mark", l.get(0)); } catch (ValidationException ex) { ex.printStackTrace(); fail("Validation error: " + ex.getMessage()); } }