001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import org.apache.commons.validator.util.ValidatorUtils;
020:
021: /**
022: * Contains validation methods for different unit tests.
023: *
024: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
025: */
026: public class TestValidator {
027:
028: /**
029: * Throws a runtime exception if the value of the argument is "RUNTIME",
030: * an exception if the value of the argument is "CHECKED", and a
031: * ValidatorException otherwise.
032: *
033: * @throws RuntimeException with "RUNTIME-EXCEPTION as message"
034: * if value is "RUNTIME"
035: * @throws Exception with "CHECKED-EXCEPTION" as message
036: * if value is "CHECKED"
037: * @throws ValidatorException with "VALIDATOR-EXCEPTION" as message
038: * otherwise
039: */
040: public static boolean validateRaiseException(final Object bean,
041: final Field field) throws Exception {
042:
043: final String value = ValidatorUtils.getValueAsString(bean,
044: field.getProperty());
045:
046: if ("RUNTIME".equals(value)) {
047: throw new RuntimeException("RUNTIME-EXCEPTION");
048:
049: } else if ("CHECKED".equals(value)) {
050: throw new Exception("CHECKED-EXCEPTION");
051:
052: } else {
053: throw new ValidatorException("VALIDATOR-EXCEPTION");
054: }
055: }
056:
057: /**
058: * Checks if the field is required.
059: *
060: * @return boolean If the field isn't <code>null</code> and
061: * has a length greater than zero, <code>true</code> is returned.
062: * Otherwise <code>false</code>.
063: */
064: public static boolean validateRequired(Object bean, Field field) {
065: String value = ValidatorUtils.getValueAsString(bean, field
066: .getProperty());
067:
068: return !GenericValidator.isBlankOrNull(value);
069: }
070:
071: /**
072: * Checks if the field can be successfully converted to a <code>byte</code>.
073: *
074: * @param value The value validation is being performed on.
075: * @return boolean If the field can be successfully converted
076: * to a <code>byte</code> <code>true</code> is returned.
077: * Otherwise <code>false</code>.
078: */
079: public static boolean validateByte(Object bean, Field field) {
080: String value = ValidatorUtils.getValueAsString(bean, field
081: .getProperty());
082:
083: return GenericValidator.isByte(value);
084: }
085:
086: /**
087: * Checks if the field can be successfully converted to a <code>short</code>.
088: *
089: * @param value The value validation is being performed on.
090: * @return boolean If the field can be successfully converted
091: * to a <code>short</code> <code>true</code> is returned.
092: * Otherwise <code>false</code>.
093: */
094: public static boolean validateShort(Object bean, Field field) {
095: String value = ValidatorUtils.getValueAsString(bean, field
096: .getProperty());
097:
098: return GenericValidator.isShort(value);
099: }
100:
101: /**
102: * Checks if the field can be successfully converted to a <code>int</code>.
103: *
104: * @param value The value validation is being performed on.
105: * @return boolean If the field can be successfully converted
106: * to a <code>int</code> <code>true</code> is returned.
107: * Otherwise <code>false</code>.
108: */
109: public static boolean validateInt(Object bean, Field field) {
110: String value = ValidatorUtils.getValueAsString(bean, field
111: .getProperty());
112:
113: return GenericValidator.isInt(value);
114: }
115:
116: /**
117: * Checks if field is positive assuming it is an integer
118: *
119: * @param value The value validation is being performed on.
120: * @param field Description of the field to be evaluated
121: * @return boolean If the integer field is greater than zero, returns
122: * true, otherwise returns false.
123: */
124: public static boolean validatePositive(Object bean, Field field) {
125: String value = ValidatorUtils.getValueAsString(bean, field
126: .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 bean, Field field) {
140: String value = ValidatorUtils.getValueAsString(bean, field
141: .getProperty());
142:
143: return GenericValidator.isLong(value);
144: }
145:
146: /**
147: * Checks if the field can be successfully converted to a <code>float</code>.
148: *
149: * @param value The value validation is being performed on.
150: * @return boolean If the field can be successfully converted
151: * to a <code>float</code> <code>true</code> is returned.
152: * Otherwise <code>false</code>.
153: */
154: public static boolean validateFloat(Object bean, Field field) {
155: String value = ValidatorUtils.getValueAsString(bean, field
156: .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
171: .getProperty());
172:
173: return GenericValidator.isDouble(value);
174: }
175:
176: /**
177: * Checks if the field is an e-mail address.
178: *
179: * @param value The value validation is being performed on.
180: * @return boolean If the field is an e-mail address
181: * <code>true</code> is returned.
182: * Otherwise <code>false</code>.
183: */
184: public static boolean validateEmail(Object bean, Field field) {
185: String value = ValidatorUtils.getValueAsString(bean, field
186: .getProperty());
187:
188: return GenericValidator.isEmail(value);
189: }
190:
191: public final static String FIELD_TEST_NULL = "NULL";
192: public final static String FIELD_TEST_NOTNULL = "NOTNULL";
193: public final static String FIELD_TEST_EQUAL = "EQUAL";
194:
195: public static boolean validateRequiredIf(Object bean, 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
205: .getProperty());
206: }
207: int i = 0;
208: String fieldJoin = "AND";
209: if (!GenericValidator.isBlankOrNull(field
210: .getVarValue("fieldJoin"))) {
211: fieldJoin = field.getVarValue("fieldJoin");
212: }
213: if (fieldJoin.equalsIgnoreCase("AND")) {
214: required = true;
215: }
216: while (!GenericValidator.isBlankOrNull(field
217: .getVarValue("field[" + i + "]"))) {
218: String dependProp = field.getVarValue("field[" + i + "]");
219: String dependTest = field.getVarValue("fieldTest[" + i
220: + "]");
221: String dependTestValue = field.getVarValue("fieldValue["
222: + i + "]");
223: String dependIndexed = field.getVarValue("fieldIndexed["
224: + i + "]");
225: if (dependIndexed == null)
226: dependIndexed = "false";
227: String dependVal = null;
228: boolean this _required = false;
229: if (field.isIndexed()
230: && dependIndexed.equalsIgnoreCase("true")) {
231: String key = field.getKey();
232: if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
233: String ind = key.substring(0, key.indexOf(".") + 1);
234: dependProp = ind + dependProp;
235: }
236: }
237: dependVal = ValidatorUtils.getValueAsString(form,
238: dependProp);
239: if (dependTest.equals(FIELD_TEST_NULL)) {
240: if ((dependVal != null) && (dependVal.length() > 0)) {
241: this _required = false;
242: } else {
243: this _required = true;
244: }
245: }
246: if (dependTest.equals(FIELD_TEST_NOTNULL)) {
247: if ((dependVal != null) && (dependVal.length() > 0)) {
248: this _required = true;
249: } else {
250: this _required = false;
251: }
252: }
253: if (dependTest.equals(FIELD_TEST_EQUAL)) {
254: this _required = dependTestValue
255: .equalsIgnoreCase(dependVal);
256: }
257: if (fieldJoin.equalsIgnoreCase("AND")) {
258: required = required && this _required;
259: } else {
260: required = required || this _required;
261: }
262: i++;
263: }
264: if (required) {
265: if ((value != null) && (value.length() > 0)) {
266: return true;
267: } else {
268: return false;
269: }
270: }
271: return true;
272: }
273:
274: private static Class stringClass = new String().getClass();
275:
276: private static boolean isString(Object o) {
277: if (o == null)
278: return true;
279: return (stringClass.isInstance(o));
280: }
281:
282: }
|