Title: [waffle-scm] [784] trunk/waffle-distribution/src/site/content: WAFFLE-91: Added StringListMapValueConverter and refactored StringNumberListMapValueConverter to extend it.

Diff

Added: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/StringListMapValueConverter.java (0 => 784)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/StringListMapValueConverter.java	                        (rev 0)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/StringListMapValueConverter.java	2008-08-26 09:21:11 UTC (rev 784)
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) terms as published in http://waffle.codehaus.org/license.html
+ */
+package org.codehaus.waffle.bind.converters;
+
+import java.lang.reflect.Type;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.codehaus.waffle.i18n.MessageResources;
+
+/**
+ * <p>
+ * <code>ValueConverter</code> that converts a text value to a Map of String Lists indexed by Strings.
+ * </p>
+ * <p>
+ * A value of the form
+ * 
+ * <pre>
+ * a=x\n
+ * b=x,y\n
+ * c=x,y,z
+ * </pre>
+ * 
+ * would be converted to a map
+ * 
+ * <pre>
+ * Map&lt;String, List&lt;String&gt;&gt; map = new HashMap&lt;String, List&lt;String&gt;&gt;();
+ * map.put(&quot;a&quot;, asList(&quot;x&quot;));
+ * map.put(&quot;b&quot;, asList(&quot;x&quot;, &quot;y&quot;));
+ * map.put(&quot;c&quot;, asList(&quot;x&quot;, &quot;y&quot;, &quot;z&quot;));
+ * </pre>
+ * 
+ * </p>
+ * 
+ * @author Mauro Talevi
+ */
+public class StringListMapValueConverter extends AbstractValueConverter {
+
+    public static final String BIND_ERROR_MAP_KEY = "bind.error.map";
+    public static final String DEFAULT_MAP_MESSAGE = "Invalid map value for field {0}";
+    protected static final String NL = "\n";
+    protected static final String EQUAL = "=";
+    protected static final String COMMA = ",";
+
+    public StringListMapValueConverter(MessageResources messageResources) {
+        this(messageResources, new Properties());
+    }
+
+    public StringListMapValueConverter(MessageResources messageResources, Properties patterns) {
+        super(messageResources, patterns);
+    }
+
+    /**
+     * Accepts parameterized types of type Map<String,List<String>>
+     */
+    public boolean accept(Type type) {
+        return acceptMap(type, String.class, String.class);
+    }
+
+    public Object convertValue(String propertyName, String value, Type toType) {
+
+        if (missingValue(value)) {
+            String fieldName = messageFor(propertyName, propertyName);
+            return convertMissingValue(BIND_ERROR_MAP_KEY, DEFAULT_MAP_MESSAGE, fieldName);
+        }
+
+        List<String> lines = split(value, NL);
+        Map<String, List<String>> map = new HashMap<String, List<String>>();
+        for (String line : lines) {
+            List<String> parts = split(line, EQUAL);
+            if (parts.size() > 1) {
+                String csv = parts.get(1);
+                map.put(parts.get(0), split(csv, COMMA));
+            }
+        }
+        return map;
+    }
+
+    protected Object convertMissingValue(String key, String defaultMessage, Object... parameters) {
+        return new HashMap<String, List<String>>();
+    }
+
+}

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/StringNumberListMapValueConverter.java (783 => 784)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/StringNumberListMapValueConverter.java	2008-08-26 03:45:48 UTC (rev 783)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/StringNumberListMapValueConverter.java	2008-08-26 09:21:11 UTC (rev 784)
@@ -42,14 +42,8 @@
  * 
  * @author Mauro Talevi
  */
