Commits

Anonymous committed 2478009

Adding testcase to reproduce the following issues (commented for now):

XW-460, XW-529
Struts2 WW-1978

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/2.0@1566e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

src/test/com/opensymphony/xwork2/util/MyBooleanConverter.java

+package com.opensymphony.xwork2.util;
+
+import ognl.DefaultTypeConverter;
+
+import java.util.Map;
+import java.util.Date;
+import java.lang.reflect.Member;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * <code>MyBooleanConverter</code>
+ *
+ * @author <a href="mailto:hermanns@aixcept.de">Rainer Hermanns</a>
+ * @version $Id: $
+ */
+public class MyBooleanConverter extends DefaultTypeConverter {
+
+    /**
+     * The static jakarta-commons-logging reference.
+     */
+    private static final Log log = LogFactory.getLog(MyBooleanConverter.class);
+
+    @Override
+    public Object convertValue(Map context, Object value, Class toType) {
+        try {
+            if (toType == String.class) {
+                Boolean bool = (Boolean) value;
+                log.error("to String type...");
+                return bool.toString();
+            } else if (toType == Boolean.class) {
+                String valueStr = (String) value;
+
+                if ( valueStr!= null && (valueStr.equalsIgnoreCase("y") || valueStr.equalsIgnoreCase("yes"))) {
+                    return Boolean.TRUE;
+                }
+                return Boolean.FALSE;
+            } else {
+                log.error("Don't know how to convert between " + value.getClass().getName() +
+                        " and " + toType.getName());
+            }
+
+        } catch (Exception e) {
+            log.error("Error while converting", e);
+        }
+
+        return null;
+
+    }
+
+    @Override
+    public Object convertValue(Map context, Object source, Member member, String property, Object value, Class toClass) {
+        return convertValue(context, value, toClass);
+    }
+}

src/test/com/opensymphony/xwork2/util/XWorkConverterTest.java

         assertEquals("blah", bar.getTitle());
     }
 
+    public void testRegisterCustomConverter() {
+        converter.registerConverter(Boolean.class.getName(), new MyBooleanConverter());
+        Boolean value = (Boolean) converter.convertValue(null, null, null, null, "Y", Boolean.class);
+        assertNotNull("conversion failed", value);
+        assertEquals(Boolean.TRUE, value);
+    }
+
     public void testStringToPrimitiveWrappers() {
         assertEquals(new Long(123), converter.convertValue(context, null, null, null, "123", Long.class));
         assertEquals(new Integer(123), converter.convertValue(context, null, null, null, "123", Integer.class));

src/test/xwork-conversion.properties

+###
+### By uncommenting the converter definition below, you'll recognize the exceptions being thrown about
+### the converter registration failure.
+###
+### Related issues:
+### https://issues.apache.org/struts/browse/WW-1978
+### http://jira.opensymphony.com/browse/XW-460
+### http://jira.opensymphony.com/browse/XW-529
+
+#java.lang.Boolean=com.opensymphony.xwork2.util.MyBooleanConverter