You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by pf...@apache.org on 2005/06/06 00:15:05 UTC
cvs commit: jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator TestNumberTranslator.java TestDateTranslator.java TestStringTranslator.java
pferraro 2005/06/05 15:15:05
Modified: framework/src/test/org/apache/tapestry/form/translator
TestNumberTranslator.java TestDateTranslator.java
TestStringTranslator.java
Added: framework/src/test/org/apache/tapestry/form/validator
TestNumberValidator.java TestDateValidator.java
TestEmailValidator.java TestRegExValidator.java
TestStringValidator.java
framework/src/java/org/apache/tapestry/form/validator
NumberValidator.java EmailValidator.java
RegExValidator.java NumberValidator.js
RegExValidator.js Validator.java
StringValidator.java DateValidator.java
StringValidator.js
Log:
Added Validator interface and implementations for dates, numbers, strings, email addresses, and regular expressions.
Revision Changes Path
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestNumberValidator.java
Index: TestNumberValidator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.util.Locale;
import org.apache.tapestry.form.FormComponentContributorTestCase;
import org.apache.tapestry.form.FormEventType;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidatorException;
/**
* Test case for {@link NumberValidator}.
*
* @author Paul Ferraro
* @since 4.0
*/
public class TestNumberValidator extends FormComponentContributorTestCase
{
private NumberValidator _validator = new NumberValidator();
public void testValidate()
{
_validator.setMin(new Integer(0));
_validator.setMax(new Integer(10));
replay();
try
{
_validator.validate(_component, new Integer(5));
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testTooSmallValidate()
{
testTooSmallValidate("Field Label must not be smaller than 10.");
}
public void testCustomTooSmallValidate()
{
_validator.setTooSmallMessage("{0} must be greater than or equal to {1}.");
testTooSmallValidate("Field Label must be greater than or equal to 10.");
}
private void testTooSmallValidate(String message)
{
_validator.setMin(new Integer(10));
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
replay();
try
{
_validator.validate(_component, new Integer(5));
unreachable();
}
catch (ValidatorException e)
{
assertEquals(message, e.getMessage());
assertEquals(ValidationConstraint.TOO_SMALL, e.getConstraint());
}
finally
{
verify();
}
}
public void testTooLargeValidate()
{
testTooLargeValidate("Field Label must not be larger than 10.");
}
public void testCustomTooLargeValidate()
{
_validator.setTooLargeMessage("{0} must be less than or equal to {1}.");
testTooLargeValidate("Field Label must be less than or equal to 10.");
}
private void testTooLargeValidate(String message)
{
_validator.setMax(new Integer(10));
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
replay();
try
{
_validator.validate(_component, new Integer(20));
unreachable();
}
catch (ValidatorException e)
{
assertEquals(message, e.getMessage());
assertEquals(ValidationConstraint.TOO_LARGE, e.getConstraint());
}
finally
{
verify();
}
}
public void testMinRenderContribution()
{
_validator.setMin(new Integer(10));
testRenderContribution(new String[] { "validate_min_number(document.formName.fieldName,10,'Field Label must not be smaller than 10.')" });
}
public void testCustomMinRenderContribution()
{
_validator.setMin(new Integer(5));
_validator.setTooSmallMessage("{0} must be greater than or equal to {1}.");
testRenderContribution(new String[] { "validate_min_number(document.formName.fieldName,5,'Field Label must be greater than or equal to 5.')" });
}
public void testMaxRenderContribution()
{
_validator.setMax(new Integer(100));
testRenderContribution(new String[] { "validate_max_number(document.formName.fieldName,100,'Field Label must not be larger than 100.')" });
}
public void testCustomMaxRenderContribution()
{
_validator.setMax(new Integer(50));
_validator.setTooLargeMessage("{0} must be less than or equal to {1}.");
testRenderContribution(new String[] { "validate_max_number(document.formName.fieldName,50,'Field Label must be less than or equal to 50.')" });
}
public void testMinMaxRenderContribution()
{
_validator.setMin(new Integer(10));
_validator.setMax(new Integer(100));
String[] handlers = new String[] {
"validate_min_number(document.formName.fieldName,10,'Field Label must not be smaller than 10.')",
"validate_max_number(document.formName.fieldName,100,'Field Label must not be larger than 100.')"
};
testRenderContribution(handlers);
}
public void testCustomMinMaxRenderContribution()
{
_validator.setMin(new Integer(5));
_validator.setMax(new Integer(50));
_validator.setTooSmallMessage("{0} must be greater than or equal to {1}.");
_validator.setTooLargeMessage("{0} must be less than or equal to {1}.");
String[] handlers = new String[] {
"validate_min_number(document.formName.fieldName,5,'Field Label must be greater than or equal to 5.')",
"validate_max_number(document.formName.fieldName,50,'Field Label must be less than or equal to 50.')"
};
testRenderContribution(handlers);
}
public void testRenderContribution(String[] handlers)
{
addScript("/org/apache/tapestry/form/validator/NumberValidator.js");
_component.getForm();
_componentControl.setReturnValue(_form);
_form.getName();
_formControl.setReturnValue("formName");
_component.getName();
_componentControl.setReturnValue("fieldName");
for (int i = 0; i < handlers.length; ++i)
{
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
_form.addEventHandler(FormEventType.SUBMIT, handlers[i]);
_formControl.setVoidCallable();
}
replay();
_validator.renderContribution(null, _cycle, _component);
verify();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestDateValidator.java
Index: TestDateValidator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.apache.tapestry.form.FormComponentContributorTestCase;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidatorException;
/**
* Test case for {@link NumberValidator}.
*
* @author Paul Ferraro
* @since 4.0
*/
public class TestDateValidator extends FormComponentContributorTestCase
{
private DateValidator _validator = new DateValidator();
private Calendar _calendar = Calendar.getInstance();
/**
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception
{
_calendar.clear();
}
private Date buildDate(int year, int month, int day)
{
_calendar.set(Calendar.YEAR, year);
_calendar.set(Calendar.MONTH, month);
_calendar.set(Calendar.DATE, day);
return _calendar.getTime();
}
public void testValidate()
{
_validator.setMin(buildDate(2005, Calendar.JANUARY, 1));
_validator.setMax(buildDate(2005, Calendar.DECEMBER, 31));
replay();
try
{
_validator.validate(_component, buildDate(2005, Calendar.JUNE, 30));
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testTooSmallValidate()
{
testTooSmallValidate("Field Label may not be earlier than Jan 1, 2005.");
}
public void testCustomTooSmallValidate()
{
_validator.setTooEarlyMessage("{0} must be after {1,date}.");
testTooSmallValidate("Field Label must be after Jan 1, 2005.");
}
private void testTooSmallValidate(String message)
{
_validator.setMin(buildDate(2005, Calendar.JANUARY, 1));
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
replay();
try
{
_validator.validate(_component, buildDate(2004, Calendar.DECEMBER, 31));
unreachable();
}
catch (ValidatorException e)
{
assertEquals(message, e.getMessage());
assertEquals(ValidationConstraint.TOO_SMALL, e.getConstraint());
}
finally
{
verify();
}
}
public void testTooLongValidate()
{
testTooLateValidate("Field Label may not be later than Dec 31, 2005.");
}
public void testCustomTooLongValidate()
{
_validator.setTooLateMessage("{0} must be before {1,date}.");
testTooLateValidate("Field Label must be before Dec 31, 2005.");
}
private void testTooLateValidate(String message)
{
_validator.setMax(buildDate(2005, Calendar.DECEMBER, 31));
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
replay();
try
{
_validator.validate(_component, buildDate(2006, Calendar.JANUARY, 1));
unreachable();
}
catch (ValidatorException e)
{
assertEquals(message, e.getMessage());
assertEquals(ValidationConstraint.TOO_LARGE, e.getConstraint());
}
finally
{
verify();
}
}
public void testRenderContribution()
{
replay();
_validator.renderContribution(null, _cycle, _component);
verify();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestEmailValidator.java
Index: TestEmailValidator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.util.Locale;
import org.apache.tapestry.form.FormEventType;
/**
* Test case for {@link EmailValidator}.
*
* @author Paul Ferraro
* @since 4.0
*/
public class TestEmailValidator extends TestRegExValidator
{
protected RegExValidator createValidator()
{
return new EmailValidator();
}
public void testValidate()
{
testValidate("pferraro@apache.org");
}
public void testInvalidValidate()
{
testInvalidValidate("Invalid email format for Field Label. Format is user@hostname.", "pferraro");
}
public void testCustomInvalidValidate()
{
_validator.setMessage("{0} is not a valid email address.");
testInvalidValidate("Field Label is not a valid email address.", "pferraro");
}
public void testRenderContribution()
{
testRenderContribution("validate_regex(document.formName.fieldName,'\\^\\\\w\\[\\-\\._\\\\w\\]\\*\\\\w\\@\\\\w\\[\\-\\._\\\\w\\]\\*\\\\w\\\\\\.\\\\w\\{2\\,3\\}\\$','Invalid email format for Field Label. Format is user@hostname.')");
}
public void testCustomRenderContribution()
{
_validator.setMessage("{0} is not a valid email address.");
testRenderContribution("validate_regex(document.formName.fieldName,'\\^\\\\w\\[\\-\\._\\\\w\\]\\*\\\\w\\@\\\\w\\[\\-\\._\\\\w\\]\\*\\\\w\\\\\\.\\\\w\\{2\\,3\\}\\$','Field Label is not a valid email address.')");
}
private void testRenderContribution(String handler)
{
addScript("/org/apache/tapestry/form/validator/RegExValidator.js");
_component.getForm();
_componentControl.setReturnValue(_form);
_form.getName();
_formControl.setReturnValue("formName");
_component.getName();
_componentControl.setReturnValue("fieldName");
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
_form.addEventHandler(FormEventType.SUBMIT, handler);
_formControl.setVoidCallable();
replay();
_validator.renderContribution(null, _cycle, _component);
verify();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestRegExValidator.java
Index: TestRegExValidator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.util.Locale;
import org.apache.tapestry.form.FormComponentContributorTestCase;
import org.apache.tapestry.form.FormEventType;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidatorException;
/**
* Test case for {@link RegExValidator}.
*
* @author Paul Ferraro
* @since 4.0
*/
public class TestRegExValidator extends FormComponentContributorTestCase
{
protected RegExValidator _validator = createValidator();
protected RegExValidator createValidator()
{
return new RegExValidator();
}
public void testValidate()
{
_validator.setExpression("\\w+");
testValidate("test");
}
protected void testValidate(String value)
{
replay();
try
{
_validator.validate(_component, value);
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testInvalidValidate()
{
_validator.setExpression("^\\w+$");
testInvalidValidate("Field Label is invalid.", "regex does not allow spaces");
}
public void testCustomInvalidValidate()
{
_validator.setExpression("^\\w+$");
_validator.setMessage("{0} does not match the regular expression: {1}");
testInvalidValidate("Field Label does not match the regular expression: ^\\w+$", "regex does not allow spaces");
}
protected void testInvalidValidate(String message, String value)
{
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
replay();
try
{
_validator.validate(_component, value);
unreachable();
}
catch (ValidatorException e)
{
assertEquals(message, e.getMessage());
assertEquals(ValidationConstraint.PATTERN_MISMATCH, e.getConstraint());
}
finally
{
verify();
}
}
public void testRenderContribution()
{
_validator.setExpression("^\\w+$");
testRenderContribution("validate_regex(document.formName.fieldName,'\\^\\\\w\\+\\$','Field Label is invalid.')");
}
public void testCustomRenderContribution()
{
_validator.setExpression("^\\w+$");
_validator.setMessage("{0} does not match the regular expression: {1}");
testRenderContribution("validate_regex(document.formName.fieldName,'\\^\\\\w\\+\\$','Field Label does not match the regular expression: ^\\w+$')");
}
private void testRenderContribution(String handler)
{
addScript("/org/apache/tapestry/form/validator/RegExValidator.js");
_component.getForm();
_componentControl.setReturnValue(_form);
_form.getName();
_formControl.setReturnValue("formName");
_component.getName();
_componentControl.setReturnValue("fieldName");
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
_form.addEventHandler(FormEventType.SUBMIT, handler);
_formControl.setVoidCallable();
replay();
_validator.renderContribution(null, _cycle, _component);
verify();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestStringValidator.java
Index: TestStringValidator.java
===================================================================
//Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.util.Locale;
import org.apache.tapestry.form.FormComponentContributorTestCase;
import org.apache.tapestry.form.FormEventType;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidatorException;
/**
* Test case for {@link StringValidator}.
*
* @author Paul Ferraro
* @since 4.0
*/
public class TestStringValidator extends FormComponentContributorTestCase
{
private StringValidator _validator = new StringValidator();
public void testValidate()
{
_validator.setMinLength(0);
_validator.setMaxLength(10);
replay();
try
{
_validator.validate(_component, "test");
}
catch (ValidatorException e)
{
unreachable();
}
finally
{
verify();
}
}
public void testTooShortValidate()
{
testTooShortValidate("You must enter at least 10 characters for Field Label.");
}
public void testCustomTooShortValidate()
{
_validator.setTooShortMessage("{1} may not be less than {0} characters in length.");
testTooShortValidate("Field Label may not be less than 10 characters in length.");
}
private void testTooShortValidate(String message)
{
_validator.setMinLength(10);
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
replay();
try
{
_validator.validate(_component, "test");
unreachable();
}
catch (ValidatorException e)
{
assertEquals(message, e.getMessage());
assertEquals(ValidationConstraint.MINIMUM_WIDTH, e.getConstraint());
}
finally
{
verify();
}
}
public void testTooLongValidate()
{
testTooLongValidate("You must enter no more than 10 characters for Field Label.");
}
public void testCustomTooLongValidate()
{
_validator.setTooLongMessage("{1} may not be more than {0} characters in length.");
testTooLongValidate("Field Label may not be more than 10 characters in length.");
}
private void testTooLongValidate(String message)
{
_validator.setMaxLength(10);
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
replay();
try
{
_validator.validate(_component, "abcdefghijklmnopqrstuvwxyz");
unreachable();
}
catch (ValidatorException e)
{
assertEquals(message, e.getMessage());
assertEquals(ValidationConstraint.MAXIMUM_WIDTH, e.getConstraint());
}
finally
{
verify();
}
}
public void testMinRenderContribution()
{
_validator.setMinLength(10);
testRenderContribution(new String[] { "validate_min_length(document.formName.fieldName,10,'You must enter at least 10 characters for Field Label.')" });
}
public void testCustomMinRenderContribution()
{
_validator.setMinLength(5);
_validator.setTooShortMessage("{1} should be at least {0} characters in length.");
testRenderContribution(new String[] { "validate_min_length(document.formName.fieldName,5,'Field Label should be at least 5 characters in length.')" });
}
public void testMaxRenderContribution()
{
_validator.setMaxLength(100);
testRenderContribution(new String[] { "validate_max_length(document.formName.fieldName,100,'You must enter no more than 100 characters for Field Label.')" });
}
public void testCustomMaxRenderContribution()
{
_validator.setMaxLength(50);
_validator.setTooLongMessage("{1} should be no more than {0} characters in length.");
testRenderContribution(new String[] { "validate_max_length(document.formName.fieldName,50,'Field Label should be no more than 50 characters in length.')" });
}
public void testMinMaxRenderContribution()
{
_validator.setMinLength(10);
_validator.setMaxLength(100);
String[] handlers = new String[] {
"validate_min_length(document.formName.fieldName,10,'You must enter at least 10 characters for Field Label.')",
"validate_max_length(document.formName.fieldName,100,'You must enter no more than 100 characters for Field Label.')"
};
testRenderContribution(handlers);
}
public void testCustomMinMaxRenderContribution()
{
_validator.setMinLength(5);
_validator.setMaxLength(50);
_validator.setTooShortMessage("{1} should be at least {0} characters in length.");
_validator.setTooLongMessage("{1} should be no more than {0} characters in length.");
String[] handlers = new String[] {
"validate_min_length(document.formName.fieldName,5,'Field Label should be at least 5 characters in length.')",
"validate_max_length(document.formName.fieldName,50,'Field Label should be no more than 50 characters in length.')"
};
testRenderContribution(handlers);
}
public void testRenderContribution(String[] handlers)
{
addScript("/org/apache/tapestry/form/validator/StringValidator.js");
_component.getForm();
_componentControl.setReturnValue(_form);
_form.getName();
_formControl.setReturnValue("formName");
_component.getName();
_componentControl.setReturnValue("fieldName");
for (int i = 0; i < handlers.length; ++i)
{
_component.getPage();
_componentControl.setReturnValue(_page);
_page.getLocale();
_pageControl.setReturnValue(Locale.US);
_component.getDisplayName();
_componentControl.setReturnValue("Field Label");
_form.addEventHandler(FormEventType.SUBMIT, handlers[i]);
_formControl.setVoidCallable();
}
replay();
_validator.renderContribution(null, _cycle, _component);
verify();
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/NumberValidator.java
Index: NumberValidator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.text.MessageFormat;
import java.util.Locale;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.AbstractFormComponentContributor;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* {@link Validator} implementation that validates a number against max and min values.
*
* @author Paul Ferraro
* @since 4.0
*/
public class NumberValidator extends AbstractFormComponentContributor implements Validator
{
private Number _min;
private Number _max;
private String _tooSmallMessage;
private String _tooLargeMessage;
protected String defaultScript()
{
return "/org/apache/tapestry/form/validator/NumberValidator.js";
}
/**
* @see org.apache.tapestry.form.validator.Validator#validate(org.apache.tapestry.form.IFormComponent, java.lang.Object)
*/
public void validate(IFormComponent field, Object object) throws ValidatorException
{
Number value = (Number) object;
if ((_min != null) && ((_min.doubleValue() - value.doubleValue()) > 0))
{
throw new ValidatorException(buildTooSmallMessage(field), ValidationConstraint.TOO_SMALL);
}
if ((_max != null) && ((_max.doubleValue() - value.doubleValue()) < 0))
{
throw new ValidatorException(buildTooLargeMessage(field), ValidationConstraint.TOO_LARGE);
}
}
protected String buildTooSmallMessage(IFormComponent field)
{
return buildMessage(field, _tooSmallMessage, ValidationStrings.VALUE_TOO_SMALL, _min);
}
protected String buildTooLargeMessage(IFormComponent field)
{
return buildMessage(field, _tooLargeMessage, ValidationStrings.VALUE_TOO_LARGE, _max);
}
protected String buildMessage(IFormComponent field, String messageOverride, String key, Number object)
{
Locale locale = field.getPage().getLocale();
String message = (messageOverride == null) ? ValidationStrings.getMessagePattern(key, locale) : messageOverride;
return MessageFormat.format(message, new Object[] { field.getDisplayName(), object });
}
/**
* @return Returns the max.
*/
public Number getMax()
{
return _max;
}
/**
* @param max The max to set.
*/
public void setMax(Number max)
{
_max = max;
}
/**
* @return Returns the min.
*/
public Number getMin()
{
return _min;
}
/**
* @param min The min to set.
*/
public void setMin(Number min)
{
_min = min;
}
/**
* @return Returns the numberTooLargeMessage.
*/
public String getTooLargeMessage()
{
return _tooLargeMessage;
}
/**
* @param message The tooLargeMessage to set.
*/
public void setTooLargeMessage(String message)
{
_tooLargeMessage = message;
}
/**
* @return Returns the tooSmallMessage.
*/
public String getTooSmallMessage()
{
return _tooSmallMessage;
}
/**
* @param message The tooSmallMessage to set.
*/
public void setTooSmallMessage(String message)
{
_tooSmallMessage = message;
}
/**
* @see org.apache.tapestry.form.AbstractFormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, org.apache.tapestry.form.IFormComponent)
*/
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, IFormComponent field)
{
super.renderContribution(writer, cycle, field);
IForm form = field.getForm();
String formName = form.getName();
String fieldName = field.getName();
if (_min != null)
{
String message = buildTooSmallMessage(field);
String handler = "validate_min_number(document." + formName + "." + fieldName + "," + _min + ",'" + message + "')";
super.addSubmitHandler(form, handler);
}
if (_max != null)
{
String message = buildTooLargeMessage(field);
String handler = "validate_max_number(document." + formName + "." + fieldName + "," + _max + ",'" + message + "')";
super.addSubmitHandler(form, handler);
}
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/EmailValidator.java
Index: EmailValidator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import org.apache.tapestry.valid.ValidationStrings;
/**
* {@link Validator} implementation that validates email addresses.
*
* @author Paul Ferraro
* @since 4.0
*/
public class EmailValidator extends RegExValidator
{
/**
* @see org.apache.tapestry.form.validator.RegExValidator#defaultExpression()
*/
protected String defaultExpression()
{
return "^\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$";
}
/**
* @see org.apache.tapestry.form.validator.RegExValidator#getMessageKey()
*/
protected String getMessageKey()
{
return ValidationStrings.INVALID_EMAIL;
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/RegExValidator.java
Index: RegExValidator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.text.MessageFormat;
import java.util.Locale;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.AbstractFormComponentContributor;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.util.RegexpMatcher;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* {@link Validator} implementation that validates against a regular expression.
*
* @author Paul Ferraro
* @since 4.0
*/
public class RegExValidator extends AbstractFormComponentContributor implements Validator
{
private RegexpMatcher _matcher = new RegexpMatcher();
private String _expression = defaultExpression();
/**
* A custom message in the event of a validation failure.
*/
private String _message;
protected String defaultScript()
{
return "/org/apache/tapestry/form/validator/RegExValidator.js";
}
protected String defaultExpression()
{
return "";
}
/**
* @see org.apache.tapestry.form.validator.Validator#validate(org.apache.tapestry.form.IFormComponent, java.lang.Object)
*/
public void validate(IFormComponent field, Object object) throws ValidatorException
{
String string = (String) object;
if (!_matcher.contains(_expression, string))
{
throw new ValidatorException(buildMessage(field), ValidationConstraint.PATTERN_MISMATCH);
}
}
protected String buildMessage(IFormComponent field)
{
Locale locale = field.getPage().getLocale();
String message = (_message == null) ? ValidationStrings.getMessagePattern(getMessageKey(), locale) : _message;
return MessageFormat.format(message, new Object[] { field.getDisplayName(), _expression });
}
protected String getMessageKey()
{
return ValidationStrings.REGEX_MISMATCH;
}
/**
* @see org.apache.tapestry.form.AbstractFormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, org.apache.tapestry.form.IFormComponent)
*/
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, IFormComponent field)
{
super.renderContribution(writer, cycle, field);
IForm form = field.getForm();
String expression = _matcher.getEscapedPatternString(_expression);
String message = buildMessage(field);
String handler = "validate_regex(document." + form.getName() + "." + field.getName() + ",'" + expression + "','" + message + "')";
addSubmitHandler(form, handler);
}
/**
* @return Returns the pattern.
*/
public String getExpression()
{
return _expression;
}
/**
* @param pattern The pattern to set.
*/
public void setExpression(String expression)
{
_expression = expression;
}
/**
* @return Returns the patternNotMatchedMessage.
*/
public String getMessage()
{
return _message;
}
/**
* @param patternNotMatchedMessage The patternNotMatchedMessage to set.
*/
public void setMessage(String message)
{
_message = message;
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/NumberValidator.js
Index: NumberValidator.js
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.
function validate_min_number(field, min, message)
{
var num = eval(field.value)
if (num < min)
{
return handle_invalid_field(field, message)
}
return true
}
function validate_max_number(field, max, message)
{
var num = eval(field.value)
if (num > max)
{
return handle_invalid_field(field, message)
}
return true
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/RegExValidator.js
Index: RegExValidator.js
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.
function validate_regex(field, pattern, message)
{
var regexp = new RegExp(pattern)
if (!regexp.test(field.value))
{
return handle_invalid_field(field, message)
}
return true
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/Validator.java
Index: Validator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.FormComponentContributor;
import org.apache.tapestry.valid.ValidatorException;
/**
* @author Paul Ferraro
* @since 3.1
*/
public interface Validator extends FormComponentContributor
{
public void validate(IFormComponent field, Object object) throws ValidatorException;
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/StringValidator.java
Index: StringValidator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.text.MessageFormat;
import java.util.Locale;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.AbstractFormComponentContributor;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* @author Paul Ferraro
* @since 3.1
*/
public class StringValidator extends AbstractFormComponentContributor implements Validator
{
private int _minLength = 0;
private int _maxLength = 0;
private String _tooLongMessage;
private String _tooShortMessage;
protected String defaultScript()
{
return "/org/apache/tapestry/form/validator/StringValidator.js";
}
/**
* @see org.apache.tapestry.form.validator.Validator#validate(java.lang.Object)
*/
public void validate(IFormComponent field, Object object) throws ValidatorException
{
String string = (String) object;
if ((_minLength > 0) && (string.length() < _minLength))
{
throw new ValidatorException(buildTooShortMessage(field), ValidationConstraint.MINIMUM_WIDTH);
}
if ((_maxLength > 0) && (string.length() > _maxLength))
{
throw new ValidatorException(buildTooLongMessage(field), ValidationConstraint.MAXIMUM_WIDTH);
}
}
public int getMinLength()
{
return _minLength;
}
public void setMinLength(int minLength)
{
_minLength = minLength;
}
public int getMaxLength()
{
return _maxLength;
}
public void setMaxLength(int maxLength)
{
_maxLength = maxLength;
}
public String getTooShortMessage()
{
return _tooShortMessage;
}
public void setTooShortMessage(String message)
{
_tooShortMessage = message;
}
public String getTooLongMessage()
{
return _tooLongMessage;
}
public void setTooLongMessage(String message)
{
_tooLongMessage = message;
}
protected String buildTooShortMessage(IFormComponent field)
{
return buildMessage(field, _tooShortMessage, ValidationStrings.VALUE_TOO_SHORT, _minLength);
}
protected String buildTooLongMessage(IFormComponent field)
{
return buildMessage(field, _tooLongMessage, ValidationStrings.VALUE_TOO_LONG, _maxLength);
}
protected String buildMessage(IFormComponent field, String messageOverride, String key, int length)
{
Locale locale = field.getPage().getLocale();
String message = (messageOverride == null) ? ValidationStrings.getMessagePattern(key, locale) : messageOverride;
return MessageFormat.format(message, new Object[] { new Integer(length), field.getDisplayName() });
}
/**
* @see org.apache.tapestry.form.AbstractFormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, org.apache.tapestry.form.IFormComponent)
*/
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, IFormComponent field)
{
super.renderContribution(writer, cycle, field);
IForm form = field.getForm();
String formName = form.getName();
String fieldName = field.getName();
if (_minLength > 0)
{
String message = buildTooShortMessage(field);
String handler = "validate_min_length(document." + formName + "." + fieldName + "," + _minLength + ",'" + message + "')";
super.addSubmitHandler(form, handler);
}
if (_maxLength > 0)
{
String message = buildTooLongMessage(field);
String handler = "validate_max_length(document." + formName + "." + fieldName + "," + _maxLength + ",'" + message + "')";
super.addSubmitHandler(form, handler);
}
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/DateValidator.java
Index: DateValidator.java
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
import org.apache.tapestry.form.AbstractFormComponentContributor;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* {@link Validator} implementation that validates a date against max and min values.
*
* @author Paul Ferraro
* @since 4.0
*/
public class DateValidator extends AbstractFormComponentContributor implements Validator
{
private Date _min;
private Date _max;
private String _tooEarlyMessage;
private String _tooLateMessage;
/**
* @see org.apache.tapestry.form.validator.Validator#validate(org.apache.tapestry.form.IFormComponent, java.lang.Object)
*/
public void validate(IFormComponent field, Object object) throws ValidatorException
{
Date date = (Date) object;
if ((_min != null) && date.before(_min))
{
throw new ValidatorException(buildTooSmallMessage(field), ValidationConstraint.TOO_SMALL);
}
if ((_max != null) && date.after(_max))
{
throw new ValidatorException(buildTooLargeMessage(field), ValidationConstraint.TOO_LARGE);
}
}
protected String buildTooSmallMessage(IFormComponent field)
{
return buildMessage(field, _tooEarlyMessage, ValidationStrings.DATE_TOO_EARLY, _min);
}
protected String buildTooLargeMessage(IFormComponent field)
{
return buildMessage(field, _tooLateMessage, ValidationStrings.DATE_TOO_LATE, _max);
}
protected String buildMessage(IFormComponent field, String messageOverride, String key, Date date)
{
Locale locale = field.getPage().getLocale();
String message = (messageOverride == null) ? ValidationStrings.getMessagePattern(key, locale) : messageOverride;
return MessageFormat.format(message, new Object[] { field.getDisplayName(), date });
}
/**
* @return Returns the max.
*/
public Date getMax()
{
return _max;
}
/**
* @param max The max to set.
*/
public void setMax(Date max)
{
_max = max;
}
/**
* @return Returns the min.
*/
public Date getMin()
{
return _min;
}
/**
* @param min The min to set.
*/
public void setMin(Date min)
{
_min = min;
}
/**
* @return Returns the numberTooLargeMessage.
*/
public String getTooLateMessage()
{
return _tooLateMessage;
}
/**
* @param message The tooLargeMessage to set.
*/
public void setTooLateMessage(String message)
{
_tooLateMessage = message;
}
/**
* @return Returns the tooSmallMessage.
*/
public String getTooEarlyMessage()
{
return _tooEarlyMessage;
}
/**
* @param message The tooSmallMessage to set.
*/
public void setTooEarlyMessage(String message)
{
_tooEarlyMessage = message;
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/StringValidator.js
Index: StringValidator.js
===================================================================
// Copyright 2004, 2005 The Apache Software Foundation
//
// 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.
function validate_min_length(field, min, message)
{
if (field.value.length < min)
{
return handle_invalid_field(field, message)
}
return true
}
function validate_max_length(field, max, message)
{
if (field.value.length > max)
{
return handle_invalid_field(field, message)
}
return true
}
1.3 +3 -3 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TestNumberTranslator.java
Index: TestNumberTranslator.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TestNumberTranslator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestNumberTranslator.java 5 Jun 2005 00:15:24 -0000 1.2
+++ TestNumberTranslator.java 5 Jun 2005 22:15:05 -0000 1.3
@@ -159,7 +159,7 @@
}
}
- public void testContributeFormEvents()
+ public void testRenderContribution()
{
addScript("/org/apache/tapestry/form/translator/NumberTranslator.js");
@@ -191,7 +191,7 @@
verify();
}
- public void testMessageContributeFormEvents()
+ public void testMessageRenderContribution()
{
_translator.setMessage("You entered a bunk value for {0}. I should look like {1}.");
@@ -225,7 +225,7 @@
verify();
}
- public void testTrimContributeFormEvents()
+ public void testTrimRenderContribution()
{
_translator.setTrim(true);
trim();
1.3 +2 -2 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TestDateTranslator.java
Index: TestDateTranslator.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TestDateTranslator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestDateTranslator.java 5 Jun 2005 00:15:24 -0000 1.2
+++ TestDateTranslator.java 5 Jun 2005 22:15:05 -0000 1.3
@@ -204,7 +204,7 @@
}
}
- public void testContributeFormEvents()
+ public void testRenderContribution()
{
replay();
@@ -213,7 +213,7 @@
verify();
}
- public void testTrimContributeFormEvents()
+ public void testTrimRenderContribution()
{
_translator.setTrim(true);
trim();
1.2 +2 -2 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TestStringTranslator.java
Index: TestStringTranslator.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator/TestStringTranslator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestStringTranslator.java 4 Jun 2005 23:53:21 -0000 1.1
+++ TestStringTranslator.java 5 Jun 2005 22:15:05 -0000 1.2
@@ -132,7 +132,7 @@
}
}
- public void testContributeFormEvents()
+ public void testRenderContribution()
{
replay();
@@ -141,7 +141,7 @@
verify();
}
- public void testTrimContributeFormEvents()
+ public void testTrimRenderContribution()
{
_translator.setTrim(true);
trim();
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org
Re: cvs commit: jakarta-tapestry/framework/src/test/org/apache/tapestry/form/translator TestNumberTranslator.java TestDateTranslator.java TestStringTranslator.java
Posted by Howard Lewis Ship <hl...@gmail.com>.
After updating from CVS, I'm getting a couple of compile errors,
concerening ValidationConstraint.MAXIMUM_WIDTH. Did you forget to
delete some files, perhaps?
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org