    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  import java.io.InputStream;
    21
    22  import junit.framework.TestCase;
    23
    24  import org.xml.sax.SAXException;
    25
    26  /***
    27   * Tests that validator rules split between 2 different XML files get
    28   * merged properly.
    29   *
    30   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
    31   */
    32  public class MultipleConfigFilesTest extends TestCase {
    33
    34      /***
    35       * Resources used for validation tests.
    36       */
    37      private ValidatorResources resources = null;
    38
    39      /***
    40       * The key used to retrieve the set of validation
    41       * rules from the xml file.
    42       */
    43      private static final String FORM_KEY = "nameForm";
    44
    45      /***
    46       * The key used to retrieve the validator action.
    47       */
    48      private static final String ACTION = "required";
    49
    50      /***
    51       * Constructor for MultipleConfigFilesTest.
    52       * @param name
    53       */
    54      public MultipleConfigFilesTest(String name) {
    55          super(name);
    56      }
    57
    58      /***
    59       * Load <code>ValidatorResources</code> from multiple xml files.
    60       */
    61      protected void setUp() throws IOException, SAXException {
    62          InputStream[] streams =
    63              new InputStream[] {
    64                  this.getClass().getResourceAsStream(
    65                      "MultipleConfigFilesTest-1-config.xml"),
    66                  this.getClass().getResourceAsStream(
    67                      "MultipleConfigFilesTest-2-config.xml")};
    68
    69          this.resources = new ValidatorResources(streams);
    70
    71          for (int i = 0; i < streams.length; i++) {
    72              streams[i].close();
    73          }
    74      }
    75
    76     /***
    77      * Check the forms and constants from different config files have
    78      * been merged into the same FormSet.
    79      */
    80      public void testMergedConfig() throws ValidatorException {
    81
    82          // *********** Default Locale *******************
    83
    84          // Check the form from the first config file exists
    85          Form form1 = resources.getForm("", "", "", "testForm1");
    86          assertNotNull("Form 'testForm1' not found", form1);
    87
    88          // Check the form from the second config file exists
    89          Form form2 = resources.getForm("", "", "", "testForm2");
    90          assertNotNull("Form 'testForm2' not found", form2);
    91
    92          // Check the Constants  for the form from the first config file
    93          Field field1 = form1.getField("testProperty1");
    94          assertEquals("testProperty1 - const 1", "testConstValue1", field1.getVarValue("var11"));
    95          assertEquals("testProperty1 - const 2", "testConstValue2", field1.getVarValue("var12"));
    96
    97          // Check the Constants  for the form from the second config file
    98          Field field2 = form2.getField("testProperty2");
    99          assertEquals("testProperty2 - const 1", "testConstValue1", field2.getVarValue("var21"));
    100         assertEquals("testProperty2 - const 2", "testConstValue2", field2.getVarValue("var22"));
    101
    102         // *********** 'fr' locale *******************
    103
    104         // Check the form from the first config file exists
    105         Form form1_fr = resources.getForm("fr", "", "", "testForm1_fr");
    106         assertNotNull("Form 'testForm1_fr' not found", form1_fr);
    107
    108         // Check the form from the second config file exists
    109         Form form2_fr = resources.getForm("fr", "", "", "testForm2_fr");
    110         assertNotNull("Form 'testForm2_fr' not found", form2_fr);
    111
    112         // Check the Constants  for the form from the first config file
    113         Field field1_fr = form1_fr.getField("testProperty1_fr");
    114         assertEquals("testProperty1_fr - const 1", "testConstValue1_fr", field1_fr.getVarValue("var11_fr"));
    115         assertEquals("testProperty1_fr - const 2", "testConstValue2_fr", field1_fr.getVarValue("var12_fr"));
    116
    117         // Check the Constants  for the form from the second config file
    118         Field field2_fr = form2_fr.getField("testProperty2_fr");
    119         assertEquals("testProperty2_fr - const 1", "testConstValue1_fr", field2_fr.getVarValue("var21_fr"));
    120         assertEquals("testProperty2_fr - const 2", "testConstValue2_fr", field2_fr.getVarValue("var22_fr"));
    121     }
    122
    123     /***
    124     * With nothing provided, we should fail both because both are required.
    125     */
    126     public void testBothBlank() throws ValidatorException {
    127         // Create bean to run test on.
    128         NameBean name = new NameBean();
    129
    130         // Construct validator based on the loaded resources
    131         // and the form key
    132         Validator validator = new Validator(resources, FORM_KEY);
    133         // add the name bean to the validator as a resource
    134         // for the validations to be performed on.
    135         validator.setParameter(Validator.BEAN_PARAM, name);
    136
    137         // Get results of the validation.
    138         ValidatorResults results = null;
    139
    140         // throws ValidatorException,
    141         // but we aren't catching for testing
    142         // since no validation methods we use
    143         // throw this
    144         results = validator.validate();
    145
    146         assertNotNull("Results are null.", results);
    147
    148         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
    149         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
    150
    151         assertNotNull(firstNameResult);
    152         assertTrue(firstNameResult.containsAction(ACTION));
    153         assertTrue(!firstNameResult.isValid(ACTION));
    154
    155         assertNotNull(lastNameResult);
    156         assertTrue(lastNameResult.containsAction(ACTION));
    157         assertTrue(!lastNameResult.isValid(ACTION));
    158         assertTrue(!lastNameResult.containsAction("int"));
    159     }
    160
    161     /***
    162      * If the first name fails required, and the second test fails int, we should get two errors.
    163     */
    164     public void testRequiredFirstNameBlankLastNameShort()
    165         throws ValidatorException {
    166         // Create bean to run test on.
    167         NameBean name = new NameBean();
    168         name.setFirstName("");
    169         name.setLastName("Test");
    170
    171         // Construct validator based on the loaded resources
    172         // and the form key
    173         Validator validator = new Validator(resources, FORM_KEY);
    174         // add the name bean to the validator as a resource
    175         // for the validations to be performed on.
    176         validator.setParameter(Validator.BEAN_PARAM, name);
    177
    178         // Get results of the validation.
    179         ValidatorResults results = null;
    180
    181         results = validator.validate();
    182
    183         assertNotNull("Results are null.", results);
    184
    185         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
    186         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
    187
    188         assertNotNull(firstNameResult);
    189         assertTrue(firstNameResult.containsAction(ACTION));
    190         assertTrue(!firstNameResult.isValid(ACTION));
    191
    192         assertNotNull(lastNameResult);
    193         assertTrue(lastNameResult.containsAction("int"));
    194         assertTrue(!lastNameResult.isValid("int"));
    195     }
    196
    197     /***
    198      * If the first name is there, and the last name fails int, we should get one error.
    199     */
    200     public void testRequiredLastNameShort() throws ValidatorException {
    201         // Create bean to run test on.
    202         NameBean name = new NameBean();
    203         name.setFirstName("Test");
    204         name.setLastName("Test");
    205
    206         // Construct validator based on the loaded resources
    207         // and the form key
    208         Validator validator = new Validator(resources, FORM_KEY);
    209         // add the name bean to the validator as a resource
    210         // for the validations to be performed on.
    211         validator.setParameter(Validator.BEAN_PARAM, name);
    212
    213         // Get results of the validation.
    214         ValidatorResults results = null;
    215
    216         results = validator.validate();
    217
    218         assertNotNull("Results are null.", results);
    219
    220         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
    221         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
    222
    223         assertNotNull(firstNameResult);
    224         assertTrue(firstNameResult.containsAction(ACTION));
    225         assertTrue(firstNameResult.isValid(ACTION));
    226
    227         assertNotNull(lastNameResult);
    228         assertTrue(lastNameResult.containsAction("int"));
    229         assertTrue(!lastNameResult.isValid("int"));
    230     }
    231
    232     /***
    233      * If first name is ok and last name is ok and is an int, no errors.
    234     */
    235     public void testRequiredLastNameLong() throws ValidatorException {
    236         // Create bean to run test on.
    237         NameBean name = new NameBean();
    238         name.setFirstName("Joe");
    239         name.setLastName("12345678");
    240
    241         // Construct validator based on the loaded resources
    242         // and the form key
    243         Validator validator = new Validator(resources, FORM_KEY);
    244         // add the name bean to the validator as a resource
    245         // for the validations to be performed on.
    246         validator.setParameter(Validator.BEAN_PARAM, name);
    247
    248         // Get results of the validation.
    249         ValidatorResults results = null;
    250
    251         results = validator.validate();
    252
    253         assertNotNull("Results are null.", results);
    254
    255         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
    256         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
    257
    258         assertNotNull(firstNameResult);
    259         assertTrue(firstNameResult.containsAction(ACTION));
    260         assertTrue(firstNameResult.isValid(ACTION));
    261
    262         assertNotNull(lastNameResult);
    263         assertTrue(lastNameResult.containsAction("int"));
    264         assertTrue(lastNameResult.isValid("int"));
    265     }
    266
    267 }