/** * 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; }
/** * 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); }
/** * 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"); }
/** * 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); }
/** * 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)); }
/** * Tests if we can override a rule. We "can" override a rule if the message shown * when the firstName required test fails and the lastName test is null. */ public void testOverrideRule() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setLastName("Smith"); // Construct validator based on the loaded resources // and the form key Validator validator = new Validator(resources, FORM_KEY2); // 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 firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName"); assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have '" + CHECK_MSG_KEY + " as a key.", firstNameResult.field.getArg(0).getKey().equals(CHECK_MSG_KEY)); assertNull("Last Name ValidatorResult should be null.", lastNameResult); }
/** * Tests the required validation for last name. */ public void testRequiredLastName() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setLastName("Smith"); // 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 firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName"); assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION)); }
ValidatorResult firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName");
/** * Tests the required validation for first name. */ public void testRequiredFirstName() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setFirstName("Joe"); // 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 firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName"); assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); }
ValidatorResult firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName");
ValidatorResult firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName");
ValidatorResult firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName");
/** * Tests the required validation for last name if it is blank. */ public void testRequiredLastNameBlank() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setLastName(""); // 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 firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName"); assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); }
/** * If the first name is there, and the last name fails int, we should get one error. */ public void testRequiredLastNameShort() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setFirstName("Test"); name.setLastName("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 firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName"); assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int")); assertTrue("Last Name ValidatorResult for the 'int' action should have failed.", !lastNameResult.isValid("int")); }
ValidatorResult firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName");
/** * Tests the required validation for first and last name. */ public void testRequiredName() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setFirstName("Joe"); name.setLastName("Smith"); // 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 firstNameResult = results.getValidatorResult("firstName"); ValidatorResult lastNameResult = results.getValidatorResult("lastName"); assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION)); }
/** * 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))); }
/** * 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); validator.setParameter(Validator.LOCALE_PARAM, Locale.US); // 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))); }
ValidatorResult result = results.getValidatorResult("value");
/** * 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")); }