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