struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1428264 - in /struts/struts2/trunk/xwork-core/src: main/java/com/opensymphony/xwork2/validator/validators/ test/java/com/opensymphony/xwork2/validator/
Date Thu, 03 Jan 2013 10:22:07 GMT
Author: lukaszlenart
Date: Thu Jan  3 10:22:07 2013
New Revision: 1428264

URL: http://svn.apache.org/viewvc?rev=1428264&view=rev
Log:
WW-3892 adds support to specify expression, caseSensitive and trim params as expressions

Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/EmailValidator.java
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/EmailValidatorTest.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/EmailValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/EmailValidator.java?rev=1428264&r1=1428263&r2=1428264&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/EmailValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/EmailValidator.java
Thu Jan  3 10:22:07 2013
@@ -18,16 +18,17 @@ package com.opensymphony.xwork2.validato
 
 /**
  * <!-- START SNIPPET: javadoc -->
- * EmailValidator checks that a given String field, if not empty,
- * is a valid email address.
- * <p/>
- * <p/>
- * The regular expression used to validate that the string is an email address
- * is:
- * </p>
+ * EmailValidator checks that a given String field, if not empty, is a valid email address.
+ *
+ * The regular expression used to validate that the string is an email address is:
+ *
  * <pre>
  * \\b^['_a-z0-9-\\+]+(\\.['_a-z0-9-\\+]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2}|aero|arpa|asia|biz|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|nato|net|org|pro|tel|travel|xxx)$\\b
  * </pre>
+ *
+ * You can also specify expression, caseSensitive and trim params as a OGNL expression, see
the example below.
+ * WARNING! Do not use ${expression}, ${caseSensitive} and ${trim} as an expression as this
will turn into infinitive loop!
+ *
  * <!-- END SNIPPET: javadoc -->
  * 
  * 
@@ -54,6 +55,16 @@ package com.opensymphony.xwork2.validato
  *           &lt;message&gt;Must provide a valid email&lt;/message&gt;
  *        &lt;/field-validator&gt;
  *     &lt;/field&gt;
+ *
+ *     &lt;!-- Field Validator Syntax with expressions --&gt;
+ *     &lt;field name="myEmail"&gt;
+ *        &lt;field-validator type="email"&gt;
+ *           &lt;param name="expression"&gt;${emailPattern}&lt;/param&gt;
&lt;!-- will be evaluated as: String getEmailPattern() --&gt;
+ *           &lt;param name="caseSensitive"&gt;${emailCaseSensitive}&lt;/param&gt;
&lt;!-- will be evaluated as: boolean getEmailCaseSensitive() --&gt;
+ *           &lt;param name="trim"&gt;${trimEmail}&lt;/param&gt; &lt;!--
will be evaluated as: boolean getTrimEmail() --&gt;
+ *           &lt;message&gt;Must provide a valid email&lt;/message&gt;
+ *        &lt;/field-validator&gt;
+ *     &lt;/field&gt;
  * <!-- END SNIPPET: example -->
  * </pre>
  *
@@ -69,9 +80,7 @@ public class EmailValidator extends Rege
 
     public EmailValidator() {
         setExpression(emailAddressPattern);
-        setCaseSensitive(false);
+        setCaseSensitive("false");
     }
 
 }
-
-

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java?rev=1428264&r1=1428263&r2=1428264&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
Thu Jan  3 10:22:07 2013
@@ -16,6 +16,7 @@
 
 package com.opensymphony.xwork2.validator.validators;
 
+import com.opensymphony.xwork2.util.TextParseUtil;
 import com.opensymphony.xwork2.validator.ValidationException;
 
 import java.util.regex.Matcher;
@@ -116,7 +117,18 @@ public class RegexFieldValidator extends
      * Sets the regular expression to be matched.
      */
     public void setExpression(String expression) {
-        this.expression = expression;
+        if (parse) {
+            this.expression = (String) parse(expression, String.class);
+        } else {
+            this.expression = expression;
+        }
+    }
+
+    protected Object parse(String expression, Class type) {
+        if (expression == null) {
+            return null;
+        }
+        return TextParseUtil.translateVariables('$', expression, stack, type);
     }
 
     /**
@@ -131,8 +143,12 @@ public class RegexFieldValidator extends
      * Sets whether the expression should be matched against in
      * a case-sensitive way.  Default is <code>true</code>.
      */