-public class StringNumberListMapValueConverter extends AbstractValueConverter {
+public class StringNumberListMapValueConverter extends StringListMapValueConverter {
 
-    public static final String BIND_ERROR_MAP_KEY = "bind.error.map";
-    public static final String DEFAULT_MAP_MESSAGE = "Invalid map value for field {0}";
-    private static final String NL = "\n";
-    private static final String EQUAL = "=";
-    private static final String COMMA = ",";
-
     private NumberFormat numberFormat;
 
     public StringNumberListMapValueConverter(MessageResources messageResources) {
@@ -99,7 +93,7 @@
     }
 
     protected Object convertMissingValue(String key, String defaultMessage, Object... parameters) {
-        return new ArrayList<Number>();
+        return new HashMap<String, List<Number>>();
     }
 
 }

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/AbstractValueConverterTest.java (783 => 784)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/AbstractValueConverterTest.java	2008-08-26 03:45:48 UTC (rev 783)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/AbstractValueConverterTest.java	2008-08-26 09:21:11 UTC (rev 784)
@@ -1,10 +1,14 @@
 package org.codehaus.waffle.bind.converters;
 
 import static java.util.Arrays.asList;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 
+import org.codehaus.waffle.bind.ValueConverter;
 import org.codehaus.waffle.i18n.MessageResourcesConfiguration;
 
 /**
@@ -31,4 +35,10 @@
 
     };
 
+    protected void assertEmptyMap(ValueConverter converter, String value) {
+        Map<?,?> map = (Map<?,?>) converter.convertValue("property-name", value, Map.class);
+        assertNotNull(map);
+        assertTrue(map.isEmpty());
+    }
+
 }

Added: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/StringListMapValueConverterTest.java (0 => 784)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/StringListMapValueConverterTest.java	                        (rev 0)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/StringListMapValueConverterTest.java	2008-08-26 09:21:11 UTC (rev 784)
@@ -0,0 +1,101 @@
+package org.codehaus.waffle.bind.converters;
+
+import static java.text.MessageFormat.format;
+import static java.util.Arrays.asList;
+import static org.codehaus.waffle.testmodel.FakeControllerWithListMethods.methodParameterType;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.beans.IntrospectionException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import ognl.OgnlException;
+
+import org.codehaus.waffle.bind.BindException;
+import org.codehaus.waffle.i18n.DefaultMessageResources;
+import org.junit.Test;
+
+/**
+ * @author Mauro Talevi
+ */
+public class StringListMapValueConverterTest extends AbstractValueConverterTest {
+
+    @Test
+    public void canAccept() throws IntrospectionException {
+        StringListMapValueConverter converter = new StringListMapValueConverter(
+                new DefaultMessageResources());
+        assertTrue(converter.accept(methodParameterType("mapOfStringLists")));
+        assertFalse(converter.accept(List.class));
+        assertFalse(converter.accept(Object.class));
+        assertFalse(converter.accept(methodParameterType("object")));
+    }
+
+    @Test
+    public void canConvertMapsOfStringNumberLists() throws OgnlException {
+        StringListMapValueConverter converter = new StringListMapValueConverter(
+                new DefaultMessageResources());
+        Map<String, List<String>> map = new HashMap<String, List<String>>();
+        map.put("a", asList("x"));
+        map.put("b", asList("x", "y"));
+        map.put("c", asList("x", "y", "z"));
+        assertCanConvertValueToList(converter, map, "a=x\n b=x,y\n c=x,y,z");
+    }
+
+    @SuppressWarnings("unchecked")
+    private void assertCanConvertValueToList(StringListMapValueConverter converter,
+            Map<String, List<String>> expected, String value) {
+        Map<String, List<String>> actual = (Map<String, List<String>>) converter.convertValue(
+                "property-name", value, Map.class);
+        assertEquals(expected.toString(), actual.toString());
+    }
+
+    @Test
+    public void canHandleMissingValues() {
+        StringListMapValueConverter converter = new StringListMapValueConverter(
+                new DefaultMessageResources());
+        assertEmptyMap(converter, null);
+        assertEmptyMap(converter, "");
+        assertEmptyMap(converter, " ");
+    }
+
+    @Test
+    public void canFailConversionWithCustomErrorMessages() {
+        DefaultMessageResources resources = new DefaultMessageResources(configuration);
+        StringListMapValueConverter converter = new StringListMapValueConverter(resources) {
+
+            @Override
+            protected Object convertMissingValue(String key, String defaultMessage, Object... parameters) {
+                throw newBindException(key, defaultMessage, parameters);
+            }
+        };
+        try {
+            converter.convertValue("property-name", null, List.class);
+            fail("Expected BindException");
+        } catch (BindException e) {
+            assertEquals(format(resources.getMessage(StringListMapValueConverter.BIND_ERROR_MAP_KEY),
+                    "property-name"), e.getMessage());
+        }
+    }
+
+    @Test
+    public void canFailConversionWithDefaultErrorMessages() {
+        StringListMapValueConverter converter = new StringListMapValueConverter(
+                new DefaultMessageResources()) {
+            @Override
+            protected Object convertMissingValue(String key, String defaultMessage, Object... parameters) {
+                throw newBindException(key, defaultMessage, parameters);
+            }
+        };
+        try {
+            converter.convertValue("property-name", null, List.class);
+            fail("Expected BindException");
+        } catch (BindException e) {
+            assertEquals(format(StringListMapValueConverter.DEFAULT_MAP_MESSAGE, "property-name"), e.getMessage());
+        }
+    }
+
+}

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/StringNumberListMapValueConverterTest.java (783 => 784)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/StringNumberListMapValueConverterTest.java	2008-08-26 03:45:48 UTC (rev 783)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/StringNumberListMapValueConverterTest.java	2008-08-26 09:21:11 UTC (rev 784)
@@ -5,7 +5,6 @@
 import static org.codehaus.waffle.testmodel.FakeControllerWithListMethods.methodParameterType;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -59,17 +58,11 @@
     public void canHandleMissingValues() {
         StringNumberListMapValueConverter converter = new StringNumberListMapValueConverter(
                 new DefaultMessageResources());
-        assertEmptyList(converter, null);
-        assertEmptyList(converter, "");
-        assertEmptyList(converter, " ");
+        assertEmptyMap(converter, null);
+        assertEmptyMap(converter, "");
+        assertEmptyMap(converter, " ");
     }
 
-    private void assertEmptyList(StringNumberListMapValueConverter converter, String value) {
-        List<?> 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/testmodel/FakeControllerWithListMethods.java (783 => 784)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/testmodel/FakeControllerWithListMethods.java	2008-08-26 03:45:48 UTC (rev 783)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/testmodel/FakeControllerWithListMethods.java	2008-08-26 09:21:11 UTC (rev 784)
@@ -26,6 +26,7 @@
     public void listOfLongs(List<Integer> list){};
     public void listOfDoubles(List<Integer> list){};
     public void listOfFloats(List<Integer> list){};
+    public void mapOfStringLists(Map<String,List<String>> map){};
     public void mapOfStringIntegerLists(Map<String,List<Integer>> map){};
     public void object(Object object){};
 }
\ No newline at end of file

Modified: trunk/waffle-distribution/src/site/content/binding.html (783 => 784)

--- trunk/waffle-distribution/src/site/content/binding.html	2008-08-26 03:45:48 UTC (rev 783)
+++ trunk/waffle-distribution/src/site/content/binding.html	2008-08-26 09:21:11 UTC (rev 784)
@@ -172,9 +172,15 @@
           </td>
         </tr>
         <tr class="b">
+          <td align="left"><a href=""
+          </td>
+          <td align="left">Converts Properties-like String=CSV-String values to Maps with configurable error messages.
+          </td>
+        </tr>
+        <tr class="b">
           <td align="left"><a href=""
           </td>
-          <td align="left">Converts Properties-like String=Numbers values to Maps with configurable error messages.
+          <td align="left">Converts Properties-like String=CSV-Number values to Maps with configurable error messages.
           </td>
         </tr>
       </tbody>


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to