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 org.apache.commons.validator.util.ValidatorUtils;
20                                                            
21  /***                                                       
22   * Contains validation methods for different unit tests.
23   *
24   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
25   */
26  public class TestValidator {
27            
28      /***
29       * Throws a runtime exception if the value of the argument is "RUNTIME", 
30       * an exception if the value of the argument is "CHECKED", and a 
31       * ValidatorException otherwise.
32       * 
33       * @throws RuntimeException with "RUNTIME-EXCEPTION as message"
34       * if value is "RUNTIME"
35       * @throws Exception with "CHECKED-EXCEPTION" as message 
36       * if value is "CHECKED"
37       * @throws ValidatorException with "VALIDATOR-EXCEPTION" as message  
38       * otherwise
39       */
40      public static boolean validateRaiseException(
41          final Object bean,
42          final Field field)
43          throws Exception {
44              
45          final String value =
46              ValidatorUtils.getValueAsString(bean, field.getProperty());
47              
48          if ("RUNTIME".equals(value)) {
49              throw new RuntimeException("RUNTIME-EXCEPTION");
50              
51          } else if ("CHECKED".equals(value)) {
52              throw new Exception("CHECKED-EXCEPTION");
53              
54          } else {
55              throw new ValidatorException("VALIDATOR-EXCEPTION");
56          }
57      }
58                                                            
59     /***
60      * Checks if the field is required.
61      *
62      * @return boolean If the field isn't <code>null</code> and
63      * has a length greater than zero, <code>true</code> is returned.  
64      * Otherwise <code>false</code>.
65      */
66     public static boolean validateRequired(Object bean, Field field) {
67        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
68  
69        return !GenericValidator.isBlankOrNull(value);
70     }
71  
72     /***
73      * Checks if the field can be successfully converted to a <code>byte</code>.
74      *
75      * @param 	value 		The value validation is being performed on.
76      * @return	boolean		If the field can be successfully converted 
77      *                           to a <code>byte</code> <code>true</code> is returned.  
78      *                           Otherwise <code>false</code>.
79      */
80     public static boolean validateByte(Object bean, Field field) {
81        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
82  
83        return GenericValidator.isByte(value);
84     }
85  
86     /***
87      * Checks if the field can be successfully converted to a <code>short</code>.
88      *
89      * @param 	value 		The value validation is being performed on.
90      * @return	boolean		If the field can be successfully converted 
91      *                           to a <code>short</code> <code>true</code> is returned.  
92      *                           Otherwise <code>false</code>.
93      */
94     public static boolean validateShort(Object bean, Field field) {
95        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
96  
97        return GenericValidator.isShort(value);
98     }
99  
100    /***
101     * Checks if the field can be successfully converted to a <code>int</code>.
102     *
103     * @param 	value 		The value validation is being performed on.
104     * @return	boolean		If the field can be successfully converted 
105     *                           to a <code>int</code> <code>true</code> is returned.  
106     *                           Otherwise <code>false</code>.
107     */
108    public static boolean validateInt(Object bean, Field field) {
109       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
110 
111       return GenericValidator.isInt(value);
112    }
113 
114    /***
115     * Checks if field is positive assuming it is an integer
116     * 
117     * @param    value       The value validation is being performed on.
118     * @param    field       Description of the field to be evaluated
119     * @return   boolean     If the integer field is greater than zero, returns
120     *                        true, otherwise returns false.
121     */
122    public static boolean validatePositive(Object bean , Field field) {
123       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
124    
125       return GenericTypeValidator.formatInt(value).intValue() > 0;                                                      
126    }
127 
128    /***
129     * Checks if the field can be successfully converted to a <code>long</code>.
130     *
131     * @param 	value 		The value validation is being performed on.
132     * @return	boolean		If the field can be successfully converted 
133     *                           to a <code>long</code> <code>true</code> is returned.  
134     *                           Otherwise <code>false</code>.
135     */
136    public static boolean validateLong(Object bean, Field field) {
137       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
138 
139       return GenericValidator.isLong(value);
140    }
141 
142    /***
143     * Checks if the field can be successfully converted to a <code>float</code>.
144     *
145     * @param 	value 		The value validation is being performed on.
146     * @return	boolean		If the field can be successfully converted 
147     *                           to a <code>float</code> <code>true</code> is returned.  
148     *                           Otherwise <code>false</code>.
149     */
150    public static boolean validateFloat(Object bean, Field field) {
151       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
152 
153       return GenericValidator.isFloat(value);
154    }
155    
156    /***
157     * Checks if the field can be successfully converted to a <code>double</code>.
158     *
159     * @param 	value 		The value validation is being performed on.
160     * @return	boolean		If the field can be successfully converted 
161     *                           to a <code>double</code> <code>true</code> is returned.  
162     *                           Otherwise <code>false</code>.
163     */
164    public static boolean validateDouble(Object bean, Field field) {
165       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
166 
167       return GenericValidator.isDouble(value);
168    }
169 
170    /***
171     * Checks if the field is an e-mail address.
172     *
173     * @param 	value 		The value validation is being performed on.
174     * @return	boolean		If the field is an e-mail address
175     *                           <code>true</code> is returned.  
176     *                           Otherwise <code>false</code>.
177     */
178    public static boolean validateEmail(Object bean, Field field) {
179       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
180 
181       return GenericValidator.isEmail(value);
182    }
183 
184   public final static String FIELD_TEST_NULL = "NULL";
185   public final static String FIELD_TEST_NOTNULL = "NOTNULL";
186   public final static String FIELD_TEST_EQUAL = "EQUAL";
187 
188     public static boolean validateRequiredIf(
189         Object bean,
190         Field field,
191         Validator validator) {
192     
193         Object form = validator.getParameterValue(Validator.BEAN_PARAM);
194         String value = null;
195         boolean required = false;
196         if (isString(bean)) {
197             value = (String) bean;
198         } else {
199             value = ValidatorUtils.getValueAsString(bean, field.getProperty());
200         }
201         int i = 0;
202         String fieldJoin = "AND";
203         if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
204             fieldJoin = field.getVarValue("fieldJoin");
205         }
206         if (fieldJoin.equalsIgnoreCase("AND")) {
207             required = true;
208         }
209         while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
210             String dependProp = field.getVarValue("field[" + i + "]");
211             String dependTest = field.getVarValue("fieldTest[" + i + "]");
212             String dependTestValue = field.getVarValue("fieldValue[" + i + "]");
213             String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");
214             if (dependIndexed == null)
215                 dependIndexed = "false";
216             String dependVal = null;
217             boolean this_required = false;
218             if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
219                 String key = field.getKey();
220                 if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
221                     String ind = key.substring(0, key.indexOf(".") + 1);
222                     dependProp = ind + dependProp;
223                 }
224             }
225             dependVal = ValidatorUtils.getValueAsString(form, dependProp);
226             if (dependTest.equals(FIELD_TEST_NULL)) {
227                 if ((dependVal != null) && (dependVal.length() > 0)) {
228                     this_required = false;
229                 } else {
230                     this_required = true;
231                 }
232             }
233             if (dependTest.equals(FIELD_TEST_NOTNULL)) {
234                 if ((dependVal != null) && (dependVal.length() > 0)) {
235                     this_required = true;
236                 } else {
237                     this_required = false;
238                 }
239             }
240             if (dependTest.equals(FIELD_TEST_EQUAL)) {
241                 this_required = dependTestValue.equalsIgnoreCase(dependVal);
242             }
243             if (fieldJoin.equalsIgnoreCase("AND")) {
244                 required = required && this_required;
245             } else {
246                 required = required || this_required;
247             }
248             i++;
249         }
250         if (required) {
251             if ((value != null) && (value.length() > 0)) {
252                 return true;
253             } else {
254                 return false;
255             }
256         }
257         return true;
258     }
259   
260   private static Class stringClass = new String().getClass();
261 
262   private static boolean isString(Object o) {
263     if (o == null) return true;
264     return (stringClass.isInstance(o));
265   }
266       
267 }