struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [03/13] struts git commit: WW-4578 Makes required validator to support collections
Date Wed, 19 Apr 2017 07:42:17 GMT
WW-4578 Makes required validator to support collections


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/e66fd538
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/e66fd538
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/e66fd538

Branch: refs/heads/master
Commit: e66fd538b519b1a156d76e4bbe4e994a35e3c464
Parents: 158eda9
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Mon Apr 10 09:13:14 2017 +0200
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Mon Apr 10 09:13:14 2017 +0200

----------------------------------------------------------------------
 .../validators/RequiredFieldValidator.java      |  7 ++
 .../validators/RequiredFieldValidatorTest.java  | 78 ++++++++++++++++++++
 .../validator/validators/ValidationAction.java  | 10 +++
 3 files changed, 95 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/e66fd538/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredFieldValidator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredFieldValidator.java
b/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredFieldValidator.java
index dffd37a..44fd92f 100644
--- a/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredFieldValidator.java
+++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredFieldValidator.java
@@ -17,6 +17,9 @@ package com.opensymphony.xwork2.validator.validators;
 
 import com.opensymphony.xwork2.validator.ValidationException;
 
+import java.lang.reflect.Array;
+import java.util.Collection;
+
 
 /**
  * <!-- START SNIPPET: javadoc -->
@@ -63,6 +66,10 @@ public class RequiredFieldValidator extends FieldValidatorSupport {
 
         if (value == null) {
             addFieldError(fieldName, object);
+        } else if (value.getClass().isArray() && Array.getLength(value) == 0) {
+            addFieldError(fieldName, object);
+        } else if (Collection.class.isAssignableFrom(value.getClass()) && ((Collection)
value).size() == 0) {
+            addFieldError(fieldName, object);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/struts/blob/e66fd538/core/src/test/java/com/opensymphony/xwork2/validator/validators/RequiredFieldValidatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/validators/RequiredFieldValidatorTest.java
b/core/src/test/java/com/opensymphony/xwork2/validator/validators/RequiredFieldValidatorTest.java
new file mode 100644
index 0000000..4fc02e1
--- /dev/null
+++ b/core/src/test/java/com/opensymphony/xwork2/validator/validators/RequiredFieldValidatorTest.java
@@ -0,0 +1,78 @@
+package com.opensymphony.xwork2.validator.validators;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.TextProviderFactory;
+import com.opensymphony.xwork2.validator.DummyValidatorContext;
+import org.apache.struts2.StrutsInternalTestCase;
+import org.junit.Test;
+
+import java.util.ArrayList;
+
+public class RequiredFieldValidatorTest extends StrutsInternalTestCase {
+
+    @Test
+    public void testNullObject() throws Exception {
+        // given
+        RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class);
+        rfv.setValueStack(ActionContext.getContext().getValueStack());
+        rfv.setFieldName("stringValue");
+        rfv.setDefaultMessage("${fieldName} field is required!");
+        ValidationAction action = new ValidationAction();
+        DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class));
+        rfv.setValidatorContext(context);
+
+        // when
+        rfv.validate(action);
+
+        // then
+        assertTrue(context.hasFieldErrors());
+        assertEquals(1, context.getFieldErrors().size());
+        assertNotNull(context.getFieldErrors().get("stringValue"));
+        assertEquals("stringValue field is required!", context.getFieldErrors().get("stringValue").get(0));
+    }
+
+    @Test
+    public void testArrayObject() throws Exception {
+        // given
+        RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class);
+        rfv.setValueStack(ActionContext.getContext().getValueStack());
+        rfv.setFieldName("ints");
+        rfv.setDefaultMessage("${fieldName} field is required!");
+        ValidationAction action = new ValidationAction();
+        action.setInts(new Integer[]{});
+        DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class));
+        rfv.setValidatorContext(context);
+
+        // when
+        rfv.validate(action);
+
+        // then
+        assertTrue(context.hasFieldErrors());
+        assertEquals(1, context.getFieldErrors().size());
+        assertNotNull(context.getFieldErrors().get("ints"));
+        assertEquals("ints field is required!", context.getFieldErrors().get("ints").get(0));
+    }
+    
+    @Test
+    public void testCollectionObject() throws Exception {
+        // given
+        RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class);
+        rfv.setValueStack(ActionContext.getContext().getValueStack());
+        rfv.setFieldName("shorts");
+        rfv.setDefaultMessage("${fieldName} field is required!");
+        ValidationAction action = new ValidationAction();
+        action.setShorts(new ArrayList<Short>());
+        DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class));
+        rfv.setValidatorContext(context);
+
+        // when
+        rfv.validate(action);
+
+        // then
+        assertTrue(context.hasFieldErrors());
+        assertEquals(1, context.getFieldErrors().size());
+        assertNotNull(context.getFieldErrors().get("shorts"));
+        assertEquals("shorts field is required!", context.getFieldErrors().get("shorts").get(0));
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/struts/blob/e66fd538/core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
b/core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
index a9fc40f..a97cea6 100644
--- a/core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
+++ b/core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
@@ -1,6 +1,7 @@
 package com.opensymphony.xwork2.validator.validators;
 
 import java.util.Date;
+import java.util.List;
 
 public class ValidationAction {
 
@@ -12,6 +13,7 @@ public class ValidationAction {
     private Short shortRange;
     private Short shortMinValue;
     private Short shortMaxValue;
+    private List<Short> shorts;
 
     private Long longRange;
     private Long longMinValue;
@@ -142,4 +144,12 @@ public class ValidationAction {
     public void setInts(Integer[] ints) {
         this.ints = ints;
     }
+
+    public List<Short> getShorts() {
+        return shorts;
+    }
+
+    public void setShorts(List<Short> shorts) {
+        this.shorts = shorts;
+    }
 }


Mime
View raw message