Index: EmailTest.java =================================================================== --- EmailTest.java (revisi¢n: 413349) +++ EmailTest.java (copia de trabajo) @@ -28,9 +28,9 @@ import org.xml.sax.SAXException; -/** +/** * Performs Validation Test for e-mail validations. - */ + */ public class EmailTest extends TestCommon { /** @@ -45,9 +45,9 @@ protected static String ACTION = "email"; - public EmailTest(String name) { - super(name); - } + public EmailTest(String name) { + super(name); + } /** * Start the tests. @@ -68,7 +68,7 @@ } /** - * Load ValidatorResources from + * Load ValidatorResources from * validator-regexp.xml. */ protected void setUp() throws IOException, SAXException { @@ -88,7 +88,7 @@ info.setValue("jsmith@apache.org"); valueTest(info, true); } - + /** * Tests the email validation with numeric domains. */ @@ -124,16 +124,16 @@ info.setValue("jsmith@apache.c"); valueTest(info, false); - + info.setValue("someone@yahoo.museum"); valueTest(info, true); - + info.setValue("someone@yahoo.mu-seum"); valueTest(info, false); } /** - *

Tests the e-mail validation with a dash in + *

Tests the e-mail validation with a dash in * the address.

*/ public void testEmailWithDash() throws ValidatorException { @@ -154,7 +154,7 @@ } /** - * Tests the e-mail validation with a dot at the end of + * Tests the e-mail validation with a dot at the end of * the address. */ public void testEmailWithDotEnd() throws ValidatorException { @@ -176,11 +176,11 @@ info.setValue("andy.noble@\u008fdata-workshop.com"); valueTest(info, false); - + // The ' character is valid in an email username. info.setValue("andy.o'reilly@data-workshop.com"); valueTest(info, true); - + // But not in the domain name. info.setValue("andy@o'reilly.data-workshop.com"); valueTest(info, false); @@ -188,7 +188,7 @@ info.setValue("foo+bar@i.am.not.in.us.example.com"); valueTest(info, true); } - + /** * Tests the email validation with commas. */ @@ -202,7 +202,7 @@ valueTest(info, false); } - + /** * Tests the email validation with spaces. */ @@ -224,6 +224,19 @@ } /** + * Tests the email validation with invalid chars. + */ + public void testEmailWithControlChars() throws ValidatorException { + for (char c = 0; c < 32; c++) { + final String emailToTest = "" + c + "local@domain.com"; + ValueBean info = new ValueBean(); + info.setValue(emailToTest); + valueTest(info, false, c); + } + + } + + /** * Write this test according to parts of RFC, as opposed to the type of character * that is being tested. * @@ -381,9 +394,9 @@ /** * Write this test based on perl Mail::RFC822::Address * which takes its example email address directly from RFC822 - * + * * @throws ValidatorException - * + * * FIXME This test fails so disable it with a leading _ for 1.1.4 release. * The real solution is to fix the email parsing. */ @@ -402,28 +415,60 @@ * @param passed Whether or not the test is expected to pass. */ private void valueTest(ValueBean info, boolean passed) throws ValidatorException { - // Construct validator based on the loaded resources + // 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 + // 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 + + // 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("Value "+info.getValue()+" ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION)); assertTrue("Value "+info.getValue()+"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. + */ + private void valueTest(ValueBean info, boolean passed, char currentChar) 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("Value "+info.getValue()+" ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION)); + assertTrue("Value "+info.getValue()+"ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") +" with ASCII char [" + (int)currentChar + "].", (passed ? result.isValid(ACTION) : !result.isValid(ACTION))); + } +}