Anonymous avatar Anonymous committed 6d89d3c

Fix broken bug in converting to Collection.

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

Comments (0)

Files changed (2)

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

         return locale;
     }
 
+    /**
+     * Creates a Collection of the specified type.
+     *
+     * @param toType the type of Collection to create
+     * @param memberType the type of object elements in this collection must be
+     * @param size the initial size of the collection (ignored if 0 or less)
+     * @return a Collection of the specified type
+     */
+    private Collection createCollection(Class toType, Class memberType, int size) {
+        Collection result;
+
+        if (toType == Set.class) {
+            if (size > 0) {
+                result = new HashSet(size);
+            } else {
+                result = new HashSet();
+            }
+        } else if (toType == SortedSet.class) {
+            result = new TreeSet();
+        } else {
+            if (size > 0) {
+                result = new XWorkList(memberType, size);
+            } else {
+                result = new XWorkList(memberType);
+            }
+        }
+
+        return result;
+    }
+
     private Object doConvertToArray(Map context, Object o, Member member, String s, Object value, Class toType) {
         Object result = null;
         Class componentType = toType.getComponentType();
             result = (Collection) value;
         } else if (value.getClass().isArray()) {
             Object[] objArray = (Object[]) value;
-
-            if (toType == Set.class) {
-                result = new HashSet(objArray.length);
-            } else if (toType == SortedSet.class) {
-                result = new TreeSet();
-            } else {
-                result = new XWorkList(memberType, objArray.length);
-            }
-
             TypeConverter converter = Ognl.getTypeConverter(context);
+            result = createCollection(toType, memberType, objArray.length);
 
             for (int i = 0; i < objArray.length; i++) {
                 result.add(converter.convertValue(context, o, member, prop, objArray[i], memberType));
             }
         } else if (Collection.class.isAssignableFrom(value.getClass())) {
             Collection col = (Collection) value;
-
-            if (toType == Set.class) {
-                result = new HashSet(col.size());
-            } else if (toType == SortedSet.class) {
-                result = new TreeSet();
-            } else {
-                result = new XWorkList(memberType, col.size());
-            }
-
             TypeConverter converter = Ognl.getTypeConverter(context);
+            result = createCollection(toType, memberType, col.size());
 
             for (Iterator it = col.iterator(); it.hasNext();) {
                 result.add(converter.convertValue(context, o, member, prop, it.next(), memberType));
             }
+        } else {
+            result = createCollection(toType, memberType, -1);
+            result.add(value);
         }
 
         return result;

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

 import com.opensymphony.xwork.TestBean;
 import com.opensymphony.xwork.config.ConfigurationManager;
 import com.opensymphony.xwork.test.ModelDrivenAction2;
+import com.opensymphony.xwork.test.User;
 
 import junit.framework.TestCase;
 
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
+    public void testArrayToNumberConversion() {
+        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 testDateConversion() throws ParseException {
         java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
         assertEquals(sqlDate, converter.convertValue(context, null, null, null, sqlDate, Date.class));
         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);
                 }, Set.class));
     }
 
+    public void testStringToCollectionConversion() {
+        OgnlValueStack stack = new OgnlValueStack();
+        Map stackContext = stack.getContext();
+        stackContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
+        stackContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE);
+        stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+
+        User user = new User();
+        stack.push(user);
+
+        stack.setValue("list", "asdf");
+        assertNotNull(user.getList());
+        assertEquals(1, user.getList().size());
+        assertEquals(String.class, user.getList().get(0).getClass());
+        assertEquals("asdf", user.getList().get(0));
+    }
+
     public void testStringToCustomTypeUsingCustomConverter() {
         // the converter needs to be registered as the Bar.class converter 
         // it won't be detected from the Foo-conversion.properties
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.