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 2008/08/06 01:29:52 UTC

svn commit: r683033 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/services/ main/java/org/apache/tapestry5/validator/ main/resources/org/apache/tapestry5/internal/ test/java/org/apache/tapestry5/validator/

Author: hlship
Date: Tue Aug  5 16:29:51 2008
New Revision: 683033

URL: http://svn.apache.org/viewvc?rev=683033&view=rev
Log:
TAPESTRY-2143: Framework should include an email validator

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/AbstractValidator.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/ValidationMessages.properties

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=683033&r1=683032&r2=683033&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java Tue Aug  5 16:29:51 2008
@@ -466,6 +466,7 @@
         configuration.add("min", new Min());
         configuration.add("max", new Max());
         configuration.add("regexp", new Regexp());
+        configuration.add("email", new Email());
     }
 
     /**

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/AbstractValidator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/AbstractValidator.java?rev=683033&r1=683032&r2=683033&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/AbstractValidator.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/AbstractValidator.java Tue Aug  5 16:29:51 2008
@@ -44,11 +44,15 @@
         return valueType;
     }
 
-    public String getMessageKey()
+    public final String getMessageKey()
     {
         return messageKey;
     }
 
+    /**
+     * Return false, which is correct for the vast majority of validators. {@link org.apache.tapestry5.validator.Required}
+     * overrides this to true.F
+     */
     public boolean isRequired()
     {
         return false;

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java?rev=683033&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java Tue Aug  5 16:29:51 2008
@@ -0,0 +1,60 @@
+// Copyright 2008 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.tapestry5.validator;
+
+import org.apache.tapestry5.Field;
+import org.apache.tapestry5.MarkupWriter;
+import org.apache.tapestry5.ValidationException;
+import org.apache.tapestry5.ioc.MessageFormatter;
+import org.apache.tapestry5.services.FormSupport;
+
+import java.util.regex.Pattern;
+
+/**
+ * A validator that checks if a given string is well-formed email address. This validator is not configurable.
+ */
+public class Email extends AbstractValidator<Void, String>
+{
+    private static final String ATOM = "[^\\x00-\\x1F^\\(^\\)^\\<^\\>^\\@^\\,^\\;^\\:^\\\\^\\\"^\\.^\\[^\\]^\\s]";
+
+    private static final String DOMAIN = "(" + ATOM + "+(\\." + ATOM + "+)*";
+
+    private static final String IP_DOMAIN = "\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\]";
+
+    private static final Pattern PATTERN = Pattern
+            .compile("^" + ATOM + "+(\\." + ATOM + "+)*@" + DOMAIN + "|" + IP_DOMAIN + ")$", Pattern.CASE_INSENSITIVE);
+
+    public Email()
+    {
+        super(Void.class, String.class, "invalid-email");
+    }
+
+    public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter markupWriter,
+                       FormSupport formSupport)
+    {
+        formSupport.addValidation(field, "email", buildMessage(formatter, field), null);
+    }
+
+    private String buildMessage(MessageFormatter formatter, Field field)
+    {
+        return formatter.format(field.getLabel());
+    }
+
+    public void validate(Field field, Void constraintValue, MessageFormatter formatter, String value)
+            throws ValidationException
+    {
+        if (!PATTERN.matcher(value).matches()) throw new ValidationException(buildMessage(formatter, field));
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/ValidationMessages.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/ValidationMessages.properties?rev=683033&r1=683032&r2=683033&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/ValidationMessages.properties (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/ValidationMessages.properties Tue Aug  5 16:29:51 2008
@@ -33,3 +33,5 @@
 # The label/alt text for the icon that is displayed next to each field.
 
 icon-label=[Error]
+
+invalid-email='%2$s' is not a valid email address.
\ No newline at end of file

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java?rev=683033&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java Tue Aug  5 16:29:51 2008
@@ -0,0 +1,85 @@
+// Copyright 2008 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.tapestry5.validator;
+
+import org.apache.tapestry5.Field;
+import org.apache.tapestry5.ValidationException;
+import org.apache.tapestry5.internal.test.InternalBaseTestCase;
+import org.apache.tapestry5.ioc.MessageFormatter;
+import org.testng.annotations.Test;
+
+public class EmailTest extends InternalBaseTestCase
+{
+    @Test
+    public void matching_pattern() throws Exception
+    {
+        Field field = mockField();
+        MessageFormatter formatter = mockMessageFormatter();
+
+        replay();
+
+        Email validator = new Email();
+
+        validator.validate(field, null, formatter, "myemail@mail.com");
+
+        verify();
+    }
+
+    @Test
+    public void input_mismatch() throws Exception
+    {
+        String label = "My Field";
+        Field field = mockFieldWithLabel(label);
+        MessageFormatter formatter = mockMessageFormatter();
+        String message = "{message}";
+
+        train_format(formatter, message, label);
+
+        replay();
+
+        Email validator = new Email();
+
+        try
+        {
+            validator.validate(field, null, formatter, "invalid_email");
+            unreachable();
+        }
+        catch (ValidationException ex)
+        {
+            assertEquals(ex.getMessage(), message);
+
+            verify();
+        }
+
+        field = mockFieldWithLabel(label);
+
+        train_format(formatter, message, label);
+
+        replay();
+
+        try
+        {
+            validator.validate(field, null, formatter, "@mail.com");
+            unreachable();
+        }
+        catch (ValidationException ex)
+        {
+            assertEquals(ex.getMessage(), message);
+
+            verify();
+        }
+
+    }
+}