001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.validator;
016:
017: import java.util.Date;
018: import java.util.List;
019: import java.util.Map;
020:
021: import org.strecks.exceptions.ApplicationConfigurationException;
022: import org.strecks.form.controller.DelegatingForm;
023: import org.strecks.form.controller.FormTestUtils;
024: import org.strecks.validator.impl.BeanWithNoConverter;
025: import org.strecks.validator.impl.ValidatableBean;
026: import org.strecks.validator.internal.MethodValidators;
027: import org.strecks.validator.internal.OrderedProperty;
028: import org.strecks.validator.internal.ValidationAnnotationReader;
029: import org.strecks.validator.internal.ValidationInfo;
030: import org.strecks.validator.internal.ValidatorWrapper;
031: import org.testng.Assert;
032: import org.testng.annotations.Test;
033:
034: /**
035: * @author Phil Zoio
036: */
037: public class TestValidationAnnotationReader {
038:
039: @Test
040: public void testBeanWithNoConverter() {
041: BeanWithNoConverter form = new BeanWithNoConverter();
042: DelegatingForm delegatingForm = FormTestUtils
043: .getDelegatingForm(form);
044: form.setDateField("duff value");
045:
046: try {
047: delegatingForm.validate(null, null);
048: Assert.fail();
049: } catch (ApplicationConfigurationException e) {
050: Assert
051: .assertEquals(
052: e.getMessage(),
053: "Supplied value of dateField in class org.strecks.validator.impl.BeanWithNoConverter "
054: + "converted to class java.lang.String and not the class java.util.Date "
055: + "expected by one or more validators. "
056: + "Check that the property contains an appropriate converter in its getter method");
057: }
058: }
059:
060: @Test
061: public void testValidatableBean() {
062:
063: ValidatableBean bean = new ValidatableBean();
064: ValidationAnnotationReader reader = new ValidationAnnotationReader();
065: ValidationInfo info = reader.readValidationHandlers(bean, null);
066:
067: Map<OrderedProperty, MethodValidators> map = info
068: .getValidators();
069:
070: System.out.println(map);
071:
072: /***************************************************************************************************************
073: *
074: * @ValidateRequired(key = "is.required") String required;
075: *
076: * @ValidateBlankOrNull(key = "must.be.blank.or.null") String blankOrNull;
077: *
078: * @ValidateMaxLength(key = "max.length.exceeded", maxLength = 1) String maxLength;
079: *
080: * @ValidateDouble(key = "not.double") String doubleValue;
081: *
082: * @ValidateLong(key = "not.long") String longValue;
083: *
084: * @ValidateInteger(key = "not.integer") String integerValue;
085: *
086: * @ValidateDate(key = "not.date", pattern = "yyyy-mm-dd") String dateValue;
087: *
088: * @ValidateDate(key = "not.strict.date", pattern = "yyyy-mm-dd", strict = false) String strictDateValue;
089: *
090: **************************************************************************************************************/
091:
092: MethodValidators methodValidators = get(map, "required");
093: assert methodValidators.getConverter() != null;
094: assert methodValidators.getRequiresConversion() == false;
095:
096: List<ValidatorWrapper> list = methodValidators.getValidators();
097: assert list.size() == 1;
098:
099: ValidatorWrapper validatorWrapper = list.get(0);
100: assert validatorWrapper.getValidator() instanceof RequiredValidator;
101:
102: assert validatorWrapper.getKey().equals("is.required");
103: assert validatorWrapper.getParams() == null;
104: assert validatorWrapper.getParameterizedType() == null;
105:
106: // blankOrNull
107:
108: methodValidators = get(map, "blankOrNull");
109: assert methodValidators.getConverter() != null;
110: assert methodValidators.getRequiresConversion() == false;
111:
112: list = methodValidators.getValidators();
113: assert list.size() == 1;
114:
115: validatorWrapper = list.get(0);
116: assert validatorWrapper.getValidator() instanceof BlankOrNullValidator;
117: assert validatorWrapper.getKey()
118: .equals("must.be.blank.or.null");
119: assert validatorWrapper.getParams() == null;
120: assert validatorWrapper.getParameterizedType() == null;
121:
122: // maxLength
123: methodValidators = get(map, "maxLength");
124: assert methodValidators.getConverter() != null;
125: assert methodValidators.getRequiresConversion() == false;
126:
127: list = methodValidators.getValidators();
128: assert list.size() == 1;
129:
130: validatorWrapper = list.get(0);
131: assert validatorWrapper.getValidator() instanceof MaxLengthValidator;
132: assert validatorWrapper.getKey().equals("max.length.exceeded");
133: assert validatorWrapper.getParams().size() == 1;
134: assert validatorWrapper.getParams().get(0).equals(1);
135: assert validatorWrapper.getParameterizedType() == null;
136:
137: MaxLengthValidator ml = (MaxLengthValidator) validatorWrapper
138: .getValidator();
139: assert ml.getLength() == 1;
140:
141: // doubleValue
142: methodValidators = get(map, "doubleValue");
143: list = methodValidators.getValidators();
144: assert methodValidators.getConverter() != null;
145: assert methodValidators.getRequiresConversion() == true;
146:
147: assert list.size() == 1;
148:
149: validatorWrapper = list.get(0);
150: assert validatorWrapper.getValidator() instanceof DoubleValidator;
151: assert validatorWrapper.getKey().equals("not.double");
152: assert validatorWrapper.getParams() == null;
153: assert validatorWrapper.getParameterizedType() == Double.class;
154:
155: // booleanValue
156: methodValidators = get(map, "booleanValue");
157: list = methodValidators.getValidators();
158: assert methodValidators.getConverter() != null;
159: assert methodValidators.getRequiresConversion() == true;
160:
161: assert list.size() == 1;
162:
163: validatorWrapper = list.get(0);
164: assert validatorWrapper.getValidator() instanceof BooleanValidator;
165: assert validatorWrapper.getKey().equals("not.boolean");
166: assert validatorWrapper.getParams() == null;
167: assert validatorWrapper.getParameterizedType() == Boolean.class;
168:
169: // byteValue
170: methodValidators = get(map, "byteValue");
171: list = methodValidators.getValidators();
172: assert methodValidators.getConverter() != null;
173: assert methodValidators.getRequiresConversion() == true;
174:
175: assert list.size() == 1;
176:
177: validatorWrapper = list.get(0);
178: assert validatorWrapper.getValidator() instanceof ByteValidator;
179: assert validatorWrapper.getKey().equals("not.byte");
180: assert validatorWrapper.getParams() == null;
181: assert validatorWrapper.getParameterizedType() == Byte.class;
182:
183: // floatValue
184: methodValidators = get(map, "floatValue");
185: list = methodValidators.getValidators();
186: assert methodValidators.getConverter() != null;
187: assert methodValidators.getRequiresConversion() == true;
188:
189: assert list.size() == 1;
190:
191: validatorWrapper = list.get(0);
192: assert validatorWrapper.getValidator() instanceof FloatValidator;
193: assert validatorWrapper.getKey().equals("not.float");
194: assert validatorWrapper.getParams() == null;
195: assert validatorWrapper.getParameterizedType() == Float.class;
196:
197: // shortValue
198: methodValidators = get(map, "shortValue");
199: list = methodValidators.getValidators();
200: assert methodValidators.getConverter() != null;
201: assert methodValidators.getRequiresConversion() == true;
202:
203: assert list.size() == 1;
204:
205: validatorWrapper = list.get(0);
206: assert validatorWrapper.getValidator() instanceof ShortValidator;
207: assert validatorWrapper.getKey().equals("not.short");
208: assert validatorWrapper.getParams() == null;
209: assert validatorWrapper.getParameterizedType() == Short.class;
210:
211: // longValue
212: methodValidators = get(map, "longValue");
213: assert methodValidators.getConverter() != null;
214: assert methodValidators.getRequiresConversion() == true;
215:
216: list = methodValidators.getValidators();
217: assert list.size() == 1;
218:
219: validatorWrapper = list.get(0);
220: assert validatorWrapper.getValidator() instanceof LongValidator;
221:
222: assert validatorWrapper.getKey().equals("not.long");
223: assert validatorWrapper.getParams() == null;
224: assert validatorWrapper.getParameterizedType() == Long.class;
225:
226: // integerValue
227: methodValidators = get(map, "integerValue");
228: assert methodValidators.getConverter() != null;
229: assert methodValidators.getRequiresConversion() == true;
230:
231: list = methodValidators.getValidators();
232: assert list.size() == 1;
233:
234: validatorWrapper = list.get(0);
235: assert validatorWrapper.getValidator() instanceof IntegerValidator;
236:
237: assert validatorWrapper.getKey().equals("not.integer");
238: assert validatorWrapper.getParams() == null;
239: assert validatorWrapper.getParameterizedType() == Integer.class;
240:
241: // dateValue
242: methodValidators = get(map, "dateValue");
243: assert methodValidators.getConverter() != null;
244: assert methodValidators.getRequiresConversion() == true;
245:
246: list = methodValidators.getValidators();
247: assert list.size() == 1;
248:
249: validatorWrapper = list.get(0);
250: assert validatorWrapper.getValidator() instanceof DateValidator;
251:
252: assert validatorWrapper.getKey().equals("not.date");
253:
254: assert validatorWrapper.getParams().size() == 0;
255: assert validatorWrapper.getParameterizedType() == Date.class;
256:
257: // strictDateValue
258: methodValidators = get(map, "strictDateValue");
259: assert methodValidators.getConverter() != null;
260: assert methodValidators.getRequiresConversion() == true;
261:
262: list = methodValidators.getValidators();
263: assert list.size() == 1;
264:
265: validatorWrapper = list.get(0);
266: assert validatorWrapper.getValidator() instanceof DateValidator;
267:
268: assert validatorWrapper.getKey().equals("not.strict.date");
269: assert validatorWrapper.getParams().size() == 0;
270: assert validatorWrapper.getParameterizedType() == Date.class;
271:
272: // integer range
273: methodValidators = get(map, "integerRangeValue");
274: list = methodValidators.getValidators();
275: assert list.size() == 1;
276:
277: validatorWrapper = list.get(0);
278: assert validatorWrapper.getValidator() instanceof IntegerRangeValidator;
279: assert validatorWrapper.getKey().equals("not.in.range");
280: assert validatorWrapper.getParams().size() == 2;
281: assert validatorWrapper.getParams().get(0).equals(0);
282: assert validatorWrapper.getParams().get(1).equals(1);
283: assert validatorWrapper.getParameterizedType() == Integer.class;
284:
285: IntegerRangeValidator irv = (IntegerRangeValidator) validatorWrapper
286: .getValidator();
287: assert irv.getMin() == 0;
288: assert irv.getMax() == 1;
289:
290: // long range
291: methodValidators = get(map, "longRangeValue");
292: list = methodValidators.getValidators();
293: assert list.size() == 1;
294:
295: validatorWrapper = list.get(0);
296: assert validatorWrapper.getValidator() instanceof LongRangeValidator;
297: assert validatorWrapper.getKey().equals("not.in.range");
298: assert validatorWrapper.getParams().size() == 2;
299: assert validatorWrapper.getParams().get(0).equals(
300: Long.MIN_VALUE);
301: assert validatorWrapper.getParams().get(1).equals(
302: Long.MAX_VALUE);
303: assert validatorWrapper.getParameterizedType() == Long.class;
304:
305: LongRangeValidator lrv = (LongRangeValidator) validatorWrapper
306: .getValidator();
307: assert lrv.getMin() == Long.MIN_VALUE;
308: assert lrv.getMax() == Long.MAX_VALUE;
309:
310: /***************************************************************************************************************
311: *
312: * @ValidatePattern(key = "not.pattern", pattern = "a*b") public void setPatternValue(String patternValue) {
313: * this.patternValue = patternValue; }
314: *
315: * @ValidateUrl(key = "not.url") public void setUrlValue(String urlValue) { this.urlValue = urlValue; }
316: *
317: * @ValidateEmail(key = "not.email") public void setEmailValue(String emailValue) { this.emailValue =
318: * emailValue; }
319: *
320: * @ValidateCreditCard(key = "not.credit.card") public void setCreditCardValue(String creditCardValue) {
321: * this.creditCardValue = creditCardValue; }
322: */
323:
324: methodValidators = get(map, "patternValue");
325: list = methodValidators.getValidators();
326: assert list.size() == 1;
327:
328: validatorWrapper = list.get(0);
329: assert validatorWrapper.getValidator() instanceof PatternValidator;
330: assert validatorWrapper.getKey().equals("not.pattern");
331: assert validatorWrapper.getParams().size() == 1;
332: assert validatorWrapper.getParams().get(0).equals("a*b");
333:
334: methodValidators = get(map, "urlValue");
335: list = methodValidators.getValidators();
336: assert list.size() == 1;
337:
338: validatorWrapper = list.get(0);
339: assert validatorWrapper.getValidator() instanceof UrlValidator;
340: assert validatorWrapper.getKey().equals("not.url");
341: assert validatorWrapper.getParams() == null;
342:
343: methodValidators = get(map, "creditCardValue");
344: list = methodValidators.getValidators();
345: assert list.size() == 1;
346:
347: validatorWrapper = list.get(0);
348: assert validatorWrapper.getValidator() instanceof CreditCardValidator;
349: assert validatorWrapper.getKey().equals("not.credit.card");
350: assert validatorWrapper.getParams() == null;
351:
352: methodValidators = get(map, "emailValue");
353: list = methodValidators.getValidators();
354: assert list.size() == 1;
355:
356: validatorWrapper = list.get(0);
357: assert validatorWrapper.getValidator() instanceof EmailValidator;
358: assert validatorWrapper.getKey().equals("not.email");
359: assert validatorWrapper.getParams() == null;
360:
361: }
362:
363: @Test
364: public void testMultiValidatableBean() {
365:
366: MultiValidatorBean bean = new MultiValidatorBean();
367: ValidationAnnotationReader reader = new ValidationAnnotationReader();
368: ValidationInfo info = reader.readValidationHandlers(bean, null);
369:
370: Map<OrderedProperty, MethodValidators> map = info
371: .getValidators();
372:
373: System.out.println(map);
374:
375: // requiredDate
376: List<ValidatorWrapper> list = get(map, "requiredDate")
377: .getValidators();
378: assert list.size() == 2;
379:
380: ValidatorWrapper validatorWrapper = list.get(0);
381: assert validatorWrapper.getValidator() instanceof RequiredValidator;
382: assert validatorWrapper.getKey().equals("is.required");
383: assert validatorWrapper.getParams() == null;
384: assert validatorWrapper.getParameterizedType() == null;
385:
386: validatorWrapper = list.get(1);
387:
388: assert validatorWrapper.getValidator() instanceof DateValidator;
389: assert validatorWrapper.getKey().equals("not.date");
390: assert validatorWrapper.getParams().size() == 0;
391: assert validatorWrapper.getParameterizedType() == Date.class;
392:
393: }
394:
395: private MethodValidators get(
396: Map<OrderedProperty, MethodValidators> map, String string) {
397: return map.get(new OrderedProperty(string, 999));
398: }
399:
400: }
|