/** * Executes the given ValidatorAction and all ValidatorActions that it * depends on. * @return true if the validation succeeded. */ private boolean validateForRule( ValidatorAction va, ValidatorResults results, Map<String, ValidatorAction> actions, Map<String, Object> params, int pos) throws ValidatorException { ValidatorResult result = results.getValidatorResult(this.getKey()); if (result != null && result.containsAction(va.getName())) { return result.isValid(va.getName()); } if (!this.runDependentValidators(va, results, actions, params, pos)) { return false; } return va.executeValidationMethod(this, params, results, pos); }
/** * Add a the result of a validator action. * * @param field The field validated. * @param validatorName The name of the validator. * @param result The result of the validation. * @param value The value returned by the validator. */ public void add( Field field, String validatorName, boolean result, Object value) { ValidatorResult validatorResult = this.getValidatorResult(field.getKey()); if (validatorResult == null) { validatorResult = new ValidatorResult(field); this.hResults.put(field.getKey(), validatorResult); } validatorResult.add(validatorName, result, value); }
/** * Get a <code>Map</code> of any <code>Object</code>s returned from * validation routines. * * @return Map of objections returned by validators. */ public Map<String, Object> getResultValueMap() { Map<String, Object> results = new HashMap<String, Object>(); for (Iterator<String> i = hResults.keySet().iterator(); i.hasNext();) { String propertyKey = i.next(); ValidatorResult vr = this.getValidatorResult(propertyKey); for (Iterator<String> x = vr.getActions(); x.hasNext();) { String actionKey = x.next(); Object result = vr.getResult(actionKey); if (result != null && !(result instanceof Boolean)) { results.put(propertyKey, result); } } } return results; }
String property = (String)propertyNames.next(); ValidatorResult result = results.getValidatorResult(property); Map actionMap = result.getActionMap(); Iterator keys = actionMap.keySet().iterator(); boolean errorFound = false; if (!result.isValid(actionName))
/** * Check a validator has not been run for a field and the result. */ private void checkNotRun(ValidatorResults results, String field, String action) { ValidatorResult result = results.getValidatorResult(field); assertNotNull(field + " result", result); assertFalse(field + "[" + action + "] run", result.containsAction(action)); // System.out.println(field + "[" + action + "] not run"); }
/** * Add the result of a validator action. * @param validatorName Name of the validator. * @param result Whether the validation passed or failed. */ public void add(String validatorName, boolean result) { this.add(validatorName, result, null); }
Map actionMap = result.getActionMap(); Iterator keys = actionMap.keySet().iterator(); while (keys.hasNext()) { + actName + "] (" + (result.isValid(actName) ? "PASSED" : "FAILED") + ")"); if (!result.isValid(actName)) { success = false; String message = apps.getString(action.getMsg());
/** * Add the result of a validator action. * @param validatorName Name of the validator. * @param result Whether the validation passed or failed. */ public void add(String validatorName, boolean result) { this.add(validatorName, result, null); }
/** * Check a validator has run for a field and the result. */ private void checkValidatorResult(ValidatorResults results, String field, String action, boolean expected) { ValidatorResult result = results.getValidatorResult(field); // System.out.println(field + "[" + action + "]=" + result.isValid(action)); assertNotNull(field + " result", result); assertTrue(field + "[" + action + "] not run", result.containsAction(action)); assertEquals(field + "[" + action + "] result", expected, result.isValid(action)); }
Map actionMap = result.getActionMap(); Iterator keys = actionMap.keySet().iterator(); while (keys.hasNext()) if (!result.isValid(actionName))
/** * Add a the result of a validator action. * * @param field The field validated. * @param validatorName The name of the validator. * @param result The result of the validation. * @param value The value returned by the validator. */ public void add( Field field, String validatorName, boolean result, Object value) { ValidatorResult validatorResult = this.getValidatorResult(field.getKey()); if (validatorResult == null) { validatorResult = new ValidatorResult(field); this.hResults.put(field.getKey(), validatorResult); } validatorResult.add(validatorName, result, value); }
/** * Get a <code>Map</code> of any <code>Object</code>s returned from * validation routines. * * @return Map of objections returned by validators. */ public Map getResultValueMap() { Map results = new HashMap(); for (Iterator i = hResults.keySet().iterator(); i.hasNext();) { String propertyKey = (String) i.next(); ValidatorResult vr = this.getValidatorResult(propertyKey); for (Iterator x = vr.getActions(); x.hasNext();) { String actionKey = (String)x.next(); Object result = vr.getResult(actionKey); if (result != null && !(result instanceof Boolean)) { results.put(propertyKey, result); } } } return results; }
/** * Add the result of a validator action. * @param validatorName Name of the validator. * @param result Whether the validation passed or failed. */ public void add(String validatorName, boolean result) { this.add(validatorName, result, null); }
/** * If middle name is there and a negative int, then the required and int * dependent tests should pass, but the positive test should fail. * * @throws ValidatorException */ public void testPassingDependentsFailingMain() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setMiddleName("-2534"); // Construct validator based on the loaded resources // and the form key Validator validator = new Validator(resources, FORM_KEY); // add the name bean to the validator as a resource // for the validations to be performed on. validator.setParameter(Validator.BEAN_PARAM, name); // Get results of the validation. ValidatorResults results = null; results = validator.validate(); assertNotNull("Results are null.", results); ValidatorResult middleNameResult = results.getValidatorResult("middleName"); assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult); assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required")); assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required")); assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int")); assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int")); assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive")); assertTrue("Middle Name ValidatorResult for the 'positive' action should have failed", !middleNameResult.isValid("positive")); }
Map actionMap = result.getActionMap(); Iterator keys = actionMap.keySet().iterator(); while (keys.hasNext()) { if (!result.isValid(actName)) { String message = _properties.getString(action.getMsg()); Object[] args = { prettyFieldName };
/** * Add a the result of a validator action. * * @param field The field validated. * @param validatorName The name of the validator. * @param result The result of the validation. * @param value The value returned by the validator. */ public void add( Field field, String validatorName, boolean result, Object value) { ValidatorResult validatorResult = this.getValidatorResult(field.getKey()); if (validatorResult == null) { validatorResult = new ValidatorResult(field); this.hResults.put(field.getKey(), validatorResult); } validatorResult.add(validatorName, result, value); }
/** * Get a <code>Map</code> of any <code>Object</code>s returned from * validation routines. * * @return Map of objections returned by validators. */ public Map<String, Object> getResultValueMap() { Map<String, Object> results = new HashMap<String, Object>(); for (Iterator<String> i = hResults.keySet().iterator(); i.hasNext();) { String propertyKey = i.next(); ValidatorResult vr = this.getValidatorResult(propertyKey); for (Iterator<String> x = vr.getActions(); x.hasNext();) { String actionKey = x.next(); Object result = vr.getResult(actionKey); if (result != null && !(result instanceof Boolean)) { results.put(propertyKey, result); } } } return results; }
assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required")); assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required")); assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int")); assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int")); assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive")); assertTrue("Middle Name ValidatorResult for the 'positive' action should have passed", middleNameResult.isValid("positive"));
/** * Utlity class to run a test on a value. * * @param info Value to run test on. * @param passed Whether or not the test is expected to pass. */ protected void valueTest(Object info, boolean passed) throws ValidatorException { // Construct validator based on the loaded resources // and the form key Validator validator = new Validator(resources, FORM_KEY); // add the name bean to the validator as a resource // for the validations to be performed on. validator.setParameter(Validator.BEAN_PARAM, info); // Get results of the validation. ValidatorResults results = null; // throws ValidatorException, // but we aren't catching for testing // since no validation methods we use // throw this results = validator.validate(); assertNotNull("Results are null.", results); ValidatorResult result = results.getValidatorResult("value"); assertNotNull(ACTION + " value ValidatorResult should not be null.", result); assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.", result.containsAction(ACTION)); assertTrue(ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION))); }
/** * If middle name is there but not int, then the required dependent test * should pass, but the int dependent test should fail. No other tests should * run. * * @throws ValidatorException */ public void testFailingNextDependentValidator() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setMiddleName("TEST"); // Construct validator based on the loaded resources // and the form key Validator validator = new Validator(resources, FORM_KEY); // add the name bean to the validator as a resource // for the validations to be performed on. validator.setParameter(Validator.BEAN_PARAM, name); // Get results of the validation. ValidatorResults results = null; results = validator.validate(); assertNotNull("Results are null.", results); ValidatorResult middleNameResult = results.getValidatorResult("middleName"); assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult); assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required")); assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required")); assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int")); assertTrue("Middle Name ValidatorResult for the 'int' action should have failed", !middleNameResult.isValid("int")); assertTrue("Middle Name ValidatorResult should not contain the 'positive' action.", !middleNameResult.containsAction("positive")); }