KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > TestValidator


1 /*
2  * $Id: TestValidator.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 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 org.apache.commons.validator.util.ValidatorUtils;
25                                                           
26 /**
27  * Contains validation methods for different unit tests.
28  */

29 public class TestValidator {
30           
31     /**
32      * Throws a runtime exception if the value of the argument is "RUNTIME",
33      * an exception if the value of the argument is "CHECKED", and a
34      * ValidatorException otherwise.
35      *
36      * @throws RuntimeException with "RUNTIME-EXCEPTION as message"
37      * if value is "RUNTIME"
38      * @throws Exception with "CHECKED-EXCEPTION" as message
39      * if value is "CHECKED"
40      * @throws ValidatorException with "VALIDATOR-EXCEPTION" as message
41      * otherwise
42      */

43     public static boolean validateRaiseException(
44         final Object JavaDoc bean,
45         final Field field)
46         throws Exception JavaDoc {
47             
48         final String JavaDoc value =
49             ValidatorUtils.getValueAsString(bean, field.getProperty());
50             
51         if ("RUNTIME".equals(value)) {
52             throw new RuntimeException JavaDoc("RUNTIME-EXCEPTION");
53             
54         } else if ("CHECKED".equals(value)) {
55             throw new Exception JavaDoc("CHECKED-EXCEPTION");
56             
57         } else {
58             throw new ValidatorException("VALIDATOR-EXCEPTION");
59         }
60     }
61                                                           
62    /**
63     * Checks if the field is required.
64     *
65     * @return boolean If the field isn't <code>null</code> and
66     * has a length greater than zero, <code>true</code> is returned.
67     * Otherwise <code>false</code>.
68     */

69    public static boolean validateRequired(Object JavaDoc bean, Field field) {
70       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
71
72       return !GenericValidator.isBlankOrNull(value);
73    }
74
75    /**
76     * Checks if the field can be successfully converted to a <code>byte</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>byte</code> <code>true</code> is returned.
81     * Otherwise <code>false</code>.
82     */

83    public static boolean validateByte(Object JavaDoc bean, Field field) {
84       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
85
86       return GenericValidator.isByte(value);
87    }
88
89    /**
90     * Checks if the field can be successfully converted to a <code>short</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>short</code> <code>true</code> is returned.
95     * Otherwise <code>false</code>.
96     */

97    public static boolean validateShort(Object JavaDoc bean, Field field) {
98       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
99
100       return GenericValidator.isShort(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 boolean validateInt(Object JavaDoc bean, Field field) {
112       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
113
114       return GenericValidator.isInt(value);
115    }
116
117    /**
118     * Checks if field is positive assuming it is an integer
119     *
120     * @param value The value validation is being performed on.
121     * @param field Description of the field to be evaluated
122     * @return boolean If the integer field is greater than zero, returns
123     * true, otherwise returns false.
124     */

125    public static boolean validatePositive(Object JavaDoc bean , Field field) {
126       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
127    
128       return GenericTypeValidator.formatInt(value).intValue() > 0;
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 boolean validateLong(Object JavaDoc bean, Field field) {
140       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
141
142       return GenericValidator.isLong(value);
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 boolean validateFloat(Object JavaDoc bean, Field field) {
154       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
155
156       return GenericValidator.isFloat(value);
157    }
158    
159    /**
160     * Checks if the field can be successfully converted to a <code>double</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>double</code> <code>true</code> is returned.
165     * Otherwise <code>false</code>.
166     */

167    public static boolean validateDouble(Object JavaDoc bean, Field field) {
168       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
169
170       return GenericValidator.isDouble(value);
171    }
172
173    /**
174     * Checks if the field is an e-mail address.
175     *
176     * @param value The value validation is being performed on.
177     * @return boolean If the field is an e-mail address
178     * <code>true</code> is returned.
179     * Otherwise <code>false</code>.
180     */

181    public static boolean validateEmail(Object JavaDoc bean, Field field) {
182       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
183
184       return GenericValidator.isEmail(value);
185    }
186
187   public final static String JavaDoc FIELD_TEST_NULL = "NULL";
188   public final static String JavaDoc FIELD_TEST_NOTNULL = "NOTNULL";
189   public final static String JavaDoc FIELD_TEST_EQUAL = "EQUAL";
190
191     public static boolean validateRequiredIf(
192         Object JavaDoc bean,
193         Field field,
194         Validator validator) {
195     
196         Object JavaDoc form = validator.getParameterValue(Validator.BEAN_PARAM);
197         String JavaDoc value = null;
198         boolean required = false;
199         if (isString(bean)) {
200             value = (String JavaDoc) bean;
201         } else {
202             value = ValidatorUtils.getValueAsString(bean, field.getProperty());
203         }
204         int i = 0;
205         String JavaDoc fieldJoin = "AND";
206         if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
207             fieldJoin = field.getVarValue("fieldJoin");
208         }
209         if (fieldJoin.equalsIgnoreCase("AND")) {
210             required = true;
211         }
212         while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
213             String JavaDoc dependProp = field.getVarValue("field[" + i + "]");
214             String JavaDoc dependTest = field.getVarValue("fieldTest[" + i + "]");
215             String JavaDoc dependTestValue = field.getVarValue("fieldValue[" + i + "]");
216             String JavaDoc dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");
217             if (dependIndexed == null)
218                 dependIndexed = "false";
219             String JavaDoc dependVal = null;
220             boolean this_required = false;
221             if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
222                 String JavaDoc key = field.getKey();
223                 if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
224                     String JavaDoc ind = key.substring(0, key.indexOf(".") + 1);
225                     dependProp = ind + dependProp;
226                 }
227             }
228             dependVal = ValidatorUtils.getValueAsString(form, dependProp);
229             if (dependTest.equals(FIELD_TEST_NULL)) {
230                 if ((dependVal != null) && (dependVal.length() > 0)) {
231                     this_required = false;
232                 } else {
233                     this_required = true;
234                 }
235             }
236             if (dependTest.equals(FIELD_TEST_NOTNULL)) {
237                 if ((dependVal != null) && (dependVal.length() > 0)) {
238                     this_required = true;
239                 } else {
240                     this_required = false;
241                 }
242             }
243             if (dependTest.equals(FIELD_TEST_EQUAL)) {
244                 this_required = dependTestValue.equalsIgnoreCase(dependVal);
245             }
246             if (fieldJoin.equalsIgnoreCase("AND")) {
247                 required = required && this_required;
248             } else {
249                 required = required || this_required;
250             }
251             i++;
252         }
253         if (required) {
254             if ((value != null) && (value.length() > 0)) {
255                 return true;
256             } else {
257                 return false;
258             }
259         }
260         return true;
261     }
262   
263   private static Class JavaDoc stringClass = new String JavaDoc().getClass();
264
265   private static boolean isString(Object JavaDoc o) {
266     if (o == null) return true;
267     return (stringClass.isInstance(o));
268   }
269       
270 }
271
Popular Tags