-    public void setCaseSensitive(boolean caseSensitive) {
-        this.caseSensitive = caseSensitive;
+    public void setCaseSensitive(String caseSensitive) {
+        if (parse) {
+            this.caseSensitive = (Boolean) parse(caseSensitive, Boolean.class);
+        } else {
+            this.caseSensitive = Boolean.parseBoolean(caseSensitive);
+        }
     }
 
     /**
@@ -147,8 +163,12 @@ public class RegexFieldValidator extends
      * Sets whether the expression should be trimed before matching.
      * Default is <code>true</code>.
      */
-    public void setTrim(boolean trim) {
-        this.trim = trim;
+    public void setTrim(String trim) {
+        if (parse) {
+            this.trim = (Boolean) parse(trim, Boolean.class);
+        } else {
+            this.trim = Boolean.parseBoolean(trim);
+        }
     }
 
 }

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/EmailValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/EmailValidatorTest.java?rev=1428264&r1=1428263&r2=1428264&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/EmailValidatorTest.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/EmailValidatorTest.java
Thu Jan  3 10:22:07 2013
@@ -18,56 +18,145 @@ package com.opensymphony.xwork2.validato
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionSupport;
 import com.opensymphony.xwork2.XWorkTestCase;
+import com.opensymphony.xwork2.util.ValueStack;
 import com.opensymphony.xwork2.validator.validators.EmailValidator;
 
 /**
  * Test case for Email Validator
- * 
- * 
+ *
  * @author tm_jee
  * @version $Date$ $Id$
  */
 public class EmailValidatorTest extends XWorkTestCase {
-	
-	public void testEmailValidity() throws Exception {
-		assertTrue(verifyEmailValidity("tmjee@yahoo.com"));
-		assertTrue(verifyEmailValidity("tm_jee@yahoo.co"));
-		assertTrue(verifyEmailValidity("tm.jee@yahoo.co.uk"));
-		assertTrue(verifyEmailValidity("tm.jee@yahoo.co.biz"));
-		assertTrue(verifyEmailValidity("tm_jee@yahoo.com"));
-		assertTrue(verifyEmailValidity("tm_jee@yahoo.net"));
-		assertTrue(verifyEmailValidity(" user@subname1.subname2.subname3.domainname.co.uk "));
+
+    public void testEmailValidity() throws Exception {
+        assertTrue(verifyEmailValidity("tmjee@yahoo.com"));
+        assertTrue(verifyEmailValidityWithExpression("tmjee@yahoo.com", "\\b^[a-z]+@[a-z]+(\\.[a-z]+)*\\.com$\\b"));
+        assertTrue(verifyEmailValidity("tm_jee@yahoo.co"));
+        assertTrue(verifyEmailValidityWithExpression("tm_jee@yahoo.co", "\\b^[a-z_]+@[a-z]+(\\.[a-z]+)*\\.co$\\b"));
+        assertTrue(verifyEmailValidity("tm.jee@yahoo.co.uk"));
+        assertTrue(verifyEmailValidity("tm.jee@yahoo.co.biz"));
+        assertTrue(verifyEmailValidity("tm_jee@yahoo.com"));
+        assertTrue(verifyEmailValidity("tm_jee@yahoo.net"));
+        assertTrue(verifyEmailValidity(" user@subname1.subname2.subname3.domainname.co.uk
"));
         assertTrue(verifyEmailValidity("tm.j'ee@yahoo.co.uk"));
         assertTrue(verifyEmailValidity("tm.j'e.e'@yahoo.co.uk"));
         assertTrue(verifyEmailValidity("tmj'ee@yahoo.com"));
         assertTrue(verifyEmailValidity("ferda+mravenec@yahoo.com"));
         assertTrue(verifyEmailValidity("Ferda+Mravenec@yaHoo.CoM"));
-		
-		assertFalse(verifyEmailValidity("tm_jee#marry@yahoo.co.uk"));
-		assertFalse(verifyEmailValidity("tm_jee@ yahoo.co.uk"));
-		assertFalse(verifyEmailValidity("tm_jee  @yahoo.co.uk"));
-		assertFalse(verifyEmailValidity("tm_j ee  @yah oo.co.uk"));
-		assertFalse(verifyEmailValidity("tm_jee  @yah oo.co.uk"));
-		assertFalse(verifyEmailValidity("tm_jee @ yahoo.com"));
-		assertFalse(verifyEmailValidity(" user@subname1.subname2.subname3.domainn#ame.co.uk "));
-		assertFalse(verifyEmailValidity("aaa@aa.aaaaaaa"));
-		assertFalse(verifyEmailValidity("+ferdamravenec@yahoo.com"));
-	}
-	
-	protected boolean verifyEmailValidity(final String email) throws Exception {
-		ActionSupport action = new ActionSupport() {
-			public String getMyEmail() {
-				return email;
-			}
-		};
-		
-		EmailValidator validator = new EmailValidator();
-		validator.setValidatorContext(new DelegatingValidatorContext(action));
-		validator.setFieldName("myEmail");
-		validator.setDefaultMessage("invalid email");
+
+        assertFalse(verifyEmailValidity("tm_jee#marry@yahoo.co.uk"));
+        assertFalse(verifyEmailValidity("tm_jee@ yahoo.co.uk"));
+        assertFalse(verifyEmailValidity("tm_jee  @yahoo.co.uk"));
+        assertFalse(verifyEmailValidity("tm_j ee  @yah oo.co.uk"));
+        assertFalse(verifyEmailValidity("tm_jee  @yah oo.co.uk"));
+        assertFalse(verifyEmailValidity("tm_jee @ yahoo.com"));
+        assertFalse(verifyEmailValidity(" user@subname1.subname2.subname3.domainn#ame.co.uk
"));
+        assertFalse(verifyEmailValidity("aaa@aa.aaaaaaa"));
+        assertFalse(verifyEmailValidity("+ferdamravenec@yahoo.com"));
+
+        assertFalse(verifyEmailValidityWithExpression("tmjee@yahoo.co", "\\b^[a-z]+@[a-z]+(\\.[a-z]+)*\\.com$\\b"));
+    }
+
+    protected boolean verifyEmailValidity(final String email) throws Exception {
+        ActionSupport action = new ActionSupport() {
+            public String getMyEmail() {
+                return email;
+            }
+        };
+
+        EmailValidator validator = new EmailValidator();
+        validator.setValidatorContext(new DelegatingValidatorContext(action));
+        validator.setFieldName("myEmail");
+        validator.setDefaultMessage("invalid email");
         validator.setValueStack(ActionContext.getContext().getValueStack());
         validator.validate(action);
-		
-		return (action.getFieldErrors().size() == 0);
-	}
+
+        return (action.getFieldErrors().size() == 0);
+    }
+
+    public boolean verifyEmailValidityWithExpression(final String email, final String expression)
throws Exception {
+        ActionSupport action = new ActionSupport() {
+            public String getMyEmail() {
+                return email;
+            }
+
+            public String getEmailExpression() {
+                return expression;
+            }
+        };
+
+        EmailValidator validator = new EmailValidator();
+        ValueStack valueStack = ActionContext.getContext().getValueStack();
+        valueStack.push(action);
+        validator.setValueStack(valueStack);
+
+        validator.setValidatorContext(new DelegatingValidatorContext(action));
+        validator.setFieldName("myEmail");
+        validator.setDefaultMessage("invalid email");
+        validator.setParse(true);
+        validator.setExpression("${emailExpression}");
+
+        validator.validate(action);
+        valueStack.pop();
+
+        return (action.getFieldErrors().size() == 0);
+    }
+
+    public void testCaseSensitiveViaExpression() throws Exception {
+        EmailValidator validator = verifyCaseSensitive(true);
+        assertTrue(validator.isCaseSensitive());
+
+        validator = verifyCaseSensitive(false);
+        assertFalse(validator.isCaseSensitive());
+    }
+
+    private EmailValidator verifyCaseSensitive(final boolean caseSensitive) {
+        ActionSupport action = new ActionSupport() {
+            public boolean getEmailCaseSensitive() {
+                return caseSensitive;
+            }
+        };
+
+        EmailValidator validator = new EmailValidator();
+        ValueStack valueStack = ActionContext.getContext().getValueStack();
+        valueStack.push(action);
+        validator.setValueStack(valueStack);
+
+        validator.setParse(true);
+        validator.setCaseSensitive("${emailCaseSensitive}");
+
+        valueStack.pop();
+
+        return validator;
+    }
+
+    public void testTrimViaExpression() throws Exception {
+        EmailValidator validator = verifyTrim(true);
+        assertTrue(validator.isTrimed());
+
+        validator = verifyTrim(false);
+        assertFalse(validator.isTrimed());
+    }
+
+    private EmailValidator verifyTrim(final boolean trim) {
+        ActionSupport action = new ActionSupport() {
+            public boolean getTrimEmail() {
+                return trim;
+            }
+        };
+
+        EmailValidator validator = new EmailValidator();
+        ValueStack valueStack = ActionContext.getContext().getValueStack();
+        valueStack.push(action);
+        validator.setValueStack(valueStack);
+
+        validator.setParse(true);
+        validator.setTrim("${trimEmail}");
+
+        valueStack.pop();
+
+        return validator;
+    }
+
 }

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java?rev=1428264&r1=1428263&r2=1428264&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java
Thu Jan  3 10:22:07 2013
@@ -60,7 +60,7 @@ public class RegexFieldValidatorTest ext
         ActionContext.getContext().setValueStack(stack);
 
         RegexFieldValidator validator = new RegexFieldValidator();
-        validator.setTrim(false);
+        validator.setTrim("false");
         validator.setExpression("^Sec.*\\s");
         validator.setValidatorContext(new GenericValidatorContext(new Object()));
         validator.setFieldName("username");
@@ -127,7 +127,7 @@ public class RegexFieldValidatorTest ext
     public void testIsTrimmed() throws Exception {
         RegexFieldValidator validator = new RegexFieldValidator();
         assertEquals(true, validator.isTrimed());
-        validator.setTrim(false);
+        validator.setTrim("false");
         assertEquals(false, validator.isTrimed());
     }
 



Mime
View raw message