    1   /*
    2    * $Id: TestTypeValidator.java 232645 2005-08-14 21:11:48Z mrdon $
    3    * $Rev: 232645 $
    4    * $Date: 2005-08-14 22:11:48 +0100 (Sun, 14 Aug 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.util.*;
    25
    26  import org.apache.commons.validator.util.ValidatorUtils;
    27
    28  /***
    29   * Contains validation methods for different unit tests.
    30   */
    31  public class TestTypeValidator {
    32
    33     /***
    34      * Checks if the field can be successfully converted to a <code>byte</code>.
    35      *
    36      * @param value The value validation is being performed on.
    37      * @return boolean If the field can be successfully converted
    38      * to a <code>byte</code> <code>true</code> is returned.
    39      * Otherwise <code>false</code>.
    40      */
    41     public static Byte validateByte(Object bean, Field field) {
    42        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    43
    44        return GenericTypeValidator.formatByte(value);
    45     }
    46
    47     /***
    48      * Checks if the field can be successfully converted to a <code>byte</code>.
    49      *
    50      * @param value The value validation is being performed on.
    51      * @return boolean If the field can be successfully converted
    52      * to a <code>byte</code> <code>true</code> is returned.
    53      * Otherwise <code>false</code>.
    54      */
    55     public static Byte validateByte(Object bean, Field field, Locale locale) {
    56        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    57
    58        return GenericTypeValidator.formatByte(value, locale);
    59     }
    60
    61     /***
    62      * Checks if the field can be successfully converted to a <code>short</code>.
    63      *
    64      * @param value The value validation is being performed on.
    65      * @return boolean If the field can be successfully converted
    66      * to a <code>short</code> <code>true</code> is returned.
    67      * Otherwise <code>false</code>.
    68      */
    69     public static Short validateShort(Object bean, Field field) {
    70        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    71
    72        return GenericTypeValidator.formatShort(value);
    73     }
    74
    75     /***
    76      * Checks if the field can be successfully converted to a <code>short</code>.
    77      *
    78      * @param value The value validation is being performed on.
    79      * @return boolean If the field can be successfully converted
    80      * to a <code>short</code> <code>true</code> is returned.
    81      * Otherwise <code>false</code>.
    82      */
    83     public static Short validateShort(Object bean, Field field, Locale locale) {
    84        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    85
    86        return GenericTypeValidator.formatShort(value, locale);
    87     }
    88
    89     /***
    90      * Checks if the field can be successfully converted to a <code>int</code>.
    91      *
    92      * @param value The value validation is being performed on.
    93      * @return boolean If the field can be successfully converted
    94      * to a <code>int</code> <code>true</code> is returned.
    95      * Otherwise <code>false</code>.
    96      */
    97     public static Integer validateInt(Object bean, Field field) {
    98        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    99
    100       return GenericTypeValidator.formatInt(value);
    101    }
    102
    103    /***
    104     * Checks if the field can be successfully converted to a <code>int</code>.
    105     *
    106     * @param value The value validation is being performed on.
    107     * @return boolean If the field can be successfully converted
    108     * to a <code>int</code> <code>true</code> is returned.
    109     * Otherwise <code>false</code>.
    110     */
    111    public static Integer validateInt(Object bean, Field field, Locale locale) {
    112       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    113
    114       return GenericTypeValidator.formatInt(value, locale);
    115    }
    116
    117    /***
    118     * Checks if the field can be successfully converted to a <code>long</code>.
    119     *
    120     * @param value The value validation is being performed on.
    121     * @return boolean If the field can be successfully converted
    122     * to a <code>long</code> <code>true</code> is returned.
    123     * Otherwise <code>false</code>.
    124     */
    125    public static Long validateLong(Object bean, Field field) {
    126       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    127
    128       return GenericTypeValidator.formatLong(value);
    129    }
    130
    131    /***
    132     * Checks if the field can be successfully converted to a <code>long</code>.
    133     *
    134     * @param value The value validation is being performed on.
    135     * @return boolean If the field can be successfully converted
    136     * to a <code>long</code> <code>true</code> is returned.
    137     * Otherwise <code>false</code>.
    138     */
    139    public static Long validateLong(Object bean, Field field, Locale locale) {
    140       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    141
    142       return GenericTypeValidator.formatLong(value, locale);
    143    }
    144
    145    /***
    146     * Checks if the field can be successfully converted to a <code>float</code>.
    147     *
    148     * @param value The value validation is being performed on.
    149     * @return boolean If the field can be successfully converted
    150     * to a <code>float</code> <code>true</code> is returned.
    151     * Otherwise <code>false</code>.
    152     */
    153    public static Float validateFloat(Object bean, Field field) {
    154       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    155
    156       return GenericTypeValidator.formatFloat(value);
    157    }
    158
    159    /***
    160     * Checks if the field can be successfully converted to a <code>float</code>.
    161     *
    162     * @param value The value validation is being performed on.
    163     * @return boolean If the field can be successfully converted
    164     * to a <code>float</code> <code>true</code> is returned.
    165     * Otherwise <code>false</code>.
    166     */
    167    public static Float validateFloat(Object bean, Field field, Locale locale) {
    168       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    169
    170       return GenericTypeValidator.formatFloat(value, locale);
    171    }
    172
    173    /***
    174     * Checks if the field can be successfully converted to a <code>double</code>.
    175     *
    176     * @param value The value validation is being performed on.
    177     * @return boolean If the field can be successfully converted
    178     * to a <code>double</code> <code>true</code> is returned.
    179     * Otherwise <code>false</code>.
    180     */
    181    public static Double validateDouble(Object bean, Field field) {
    182       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    183
    184       return GenericTypeValidator.formatDouble(value);
    185    }
    186
    187    /***
    188     * Checks if the field can be successfully converted to a <code>double</code>.
    189     *
    190     * @param value The value validation is being performed on.
    191     * @return boolean If the field can be successfully converted
    192     * to a <code>double</code> <code>true</code> is returned.
    193     * Otherwise <code>false</code>.
    194     */
    195    public static Double validateDouble(Object bean, Field field, Locale locale) {
    196       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    197
    198       return GenericTypeValidator.formatDouble(value, locale);
    199    }
    200
    201    /***
    202     * Checks if the field can be successfully converted to a <code>date</code>.
    203     *
    204     * @param value The value validation is being performed on.
    205     * @return boolean If the field can be successfully converted
    206     * to a <code>date</code> <code>true</code> is returned.
    207     * Otherwise <code>false</code>.
    208     */
    209    public static Date validateDate(Object bean, Field field, Locale locale) {
    210       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    211
    212       return GenericTypeValidator.formatDate(value, locale);
    213    }
    214
    215    /***
    216     * Checks if the field can be successfully converted to a <code>date</code>.
    217     *
    218     * @param value The value validation is being performed on.
    219     * @return boolean If the field can be successfully converted
    220     * to a <code>date</code> <code>true</code> is returned.
    221     * Otherwise <code>false</code>.
    222     */
    223    public static Date validateDate(Object bean, Field field) {
    224       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    225       String datePattern = field.getVarValue("datePattern");
    226       String datePatternStrict = field.getVarValue("datePatternStrict");
    227
    228       Date result = null;
    229       if (datePattern != null && datePattern.length() > 0) {
    230             result = GenericTypeValidator.formatDate(value, datePattern, false);
    231         } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
    232             result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
    233         }
    234
    235       return result;
    236    }
    237 }