backed out 2.0.x validation enhancements in favor of adding them in 3.0.0 aligned with spring 3
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
import org.springframework.binding.validation.ConfirmationConstraint.ConfirmationForm;
|
||||
|
||||
public class AccountRegistrationForm {
|
||||
private String username;
|
||||
private String password;
|
||||
private String confirmedPassword;
|
||||
|
||||
public void validate(ValidationContext context) {
|
||||
context.setProperty("username");
|
||||
context.validate(new RequiredConstraint());
|
||||
context.validate(new LengthConstraint(3, 10));
|
||||
|
||||
context.setProperty("password");
|
||||
context.validate(new RequiredConstraint());
|
||||
context.validate(new LengthConstraint(6, 10));
|
||||
context.validate(new PasswordStrengthConstraint());
|
||||
|
||||
context.setProperty("confirmedPassword");
|
||||
context.validate(new ConfirmationConstraint(), new ConfirmationForm(password, confirmedPassword));
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
public class ConfirmationConstraint {
|
||||
|
||||
public void validate(ConfirmationForm form, ValidationContext context) {
|
||||
if (form.getValue() == null) {
|
||||
return;
|
||||
}
|
||||
if (!ObjectUtils.nullSafeEquals(form.getValue(), form.getConfirmedValue())) {
|
||||
context.addDefaultFailure();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConfirmationForm {
|
||||
|
||||
private Object value;
|
||||
|
||||
private Object confirmedValue;
|
||||
|
||||
public ConfirmationForm(String value, String confirmedValue) {
|
||||
this.value = value;
|
||||
this.confirmedValue = confirmedValue;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Object getConfirmedValue() {
|
||||
return confirmedValue;
|
||||
}
|
||||
|
||||
public void setConfirmedValue(Object confirmedValue) {
|
||||
this.confirmedValue = confirmedValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class DefaultSpringMvcValidationFailureMessageCodesFactoryTests extends TestCase {
|
||||
private DefaultSpringMvcValidationFailureMessageCodesFactory factory = new DefaultSpringMvcValidationFailureMessageCodesFactory();
|
||||
|
||||
public void testCreateGeneralModelFailureMessageCodes() {
|
||||
ValidationFailure failure = new ValidationFailureBuilder().constraint("invalid").build();
|
||||
String[] codes = factory.createMessageCodes(failure, new TestValidationFailureModelContext("testBean", null,
|
||||
null, null));
|
||||
assertEquals("validation.invalid.testBean", codes[0]);
|
||||
assertEquals("validation.invalid", codes[1]);
|
||||
}
|
||||
|
||||
public void testCreatePropertyFailureMessageCodes() {
|
||||
ValidationFailure failure = new ValidationFailureBuilder().forProperty("foo").constraint("required").build();
|
||||
String[] codes = factory.createMessageCodes(failure, new TestValidationFailureModelContext("testBean", null,
|
||||
String.class, null));
|
||||
assertEquals("validation.required.testBean.foo", codes[0]);
|
||||
assertEquals("validation.required.java.lang.String", codes[1]);
|
||||
assertEquals("validation.required", codes[2]);
|
||||
}
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.jboss.el.ExpressionFactoryImpl;
|
||||
import org.springframework.binding.convert.converters.StringToObject;
|
||||
import org.springframework.binding.convert.service.DefaultConversionService;
|
||||
import org.springframework.binding.expression.el.ELExpressionParser;
|
||||
import org.springframework.binding.message.Message;
|
||||
import org.springframework.binding.message.MessageResolver;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
|
||||
public class DefaultValidationFailureMessageResolverFactoryTests extends TestCase {
|
||||
|
||||
private ELExpressionParser parser = new ELExpressionParser(new ExpressionFactoryImpl());
|
||||
|
||||
private DefaultConversionService conversionService = new DefaultConversionService();
|
||||
|
||||
private ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
|
||||
private DefaultValidationFailureMessageResolverFactory factory;
|
||||
|
||||
private ValidationFailureBuilder builder = new ValidationFailureBuilder();
|
||||
|
||||
public void setUp() {
|
||||
factory = new DefaultValidationFailureMessageResolverFactory(parser, conversionService);
|
||||
messageSource.setBasename("org.springframework.binding.validation.messages");
|
||||
}
|
||||
|
||||
public void testResolveMessage() {
|
||||
ValidationFailure failure = builder.forProperty("foo").constraint("required").build();
|
||||
MessageResolver resolver = factory.createMessageResolver(failure, new TestValidationFailureModelContext(
|
||||
"testBean", "", String.class, null));
|
||||
Message message = resolver.resolveMessage(messageSource, Locale.getDefault());
|
||||
assertEquals("Foo is required", message.getText());
|
||||
}
|
||||
|
||||
public void testResolveMessageNoPropertyLabel() {
|
||||
ValidationFailure failure = builder.forProperty("bogus").constraint("required").build();
|
||||
MessageResolver resolver = factory.createMessageResolver(failure, new TestValidationFailureModelContext(
|
||||
"testBean", "", String.class, null));
|
||||
Message message = resolver.resolveMessage(messageSource, Locale.getDefault());
|
||||
assertEquals("bogus is required", message.getText());
|
||||
}
|
||||
|
||||
public void testResolveMessageWithCustomArg() {
|
||||
ValidationFailureBuilder builder = new ValidationFailureBuilder();
|
||||
ValidationFailure failure = builder.forProperty("checkinDate").constraint("invalidFormat").arg("format",
|
||||
"yyyy-MM-dd").build();
|
||||
MessageResolver resolver = factory.createMessageResolver(failure, new TestValidationFailureModelContext(
|
||||
"testBean", "bogus", Date.class, null));
|
||||
Message message = resolver.resolveMessage(messageSource, Locale.getDefault());
|
||||
assertEquals("Check In Date must be in format yyyy-MM-dd", message.getText());
|
||||
}
|
||||
|
||||
public void testResolveMessageWithCustomResolvableArg() {
|
||||
ValidationFailure failure = builder.forProperty("checkinDate").constraint("invalidFormat").resolvableArg(
|
||||
"format", "formats.dateFormat").build();
|
||||
MessageResolver resolver = factory.createMessageResolver(failure, new TestValidationFailureModelContext(
|
||||
"testBean", "bogus", Date.class, null));
|
||||
Message message = resolver.resolveMessage(messageSource, Locale.getDefault());
|
||||
assertEquals("Check In Date must be in format yyyy-MM-dd", message.getText());
|
||||
}
|
||||
|
||||
public void testResolveMessageWithValue() {
|
||||
ValidationFailure failure = builder.forProperty("checkinDate").constraint("invalidFormat2").resolvableArg(
|
||||
"format", "formats.dateFormat").build();
|
||||
MessageResolver resolver = factory.createMessageResolver(failure, new TestValidationFailureModelContext(
|
||||
"testBean", "bogus", Date.class, null));
|
||||
Message message = resolver.resolveMessage(messageSource, Locale.getDefault());
|
||||
assertEquals("Check In Date must be in format yyyy-MM-dd but it was 'bogus'", message.getText());
|
||||
}
|
||||
|
||||
public void testResolveMessageWithArgDefaultConversion() {
|
||||
ValidationFailure failure = builder.forProperty("amount").constraint("range").arg("min", new Integer(1)).arg(
|
||||
"max", new Integer(100)).build();
|
||||
MessageResolver resolver = factory.createMessageResolver(failure, new TestValidationFailureModelContext(
|
||||
"testBean", "bogus", Integer.class, null));
|
||||
Message message = resolver.resolveMessage(messageSource, Locale.getDefault());
|
||||
assertEquals("Amount must be between 1 and 100", message.getText());
|
||||
}
|
||||
|
||||
public void testResolveMessageWithArgCustomConversion() {
|
||||
conversionService.addConverter("stringToMoney", new StringToMoney());
|
||||
ValidationFailure failure = builder.forProperty("amount").constraint("range").arg("min", new Money(1)).arg(
|
||||
"max", new Money(100)).build();
|
||||
MessageResolver resolver = factory.createMessageResolver(failure, new TestValidationFailureModelContext(
|
||||
"testBean", "bogus", Money.class, "stringToMoney"));
|
||||
Message message = resolver.resolveMessage(messageSource, Locale.getDefault());
|
||||
assertEquals("Amount must be between $1 and $100", message.getText());
|
||||
}
|
||||
|
||||
public static class Money {
|
||||
private int amount;
|
||||
|
||||
public Money(int amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
|
||||
public static class StringToMoney extends StringToObject {
|
||||
|
||||
public StringToMoney() {
|
||||
super(Money.class);
|
||||
}
|
||||
|
||||
protected Object toObject(String string, Class targetClass) throws Exception {
|
||||
throw new UnsupportedOperationException("Not supported");
|
||||
}
|
||||
|
||||
protected String toString(Object object) throws Exception {
|
||||
return "$" + String.valueOf(((Money) object).amount);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
public class LengthConstraint {
|
||||
|
||||
private Integer min;
|
||||
|
||||
private Integer max;
|
||||
|
||||
public LengthConstraint(int min, int max) {
|
||||
this.min = new Integer(min);
|
||||
this.max = new Integer(max);
|
||||
}
|
||||
|
||||
public Integer getMin() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public Integer getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public void validate(String value, ValidationContext context) {
|
||||
if (value == null || value.length() < min.intValue() || value.length() > max.intValue()) {
|
||||
context.addFailure(createFailure());
|
||||
}
|
||||
}
|
||||
|
||||
protected ValidationFailure createFailure() {
|
||||
return new ValidationFailureBuilder().arg("min", getMin()).arg("max", getMax()).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PasswordStrengthConstraint {
|
||||
|
||||
private Pattern pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]$");
|
||||
|
||||
public void validate(String password, ValidationContext context) {
|
||||
Matcher matcher = pattern.matcher(password);
|
||||
if (!matcher.find()) {
|
||||
context.addFailure(createFailure());
|
||||
}
|
||||
}
|
||||
|
||||
protected ValidationFailure createFailure() {
|
||||
return new ValidationFailureBuilder().warning().detail("cause").detail("recommendedAction")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
public class RequiredConstraint {
|
||||
|
||||
public void validate(Object value, ValidationContext context) {
|
||||
if (value == null) {
|
||||
context.addDefaultFailure();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 2008 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.springframework.binding.validation;
|
||||
|
||||
import org.springframework.binding.validation.ValidationFailureMessageResolverFactory;
|
||||
import org.springframework.binding.validation.ValidationFailureModelContext;
|
||||
|
||||
/**
|
||||
* Provides additional model context regarding a validation failure. Used by a
|
||||
* {@link ValidationFailureMessageResolverFactory} to resolve failure messages.
|
||||
*/
|
||||
public class TestValidationFailureModelContext implements ValidationFailureModelContext {
|
||||
|
||||
private String model;
|
||||
|
||||
private Object invalidValue;
|
||||
|
||||
private Class propertyType;
|
||||
|
||||
private String propertyConverter;
|
||||
|
||||
/**
|
||||
* Creates a new validation model context.
|
||||
* @param model the name of the model object that was validated
|
||||
* @param invalidValue the invalid value the user entered (may be null)
|
||||
* @param propertyType the type of the property that failed to validate (may be null)
|
||||
* @param propertyConverter the id of the custom converter configured to format the property value (may be null)
|
||||
*/
|
||||
public TestValidationFailureModelContext(String model, Object invalidValue, Class propertyType,
|
||||
String propertyConverter) {
|
||||
this.model = model;
|
||||
this.invalidValue = invalidValue;
|
||||
this.propertyType = propertyType;
|
||||
this.propertyConverter = propertyConverter;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public Object getInvalidValue() {
|
||||
return invalidValue;
|
||||
}
|
||||
|
||||
public Class getPropertyType() {
|
||||
return propertyType;
|
||||
}
|
||||
|
||||
public String getPropertyConverter() {
|
||||
return propertyConverter;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidationFailureMessageCodesFactoryTests extends TestCase {
|
||||
private ValidationFailureMessageCodesFactory factory = new ValidationFailureMessageCodesFactory();
|
||||
|
||||
public void testCreateGeneralModelFailureMessageCodes() {
|
||||
ValidationFailure failure = new ValidationFailureBuilder().constraint("invalid").build();
|
||||
String[] codes = factory.createMessageCodes(failure, new TestValidationFailureModelContext("testBean", null,
|
||||
null, null));
|
||||
assertEquals("validation.testBean.invalid", codes[0]);
|
||||
assertEquals("validation.invalid", codes[1]);
|
||||
}
|
||||
|
||||
public void testCreatePropertyFailureMessageCodes() {
|
||||
ValidationFailure failure = new ValidationFailureBuilder().forProperty("foo").constraint("required").build();
|
||||
String[] codes = factory.createMessageCodes(failure, new TestValidationFailureModelContext("testBean", null,
|
||||
String.class, null));
|
||||
assertEquals("validation.testBean.foo.required", codes[0]);
|
||||
assertEquals("validation.java.lang.String.required", codes[1]);
|
||||
assertEquals("validation.required", codes[2]);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
validation.required=#{label} is required
|
||||
validation.invalidFormat=#{label} must be in format #{format}
|
||||
validation.invalidFormat2=#{label} must be in format #{format} but it was '#{value}'
|
||||
validation.range=#{label} must be between #{min} and #{max}
|
||||
|
||||
label.testBean.foo=Foo
|
||||
label.testBean.checkinDate=Check In Date
|
||||
label.testBean.amount=Amount
|
||||
|
||||
formats.dateFormat=yyyy-MM-dd
|
||||
Reference in New Issue
Block a user