commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwinterfe...@apache.org
Subject cvs commit: jakarta-commons/validator/src/test/org/apache/commons/validator EmailTest.java validator-regexp.xml TestValidator.java ValidatorTestSuite.java
Date Tue, 02 Apr 2002 01:59:45 GMT
dwinterfeldt    02/04/01 17:59:45

  Modified:    validator/src/test/org/apache/commons/validator
                        TestValidator.java ValidatorTestSuite.java
  Added:       validator/src/test/org/apache/commons/validator
                        EmailTest.java validator-regexp.xml
  Log:
  Added new unit tests.
  
  Revision  Changes    Path
  1.4       +18 -4     jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java
  
  Index: TestValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestValidator.java	30 Mar 2002 04:32:14 -0000	1.3
  +++ TestValidator.java	2 Apr 2002 01:59:44 -0000	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java,v
1.3 2002/03/30 04:32:14 dwinterfeldt Exp $
  - * $Revision: 1.3 $
  - * $Date: 2002/03/30 04:32:14 $
  + * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java,v
1.4 2002/04/02 01:59:44 dwinterfeldt Exp $
  + * $Revision: 1.4 $
  + * $Date: 2002/04/02 01:59:44 $
    *
    * ====================================================================
    *
  @@ -67,7 +67,7 @@
    * unit tests.</p> 
    *
    * @author David Winterfeldt
  - * @version $Revision: 1.3 $ $Date: 2002/03/30 04:32:14 $
  + * @version $Revision: 1.4 $ $Date: 2002/04/02 01:59:44 $
   */                                                       
   public class TestValidator {
                                                             
  @@ -168,6 +168,20 @@
         String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
   
         return GenericValidator.isDouble(value);
  +   }
  +
  +   /**
  +    * Checks if the field is an e-mail address.
  +    *
  +    * @param 	value 		The value validation is being performed on.
  +    * @return	boolean		If the field is an e-mail address
  +    *                           <code>true</code> is returned.  
  +    *                           Otherwise <code>false</code>.
  +   */
  +   public static boolean validateEmail(Object bean, Field field) {
  +      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +
  +      return GenericValidator.isEmail(value);
      }
         
   }                                                         
  
  
  
  1.3       +5 -4      jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java
  
  Index: ValidatorTestSuite.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ValidatorTestSuite.java	30 Mar 2002 04:31:29 -0000	1.2
  +++ ValidatorTestSuite.java	2 Apr 2002 01:59:44 -0000	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java,v
1.2 2002/03/30 04:31:29 dwinterfeldt Exp $
  - * $Revision: 1.2 $
  - * $Date: 2002/03/30 04:31:29 $
  + * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java,v
1.3 2002/04/02 01:59:44 dwinterfeldt Exp $
  + * $Revision: 1.3 $
  + * $Date: 2002/04/02 01:59:44 $
    *
    * ====================================================================
    *
  @@ -72,7 +72,7 @@
    * package.
    *
    * @author David Winterfeldt
  - * @version $Revision: 1.2 $ $Date: 2002/03/30 04:31:29 $
  + * @version $Revision: 1.3 $ $Date: 2002/04/02 01:59:44 $
   */                                                       
   public class ValidatorTestSuite extends TestCase {            
                                                             
  @@ -91,6 +91,7 @@
          suite.addTest(FloatTest.suite()); 
          suite.addTest(DoubleTest.suite()); 
          suite.addTest(TypeTest.suite()); 
  +       suite.addTest(EmailTest.suite()); 
          suite.addTest(ValidatorTest.suite()); 
   
          return suite;
  
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/EmailTest.java
  
  Index: EmailTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/EmailTest.java,v
1.1 2002/04/02 01:59:44 dwinterfeldt Exp $
   * $Revision: 1.1 $
   * $Date: 2002/04/02 01:59:44 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.validator;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.Map;
  import junit.framework.Test;                           
  import junit.framework.TestCase;                          
  import junit.framework.TestSuite;
  import junit.framework.AssertionFailedError;              
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogSource;
  
                                                            
  /**                                                       
   * <p>Performs Validation Test for e-mail validations.</p> 
   *
   * @author David Winterfeldt
   * @version $Revision: 1.1 $ $Date: 2002/04/02 01:59:44 $
  */                                                       
  public class EmailTest extends TestCase {            
     
     /**
      * The key used to retrieve the set of validation 
      * rules from the xml file.
     */
     protected static String FORM_KEY = "emailForm";   
  
     /**
      * The key used to retrieve the validator action.
     */
     protected static String ACTION = "email";
  
     
     /**
      * Commons Logging instance.
     */
     private Log log = LogSource.getInstance(this.getClass().getName());
     
     /**
      * Resources used for validation tests.
     */
     private ValidatorResources resources = null;
     
     public EmailTest(String name) {                  
         super(name);                                      
     }                                                     
  
     /**
      * Start the tests.
      *
      * @param theArgs the arguments. Not used
      */
     public static void main(String[] theArgs) {
         junit.awtui.TestRunner.main(new String[] {EmailTest.class.getName()});
     }
  
     /**
      * @return a test suite (<code>TestSuite</code>) that includes all methods
      *         starting with "test"
      */
     public static Test suite() {
         // All methods starting with "test" will be executed in the test suite.
         return new TestSuite(EmailTest.class);
     }
  
     /**
      * Load <code>ValidatorResources</code> from 
      * validator-name-required.xml.
     */
     protected void setUp() throws IOException {
        // Load resources
        InputStream in = null;
        resources = new ValidatorResources();
        
        try {
           in = this.getClass().getResourceAsStream("validator-regexp.xml");
           ValidatorResourcesInitializer.initialize(resources, in);
        } catch (IOException e) {
           log.error(e.getMessage(), e);
           throw e;
        } finally {
           if (in != null) {
              try { in.close(); } catch (Exception e) {}	
           }
        }
     }
  
     protected void tearDown() {
     }
  
     /**
      * Tests the e-mail validation.
     */
     public void testEmail() throws ValidatorException {
        // Create bean to run test on.
        ValueBean info = new ValueBean();
  
        info.setValue("jsmith@apache.org");
        valueTest(info, true);
     }
  
     /**
      * Tests the e-mail validation.
     */
     public void testEmailExtension() throws ValidatorException {
        // Create bean to run test on.
        ValueBean info = new ValueBean();
  
        info.setValue("jsmith@apache.org");
        valueTest(info, true);
  
        info.setValue("jsmith@apache.com");
        valueTest(info, true);
  
        info.setValue("jsmith@apache.net");
        valueTest(info, true);
  
        info.setValue("jsmith@apache.info");
        valueTest(info, true);
  
     }
  
     /**
      * <p>Tests the e-mail validation with a dash in 
      * the address.</p>
     */
     public void testEmailWithDash() throws ValidatorException {
        // Create bean to run test on.
        ValueBean info = new ValueBean();
  
        info.setValue("andy.noble@data-workshop.com");
        valueTest(info, true);
     }
  
     /**
      * 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(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.addResource(Validator.BEAN_KEY, 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("ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
        assertTrue("ValidatorResult for the '" + ACTION +"' action should have " + (passed
? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
     }
  }                                                         
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/validator-regexp.xml
  
  Index: validator-regexp.xml
  ===================================================================
  <form-validation>
     <global>
        <validator name="email"
                   classname="org.apache.commons.validator.TestValidator"
                   method="validateEmail"
                   methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
     </global>
     <formset>
        <form    name="emailForm">
           <field    property="value"
           	   depends="email">
           </field>    
        </form>
     </formset>   
  </form-validation>
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message