    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.util.Locale;
    21
    22  import junit.framework.Test;
    23  import junit.framework.TestSuite;
    24
    25  import org.xml.sax.SAXException;
    26
    27  /***
    28   * Performs Validation Test for locale validations.
    29   *
    30   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
    31   */
    32  public class LocaleTest extends TestCommon {
    33
    34      /***
    35       * The key used to retrieve the set of validation rules from the xml file.
    36       */
    37      protected static String FORM_KEY = "nameForm";
    38
    39      /*** The key used to retrieve the validator action.  */
    40      protected static String ACTION = "required";
    41
    42      /***
    43       * Constructor for the LocaleTest object
    44       *
    45       * @param name  param
    46       */
    47      public LocaleTest(String name) {
    48          super(name);
    49      }
    50
    51      /***
    52       * Start the tests.
    53       *
    54       * @param theArgs  the arguments. Not used
    55       */
    56      public static void main(String[] theArgs) {
    57          junit.awtui.TestRunner.main(new String[]{LocaleTest.class.getName()});
    58      }
    59
    60      /***
    61       * @return   a test suite (<code>TestSuite</code>) that includes all methods
    62       *      starting with "test"
    63       */
    64      public static Test suite() {
    65          // All methods starting with "test" will be executed in the test suite.
    66          return new TestSuite(LocaleTest.class);
    67      }
    68
    69      /***
    70       * Load <code>ValidatorResources</code> from validator-locale.xml.
    71       *
    72       * @exception IOException   If something goes wrong
    73       * @exception SAXException  If something goes wrong
    74       */
    75      protected void setUp()
    76          throws IOException, SAXException {
    77          // Load resources
    78          loadResources("LocaleTest-config.xml");
    79      }
    80
    81      /*** The teardown method for JUnit */
    82      protected void tearDown() {
    83      }
    84
    85      /***
    86       * See what happens when we try to validate with a Locale, Country and
    87       * variant. Also check if the added locale validation field is getting used.
    88       *
    89       * @exception ValidatorException  If something goes wrong
    90       */
    91      public void testLocale1()
    92          throws ValidatorException {
    93          // Create bean to run test on.
    94          NameBean name = new NameBean();
    95          name.setFirstName("");
    96          name.setLastName("");
    97
    98          valueTest(name, new Locale("en", "US", "TEST1"), false, false, false);
    99      }
    100
    101     /***
    102      * See what happens when we try to validate with a Locale, Country and
    103      * variant
    104      *
    105      * @exception ValidatorException  If something goes wrong
    106      */
    107     public void testLocale2()
    108         throws ValidatorException {
    109         // Create bean to run test on.
    110         NameBean name = new NameBean();
    111         name.setFirstName("");
    112         name.setLastName("");
    113
    114         valueTest(name, new Locale("en", "US", "TEST2"), true, false, true);
    115     }
    116
    117     /***
    118      * See what happens when we try to validate with a Locale, Country and
    119      * variant
    120      *
    121      * @exception ValidatorException  If something goes wrong
    122      */
    123     public void testLocale3()
    124         throws ValidatorException {
    125         // Create bean to run test on.
    126         NameBean name = new NameBean();
    127         name.setFirstName("");
    128         name.setLastName("");
    129
    130         valueTest(name, new Locale("en", "UK"), false, true, true);
    131     }
    132
    133     /***
    134      * See if a locale of en_UK_TEST falls back to en_UK instead of default form
    135      * set. Bug #16920 states that this isn't happening, even though it is
    136      * passing this test. see #16920.
    137      *
    138      * @exception ValidatorException  If something goes wrong
    139      */
    140     public void testLocale4()
    141         throws ValidatorException {
    142         // Create bean to run test on.
    143         NameBean name = new NameBean();
    144         name.setFirstName("");
    145         name.setLastName("");
    146
    147         valueTest(name, new Locale("en", "UK", "TEST"), false, true, true);
    148     }
    149
    150     /***
    151      * See if a locale of language=en falls back to default form set.
    152      *
    153      * @exception ValidatorException  If something goes wrong
    154      */
    155     public void testLocale5()
    156         throws ValidatorException {
    157         // Create bean to run test on.
    158         NameBean name = new NameBean();
    159         name.setFirstName("");
    160         name.setLastName("");
    161
    162         valueTest(name, new Locale("en", ""), false, false, true);
    163     }
    164
    165     /***
    166      * Utlity class to run a test on a value.
    167      *
    168      * @param name                    param
    169      * @param loc                     param
    170      * @param firstGood               param
    171      * @param lastGood                param
    172      * @param middleGood              param
    173      * @exception ValidatorException  If something goes wrong
    174      */
    175     private void valueTest(Object name, Locale loc, boolean firstGood, boolean lastGood, boolean middleGood)
    176         throws ValidatorException {
    177
    178         // Construct validator based on the loaded resources
    179         // and the form key
    180         Validator validator = new Validator(resources, FORM_KEY);
    181         // add the name bean to the validator as a resource
    182         // for the validations to be performed on.
    183         validator.setParameter(Validator.BEAN_PARAM, name);
    184         validator.setParameter(Validator.LOCALE_PARAM, loc);
    185         // Get results of the validation.
    186         ValidatorResults results = null;
    187
    188         // throws ValidatorException,
    189         // but we aren't catching for testing
    190         // since no validation methods we use
    191         // throw this
    192         results = validator.validate();
    193
    194         assertNotNull("Results are null.", results);
    195
    196         ValidatorResult resultlast = results.getValidatorResult("lastName");
    197         ValidatorResult resultfirst = results.getValidatorResult("firstName");
    198         ValidatorResult resultmiddle = results.getValidatorResult("middleName");
    199
    200         if (firstGood) {
    201             assertNull(resultfirst);
    202         }
    203         else {
    204             assertNotNull(resultfirst);
    205         }
    206
    207         if (middleGood) {
    208             assertNull(resultmiddle);
    209         }
    210         else {
    211             assertNotNull(resultmiddle);
    212         }
    213
    214         if (lastGood) {
    215             assertNull(resultlast);
    216         }
    217         else {
    218             assertNotNull(resultlast);
    219         }
    220     }
    221 }
    222