/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.validator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.form.controller.DelegatingForm;
import org.strecks.form.controller.FormTestUtils;
import org.strecks.validator.impl.BeanWithNoConverter;
import org.strecks.validator.impl.ValidatableBean;
import org.strecks.validator.internal.MethodValidators;
import org.strecks.validator.internal.OrderedProperty;
import org.strecks.validator.internal.ValidationAnnotationReader;
import org.strecks.validator.internal.ValidationInfo;
import org.strecks.validator.internal.ValidatorWrapper;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestValidationAnnotationReader
{
@Test
public void testBeanWithNoConverter()
{
BeanWithNoConverter form = new BeanWithNoConverter();
DelegatingForm delegatingForm = FormTestUtils.getDelegatingForm(form);
form.setDateField("duff value");
try
{
delegatingForm.validate(null, null);
Assert.fail();
}
catch (ApplicationConfigurationException e)
{
Assert.assertEquals(e.getMessage(),
"Supplied value of dateField in class org.strecks.validator.impl.BeanWithNoConverter "
+ "converted to class java.lang.String and not the class java.util.Date "
+ "expected by one or more validators. "
+ "Check that the property contains an appropriate converter in its getter method");
}
}
@Test
public void testValidatableBean()
{
ValidatableBean bean = new ValidatableBean();
ValidationAnnotationReader reader = new ValidationAnnotationReader();
ValidationInfo info = reader.readValidationHandlers(bean, null);
Map<OrderedProperty, MethodValidators> map = info.getValidators();
System.out.println(map);
/***************************************************************************************************************
*
* @ValidateRequired(key = "is.required") String required;
*
* @ValidateBlankOrNull(key = "must.be.blank.or.null") String blankOrNull;
*
* @ValidateMaxLength(key = "max.length.exceeded", maxLength = 1) String maxLength;
*
* @ValidateDouble(key = "not.double") String doubleValue;
*
* @ValidateLong(key = "not.long") String longValue;
*
* @ValidateInteger(key = "not.integer") String integerValue;
*
* @ValidateDate(key = "not.date", pattern = "yyyy-mm-dd") String dateValue;
*
* @ValidateDate(key = "not.strict.date", pattern = "yyyy-mm-dd", strict = false) String strictDateValue;
*
**************************************************************************************************************/
MethodValidators methodValidators = get(map, "required");
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == false;
List<ValidatorWrapper> list = methodValidators.getValidators();
assert list.size() == 1;
ValidatorWrapper validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof RequiredValidator;
assert validatorWrapper.getKey().equals("is.required");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == null;
// blankOrNull
methodValidators = get(map, "blankOrNull");
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == false;
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof BlankOrNullValidator;
assert validatorWrapper.getKey().equals("must.be.blank.or.null");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == null;
// maxLength
methodValidators = get(map, "maxLength");
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == false;
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof MaxLengthValidator;
assert validatorWrapper.getKey().equals("max.length.exceeded");
assert validatorWrapper.getParams().size() == 1;
assert validatorWrapper.getParams().get(0).equals(1);
assert validatorWrapper.getParameterizedType() == null;
MaxLengthValidator ml = (MaxLengthValidator) validatorWrapper.getValidator();
assert ml.getLength() == 1;
// doubleValue
methodValidators = get(map, "doubleValue");
list = methodValidators.getValidators();
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == true;
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof DoubleValidator;
assert validatorWrapper.getKey().equals("not.double");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == Double.class;
// booleanValue
methodValidators = get(map, "booleanValue");
list = methodValidators.getValidators();
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == true;
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof BooleanValidator;
assert validatorWrapper.getKey().equals("not.boolean");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == Boolean.class;
// byteValue
methodValidators = get(map, "byteValue");
list = methodValidators.getValidators();
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == true;
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof ByteValidator;
assert validatorWrapper.getKey().equals("not.byte");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == Byte.class;
// floatValue
methodValidators = get(map, "floatValue");
list = methodValidators.getValidators();
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == true;
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof FloatValidator;
assert validatorWrapper.getKey().equals("not.float");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == Float.class;
// shortValue
methodValidators = get(map, "shortValue");
list = methodValidators.getValidators();
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == true;
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof ShortValidator;
assert validatorWrapper.getKey().equals("not.short");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == Short.class;
// longValue
methodValidators = get(map, "longValue");
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == true;
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof LongValidator;
assert validatorWrapper.getKey().equals("not.long");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == Long.class;
// integerValue
methodValidators = get(map, "integerValue");
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == true;
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof IntegerValidator;
assert validatorWrapper.getKey().equals("not.integer");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == Integer.class;
// dateValue
methodValidators = get(map, "dateValue");
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == true;
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof DateValidator;
assert validatorWrapper.getKey().equals("not.date");
assert validatorWrapper.getParams().size() == 0;
assert validatorWrapper.getParameterizedType() == Date.class;
// strictDateValue
methodValidators = get(map, "strictDateValue");
assert methodValidators.getConverter() != null;
assert methodValidators.getRequiresConversion() == true;
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof DateValidator;
assert validatorWrapper.getKey().equals("not.strict.date");
assert validatorWrapper.getParams().size() == 0;
assert validatorWrapper.getParameterizedType() == Date.class;
// integer range
methodValidators = get(map, "integerRangeValue");
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof IntegerRangeValidator;
assert validatorWrapper.getKey().equals("not.in.range");
assert validatorWrapper.getParams().size() == 2;
assert validatorWrapper.getParams().get(0).equals(0);
assert validatorWrapper.getParams().get(1).equals(1);
assert validatorWrapper.getParameterizedType() == Integer.class;
IntegerRangeValidator irv = (IntegerRangeValidator) validatorWrapper.getValidator();
assert irv.getMin() == 0;
assert irv.getMax() == 1;
// long range
methodValidators = get(map, "longRangeValue");
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof LongRangeValidator;
assert validatorWrapper.getKey().equals("not.in.range");
assert validatorWrapper.getParams().size() == 2;
assert validatorWrapper.getParams().get(0).equals(Long.MIN_VALUE);
assert validatorWrapper.getParams().get(1).equals(Long.MAX_VALUE);
assert validatorWrapper.getParameterizedType() == Long.class;
LongRangeValidator lrv = (LongRangeValidator) validatorWrapper.getValidator();
assert lrv.getMin() == Long.MIN_VALUE;
assert lrv.getMax() == Long.MAX_VALUE;
/***************************************************************************************************************
*
* @ValidatePattern(key = "not.pattern", pattern = "a*b") public void setPatternValue(String patternValue) {
* this.patternValue = patternValue; }
*
* @ValidateUrl(key = "not.url") public void setUrlValue(String urlValue) { this.urlValue = urlValue; }
*
* @ValidateEmail(key = "not.email") public void setEmailValue(String emailValue) { this.emailValue =
* emailValue; }
*
* @ValidateCreditCard(key = "not.credit.card") public void setCreditCardValue(String creditCardValue) {
* this.creditCardValue = creditCardValue; }
*/
methodValidators = get(map, "patternValue");
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof PatternValidator;
assert validatorWrapper.getKey().equals("not.pattern");
assert validatorWrapper.getParams().size() == 1;
assert validatorWrapper.getParams().get(0).equals("a*b");
methodValidators = get(map, "urlValue");
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof UrlValidator;
assert validatorWrapper.getKey().equals("not.url");
assert validatorWrapper.getParams() == null;
methodValidators = get(map, "creditCardValue");
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof CreditCardValidator;
assert validatorWrapper.getKey().equals("not.credit.card");
assert validatorWrapper.getParams() == null;
methodValidators = get(map, "emailValue");
list = methodValidators.getValidators();
assert list.size() == 1;
validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof EmailValidator;
assert validatorWrapper.getKey().equals("not.email");
assert validatorWrapper.getParams() == null;
}
@Test
public void testMultiValidatableBean()
{
MultiValidatorBean bean = new MultiValidatorBean();
ValidationAnnotationReader reader = new ValidationAnnotationReader();
ValidationInfo info = reader.readValidationHandlers(bean, null);
Map<OrderedProperty, MethodValidators> map = info.getValidators();
System.out.println(map);
// requiredDate
List<ValidatorWrapper> list = get(map, "requiredDate").getValidators();
assert list.size() == 2;
ValidatorWrapper validatorWrapper = list.get(0);
assert validatorWrapper.getValidator() instanceof RequiredValidator;
assert validatorWrapper.getKey().equals("is.required");
assert validatorWrapper.getParams() == null;
assert validatorWrapper.getParameterizedType() == null;
validatorWrapper = list.get(1);
assert validatorWrapper.getValidator() instanceof DateValidator;
assert validatorWrapper.getKey().equals("not.date");
assert validatorWrapper.getParams().size() == 0;
assert validatorWrapper.getParameterizedType() == Date.class;
}
private MethodValidators get(Map<OrderedProperty, MethodValidators> map, String string)
{
return map.get(new OrderedProperty(string, 999));
}
}