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