From e6c3227d2d1e95777941e0eed356f0ddff91a86c Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Tue, 22 Sep 2009 20:28:23 +0000 Subject: [PATCH] more generic converters --- .../ArrayToStringGenericConverter.java | 69 +++++ .../CollectionToStringGenericConverter.java | 75 ++++++ .../core/convert/support/ConversionUtils.java | 21 +- .../support/GenericConversionService.java | 5 +- .../StringToArrayGenericConverter.java | 53 ++++ .../StringToCollectionGenericConverter.java | 58 ++++ .../GenericConversionServiceTests.java | 254 ++++++++++++------ 7 files changed, 443 insertions(+), 92 deletions(-) create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringGenericConverter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringGenericConverter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToArrayGenericConverter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCollectionGenericConverter.java diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringGenericConverter.java new file mode 100644 index 0000000000..d283e73202 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringGenericConverter.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.support; + +import static org.springframework.core.convert.support.ConversionUtils.invokeConverter; + +import java.lang.reflect.Array; + +import org.springframework.core.convert.ConverterNotFoundException; +import org.springframework.core.convert.TypeDescriptor; + +final class ArrayToStringGenericConverter implements GenericConverter { + + private static final String DELIMITER = ","; + + private final GenericConversionService conversionService; + + public ArrayToStringGenericConverter(GenericConversionService conversionService) { + this.conversionService = conversionService; + } + + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + int length = Array.getLength(source); + if (length == 0) { + return ""; + } else { + TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); + if (sourceElementType.isAssignableTo(targetType)) { + StringBuilder string = new StringBuilder(); + for (int i = 0; i < length; i++) { + if (i > 0) { + string.append(DELIMITER); + } + string.append(Array.get(source, i)); + } + return string.toString(); + } else { + GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetType); + if (converter == null) { + throw new ConverterNotFoundException(sourceElementType, targetType); + } + StringBuilder string = new StringBuilder(); + for (int i = 0; i < length; i++) { + if (i > 0) { + string.append(DELIMITER); + } + Object sourceElement = Array.get(source, i); + Object targetElement = invokeConverter(converter, sourceElement, sourceElementType, targetType); + string.append(targetElement); + } + return string.toString(); + } + } + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringGenericConverter.java new file mode 100644 index 0000000000..913ad883f3 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringGenericConverter.java @@ -0,0 +1,75 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.support; + +import static org.springframework.core.convert.support.ConversionUtils.getElementType; +import static org.springframework.core.convert.support.ConversionUtils.invokeConverter; + +import java.util.Collection; + +import org.springframework.core.convert.ConverterNotFoundException; +import org.springframework.core.convert.TypeDescriptor; + +final class CollectionToStringGenericConverter implements GenericConverter { + + private static final String DELIMITER = ","; + + private final GenericConversionService conversionService; + + public CollectionToStringGenericConverter(GenericConversionService conversionService) { + this.conversionService = conversionService; + } + + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + Collection sourceCollection = (Collection) source; + if (sourceCollection.size() == 0) { + return ""; + } else { + TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); + if (sourceElementType == TypeDescriptor.NULL) { + sourceElementType = getElementType(sourceCollection); + } + if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetType)) { + StringBuilder string = new StringBuilder(); + int i = 0; + for (Object element : sourceCollection) { + if (i > 0) { + string.append(DELIMITER); + } + string.append(element); + i++; + } + return string.toString(); + } else { + GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetType); + if (converter == null) { + throw new ConverterNotFoundException(sourceElementType, targetType); + } + StringBuilder string = new StringBuilder(); + int i = 0; + for (Object sourceElement : sourceCollection) { + if (i > 0) { + string.append(DELIMITER); + } + Object targetElement = invokeConverter(converter, sourceElement, sourceElementType, targetType); + string.append(targetElement); + i++; + } + return string.toString(); + } + } + } +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java index 33f39f341e..3d8fa6b34e 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java @@ -1,3 +1,18 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.core.convert.support; import java.util.Collection; @@ -6,16 +21,16 @@ import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.TypeDescriptor; final class ConversionUtils { + public static Object invokeConverter(GenericConverter converter, Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { try { return converter.convert(source, sourceType, targetType); - } - catch (Exception ex) { + } catch (Exception ex) { throw new ConversionFailedException(sourceType, targetType, source, ex); } } - + public static TypeDescriptor getElementType(Collection collection) { for (Object element : collection) { if (element != null) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index e54da0edd7..3957698833 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -184,14 +184,17 @@ public class GenericConversionService implements ConversionService, ConverterReg protected void initGenericConverters() { addGenericConverter(Object[].class, Object[].class, new ArrayToArrayGenericConverter(this)); addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionGenericConverter(this)); + addGenericConverter(Object[].class, String.class, new ArrayToStringGenericConverter(this)); addGenericConverter(Object[].class, Object.class, new ArrayToObjectGenericConverter(this)); addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionGenericConverter(this)); addGenericConverter(Collection.class, Object[].class, new CollectionToArrayGenericConverter(this)); + addGenericConverter(Collection.class, String.class, new CollectionToStringGenericConverter(this)); addGenericConverter(Collection.class, Object.class, new CollectionToObjectGenericConverter(this)); addGenericConverter(Map.class, Map.class, new MapToMapGenericConverter(this)); + addGenericConverter(String.class, Object[].class, new StringToArrayGenericConverter(this)); + addGenericConverter(String.class, Collection.class, new StringToCollectionGenericConverter(this)); addGenericConverter(Object.class, Object[].class, new ObjectToArrayGenericConverter(this)); addGenericConverter(Object.class, Collection.class, new ObjectToCollectionGenericConverter(this)); - addGenericConverter(String.class, Object[].class, new StringToArrayGenericConverter(this)); } /** diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToArrayGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToArrayGenericConverter.java new file mode 100644 index 0000000000..22f7699e0d --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToArrayGenericConverter.java @@ -0,0 +1,53 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.support; + +import static org.springframework.core.convert.support.ConversionUtils.invokeConverter; + +import java.lang.reflect.Array; + +import org.springframework.core.convert.ConverterNotFoundException; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.util.StringUtils; + +final class StringToArrayGenericConverter implements GenericConverter { + + private final GenericConversionService conversionService; + + public StringToArrayGenericConverter(GenericConversionService conversionService) { + this.conversionService = conversionService; + } + + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + String string = (String) source; + String[] fields = StringUtils.commaDelimitedListToStringArray(string); + TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); + if (sourceType.isAssignableTo(targetElementType)) { + return fields; + } else { + Object target = Array.newInstance(targetElementType.getType(), fields.length); + GenericConverter converter = this.conversionService.getConverter(sourceType, targetElementType); + if (converter == null) { + throw new ConverterNotFoundException(sourceType, targetElementType); + } + for (int i = 0; i < fields.length; i++) { + Array.set(target, i, invokeConverter(converter, fields[i], sourceType, targetElementType)); + } + return target; + } + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCollectionGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCollectionGenericConverter.java new file mode 100644 index 0000000000..b401866726 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCollectionGenericConverter.java @@ -0,0 +1,58 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.support; + +import static org.springframework.core.convert.support.ConversionUtils.invokeConverter; + +import java.util.Collection; + +import org.springframework.core.CollectionFactory; +import org.springframework.core.convert.ConverterNotFoundException; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.util.StringUtils; + +final class StringToCollectionGenericConverter implements GenericConverter { + + private final GenericConversionService conversionService; + + public StringToCollectionGenericConverter(GenericConversionService conversionService) { + this.conversionService = conversionService; + } + + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + String string = (String) source; + String[] fields = StringUtils.commaDelimitedListToStringArray(string); + Collection target = CollectionFactory.createCollection(targetType.getType(), 1); + TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); + if (targetElementType == TypeDescriptor.NULL || sourceType.isAssignableTo(targetElementType)) { + for (int i = 0; i < fields.length; i++) { + target.add(fields[i]); + } + } else { + GenericConverter converter = this.conversionService.getConverter(sourceType, targetElementType); + if (converter == null) { + throw new ConverterNotFoundException(sourceType, targetElementType); + } + for (int i = 0; i < fields.length; i++) { + String sourceElement = fields[i]; + Object targetElement = invokeConverter(converter, sourceElement, sourceType, targetElementType); + target.add(targetElement); + } + } + return target; + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 7bc9800371..9bfc080cb8 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue; import java.util.AbstractList; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashSet; @@ -32,7 +33,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.junit.Ignore; import org.junit.Test; import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; @@ -44,28 +44,28 @@ import org.springframework.core.convert.converter.Converter; */ public class GenericConversionServiceTests { - private GenericConversionService converter = new GenericConversionService(); + private GenericConversionService conversionService = new GenericConversionService(); @Test public void executeConversion() { - converter.addConverterFactory(new StringToNumberConverterFactory()); - assertEquals(new Integer(3), converter.convert("3", Integer.class)); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + assertEquals(new Integer(3), conversionService.convert("3", Integer.class)); } @Test public void executeConversionNullSource() { - assertEquals(null, converter.convert(null, Integer.class)); + assertEquals(null, conversionService.convert(null, Integer.class)); } @Test public void executeCompatibleSource() { - assertEquals(Boolean.FALSE, converter.convert(false, boolean.class)); + assertEquals(Boolean.FALSE, conversionService.convert(false, boolean.class)); } @Test public void converterNotFound() { try { - converter.convert("3", Integer.class); + conversionService.convert("3", Integer.class); fail("Should have thrown an exception"); } catch (ConverterNotFoundException e) { } @@ -74,7 +74,7 @@ public class GenericConversionServiceTests { @Test public void addConverterNoSourceTargetClassInfoAvailable() { try { - converter.addConverter(new Converter() { + conversionService.addConverter(new Converter() { public Object convert(Object source) { return source; } @@ -87,24 +87,24 @@ public class GenericConversionServiceTests { @Test public void convertNull() { - assertNull(converter.convert(null, Integer.class)); + assertNull(conversionService.convert(null, Integer.class)); } @Test(expected = IllegalArgumentException.class) public void convertNullTargetClass() { - assertEquals("3", converter.convert("3", (Class) null)); + assertEquals("3", conversionService.convert("3", (Class) null)); } @Test public void convertNullTypeDescriptor() { - assertEquals(null, converter.convert(3, TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL)); + assertEquals(null, conversionService.convert(3, TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL)); } @Test public void convertWrongTypeArgument() { - converter.addConverterFactory(new StringToNumberConverterFactory()); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); try { - converter.convert("BOGUS", Integer.class); + conversionService.convert("BOGUS", Integer.class); fail("Should have failed"); } catch (ConversionFailedException e) { @@ -113,26 +113,26 @@ public class GenericConversionServiceTests { @Test public void convertSuperSourceType() { - converter.addConverter(new Converter() { + conversionService.addConverter(new Converter() { public Integer convert(CharSequence source) { return Integer.valueOf(source.toString()); } }); - Integer result = converter.convert("3", Integer.class); + Integer result = conversionService.convert("3", Integer.class); assertEquals(new Integer(3), result); } @Test public void convertObjectToPrimitive() { - converter.addConverterFactory(new StringToNumberConverterFactory()); - Integer three = converter.convert("3", int.class); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + Integer three = conversionService.convert("3", int.class); assertEquals(3, three.intValue()); } @Test public void convertArrayToArray() { - converter.addConverterFactory(new StringToNumberConverterFactory()); - Integer[] result = converter.convert(new String[] { "1", "2", "3" }, Integer[].class); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + Integer[] result = conversionService.convert(new String[] { "1", "2", "3" }, Integer[].class); assertEquals(new Integer(1), result[0]); assertEquals(new Integer(2), result[1]); assertEquals(new Integer(3), result[2]); @@ -140,8 +140,8 @@ public class GenericConversionServiceTests { @Test public void convertArrayToPrimitiveArray() { - converter.addConverterFactory(new StringToNumberConverterFactory()); - int[] result = converter.convert(new String[] { "1", "2", "3" }, int[].class); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + int[] result = conversionService.convert(new String[] { "1", "2", "3" }, int[].class); assertEquals(1, result[0]); assertEquals(2, result[1]); assertEquals(3, result[2]); @@ -149,7 +149,7 @@ public class GenericConversionServiceTests { @Test public void convertArrayToArrayAssignable() { - int[] result = converter.convert(new int[] { 1, 2, 3 }, int[].class); + int[] result = conversionService.convert(new int[] { 1, 2, 3 }, int[].class); assertEquals(1, result[0]); assertEquals(2, result[1]); assertEquals(3, result[2]); @@ -157,7 +157,7 @@ public class GenericConversionServiceTests { @Test public void convertArrayToListInterface() { - List result = converter.convert(new String[] { "1", "2", "3" }, List.class); + List result = conversionService.convert(new String[] { "1", "2", "3" }, List.class); assertEquals("1", result.get(0)); assertEquals("2", result.get(1)); assertEquals("3", result.get(2)); @@ -167,8 +167,8 @@ public class GenericConversionServiceTests { @Test public void convertArrayToListGenericTypeConversion() throws Exception { - converter.addConverterFactory(new StringToNumberConverterFactory()); - List result = (List) converter.convert(new String[] { "1", "2", "3" }, TypeDescriptor + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + List result = (List) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor .valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList"))); assertEquals(new Integer("1"), result.get(0)); assertEquals(new Integer("2"), result.get(1)); @@ -177,7 +177,7 @@ public class GenericConversionServiceTests { @Test public void convertArrayToListImpl() { - LinkedList result = converter.convert(new String[] { "1", "2", "3" }, LinkedList.class); + LinkedList result = conversionService.convert(new String[] { "1", "2", "3" }, LinkedList.class); assertEquals("1", result.get(0)); assertEquals("2", result.get(1)); assertEquals("3", result.get(2)); @@ -185,29 +185,63 @@ public class GenericConversionServiceTests { @Test(expected = ConversionFailedException.class) public void convertArrayToAbstractList() { - converter.convert(new String[] { "1", "2", "3" }, AbstractList.class); + conversionService.convert(new String[] { "1", "2", "3" }, AbstractList.class); } @Test - public void convertListToArray() { + public void convertArrayToString() { + String result = conversionService.convert(new String[] { "1", "2", "3" }, String.class); + assertEquals("1,2,3", result); + } + + @Test + public void convertArrayToStringWithElementConversion() { + conversionService.addConverter(new ObjectToStringConverter()); + String result = conversionService.convert(new Integer[] { 1, 2, 3 }, String.class); + assertEquals("1,2,3", result); + } + + @Test + public void convertEmptyArrayToString() { + String result = conversionService.convert(new String[0], String.class); + assertEquals("", result); + } + + @Test + public void convertArrayToObject() { + Object[] array = new Object[] { 3L }; + Object result = conversionService.convert(array, Object.class); + assertEquals(3L, result); + } + + @Test + public void convertArrayToObjectWithElementConversion() { + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + String[] array = new String[] { "3" }; + Integer result = conversionService.convert(array, Integer.class); + assertEquals(new Integer(3), result); + } + + @Test + public void convertCollectionToArray() { List list = new ArrayList(); list.add("1"); list.add("2"); list.add("3"); - String[] result = converter.convert(list, String[].class); + String[] result = conversionService.convert(list, String[].class); assertEquals("1", result[0]); assertEquals("2", result[1]); assertEquals("3", result[2]); } @Test - public void convertListToArrayWithComponentConversion() { - converter.addConverterFactory(new StringToNumberConverterFactory()); + public void convertCollectionToArrayWithElementConversion() { + conversionService.addConverterFactory(new StringToNumberConverterFactory()); List list = new ArrayList(); list.add("1"); list.add("2"); list.add("3"); - Integer[] result = converter.convert(list, Integer[].class); + Integer[] result = conversionService.convert(list, Integer[].class); assertEquals(new Integer(1), result[0]); assertEquals(new Integer(2), result[1]); assertEquals(new Integer(3), result[2]); @@ -215,18 +249,48 @@ public class GenericConversionServiceTests { @Test public void convertCollectionToCollection() throws Exception { - converter.addConverterFactory(new StringToNumberConverterFactory()); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); Set foo = new LinkedHashSet(); foo.add("1"); foo.add("2"); foo.add("3"); - List bar = (List) converter.convert(foo, TypeDescriptor.valueOf(List.class), + List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(List.class), new TypeDescriptor(getClass().getField("genericList"))); assertEquals(new Integer(1), bar.get(0)); assertEquals(new Integer(2), bar.get(1)); assertEquals(new Integer(3), bar.get(2)); } + @Test + public void convertCollectionToString() { + List list = Arrays.asList(new String[] { "foo", "bar" }); + String result = conversionService.convert(list, String.class); + assertEquals("foo,bar", result); + } + + @Test + public void convertCollectionToStringWithElementConversion() throws Exception { + conversionService.addConverter(new ObjectToStringConverter()); + List list = Arrays.asList(new Integer[] { 3, 5 }); + String result = (String) conversionService.convert(list, new TypeDescriptor(getClass().getField("genericList")), TypeDescriptor.valueOf(String.class)); + assertEquals("3,5", result); + } + + @Test + public void convertCollectionToObject() { + List list = Collections.singletonList(3L); + Long result = conversionService.convert(list, Long.class); + assertEquals(new Long(3), result); + } + + @Test + public void convertCollectionToObjectWithElementConversion() { + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + List list = Collections.singletonList("3"); + Integer result = conversionService.convert(list, Integer.class); + assertEquals(new Integer(3), result); + } + public Map genericMap = new HashMap(); @Test @@ -234,89 +298,103 @@ public class GenericConversionServiceTests { Map foo = new HashMap(); foo.put("1", "BAR"); foo.put("2", "BAZ"); - converter.addConverterFactory(new StringToNumberConverterFactory()); - converter.addConverterFactory(new StringToEnumConverterFactory()); - Map map = (Map) converter.convert(foo, TypeDescriptor.valueOf(Map.class), - new TypeDescriptor(getClass().getField("genericMap"))); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + conversionService.addConverterFactory(new StringToEnumConverterFactory()); + Map map = (Map) conversionService.convert(foo, TypeDescriptor + .valueOf(Map.class), new TypeDescriptor(getClass().getField("genericMap"))); assertEquals(map.get(1), FooEnum.BAR); assertEquals(map.get(2), FooEnum.BAZ); } @Test - public void convertObjectToCollection() { - List result = (List) converter.convert("test", List.class); - assertEquals(1, result.size()); - assertEquals("test", result.get(0)); + public void convertStringToArray() { + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + String[] result = conversionService.convert("1,2,3", String[].class); + assertEquals(3, result.length); + assertEquals("1", result[0]); + assertEquals("2", result[1]); + assertEquals("3", result[2]); } @Test - public void convertObjectToCollectionWithElementConversion() throws Exception { - converter.addConverterFactory(new StringToNumberConverterFactory()); - List result = (List) converter.convert("3", TypeDescriptor.valueOf(String.class), - new TypeDescriptor(getClass().getField("genericList"))); - assertEquals(1, result.size()); - assertEquals(new Integer(3), result.get(0)); + public void convertStringToArrayWithElementConversion() { + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + Integer[] result = conversionService.convert("1,2,3", Integer[].class); + assertEquals(3, result.length); + assertEquals(new Integer(1), result[0]); + assertEquals(new Integer(2), result[1]); + assertEquals(new Integer(3), result[2]); } @Test - public void convertCollectionToObject() { - List list = Collections.singletonList("test"); - String result = converter.convert(list, String.class); - assertEquals("test", result); + public void convertEmptyStringToArray() { + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + String[] result = conversionService.convert("", String[].class); + assertEquals(0, result.length); } @Test - public void convertCollectionToObjectWithElementConversion() { - converter.addConverterFactory(new StringToNumberConverterFactory()); - List list = Collections.singletonList("3"); - Integer result = converter.convert(list, Integer.class); - assertEquals(new Integer(3), result); + public void convertStringToCollection() { + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + List result = conversionService.convert("1,2,3", List.class); + assertEquals(3, result.size()); + assertEquals("1", result.get(0)); + assertEquals("2", result.get(1)); + assertEquals("3", result.get(2)); } @Test - public void convertObjectToArray() { - String[] result = converter.convert("test", String[].class); - assertEquals(1, result.length); - assertEquals("test", result[0]); + public void convertStringToCollectionWithElementConversion() throws Exception { + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericList"))); + assertEquals(3, result.size()); + assertEquals(new Integer(1), result.get(0)); + assertEquals(new Integer(2), result.get(1)); + assertEquals(new Integer(3), result.get(2)); } @Test - public void convertObjectToArrayWithElementConversion() { - converter.addConverterFactory(new StringToNumberConverterFactory()); - Integer[] result = converter.convert("1", Integer[].class); - assertEquals(1, result.length); - assertEquals(new Integer(1), result[0]); + public void convertEmptyStringToCollection() { + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + String[] result = conversionService.convert("", String[].class); + assertEquals(0, result.length); } @Test - public void convertArrayToObject() { - String[] array = new String[] { "test" }; - String result = converter.convert(array, String.class); - assertEquals("test", result); + public void convertObjectToCollection() { + List result = (List) conversionService.convert(3L, List.class); + assertEquals(1, result.size()); + assertEquals(3L, result.get(0)); } - + @Test - public void convertArrayToObjectWithElementConversion() { - converter.addConverterFactory(new StringToNumberConverterFactory()); - String[] array = new String[] { "3" }; - Integer result = converter.convert(array, Integer.class); - assertEquals(new Integer(3), result); + public void convertObjectToCollectionWithElementConversion() throws Exception { + conversionService.addConverterFactory(new NumberToNumberConverterFactory()); + List result = (List) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class), + new TypeDescriptor(getClass().getField("genericList"))); + assertEquals(1, result.size()); + assertEquals(new Integer(3), result.get(0)); } - + @Test - public void convertStringToArrayWithElementConversion() { - converter.addConverterFactory(new StringToNumberConverterFactory()); - Integer[] result = converter.convert("1,2,3", Integer[].class); - assertEquals(3, result.length); - assertEquals(new Integer(1), result[0]); - assertEquals(new Integer(2), result[1]); - assertEquals(new Integer(3), result[2]); + public void convertObjectToArray() { + Object[] result = conversionService.convert(3L, Object[].class); + assertEquals(1, result.length); + assertEquals(3L, result[0]); + } + + @Test + public void convertObjectToArrayWithElementConversion() { + conversionService.addConverterFactory(new NumberToNumberConverterFactory()); + Integer[] result = conversionService.convert(3L, Integer[].class); + assertEquals(1, result.length); + assertEquals(new Integer(3), result[0]); } @Test public void genericConverterDelegatingBackToConversionServiceConverterNotFound() { try { - converter.convert("1", Integer[].class); + conversionService.convert("1", Integer[].class); } catch (ConversionFailedException e) { assertTrue(e.getCause() instanceof ConverterNotFoundException); } @@ -325,10 +403,10 @@ public class GenericConversionServiceTests { @Test public void parent() { GenericConversionService parent = new GenericConversionService(); - converter.setParent(parent); - assertFalse(converter.canConvert(String.class, Integer.class)); + conversionService.setParent(parent); + assertFalse(conversionService.canConvert(String.class, Integer.class)); try { - converter.convert("3", Integer.class); + conversionService.convert("3", Integer.class); } catch (ConverterNotFoundException e) { }