    1   /*
    2    * $Id: TypeTest.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.Iterator;
    26  import java.util.Locale;
    27  import java.util.Map;
    28
    29  import junit.framework.Test;
    30  import junit.framework.TestSuite;
    31
    32  import org.xml.sax.SAXException;
    33
    34  /***
    35   * Performs Validation Test for type validations.
    36   */
    37  public class TypeTest extends TestCommon {
    38
    39     /***
    40      * The key used to retrieve the set of validation
    41      * rules from the xml file.
    42      */
    43     protected static String FORM_KEY = "typeForm";
    44
    45     /***
    46      * The key used to retrieve the validator action.
    47      */
    48     protected static String ACTION = "byte";
    49
    50     public TypeTest(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[] {TypeTest.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(TypeTest.class);
    70     }
    71
    72     /***
    73      * Load <code>ValidatorResources</code> from
    74      * validator-type.xml.
    75      */
    76     protected void setUp() throws IOException, SAXException {
    77        // Load resources
    78        loadResources("TypeTest-config.xml");
    79     }
    80
    81     protected void tearDown() {
    82     }
    83
    84     /***
    85      * Tests the byte validation.
    86      */
    87     public void testType() throws ValidatorException {
    88        // Create bean to run test on.
    89        TypeBean info = new TypeBean();
    90        info.setByte("12");
    91        info.setShort("129");
    92        info.setInteger("-144");
    93        info.setLong("88000");
    94        info.setFloat("12.1555f");
    95        info.setDouble("129.1551511111d");
    96
    97        // Construct validator based on the loaded resources
    98        // and the form key
    99        Validator validator = new Validator(resources, FORM_KEY);
    100       // add the name bean to the validator as a resource
    101       // for the validations to be performed on.
    102       validator.setParameter(Validator.BEAN_PARAM, info);
    103
    104       // Get results of the validation.
    105       ValidatorResults results = null;
    106
    107       // throws ValidatorException,
    108       // but we aren't catching for testing
    109       // since no validation methods we use
    110       // throw this
    111       results = validator.validate();
    112
    113       assertNotNull("Results are null.", results);
    114
    115       Map hResultValues = results.getResultValueMap();
    116
    117       assertTrue("Expecting byte result to be an instance of Byte.", (hResultValues.get("byte") instanceof Byte));
    118       assertTrue("Expecting short result to be an instance of Short.", (hResultValues.get("short") instanceof Short));
    119       assertTrue("Expecting integer result to be an instance of Integer.", (hResultValues.get("integer") instanceof Integer));
    120       assertTrue("Expecting long result to be an instance of Long.", (hResultValues.get("long") instanceof Long));
    121       assertTrue("Expecting float result to be an instance of Float.", (hResultValues.get("float") instanceof Float));
    122       assertTrue("Expecting double result to be an instance of Double.", (hResultValues.get("double") instanceof Double));
    123
    124       for (Iterator i = hResultValues.keySet().iterator(); i.hasNext(); ) {
    125          String key = (String)i.next();
    126          Object value = hResultValues.get(key);
    127
    128          assertNotNull("value ValidatorResults.getResultValueMap() should not be null.", value);
    129       }
    130
    131       //ValidatorResult result = results.getValidatorResult("value");
    132
    133       //assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
    134       //assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
    135       //assertTrue(ACTION + " value ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
    136
    137    }
    138
    139    /***
    140     * Tests the us locale
    141     */
    142    public void testUSLocale() throws ValidatorException {
    143       // Create bean to run test on.
    144       TypeBean info = new TypeBean();
    145       info.setByte("12");
    146       info.setShort("129");
    147       info.setInteger("-144");
    148       info.setLong("88000");
    149       info.setFloat("12.1555");
    150       info.setDouble("129.1551511111");
    151       localeTest(info, Locale.US);
    152    }
    153
    154    /***
    155     * Tests the fr locale.
    156     */
    157    public void testFRLocale() throws ValidatorException {
    158       // Create bean to run test on.
    159       TypeBean info = new TypeBean();
    160       info.setByte("12");
    161       info.setShort("-129");
    162       info.setInteger("1443");
    163       info.setLong("88000");
    164       info.setFloat("12,1555");
    165       info.setDouble("129,1551511111");
    166       Map map = localeTest(info, Locale.FRENCH);
    167       assertTrue("float value not correct", ((Float)map.get("float")).intValue() == 12);
    168       assertTrue("double value not correct", ((Double)map.get("double")).intValue() == 129);
    169   }
    170
    171   /***
    172     * Tests the locale.
    173     */
    174    private Map localeTest(TypeBean info, Locale locale) throws ValidatorException {
    175
    176       // Construct validator based on the loaded resources
    177       // and the form key
    178       Validator validator = new Validator(resources, "typeLocaleForm");
    179       // add the name bean to the validator as a resource
    180       // for the validations to be performed on.
    181       validator.setParameter(Validator.BEAN_PARAM, info);
    182       validator.setParameter("java.util.Locale", locale);
    183
    184       // Get results of the validation.
    185       ValidatorResults results = null;
    186
    187       // throws ValidatorException,
    188       // but we aren't catching for testing
    189       // since no validation methods we use
    190       // throw this
    191       results = validator.validate();
    192
    193       assertNotNull("Results are null.", results);
    194
    195       Map hResultValues = results.getResultValueMap();
    196
    197       assertTrue("Expecting byte result to be an instance of Byte for locale: "+locale, (hResultValues.get("byte") instanceof Byte));
    198       assertTrue("Expecting short result to be an instance of Short for locale: "+locale, (hResultValues.get("short") instanceof Short));
    199       assertTrue("Expecting integer result to be an instance of Integer for locale: "+locale, (hResultValues.get("integer") instanceof Integer));
    200       assertTrue("Expecting long result to be an instance of Long for locale: "+locale, (hResultValues.get("long") instanceof Long));
    201       assertTrue("Expecting float result to be an instance of Float for locale: "+locale, (hResultValues.get("float") instanceof Float));
    202       assertTrue("Expecting double result to be an instance of Double for locale: "+locale, (hResultValues.get("double") instanceof Double));
    203
    204       for (Iterator i = hResultValues.keySet().iterator(); i.hasNext(); ) {
    205          String key = (String)i.next();
    206          Object value = hResultValues.get(key);
    207
    208          assertNotNull("value ValidatorResults.getResultValueMap() should not be null for locale: "+locale, value);
    209       }
    210       return hResultValues;
    211    }
    212
    213 }