struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1432603 - in /struts/struts2/trunk/xwork-core/src: main/java/com/opensymphony/xwork2/validator/validators/ test/java/com/opensymphony/xwork2/validator/ test/java/com/opensymphony/xwork2/validator/validators/
Date Sun, 13 Jan 2013 13:05:21 GMT
Author: lukaszlenart
Date: Sun Jan 13 13:05:21 2013
New Revision: 1432603

URL: http://svn.apache.org/viewvc?rev=1432603&view=rev
Log:
WW-3960 adds support to specify param as an expression

Added:
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidatorTest.java
Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringValidatorTest.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java?rev=1432603&r1=1432602&r2=1432603&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java
Sun Jan 13 13:05:21 2013
@@ -29,11 +29,15 @@ import com.opensymphony.xwork2.validator
  * <!-- START SNIPPET: parameters -->
  * <ul>
  * 		<li>fieldName - The field name this validator is validating. Required if using
Plain-Validator Syntax otherwise not required</li>
- *      <li>trim - trim the field name value before validating (default is true)</li>
+ *      <li>trim - (Optional) Boolean, default true. Trims the field name value before
validating.</li>
+ *      <li>trimExpression - (Optional) String. Specifies the trim param as an OGNL
expression.</li>
  * </ul>
  * <!-- END SNIPPET: parameters -->
  * 
- * 
+ * <!-- START SNIPPET: parameters-warning -->
+ * Do not use ${trimExpression} as an expression as this will turn into infinitive loop!
+ * <!-- END SNIPPET: parameters-warning -->
+ *
  * <pre>
  * <!-- START SNIPPET: examples -->
  *     &lt;validators&gt;
@@ -51,6 +55,14 @@ import com.opensymphony.xwork2.validator
  *                 &lt;message&gt;username is required&lt;/message&gt;
  *            &lt;/field-validator&gt;
  *         &lt;/field&gt;
+ *
+ *         &lt;!-- Field-Validator Syntax with expression --&gt;
+ *         &lt;field name="username"&gt;
+ *         	  &lt;field-validator type="requiredstring"&gt;
+ *                 &lt;param name="trimExpression"&gt;${trimValue}&lt;/param&gt;
&lt;!-- will be evaluated as: boolean getTrimValue() --&gt;
+ *                 &lt;message&gt;username is required&lt;/message&gt;
+ *            &lt;/field-validator&gt;
+ *         &lt;/field&gt;
  *     &lt;/validators&gt;
  * <!-- END SNIPPET: examples -->
  * </pre>
@@ -60,15 +72,18 @@ import com.opensymphony.xwork2.validator
  */
 public class RequiredStringValidator extends FieldValidatorSupport {
 
-    private boolean doTrim = true;
-
+    private boolean trim = true;
 
     public void setTrim(boolean trim) {
-        doTrim = trim;
+        this.trim = trim;
     }
 
-    public boolean getTrim() {
-        return doTrim;
+    public void setTrimExpression(String trimExpression) {
+        trim = (Boolean) parse(trimExpression, Boolean.class);
+    }
+
+    public boolean isTrim() {
+        return trim;
     }
 
     public void validate(Object object) throws ValidationException {
@@ -80,7 +95,7 @@ public class RequiredStringValidator ext
         } else {
             String s = (String) value;
 
-            if (doTrim) {
+            if (trim) {
                 s = s.trim();
             }
 
@@ -89,4 +104,5 @@ public class RequiredStringValidator ext
             }
         }
     }
+
 }

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringValidatorTest.java?rev=1432603&r1=1432602&r2=1432603&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringValidatorTest.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringValidatorTest.java
Sun Jan 13 13:05:21 2013
@@ -15,17 +15,21 @@
  */
 package com.opensymphony.xwork2.validator;
 
-import com.opensymphony.xwork2.*;
-import com.opensymphony.xwork2.config.providers.MockConfigurationProvider;
+import com.opensymphony.xwork2.Action;
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.ActionProxy;
+import com.opensymphony.xwork2.ValidationAwareSupport;
+import com.opensymphony.xwork2.XWorkTestCase;
 import com.opensymphony.xwork2.config.entities.ActionConfig;
