- Revision
- 669
- Author
- mauro
- Date
- 2008-05-07 06:22:21 -0500 (Wed, 07 May 2008)
Log Message
Refactored ListValueConverter to return an empty list on missing values. The blank CSV values are also discarted. Modified freemarker-example to show how to allow de-selection of list properties.
Modified Paths
- trunk/examples/freemarker-example/src/main/webapp/people/edit.ftl
- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java
- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java
- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java
Diff
Modified: trunk/examples/freemarker-example/src/main/webapp/people/edit.ftl (668 => 669)
--- trunk/examples/freemarker-example/src/main/webapp/people/edit.ftl 2008-05-07 09:52:10 UTC (rev 668) +++ trunk/examples/freemarker-example/src/main/webapp/people/edit.ftl 2008-05-07 11:22:21 UTC (rev 669) @@ -51,6 +51,7 @@ </p> <p class="fieldRow"> <label for="" + <@w.hidden "person.skills" ""/> <@w.selectMultiple "person.skills" controller.getSkills() person.getSkills() "size='5'"/> </p> <p class="fieldRow">
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java (668 => 669)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java 2008-05-07 09:52:10 UTC (rev 668) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java 2008-05-07 11:22:21 UTC (rev 669) @@ -10,8 +10,6 @@ *****************************************************************************/ package org.codehaus.waffle.bind.converters; -import static java.util.Arrays.asList; - import java.text.NumberFormat; import java.text.ParseException; import java.util.ArrayList; @@ -23,7 +21,7 @@ /** * <p> * <code>ValueConverter</code> that converts a CSV value to a List. A <code>null</code> or empty value (once - * trimmed) will be returned as <code>null</code> (behaviour which can be overridden via the + * trimmed) will be returned as an empty list (behaviour which can be overridden via the * [EMAIL PROTECTED] convertMissingValue()} method). The message keys and default values used are: * <ul> * <li>"bind.error.list" ([EMAIL PROTECTED] #BIND_ERROR_LIST_KEY}): list is <code>null</code> or empty (message defaults to @@ -77,7 +75,7 @@ return (T) convertMissingValue(BIND_ERROR_LIST_KEY, DEFAULT_LIST_MESSAGE, fieldName); } - List<String> values = asList(value.split(COMMA)); + List<String> values = listValues(value); if (areNumbers(values)) { try { return (T) toNumbers(values); @@ -88,6 +86,17 @@ return (T) values; } + private List<String> listValues(String value) { + String[] values = value.split(COMMA); + List<String> list = new ArrayList<String>(); + for ( String current : values ){ + if ( current.trim().length() > 0 ){ + list.add(current); + } + } + return list; + } + public Properties getPatterns() { return patterns; } @@ -95,6 +104,11 @@ public void changePatterns(Properties patterns) { this.patterns = patterns; } + + @SuppressWarnings("unchecked") + protected Object convertMissingValue(String key, String defaultMessage, Object... parameters) { + return new ArrayList(); + } protected boolean areNumbers(List<String> values) { if (values.size() == 0) {
Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java (668 => 669)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java 2008-05-07 09:52:10 UTC (rev 668) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java 2008-05-07 11:22:21 UTC (rev 669) @@ -3,7 +3,7 @@ import static java.text.MessageFormat.format; import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -56,6 +56,8 @@ assertCanConvertValueToList(converter, DOUBLES, "0.1,0.2,0.3", Double.class); assertCanConvertValueToList(converter, FLOATS, "0.1,0.2,0.3", Float.class); assertCanConvertValueToList(converter, STRINGS, "one,two,three", String.class); + assertCanConvertValueToList(converter, STRINGS, ",one,two,three", String.class); + assertCanConvertValueToList(converter, STRINGS, "one,,two,three", String.class); assertCanConvertValueToList(converter, MIXED_STRINGS, "0#.A,1#.B", String.class); } @@ -67,11 +69,17 @@ @Test public void canHandleMissingValues() { ListValueConverter converter = new ListValueConverter(new DefaultMessageResources()); - assertNull(converter.convertValue("property-name", null, List.class)); - assertNull(converter.convertValue("property-name", "", List.class)); - assertNull(converter.convertValue("property-name", " ", List.class)); + assertEmptyList(converter, null); + assertEmptyList(converter, ""); + assertEmptyList(converter, " "); } + private void assertEmptyList(ListValueConverter converter, String value) { + List<?> list = converter.convertValue("property-name", value, List.class); + assertNotNull(list); + assertTrue(list.isEmpty()); + } + @Test public void canFailConversionWithCustomErrorMessages() { DefaultMessageResources resources = new DefaultMessageResources(configuration);
Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java (668 => 669)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java 2008-05-07 09:52:10 UTC (rev 668) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java 2008-05-07 11:22:21 UTC (rev 669) @@ -2,7 +2,6 @@ import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import java.util.List; @@ -44,7 +43,7 @@ final ValueConverter valueConverter = new ListValueConverter(new DefaultMessageResources()); DelegatingTypeConverter converter = new DelegatingTypeConverter(new OgnlValueConverterFinder(valueConverter)); assertEquals(asList("one","two"), converter.convertValue("propertyName", "one,two", List.class)); - assertNull(converter.convertValue("propertyName", "", List.class)); + assertEquals(asList(), converter.convertValue("propertyName", "", List.class)); } @Test
To unsubscribe from this list please visit:
