You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2005/06/20 18:32:05 UTC
cvs commit: jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator Pattern.java Email.java RegExValidator.java EmailValidator.java
hlship 2005/06/20 09:32:05
Modified: framework/src/test/org/apache/tapestry/form/validator
TestRequired.java
framework/src/java/org/apache/tapestry/form
FormComponentContributorContext.java
Added: framework/src/test/org/apache/tapestry/form/validator
TestPattern.java TestEmail.java
framework/src/java/org/apache/tapestry/form/validator
Pattern.java Email.java
Removed: framework/src/test/org/apache/tapestry/form/validator
TestEmailValidator.java TestRegExValidator.java
framework/src/java/org/apache/tapestry/form/validator
RegExValidator.java EmailValidator.java
Log:
Replace EmailValidator with Email and RegExVaidator with Pattern.
Revision Changes Path
1.3 +6 -0 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestRequired.java
Index: TestRequired.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestRequired.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestRequired.java 18 Jun 2005 19:56:26 -0000 1.2
+++ TestRequired.java 20 Jun 2005 16:32:05 -0000 1.3
@@ -66,6 +66,8 @@
assertEquals("Default Message for Fred.", ex.getMessage());
assertSame(ValidationConstraint.REQUIRED, ex.getConstraint());
}
+
+ verifyControls();
}
public void testValidateNullCustomMessage() throws Exception
@@ -92,6 +94,8 @@
assertEquals("Custom Message for Fred.", ex.getMessage());
assertSame(ValidationConstraint.REQUIRED, ex.getConstraint());
}
+
+ verifyControls();
}
public void testRenderContribution()
@@ -123,5 +127,7 @@
replayControls();
new Required().renderContribution(writer, cycle, context, field);
+
+ verifyControls();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestPattern.java
Index: TestPattern.java
===================================================================
// Copyright 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.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.util.RegexpMatcher;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
import org.easymock.MockControl;
/**
* Tests for {@link org.apache.tapestry.form.validator.Pattern}
*
* @author Howard Lewis Ship
* @since 4.0
*/
public class TestPattern extends BaseValidatorTestCase
{
public void testOK() throws Exception
{
IFormComponent field = newField();
ValidationMessages messages = newMessages();
replayControls();
new Pattern("pattern=\\d+").validate(field, messages, "1232");
verifyControls();
}
public void testFail()
{
IFormComponent field = newField("My Pattern");
ValidationMessages messages = newMessages(
null,
ValidationStrings.REGEX_MISMATCH,
new Object[]
{ "My Pattern" },
"default message");
replayControls();
try
{
new Pattern("pattern=\\d+").validate(field, messages, "fred");
unreachable();
}
catch (ValidatorException ex)
{
assertEquals("default message", ex.getMessage());
assertEquals(ValidationConstraint.PATTERN_MISMATCH, ex.getConstraint());
}
verifyControls();
}
public void testFailCustomMessage()
{
IFormComponent field = newField("My Pattern");
ValidationMessages messages = newMessages(
"custom",
ValidationStrings.REGEX_MISMATCH,
new Object[]
{ "My Pattern" },
"custom message");
replayControls();
try
{
new Pattern("pattern=\\d+,message=custom").validate(field, messages, "fred");
unreachable();
}
catch (ValidatorException ex)
{
assertEquals("custom message", ex.getMessage());
assertEquals(ValidationConstraint.PATTERN_MISMATCH, ex.getConstraint());
}
verifyControls();
}
public void testRenderContribution()
{
String pattern = new RegexpMatcher().getEscapedPatternString("\\d+");
IMarkupWriter writer = newWriter();
IRequestCycle cycle = newCycle();
MockControl contextc = newControl(FormComponentContributorContext.class);
FormComponentContributorContext context = (FormComponentContributorContext) contextc
.getMock();
context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
IFormComponent field = newField("Fred");
trainFormatMessage(contextc, context, null, ValidationStrings.REGEX_MISMATCH, new Object[]
{ "Fred" }, "default message");
context.getFieldDOM();
contextc.setReturnValue("document.fred.barney");
context
.addSubmitListener("function(event) { validate_regexp(event, document.fred.barney, '"
+ pattern + "', 'default message'); }");
replayControls();
new Pattern("pattern=\\d+").renderContribution(writer, cycle, context, field);
verifyControls();
}
public void testRenderContributionCustomMessage()
{
String pattern = new RegexpMatcher().getEscapedPatternString("\\d+");
IMarkupWriter writer = newWriter();
IRequestCycle cycle = newCycle();
MockControl contextc = newControl(FormComponentContributorContext.class);
FormComponentContributorContext context = (FormComponentContributorContext) contextc
.getMock();
context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
IFormComponent field = newField("Fred");
trainFormatMessage(
contextc,
context,
"custom",
ValidationStrings.REGEX_MISMATCH,
new Object[]
{ "Fred" },
"custom message");
context.getFieldDOM();
contextc.setReturnValue("document.fred.barney");
context
.addSubmitListener("function(event) { validate_regexp(event, document.fred.barney, '"
+ pattern + "', 'custom message'); }");
replayControls();
new Pattern("pattern=\\d+,message=custom").renderContribution(writer, cycle, context, field);
verifyControls();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestEmail.java
Index: TestEmail.java
===================================================================
// Copyright 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.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.util.RegexpMatcher;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
import org.easymock.MockControl;
/**
* Tests for {@link org.apache.tapestry.form.validator.Email}.
*
* @author Howard Lewis Ship
* @since 4.0
*/
public class TestEmail extends BaseValidatorTestCase
{
public void testOK() throws Exception
{
IFormComponent field = newField();
ValidationMessages messages = newMessages();
replayControls();
new Email().validate(field, messages, "hlship@apache.org");
verifyControls();
}
public void testFail()
{
IFormComponent field = newField("My Email");
ValidationMessages messages = newMessages(
null,
ValidationStrings.INVALID_EMAIL,
new Object[]
{ "My Email" },
"default message");
replayControls();
try
{
new Email().validate(field, messages, "fred");
unreachable();
}
catch (ValidatorException ex)
{
assertEquals("default message", ex.getMessage());
assertEquals(ValidationConstraint.EMAIL_FORMAT, ex.getConstraint());
}
verifyControls();
}
public void testFailCustomMessage()
{
IFormComponent field = newField("My Email");
ValidationMessages messages = newMessages(
"custom",
ValidationStrings.INVALID_EMAIL,
new Object[]
{ "My Email" },
"custom message");
replayControls();
try
{
new Email("message=custom").validate(field, messages, "fred");
unreachable();
}
catch (ValidatorException ex)
{
assertEquals("custom message", ex.getMessage());
assertEquals(ValidationConstraint.EMAIL_FORMAT, ex.getConstraint());
}
verifyControls();
}
public void testRenderContribution()
{
String pattern = new RegexpMatcher().getEscapedPatternString(Email.PATTERN);
IMarkupWriter writer = newWriter();
IRequestCycle cycle = newCycle();
MockControl contextc = newControl(FormComponentContributorContext.class);
FormComponentContributorContext context = (FormComponentContributorContext) contextc
.getMock();
context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
IFormComponent field = newField("Fred");
trainFormatMessage(contextc, context, null, ValidationStrings.INVALID_EMAIL, new Object[]
{ "Fred" }, "default message");
context.getFieldDOM();
contextc.setReturnValue("document.fred.barney");
context
.addSubmitListener("function(event) { validate_regexp(event, document.fred.barney, '"
+ pattern + "', 'default message'); }");
replayControls();
new Email().renderContribution(writer, cycle, context, field);
verifyControls();
}
public void testRenderContributionCustomMessage()
{
String pattern = new RegexpMatcher().getEscapedPatternString(Email.PATTERN);
IMarkupWriter writer = newWriter();
IRequestCycle cycle = newCycle();
MockControl contextc = newControl(FormComponentContributorContext.class);
FormComponentContributorContext context = (FormComponentContributorContext) contextc
.getMock();
context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
IFormComponent field = newField("Fred");
trainFormatMessage(
contextc,
context,
"custom",
ValidationStrings.INVALID_EMAIL,
new Object[]
{ "Fred" },
"custom message");
context.getFieldDOM();
contextc.setReturnValue("document.fred.barney");
context
.addSubmitListener("function(event) { validate_regexp(event, document.fred.barney, '"
+ pattern + "', 'custom message'); }");
replayControls();
new Email("message=custom").renderContribution(writer, cycle, context, field);
verifyControls();
}
}
1.2 +3 -0 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/FormComponentContributorContext.java
Index: FormComponentContributorContext.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/form/FormComponentContributorContext.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FormComponentContributorContext.java 18 Jun 2005 12:54:39 -0000 1.1
+++ FormComponentContributorContext.java 20 Jun 2005 16:32:05 -0000 1.2
@@ -18,6 +18,9 @@
* Object that provides support to objects that implement
* {@link org.apache.tapestry.form.FormComponentContributor}. For the moment, at least, this is all
* about client-side JavaScript generation.
+ * <p>
+ * TODO: Having support for regular expressions might be useful (and would allow a single
+ * {@link RegexpMatcher to be shared).
*
* @author Howard Lewis Ship
* @since 4.0
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/Pattern.java
Index: Pattern.java
===================================================================
// Copyright 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.hivemind.util.PropertyUtils;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.util.RegexpMatcher;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* Validates a user input string against a regular expression pattern.
*
* @author Howard Lewis Ship
* @since 4.0
* @see org.apache.tapestry.util.RegexpMatcher
*/
public class Pattern implements Validator
{
// TODO: Possible thread safety issue if the validator
// is shared across threads, because the matcher
// will be too.
private RegexpMatcher _matcher = new RegexpMatcher();
private String _pattern;
private String _message;
public Pattern()
{
}
public Pattern(String initializer)
{
PropertyUtils.configureProperties(this, initializer);
}
public void validate(IFormComponent field, ValidationMessages messages, Object object)
throws ValidatorException
{
String input = (String) object;
if (!_matcher.matches(_pattern, input))
throw new ValidatorException(buildMessage(messages, field),
ValidationConstraint.PATTERN_MISMATCH);
}
private String buildMessage(ValidationMessages messages, IFormComponent field)
{
return messages.formatValidationMessage(
_message,
ValidationStrings.REGEX_MISMATCH,
new Object[]
{ field.getDisplayName() });
}
public boolean getAcceptsNull()
{
return false;
}
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
FormComponentContributorContext context, IFormComponent field)
{
context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
String pattern = _matcher.getEscapedPatternString(_pattern);
String message = buildMessage(context, field);
StringBuffer buffer = new StringBuffer("function(event) { validate_regexp(event, ");
buffer.append(context.getFieldDOM());
buffer.append(", '");
buffer.append(pattern);
buffer.append("', '");
buffer.append(message);
buffer.append("'); }");
context.addSubmitListener(buffer.toString());
}
public void setMessage(String message)
{
_message = message;
}
public void setPattern(String pattern)
{
_pattern = pattern;
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/Email.java
Index: Email.java
===================================================================
// Copyright 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.hivemind.util.PropertyUtils;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.util.RegexpMatcher;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* Validates that the user input, a string, is an email address (by checking it against a regular
* expression).
*
* @author Howard Lewis Ship
* @since 4.0
*/
public class Email implements Validator
{
static final String PATTERN = "^\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$";
// TODO: Possible thread safety issue if the validator
// is shared across threads, because the matcher
// will be too.
private RegexpMatcher _matcher = new RegexpMatcher();
private String _message;
public Email()
{
}
public Email(String initializer)
{
PropertyUtils.configureProperties(this, initializer);
}
public void validate(IFormComponent field, ValidationMessages messages, Object object)
throws ValidatorException
{
String input = (String) object;
if (!_matcher.matches(PATTERN, input))
throw new ValidatorException(buildMessage(messages, field),
ValidationConstraint.EMAIL_FORMAT);
}
private String buildMessage(ValidationMessages messages, IFormComponent field)
{
return messages.formatValidationMessage(
_message,
ValidationStrings.INVALID_EMAIL,
new Object[]
{ field.getDisplayName() });
}
/** Returns false */
public boolean getAcceptsNull()
{
return false;
}
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
FormComponentContributorContext context, IFormComponent field)
{
context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
String pattern = _matcher.getEscapedPatternString(PATTERN);
String message = buildMessage(context, field);
StringBuffer buffer = new StringBuffer("function(event) { validate_regexp(event, ");
buffer.append(context.getFieldDOM());
buffer.append(", '");
buffer.append(pattern);
buffer.append("', '");
buffer.append(message);
buffer.append("'); }");
context.addSubmitListener(buffer.toString());
}
public void setMessage(String message)
{
_message = message;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org