+import com.opensymphony.xwork2.config.providers.MockConfigurationProvider;
 import com.opensymphony.xwork2.test.Equidae;
 import com.opensymphony.xwork2.validator.validators.RequiredStringValidator;
+import org.easymock.EasyMock;
 
 import java.util.List;
 import java.util.Map;
 
-import org.easymock.EasyMock;
-
 /**
  * @author Mark Woon
  * @author tm_jee (tm_jee (at) yahoo.co.uk )
@@ -183,10 +187,10 @@ public class StringValidatorTest extends
         RequiredStringValidator val = new RequiredStringValidator();
 
         val.setTrim(true);
-        assertEquals(true, val.getTrim());
+        assertEquals(true, val.isTrim());
 
         val.setTrim(false);
-        assertEquals(false, val.getTrim());
+        assertEquals(false, val.isTrim());
     }
 
     @Override

Added: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidatorTest.java?rev=1432603&view=auto
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidatorTest.java
(added)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidatorTest.java
Sun Jan 13 13:05:21 2013
@@ -0,0 +1,79 @@
+package com.opensymphony.xwork2.validator.validators;
+
+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.GenericValidatorContext;
+import com.opensymphony.xwork2.validator.ValidatorContext;
+
+public class RequiredStringValidatorTest extends XWorkTestCase {
+
+    public void testRequiredStringPass() throws Exception {
+        // given
+        ValueStack valueStack = ActionContext.getContext().getValueStack();
+
+        ValidationAction action = new ValidationAction();
+        action.setStringValue("a string");
+        valueStack.push(action);
+
+        ValidatorContext context = new GenericValidatorContext(action);
+        RequiredStringValidator validator = new RequiredStringValidator();
+        validator.setValidatorContext(context);
+        validator.setFieldName("stringValue");
+        validator.setValueStack(valueStack);
+
+        // when
+        validator.validate(action);
+
+        // then
+        assertTrue(context.getFieldErrors().size() == 0);
+    }
+
+    public void testRequiredStringFails() throws Exception {
+        // given
+        ValueStack valueStack = ActionContext.getContext().getValueStack();
+
+        ValidationAction action = new ValidationAction();
+        valueStack.push(action);
+
+        ValidatorContext context = new GenericValidatorContext(action);
+        RequiredStringValidator validator = new RequiredStringValidator();
+        validator.setValidatorContext(context);
+        validator.setFieldName("stringValue");
+        validator.setValueStack(valueStack);
+        validator.setDefaultMessage("Field ${fieldName} is required");
+
+        // when
+        validator.validate(action);
+
+        // then
+        assertTrue(context.getFieldErrors().size() == 1);
+        assertEquals(context.getFieldErrors().get("stringValue").get(0), "Field stringValue
is required");
+    }
+
+
+    public void testTrimAsExpression() throws Exception {
+        // given
+        ValueStack valueStack = ActionContext.getContext().getValueStack();
+
+        ActionSupport action = new ActionSupport() {
+            public boolean getTrimValue() {
+                return false;
+            }
+        };
+        valueStack.push(action);
+
+        RequiredStringValidator validator = new RequiredStringValidator();
+        validator.setValueStack(valueStack);
+
+        assertTrue(validator.isTrim());
+
+        // when
+        validator.setTrimExpression("${trimValue}");
+
+        // then
+        assertFalse(validator.isTrim());
+    }
+
+}

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java?rev=1432603&r1=1432602&r2=1432603&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
Sun Jan 13 13:05:21 2013
@@ -20,6 +20,7 @@ public class ValidationAction {
     private Date dateMinValue;
     private Date dateMaxValue;
     private String dateFormat;
+    private String stringValue;
 
     public Integer getIntRange() {
         return intRange;
@@ -124,4 +125,12 @@ public class ValidationAction {
     public void setDateFormat(String dateFormat) {
         this.dateFormat = dateFormat;
     }
+
+    public void setStringValue(String stringValue) {
+        this.stringValue = stringValue;
+    }
+
+    public String getStringValue() {
+        return stringValue;
+    }
 }



Mime
View raw message