    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.validator;
    18
    19  import java.io.IOException;
    20
    21  import junit.framework.Test;
    22  import junit.framework.TestSuite;
    23
    24  import org.xml.sax.SAXException;
    25
    26  /***
    27   * Performs Validation Test.
    28   *
    29   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
    30   */
    31  public class RequiredIfTest extends TestCommon {
    32
    33     /***
    34      * The key used to retrieve the set of validation
    35      * rules from the xml file.
    36      */
    37     protected static String FORM_KEY = "nameForm";
    38
    39     /***
    40      * The key used to retrieve the validator action.
    41      */
    42     protected static String ACTION = "requiredif";
    43
    44     public RequiredIfTest(String name) {
    45         super(name);
    46     }
    47
    48     /***
    49      * Start the tests.
    50      *
    51      * @param theArgs the arguments. Not used
    52      */
    53     public static void main(String[] theArgs) {
    54         junit.awtui.TestRunner.main(new String[] {RequiredIfTest.class.getName()});
    55     }
    56
    57     /***
    58      * @return a test suite (<code>TestSuite</code>) that includes all methods
    59      *         starting with "test"
    60      */
    61     public static Test suite() {
    62         // All methods starting with "test" will be executed in the test suite.
    63         return new TestSuite(RequiredIfTest.class);
    64     }
    65
    66     /***
    67      * Load <code>ValidatorResources</code> from
    68      * validator-requiredif.xml.
    69      */
    70     protected void setUp() throws IOException, SAXException {
    71        // Load resources
    72        loadResources("RequiredIfTest-config.xml");
    73     }
    74
    75     protected void tearDown() {
    76     }
    77
    78     /***
    79      * With nothing provided, we should pass since the fields only fail on
    80      * null if the other field is non-blank.
    81      */
    82     public void testRequired() throws ValidatorException {
    83        // Create bean to run test on.
    84        NameBean name = new NameBean();
    85
    86        // Construct validator based on the loaded resources
    87        // and the form key
    88        Validator validator = new Validator(resources, FORM_KEY);
    89        // add the name bean to the validator as a resource
    90        // for the validations to be performed on.
    91        validator.setParameter(Validator.BEAN_PARAM, name);
    92
    93        // Get results of the validation.
    94        ValidatorResults results = null;
    95
    96        // throws ValidatorException,
    97        // but we aren't catching for testing
    98        // since no validation methods we use
    99        // throw this
    100       results = validator.validate();
    101
    102       assertNotNull("Results are null.", results);
    103
    104       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
    105       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
    106
    107       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
    108       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
    109       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
    110
    111       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
    112       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
    113       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
    114    }
    115
    116    /***
    117     * Tests the required validation for first name if it is blank.
    118     */
    119    public void testRequiredFirstNameBlank() throws ValidatorException {
    120       // Create bean to run test on.
    121       NameBean name = new NameBean();
    122       name.setFirstName("");
    123       name.setLastName("Test");
    124
    125       // Construct validator based on the loaded resources
    126       // and the form key
    127       Validator validator = new Validator(resources, FORM_KEY);
    128       // add the name bean to the validator as a resource
    129       // for the validations to be performed on.
    130       validator.setParameter(Validator.BEAN_PARAM, name);
    131
    132       // Get results of the validation.
    133       ValidatorResults results = null;
    134
    135       results = validator.validate();
    136
    137       assertNotNull("Results are null.", results);
    138
    139       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
    140       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
    141
    142       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
    143       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
    144       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
    145
    146       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
    147       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
    148       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
    149    }
    150
    151    /***
    152     * Tests the required validation for last name.
    153     */
    154    public void testRequiredFirstName() throws ValidatorException {
    155       // Create bean to run test on.
    156       NameBean name = new NameBean();
    157       name.setFirstName("Test");
    158       name.setLastName("Test");
    159
    160       // Construct validator based on the loaded resources
    161       // and the form key
    162       Validator validator = new Validator(resources, FORM_KEY);
    163       // add the name bean to the validator as a resource
    164       // for the validations to be performed on.
    165       validator.setParameter(Validator.BEAN_PARAM, name);
    166
    167       // Get results of the validation.
    168       ValidatorResults results = null;
    169
    170       results = validator.validate();
    171
    172       assertNotNull("Results are null.", results);
    173
    174       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
    175       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
    176
    177       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
    178       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
    179       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
    180
    181       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
    182       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
    183       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
    184    }
    185
    186    /***
    187     * Tests the required validation for last name if it is blank.
    188     */
    189    public void testRequiredLastNameBlank() throws ValidatorException {
    190       // Create bean to run test on.
    191       NameBean name = new NameBean();
    192       name.setFirstName("Joe");
    193       name.setLastName("");
    194
    195       // Construct validator based on the loaded resources
    196       // and the form key
    197       Validator validator = new Validator(resources, FORM_KEY);
    198       // add the name bean to the validator as a resource
    199       // for the validations to be performed on.
    200       validator.setParameter(Validator.BEAN_PARAM, name);
    201
    202       // Get results of the validation.
    203       ValidatorResults results = null;
    204
    205       results = validator.validate();
    206
    207       assertNotNull("Results are null.", results);
    208
    209       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
    210       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
    211
    212       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
    213       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
    214       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
    215
    216       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
    217       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
    218       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
    219    }
    220
    221    /***
    222     * Tests the required validation for last name.
    223     */
    224    public void testRequiredLastName() throws ValidatorException {
    225       // Create bean to run test on.
    226       NameBean name = new NameBean();
    227       name.setFirstName("Joe");
    228       name.setLastName("Smith");
    229
    230       // Construct validator based on the loaded resources
    231       // and the form key
    232       Validator validator = new Validator(resources, FORM_KEY);
    233       // add the name bean to the validator as a resource
    234       // for the validations to be performed on.
    235       validator.setParameter(Validator.BEAN_PARAM, name);
    236
    237       // Get results of the validation.
    238       ValidatorResults results = null;
    239
    240       results = validator.validate();
    241
    242       assertNotNull("Results are null.", results);
    243
    244       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
    245       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
    246
    247       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
    248       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
    249       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
    250
    251       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
    252       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
    253       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
    254
    255    }
    256
    257 }