Author: drobiazko
Date: Sun Nov 22 14:48:11 2009
New Revision: 883085
URL: http://svn.apache.org/viewvc?rev=883085&view=rev
Log:
TAP5-912: Validation of properties of type java.util.Collection should fail
when the collection is empty
Modified:
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/java/org/apache/tapestry5/validator/Required.java
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/test/java/org/apache/tapestry5/validator/RequiredTest.java
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java
Modified:
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/java/org/apache/tapestry5/validator/Required.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/java/org/apache/tapestry5/validator/Required.java?rev=883085&r1=883084&r2=883085&view=diff
==============================================================================
---
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/java/org/apache/tapestry5/validator/Required.java
(original)
+++
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/java/org/apache/tapestry5/validator/Required.java
Sun Nov 22 14:48:11 2009
@@ -34,7 +34,7 @@
public void validate(Field field, Void constraintValue, MessageFormatter
formatter, Object value)
throws ValidationException
{
- if (value == null || InternalUtils.isBlank(value.toString()))
+ if (value == null || InternalUtils.isEmptyCollection(value) ||
InternalUtils.isBlank(value.toString()))
throw new ValidationException(buildMessage(formatter, field));
}
Modified:
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js?rev=883085&r1=883084&r2=883085&view=diff
==============================================================================
---
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
(original)
+++
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
Sun Nov 22 14:48:11 2009
@@ -919,7 +919,7 @@
{
$(field).getFieldEventManager().requiredCheck = function(value)
{
- if (value.strip() == '')
+ if ((Object.isString(value) && value.strip() == '') || value ==
null)
$(field).showValidationMessage(message);
};
},
@@ -1311,7 +1311,7 @@
// Don't try to validate blank values; if the field is required, that
error is already
// noted and presented to the user.
- if (!t.validationError && ! value.blank())
+ if (!t.validationError && ! (Object.isString(value) && value.blank()))
{
var translated = this.translator(value);
Modified:
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/test/java/org/apache/tapestry5/validator/RequiredTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/test/java/org/apache/tapestry5/validator/RequiredTest.java?rev=883085&r1=883084&r2=883085&view=diff
==============================================================================
---
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/test/java/org/apache/tapestry5/validator/RequiredTest.java
(original)
+++
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-core/src/test/java/org/apache/tapestry5/validator/RequiredTest.java
Sun Nov 22 14:48:11 2009
@@ -14,6 +14,8 @@
package org.apache.tapestry5.validator;
+import java.util.Arrays;
+
import org.apache.tapestry5.Field;
import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.ioc.MessageFormatter;
@@ -67,6 +69,43 @@
verify();
}
+
+ @Test
+ public void empty_collection_value()
+ {
+ MessageFormatter formatter = mockMessageFormatter();
+ Field field = mockFieldWithLabel("My Field");
+
+ train_format(formatter, "{message}", "My Field");
+
+ replay();
+
+ try
+ {
+ new Required().validate(field, null, formatter, Arrays.asList());
+ unreachable();
+ }
+ catch (ValidationException ex)
+ {
+ assertEquals(ex.getMessage(), "{message}");
+ }
+
+ verify();
+ }
+
+ @Test
+ public void not_empty_collection_value() throws Exception
+ {
+ MessageFormatter formatter = mockMessageFormatter();
+ Field field = mockField();
+
+ replay();
+
+ new Required().validate(field, null, formatter, Arrays.asList("A",
"B"));
+
+ verify();
+ }
+
@Test
public void non_blank_value() throws Exception
Modified:
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java?rev=883085&r1=883084&r2=883085&view=diff
==============================================================================
---
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java
(original)
+++
tapestry/tapestry5/branches/5.1.0.x-dev/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java
Sun Nov 22 14:48:11 2009
@@ -477,6 +477,20 @@
{
return input == null || input.length() == 0 || input.trim().length()
== 0;
}
+
+ /**
+ * Returns true if the input is an empty collection.
+ */
+
+ public static boolean isEmptyCollection(Object input)
+ {
+ if(input instanceof Collection)
+ {
+ return ((Collection)input).isEmpty();
+ }
+
+ return false;
+ }
public static boolean isNonBlank(String input)
{