struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [06/13] struts git commit: WW-4578 Converts length string validator to support collections
Date Wed, 19 Apr 2017 07:42:20 GMT
WW-4578 Converts length string 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/16e8f10f
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/16e8f10f
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/16e8f10f

Branch: refs/heads/master
Commit: 16e8f10f6664f4d5abbc64650a0b7a93cd49290b
Parents: df5583f
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Tue Apr 11 09:10:19 2017 +0200
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Tue Apr 11 09:10:19 2017 +0200

----------------------------------------------------------------------
 .../validators/StringLengthFieldValidator.java  |  45 +++-
 .../StringLengthFieldValidatorTest.java         | 270 +++++++++++--------
 2 files changed, 192 insertions(+), 123 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/16e8f10f/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
b/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
index 9d28a55..0c4f0fc 100644
--- a/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
+++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
@@ -17,6 +17,11 @@ package com.opensymphony.xwork2.validator.validators;
 
 import com.opensymphony.xwork2.validator.ValidationException;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.Collection;
+import java.util.Objects;
 
 /**
  * <!-- START SNIPPET: javadoc -->
@@ -84,6 +89,8 @@ import org.apache.commons.lang3.StringUtils;
  */
 public class StringLengthFieldValidator extends FieldValidatorSupport {
 
+    private static final Logger LOG = LogManager.getLogger(StringLengthFieldValidator.class);
+
     private boolean trim = true;
     private int maxLength = -1;
     private int minLength = -1;
@@ -138,17 +145,37 @@ public class StringLengthFieldValidator extends FieldValidatorSupport
{
     }
 
     public void validate(Object object) throws ValidationException {
-        String fieldName = getFieldName();
-        String val = (String) getFieldValue(fieldName, object);
+        Object fieldValue = getFieldValue(fieldName, object);
+
+        if (fieldValue == null) {
+            LOG.debug("Value for field {} is null, use a required validator", getFieldName());
+        } else if (fieldValue.getClass().isArray()) {
+            Object[] values = (Object[]) fieldValue;
+            for (Object value : values) {
+                validateValue(object, value);
+            }
+        } else if (Collection.class.isAssignableFrom(fieldValue.getClass())) {
+            Collection values = (Collection) fieldValue;
+            for (Object value : values) {
+                validateValue(object, value);
+            }
+        } else {
+            validateValue(object, fieldValue);
+        }
+    }
 
-        if (StringUtils.isEmpty(val)) {
-            // use a required validator for these
+    protected void validateValue(Object object, Object value) {
+        String stringValue = Objects.toString(value, "");
+
+        if (StringUtils.isEmpty(stringValue)) {
+            LOG.debug("Value is empty, use a required validator");
             return;
         }
+
         if (isTrim()) {
-            val = val.trim();
-            if (val.length() <= 0) {
-                // use a required validator
+            stringValue = stringValue.trim();
+            if (StringUtils.isEmpty(stringValue)) {
+                LOG.debug("Value is empty, use a required validator");
                 return;
             }
         }
@@ -156,9 +183,9 @@ public class StringLengthFieldValidator extends FieldValidatorSupport
{
         int minLengthToUse = getMinLength();
         int maxLengthToUse = getMaxLength();
 
-        if ((minLengthToUse > -1) && (val.length() < minLengthToUse)) {
+        if ((minLengthToUse > -1) && (stringValue.length() < minLengthToUse))
{
             addFieldError(fieldName, object);
-        } else if ((maxLengthToUse > -1) && (val.length() > maxLengthToUse))
{
+        } else if ((maxLengthToUse > -1) && (stringValue.length() > maxLengthToUse))
{
             addFieldError(fieldName, object);
         }
     }

http://git-wip-us.apache.org/repos/asf/struts/blob/16e8f10f/core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java
b/core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java
index ee6daf7..26e3335 100644
--- a/core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java
@@ -22,106 +22,130 @@ import com.opensymphony.xwork2.XWorkTestCase;
 import com.opensymphony.xwork2.util.ValueStack;
 import com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator;
 
+import java.util.Arrays;
+import java.util.Collection;
+
 /**
- * 
  * @author tm_jee
  * @version $Date$ $Id$
  */
 public class StringLengthFieldValidatorTest extends XWorkTestCase {
 
-	protected InternalActionSupport action;
-	protected StringLengthFieldValidator validator;
-	
-	public void testStringLengthEmptyNoTrim1() throws Exception {
-		action.setMyField("");
-		
-		validator.setTrim(false);
-		validator.validate(action);
-		
-		assertEquals(action.getMyField(), "");
-		assertFalse(action.hasFieldErrors());
-	}
-	
-	public void testStringLengthNullNoTrim() throws Exception {
-		action.setMyField(null);
-
-		validator.setTrim(false);
-		validator.validate(action);
-		
-		assertEquals(action.getMyField(), null);
-		assertFalse(action.hasFieldErrors());
-	}
-	
-	public void testStringLengthEmptyTrim1() throws Exception {
-		action.setMyField("   ");
-		
-		validator.setTrim(true);
-		validator.validate(action);
-		
-		assertEquals(action.getMyField(), "   ");
-		assertFalse(action.hasFieldErrors());
-	}
-	
-	public void testStringLengthEmptyNoTrim2() throws Exception {
-		action.setMyField("          ");
-		
-		validator.setTrim(false);
-		validator.validate(action);
-		
-		assertEquals(action.getMyField(), "          ");
-		assertTrue(action.hasFieldErrors());
-	}
-	
-	
-	public void testStringLengthNullTrim() throws Exception {
-		action.setMyField(null);
-
-		validator.setTrim(true);
-		validator.validate(action);
-		
-		assertEquals(action.getMyField(), null);
-		assertFalse(action.hasFieldErrors());
-	}
-	
-	public void testInvalidStringLengthNoTrim() throws Exception {
-		action.setMyField("abcdefghijklmn");
-		
-		validator.setTrim(false);
-		validator.validate(action);
-		
-		assertEquals(action.getMyField(), "abcdefghijklmn");
-		assertTrue(action.hasFieldErrors());
-	}
-	
-	public void testInvalidStringLengthTrim() throws Exception {
-		action.setMyField("abcdefghijklmn   ");
-		
-		validator.setTrim(true);
-		validator.validate(action);
-		
-		assertEquals(action.getMyField(), "abcdefghijklmn   ");
-		assertTrue(action.hasFieldErrors());
-	}
-	
-	public void testValidStringLengthNoTrim() throws Exception {
-		action.setMyField("   ");
-		
-		validator.setTrim(false);
-		validator.validate(action);
-		
-		assertEquals(action.getMyField(), "   ");
-		assertFalse(action.hasFieldErrors());
-	}
-	
-	public void testValidStringLengthTrim() throws Exception {
-		action.setMyField("asd          ");
-		
-		validator.setTrim(true);
-		validator.validate(action);
-		
-		assertEquals(action.getMyField(), "asd          ");
-		assertFalse(action.hasFieldErrors());
-	}
+    protected InternalActionSupport action;
+    protected StringLengthFieldValidator validator;
+
+    public void testStringLengthEmptyNoTrim1() throws Exception {
+        action.setMyField("");
+
+        validator.setTrim(false);
+        validator.validate(action);
+
+        assertEquals(action.getMyField(), "");
+        assertFalse(action.hasFieldErrors());
+    }
+
+    public void testStringLengthNullNoTrim() throws Exception {
+        action.setMyField(null);
+
+        validator.setTrim(false);
+        validator.validate(action);
+
+        assertEquals(action.getMyField(), null);
+        assertFalse(action.hasFieldErrors());
+    }
+
+    public void testStringLengthEmptyTrim1() throws Exception {
+        action.setMyField("   ");
+
+        validator.setTrim(true);
+        validator.validate(action);
+
+        assertEquals(action.getMyField(), "   ");
+        assertFalse(action.hasFieldErrors());
+    }
+
+    public void testStringLengthEmptyNoTrim2() throws Exception {
+        action.setMyField("          ");
+
+        validator.setTrim(false);
+        validator.validate(action);
+
+        assertEquals(action.getMyField(), "          ");
+        assertTrue(action.hasFieldErrors());
+    }
+
+
+    public void testStringLengthNullTrim() throws Exception {
+        action.setMyField(null);
+
+        validator.setTrim(true);
+        validator.validate(action);
+
+        assertEquals(action.getMyField(), null);
+        assertFalse(action.hasFieldErrors());
+    }
+
+    public void testInvalidStringLengthNoTrim() throws Exception {
+        action.setMyField("abcdefghijklmn");
+
+        validator.setTrim(false);
+        validator.validate(action);
+
+        assertEquals(action.getMyField(), "abcdefghijklmn");
+        assertTrue(action.hasFieldErrors());
+    }
+
+    public void testInvalidStringLengthTrim() throws Exception {
+        action.setMyField("abcdefghijklmn   ");
+
+        validator.setTrim(true);
+        validator.validate(action);
+
+        assertEquals(action.getMyField(), "abcdefghijklmn   ");
+        assertTrue(action.hasFieldErrors());
+    }
+
+    public void testValidStringLengthNoTrim() throws Exception {
+        action.setMyField("   ");
+
+        validator.setTrim(false);
+        validator.validate(action);
+
+        assertEquals(action.getMyField(), "   ");
+        assertFalse(action.hasFieldErrors());
+    }
+
+    public void testValidStringLengthTrim() throws Exception {
+        action.setMyField("asd          ");
+
+        validator.setTrim(true);
+        validator.validate(action);
+
+        assertEquals(action.getMyField(), "asd          ");
+        assertFalse(action.hasFieldErrors());
+    }
+
+    public void testArrayOfStringsLengthTrim() throws Exception {
+        action.setStrings(new String[]{"123456", "    ", null});
+
+        validator.setFieldName("strings");
+        validator.setTrim(true);
+        validator.validate(action);
+
+        assertTrue(action.hasFieldErrors());
+        assertEquals(1, action.getFieldErrors().get("strings").size());
+    }
+
+    public void testCollectionOfStringsLengthTrim() throws Exception {
+        action.setStringCollection(Arrays.asList("123456", "    ", null));
+
+        validator.setFieldName("stringCollection");
+        validator.setTrim(true);
+        validator.validate(action);
+
+        assertTrue(action.hasFieldErrors());
+        assertEquals(1, action.getFieldErrors().get("stringCollection").size());
+    }
 
     public void testMinLengthViaExpression() throws Exception {
         assertEquals(2, validator.getMinLength());
@@ -151,41 +175,44 @@ public class StringLengthFieldValidatorTest extends XWorkTestCase {
     }
 
     @Override
-	protected void setUp() throws Exception {
-		super.setUp();
+    protected void setUp() throws Exception {
+        super.setUp();
 
         action = new InternalActionSupport();
-		container.inject(action);
+        container.inject(action);
 
         ValueStack valueStack = ActionContext.getContext().getValueStack();
         valueStack.push(action);
 
-		validator = new StringLengthFieldValidator();
-		validator.setFieldName("myField");
-		validator.setMessageKey("error");
-		validator.setValidatorContext(new DelegatingValidatorContext(action, container.getInstance(TextProviderFactory.class)));
-		validator.setMaxLength(5);
-		validator.setMinLength(2);
+        validator = new StringLengthFieldValidator();
+        validator.setFieldName("myField");
+        validator.setMessageKey("error");
+        validator.setValidatorContext(new DelegatingValidatorContext(action, container.getInstance(TextProviderFactory.class)));
+        validator.setMaxLength(5);
+        validator.setMinLength(2);
         validator.setValueStack(valueStack);
     }
-	
-	
-	@Override
-	protected void tearDown() throws Exception {
-		super.tearDown();
-		action = null;
-		validator = null;
-	}
-	
-	public static class InternalActionSupport extends ActionSupport {
 
-		private static final long serialVersionUID = 1L;
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        action = null;
+        validator = null;
+    }
+
+    public static class InternalActionSupport extends ActionSupport {
+
+        private static final long serialVersionUID = 1L;
 
         private String myField;
         private boolean trimValue;
         private int minLengthValue;
         private int maxLengthValue;
 
+        private String[] strings;
+        private Collection<String> stringCollection;
+
         public String getMyField() {
             return this.myField;
         }
@@ -218,6 +245,21 @@ public class StringLengthFieldValidatorTest extends XWorkTestCase {
             this.maxLengthValue = maxLengthValue;
         }
 
+        public String[] getStrings() {
+            return strings;
+        }
+
+        public void setStrings(String[] strings) {
+            this.strings = strings;
+        }
+
+        public Collection<String> getStringCollection() {
+            return stringCollection;
+        }
+
+        public void setStringCollection(Collection<String> stringCollection) {
+            this.stringCollection = stringCollection;
+        }
     }
 
 }


Mime
View raw message