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