/** * 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)); }
/** * 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))); }
/** * 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")); }
/** * 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)); }
/** * 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)); }
/** * 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)); }
/** * 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")); }
/** * If the first name fails required, and the second test fails int, we should get two errors. */ public void testRequiredFirstNameBlankLastNameShort() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setFirstName(""); 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 failed.", !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")); }
/** * With nothing provided, we should fail both because both are required. */ public void testBothBlank() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); // 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; // 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 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("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 failed.", !lastNameResult.isValid(ACTION)); assertTrue("Last Name ValidatorResults should not contain the 'int' action.", !lastNameResult.containsAction("int")); }
/** * If the first name fails required, and the second test fails int, we should get two errors. */ public void testRequiredFirstNameBlankLastNameShort() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setFirstName(""); 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(firstNameResult); assertTrue(firstNameResult.containsAction(ACTION)); assertTrue(!firstNameResult.isValid(ACTION)); assertNotNull(lastNameResult); assertTrue(lastNameResult.containsAction("int")); assertTrue(!lastNameResult.isValid("int")); }
/** * 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)); }
/** * 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(firstNameResult); assertTrue(firstNameResult.containsAction(ACTION)); assertTrue(firstNameResult.isValid(ACTION)); assertNotNull(lastNameResult); assertTrue(lastNameResult.containsAction("int")); assertTrue(!lastNameResult.isValid("int")); }