1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import java.util.*;
20
21 import org.apache.commons.validator.util.ValidatorUtils;
22
23 /***
24 * Contains validation methods for different unit tests.
25 *
26 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
27 */
28 public class TestTypeValidator {
29
30 /***
31 * Checks if the field can be successfully converted to a <code>byte</code>.
32 *
33 * @param value The value validation is being performed on.
34 * @return boolean If the field can be successfully converted
35 * to a <code>byte</code> <code>true</code> is returned.
36 * Otherwise <code>false</code>.
37 */
38 public static Byte validateByte(Object bean, Field field) {
39 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
40
41 return GenericTypeValidator.formatByte(value);
42 }
43
44 /***
45 * Checks if the field can be successfully converted to a <code>byte</code>.
46 *
47 * @param value The value validation is being performed on.
48 * @return boolean If the field can be successfully converted
49 * to a <code>byte</code> <code>true</code> is returned.
50 * Otherwise <code>false</code>.
51 */
52 public static Byte validateByte(Object bean, Field field, Locale locale) {
53 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
54
55 return GenericTypeValidator.formatByte(value, locale);
56 }
57
58 /***
59 * Checks if the field can be successfully converted to a <code>short</code>.
60 *
61 * @param value The value validation is being performed on.
62 * @return boolean If the field can be successfully converted
63 * to a <code>short</code> <code>true</code> is returned.
64 * Otherwise <code>false</code>.
65 */
66 public static Short validateShort(Object bean, Field field) {
67 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
68
69 return GenericTypeValidator.formatShort(value);
70 }
71
72 /***
73 * Checks if the field can be successfully converted to a <code>short</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>short</code> <code>true</code> is returned.
78 * Otherwise <code>false</code>.
79 */
80 public static Short validateShort(Object bean, Field field, Locale locale) {
81 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
82
83 return GenericTypeValidator.formatShort(value, locale);
84 }
85
86 /***
87 * Checks if the field can be successfully converted to a <code>int</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>int</code> <code>true</code> is returned.
92 * Otherwise <code>false</code>.
93 */
94 public static Integer validateInt(Object bean, Field field) {
95 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
96
97 return GenericTypeValidator.formatInt(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 Integer validateInt(Object bean, Field field, Locale locale) {
109 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
110
111 return GenericTypeValidator.formatInt(value, locale);
112 }
113
114 /***
115 * Checks if the field can be successfully converted to a <code>long</code>.
116 *
117 * @param value The value validation is being performed on.
118 * @return boolean If the field can be successfully converted
119 * to a <code>long</code> <code>true</code> is returned.
120 * Otherwise <code>false</code>.
121 */
122 public static Long validateLong(Object bean, Field field) {
123 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
124
125 return GenericTypeValidator.formatLong(value);
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 Long validateLong(Object bean, Field field, Locale locale) {
137 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
138
139 return GenericTypeValidator.formatLong(value, locale);
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 Float validateFloat(Object bean, Field field) {
151 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
152
153 return GenericTypeValidator.formatFloat(value);
154 }
155
156 /***
157 * Checks if the field can be successfully converted to a <code>float</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>float</code> <code>true</code> is returned.
162 * Otherwise <code>false</code>.
163 */
164 public static Float validateFloat(Object bean, Field field, Locale locale) {
165 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
166
167 return GenericTypeValidator.formatFloat(value, locale);
168 }
169
170 /***
171 * Checks if the field can be successfully converted to a <code>double</code>.
172 *
173 * @param value The value validation is being performed on.
174 * @return boolean If the field can be successfully converted
175 * to a <code>double</code> <code>true</code> is returned.
176 * Otherwise <code>false</code>.
177 */
178 public static Double validateDouble(Object bean, Field field) {
179 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
180
181 return GenericTypeValidator.formatDouble(value);
182 }
183
184 /***
185 * Checks if the field can be successfully converted to a <code>double</code>.
186 *
187 * @param value The value validation is being performed on.
188 * @return boolean If the field can be successfully converted
189 * to a <code>double</code> <code>true</code> is returned.
190 * Otherwise <code>false</code>.
191 */
192 public static Double validateDouble(Object bean, Field field, Locale locale) {
193 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
194
195 return GenericTypeValidator.formatDouble(value, locale);
196 }
197
198 /***
199 * Checks if the field can be successfully converted to a <code>date</code>.
200 *
201 * @param value The value validation is being performed on.
202 * @return boolean If the field can be successfully converted
203 * to a <code>date</code> <code>true</code> is returned.
204 * Otherwise <code>false</code>.
205 */
206 public static Date validateDate(Object bean, Field field, Locale locale) {
207 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
208
209 return GenericTypeValidator.formatDate(value, locale);
210 }
211
212 /***
213 * Checks if the field can be successfully converted to a <code>date</code>.
214 *
215 * @param value The value validation is being performed on.
216 * @return boolean If the field can be successfully converted
217 * to a <code>date</code> <code>true</code> is returned.
218 * Otherwise <code>false</code>.
219 */
220 public static Date validateDate(Object bean, Field field) {
221 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
222 String datePattern = field.getVarValue("datePattern");
223 String datePatternStrict = field.getVarValue("datePatternStrict");
224
225 Date result = null;
226 if (datePattern != null && datePattern.length() > 0) {
227 result = GenericTypeValidator.formatDate(value, datePattern, false);
228 } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
229 result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
230 }
231
232 return result;
233 }
234 }