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