Commits

Anonymous committed 2883e7a

Fix for numeric type conversion reported by Francisco Hernandez.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@322e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 03f4852

Comments (0)

Files changed (2)

File src/java/com/opensymphony/xwork/util/XWorkBasicConverter.java

 import java.lang.reflect.Array;
 import java.lang.reflect.Member;
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
 import java.text.DateFormat;
 import java.text.NumberFormat;
 import java.text.ParseException;
                 }
 
                 // let's try to convert the first element only
-                result = this.convertValue(context, o, member, s, value, toType);
+                result = convertValue(context, o, member, s, value, toType);
             } else {
                 result = super.convertValue(context, value, toType);
             }
 
     private Object doConvertToNumber(Map context, Object value, Class toType) {
         if (value instanceof String) {
-            NumberFormat numFormat = NumberFormat.getInstance(getLocale(context));
+            if (toType == BigDecimal.class) {
+                return new BigDecimal((String) value);
+            } else if (toType == BigInteger.class) {
+                return new BigInteger((String) value);
+            } else {
+                NumberFormat numFormat = NumberFormat.getInstance(getLocale(context));
 
-            try {
-                // convert it to a Number
-                value = super.convertValue(context, numFormat.parse((String) value), toType);
-            } catch (ParseException ex) {
-                // ignore parse failure and hope default behavior works
+                try {
+                    // convert it to a Number
+                    value = super.convertValue(context, numFormat.parse((String) value), toType);
+                } catch (ParseException ex) {
+                    // ignore parse failure and hope default behavior works
+                }
+            }
+        } else if (value instanceof Object[]) {
+            Object[] objArray = (Object[]) value;
+
+            if (objArray.length == 1) {
+                return doConvertToNumber(context, objArray[0], toType);
             }
         }
 

File src/test/com/opensymphony/xwork/util/XWorkConverterTest.java

         OgnlValueStack stack = new OgnlValueStack();
         stack.push(action);
 
-        Map context = stack.getContext();
-        context.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
-        context.put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, "bean.birth");
+        Map ognlStackContext = stack.getContext();
+        ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+        ognlStackContext.put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, "bean.birth");
 
         String[] value = new String[] {"invalid date"};
-        assertEquals("Conversion should have failed.", null, converter.convertValue(context, action.getBean(), null, "birth", value, Date.class));
+        assertEquals("Conversion should have failed.", null, converter.convertValue(ognlStackContext, action.getBean(), null, "birth", value, Date.class));
         stack.pop();
 
         Map conversionErrors = (Map) stack.getContext().get(ActionContext.CONVERSION_ERRORS);
         OgnlValueStack stack = new OgnlValueStack();
         stack.push(action);
 
-        Map context = stack.getContext();
-        context.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+        Map ognlStackContext = stack.getContext();
+        ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
 
         String[] value = new String[] {"invalid date"};
-        assertEquals("Conversion should have failed.", null, converter.convertValue(context, action, null, "date", value, Date.class));
+        assertEquals("Conversion should have failed.", null, converter.convertValue(ognlStackContext, action, null, "date", value, Date.class));
         stack.pop();
 
-        Map conversionErrors = (Map) context.get(ActionContext.CONVERSION_ERRORS);
+        Map conversionErrors = (Map) ognlStackContext.get(ActionContext.CONVERSION_ERRORS);
         assertNotNull(conversionErrors);
         assertEquals(1, conversionErrors.size());
         assertNotNull(conversionErrors.get("date"));
         stack.push(action);
         stack.push(action.getModel());
 
-        Map context = stack.getContext();
-        context.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+        Map ognlStackContext = stack.getContext();
+        ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
 
         String[] value = new String[] {"invalid date"};
-        assertEquals("Conversion should have failed.", null, converter.convertValue(context, action, null, "birth", value, Date.class));
+        assertEquals("Conversion should have failed.", null, converter.convertValue(ognlStackContext, action, null, "birth", value, Date.class));
         stack.pop();
         stack.pop();
 
-        Map conversionErrors = (Map) context.get(ActionContext.CONVERSION_ERRORS);
+        Map conversionErrors = (Map) ognlStackContext.get(ActionContext.CONVERSION_ERRORS);
         assertNotNull(conversionErrors);
         assertEquals(1, conversionErrors.size());
         assertNotNull(conversionErrors.get("birth"));
         stack.push(action);
         stack.push(action.getModel());
 
-        Map context = stack.getContext();
-        context.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+        Map ognlStackContext = stack.getContext();
+        ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
 
         String value = "asdf:123";
-        Object o = converter.convertValue(context, action.getModel(), null, "barObj", value, Bar.class);
+        Object o = converter.convertValue(ognlStackContext, action.getModel(), null, "barObj", value, Bar.class);
         assertNotNull(o);
         assertTrue(o instanceof Bar);
 
         assertEquals(dateString, converter.convertValue(context, null, null, null, date, String.class));
     }
 
+    public void testNumericConversion() {
+        String[] value = new String[] {"12345"};
+        assertEquals(new Integer(12345), converter.convertValue(context, null, null, null, value, Integer.class));
+        assertEquals(new Long(12345), converter.convertValue(context, null, null, null, value, Long.class));
+        value[0] = "123.45";
+        assertEquals(new Float(123.45), converter.convertValue(context, null, null, null, value, Float.class));
+        assertEquals(new Double(123.45), converter.convertValue(context, null, null, null, value, Double.class));
+        value[0] = "1234567890123456789012345678901234567890";
+        assertEquals(new BigInteger(value[0]), converter.convertValue(context, null, null, null, value, BigInteger.class));
+        value[0] = "1234567890123456789.012345678901234567890";
+        assertEquals(new BigDecimal(value[0]), converter.convertValue(context, null, null, null, value, BigDecimal.class));
+    }
+
     public void testPrimitiveToString() {
         Locale locale = Locale.GERMANY;
         NumberFormat nf = NumberFormat.getInstance(locale);
         assertEquals(longValue, converter.convertValue(context, null, null, null, origValue, Long.class));
 
         origValue = "123456.789";
+
         Float floatValue = new Float(origValue);
         formattedValue = nf.format(new Float(origValue));
         assertEquals(formattedValue, converter.convertValue(context, null, null, null, new Float(origValue), String.class));
     }
 
     public void testStringArrayToCollection() {
-        ArrayList list = new ArrayList();
+        List list = new ArrayList();
         list.add("foo");
         list.add("bar");
         list.add("baz");
     }
 
     public void testStringArrayToList() {
-        ArrayList list = new ArrayList();
+        List list = new ArrayList();
         list.add("foo");
         list.add("bar");
         list.add("baz");
     }
 
     public void testStringArrayToSet() {
-        HashSet list = new HashSet();
+        Set list = new HashSet();
         list.add("foo");
         list.add("bar");
         list.add("baz");