diff --git a/spring-binding/src/main/java/org/springframework/binding/convert/service/GenericConversionService.java b/spring-binding/src/main/java/org/springframework/binding/convert/service/GenericConversionService.java index 30e74db1..106b0b9b 100644 --- a/spring-binding/src/main/java/org/springframework/binding/convert/service/GenericConversionService.java +++ b/spring-binding/src/main/java/org/springframework/binding/convert/service/GenericConversionService.java @@ -171,11 +171,6 @@ public class GenericConversionService implements ConversionService { Assert.hasText(id, "The id of the custom converter is required"); Assert.notNull(sourceClass, "The source class to convert from is required"); Assert.notNull(targetClass, "The target class to convert to is required"); - sourceClass = convertToWrapperClassIfNecessary(sourceClass); - targetClass = convertToWrapperClassIfNecessary(targetClass); - if (targetClass.isAssignableFrom(sourceClass)) { - return new StaticConversionExecutor(sourceClass, targetClass, new NoOpConverter(sourceClass, targetClass)); - } Converter converter = (Converter) customConverters.get(id); if (converter == null) { if (parent != null) { @@ -186,6 +181,8 @@ public class GenericConversionService implements ConversionService { + sourceClass.getName() + "] to targetClass [" + targetClass.getName() + "]"); } } + sourceClass = convertToWrapperClassIfNecessary(sourceClass); + targetClass = convertToWrapperClassIfNecessary(targetClass); if (converter.getSourceClass().isAssignableFrom(sourceClass)) { if (!converter.getTargetClass().isAssignableFrom(targetClass)) { throw new ConversionExecutorNotFoundException(sourceClass, targetClass, diff --git a/spring-binding/src/main/java/org/springframework/binding/convert/service/StaticConversionExecutor.java b/spring-binding/src/main/java/org/springframework/binding/convert/service/StaticConversionExecutor.java index 52754390..09291b17 100644 --- a/spring-binding/src/main/java/org/springframework/binding/convert/service/StaticConversionExecutor.java +++ b/spring-binding/src/main/java/org/springframework/binding/convert/service/StaticConversionExecutor.java @@ -87,13 +87,9 @@ public class StaticConversionExecutor implements ConversionExecutor { } public Object execute(Object source) throws ConversionExecutionException { - if (targetClass.isInstance(source)) { - // source is already assignment compatible with target class - return source; - } if (source != null && !sourceClass.isInstance(source)) { throw new ConversionExecutionException(source, getSourceClass(), getTargetClass(), "Source object " - + source + " is expected to be an instance of " + getSourceClass()); + + source + " to convert is expected to be an instance of " + getSourceClass()); } try { return converter.convertSourceToTargetClass(source, targetClass); diff --git a/spring-binding/src/test/java/org/springframework/binding/convert/service/DefaultConversionServiceTests.java b/spring-binding/src/test/java/org/springframework/binding/convert/service/DefaultConversionServiceTests.java index 5ada66e7..d71faa2e 100644 --- a/spring-binding/src/test/java/org/springframework/binding/convert/service/DefaultConversionServiceTests.java +++ b/spring-binding/src/test/java/org/springframework/binding/convert/service/DefaultConversionServiceTests.java @@ -118,6 +118,13 @@ public class DefaultConversionServiceTests extends TestCase { assertEquals("3,000", string); } + public void testRegisterCustomConverterForSameType() { + DefaultConversionService service = new DefaultConversionService(); + service.addConverter("trimmer", new Trimmer()); + ConversionExecutor executor = service.getConversionExecutor("trimmer", String.class, String.class); + assertEquals("a string", executor.execute("a string ")); + } + public void testConversionPrimitive() { DefaultConversionService service = new DefaultConversionService(); ConversionExecutor executor = service.getConversionExecutor(String.class, int.class); @@ -242,7 +249,6 @@ public class DefaultConversionServiceTests extends TestCase { private static class CustomConverter implements Converter { public Object convertSourceToTargetClass(Object source, Class targetClass) throws Exception { - // TODO Auto-generated method stub throw new UnsupportedOperationException("Auto-generated method stub"); } @@ -256,357 +262,20 @@ public class DefaultConversionServiceTests extends TestCase { } - // public void testGenericTypeConversionOGNL() { - // DefaultConversionService service = new DefaultConversionService(); - // Map map = new HashMap(); - // map.put("stringArray", new String[] { "1", "2", "3" }); - // map.put("string", "1"); - // TestBean bean = new TestBean(); - // OgnlExpressionParser parser = new OgnlExpressionParser(); - // Expression stringArray = parser.parseExpression("stringArray", null); - // Expression integerList = parser.parseExpression("integerList", null); - // DefaultMappingContext context = new DefaultMappingContext(map, bean, service); - // DefaultMapping mapping = new DefaultMapping(stringArray, integerList); - // mapping.map(context); - // assertEquals(new Integer(1), bean.getIntegerList().get(0)); - // assertEquals(new Integer(2), bean.getIntegerList().get(1)); - // assertEquals(new Integer(3), bean.getIntegerList().get(2)); - // } - // - // public void testGenericTypeConversionBeanWrapper() { - // DefaultConversionService service = new DefaultConversionService(); - // Map map = new HashMap(); - // map.put("stringArray", new String[] { "1", "2", "3" }); - // map.put("string", "1"); - // CollectionWrapperBean wrapper = new CollectionWrapperBean(map); - // TestBean bean = new TestBean(); - // BeanWrapperExpressionParser parser = new BeanWrapperExpressionParser(); - // Expression stringArray = parser.parseExpression("map[stringArray]", null); - // Expression integerList = parser.parseExpression("integerList", null); - // DefaultMappingContext context = new DefaultMappingContext(wrapper, bean, service); - // DefaultMapping mapping = new DefaultMapping(stringArray, integerList); - // mapping.map(context); - // assertEquals(new Integer(1), bean.getIntegerList().get(0)); - // assertEquals(new Integer(2), bean.getIntegerList().get(1)); - // assertEquals(new Integer(3), bean.getIntegerList().get(2)); - // } - // - // public void testGenericTypeConversionBeanWrapperWithCustomConversion() { - // DefaultConversionService service = new DefaultConversionService(); - // service.addConverter(new StringToAuthority()); - // Map map = new HashMap(); - // map.put("authorityArray", new String[] { "keith", "keri", "annabelle" }); - // CollectionWrapperBean wrapper = new CollectionWrapperBean(map); - // TestBean bean = new TestBean(); - // BeanWrapperExpressionParser parser = new BeanWrapperExpressionParser(); - // parser.setConversionService(service); - // Expression authorityArray = parser.parseExpression("map[authorityArray]", null); - // Expression authorityList = parser.parseExpression("authorityList", null); - // DefaultMappingContext context = new DefaultMappingContext(wrapper, bean, service); - // DefaultMapping mapping = new DefaultMapping(authorityArray, authorityList); - // mapping.map(context); - // assertEquals(new Authority("keith"), bean.getAuthorityList().get(0)); - // assertEquals(new Authority("keri"), bean.getAuthorityList().get(1)); - // assertEquals(new Authority("annabelle"), bean.getAuthorityList().get(2)); - // } - // - // public void testGenericTypeConversionBeanWrapperWithNestedCustomConversion() { - // DefaultConversionService service = new DefaultConversionService(); - // service.addConverter(new StringToAuthority()); - // Map map = new HashMap(); - // map.put("foo", "keith"); - // CollectionWrapperBean wrapper = new CollectionWrapperBean(map); - // TestBean bean = new TestBean(); - // BeanWrapperExpressionParser parser = new BeanWrapperExpressionParser(); - // parser.setConversionService(service); - // Expression keith = parser.parseExpression("map[foo]", null); - // Expression nestedMapEntry = parser.parseExpression("nested[0][0]", null); - // DefaultMappingContext context = new DefaultMappingContext(wrapper, bean, service); - // DefaultMapping mapping = new DefaultMapping(keith, nestedMapEntry); - // mapping.map(context); - // assertEquals(new Authority("keith"), bean.getNested().get(0).get(0)); - // } - // - // public void testGenericTypeConversionEL() { - // DefaultConversionService service = new DefaultConversionService(); - // Map map = new HashMap(); - // map.put("stringArray", new String[] { "1", "2", "3" }); - // map.put("string", "1"); - // TestBean bean = new TestBean(); - // ELExpressionParser parser = new ELExpressionParser(new ExpressionFactoryImpl()); - // Expression stringArray = parser.parseExpression("stringArray", null); - // Expression integerList = parser.parseExpression("integerList", null); - // DefaultMappingContext context = new DefaultMappingContext(map, bean, service); - // DefaultMapping mapping = new DefaultMapping(stringArray, integerList); - // mapping.map(context); - // assertEquals(new Integer(1), bean.getIntegerList().get(0)); - // assertEquals(new Integer(2), bean.getIntegerList().get(1)); - // assertEquals(new Integer(3), bean.getIntegerList().get(2)); - // } - // - // public void testArrayConversionOGNL() { - // DefaultConversionService service = new DefaultConversionService(); - // Map map = new HashMap(); - // map.put("integerArray", new Integer[] { 1, 2, 3 }); - // map.put("string", "1"); - // TestBean bean = new TestBean(); - // OgnlExpressionParser parser = new OgnlExpressionParser(); - // Expression stringArray = parser.parseExpression("integerArray", null); - // Expression integerList = parser.parseExpression("primitiveArray", null); - // DefaultMappingContext context = new DefaultMappingContext(map, bean, service); - // DefaultMapping mapping = new DefaultMapping(stringArray, integerList); - // mapping.map(context); - // } - // - // public void testArrayConversionEL() { - // DefaultConversionService service = new DefaultConversionService(); - // Map map = new HashMap(); - // map.put("stringArray", new String[] { "1", "2", "3" }); - // map.put("string", "1"); - // TestBean bean = new TestBean(); - // ELExpressionParser parser = new ELExpressionParser(new ExpressionFactoryImpl()); - // Expression stringArray = parser.parseExpression("stringArray", null); - // Expression integerList = parser.parseExpression("primitiveArray", null); - // DefaultMappingContext context = new DefaultMappingContext(map, bean, service); - // DefaultMapping mapping = new DefaultMapping(stringArray, integerList); - // mapping.map(context); - // } - // - // public static class CollectionWrapperBean { - // - // private Collection collection; - // - // private Map map; - // - // public CollectionWrapperBean(Map map) { - // this.map = map; - // } - // - // public CollectionWrapperBean(Collection collection) { - // this.collection = collection; - // } - // - // public Collection getCollection() { - // return collection; - // } - // - // public Map getMap() { - // return map; - // } - // } - // - // public static class TestBean { - // - // private List integerList; - // - // private int primitive; - // - // private int[] primitiveArray; - // - // private List authorityList; - // - // private List> nested; - // - // public TestBean() { - // nested = new ArrayList>(); - // nested.add(new HashMap()); - // nested.get(0).put(0, new Authority("bubba")); - // } - // - // public List getIntegerList() { - // return integerList; - // } - // - // public void setIntegerList(List integerList) { - // this.integerList = integerList; - // } - // - // public int getPrimitive() { - // return primitive; - // } - // - // public void setPrimitive(int primitive) { - // this.primitive = primitive; - // } - // - // public int[] getPrimitiveArray() { - // return primitiveArray; - // } - // - // public void setPrimitiveArray(int[] primitiveArray) { - // this.primitiveArray = primitiveArray; - // } - // - // public List getAuthorityList() { - // return authorityList; - // } - // - // public void setAuthorityList(List authorityList) { - // this.authorityList = authorityList; - // } - // - // // nested[0][1] - // public List> getNested() { - // return nested; - // } - // - // public void setNested(List> nested) { - // this.nested = nested; - // } - // - // } - // - // public static class Authority { - // private String name; - // - // public Authority(String name) { - // this.name = name; - // } - // - // public String getName() { - // return name; - // } - // - // public boolean equals(Object o) { - // if (!(o instanceof Authority)) { - // return false; - // } - // Authority auth = (Authority) o; - // return name.equals(auth.name); - // } - // - // public int hashCode() { - // return name.hashCode(); - // } - // - // public String toString() { - // return name; - // } - // } - // - // public static class StringToAuthority extends StringToObject { - // - // public StringToAuthority() { - // super(Authority.class); - // } - // - // protected Object toObject(String string, Class targetClass) throws Exception { - // return new Authority(string); - // } - // - // protected String toString(Object object) throws Exception { - // return object.toString(); - // } - // - // } + private static class Trimmer implements Converter { - // public void testArrayListConversionWithElementConversion() throws Exception { - // DefaultConversionService service = new DefaultConversionService(); - // ConversionExecutor executor = service.getConversionExecutor(String[].class, IntegerArrayList.class); - // List result = (List) executor.execute(new String[] { "1", "2", "3" }); - // assertEquals(new Integer(1), result.get(0)); - // assertEquals(new Integer(2), result.get(1)); - // assertEquals(new Integer(3), result.get(2)); - // } - // - // public static class IntegerArrayList implements List { - // - // private ArrayList realList = new ArrayList(); - // - // public IntegerArrayList() { - // } - // - // public void add(int index, Integer element) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public boolean add(Integer o) { - // return realList.add(o); - // } - // - // public boolean addAll(Collection c) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public boolean addAll(int index, Collection c) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public void clear() { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public boolean contains(Object o) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public boolean containsAll(Collection c) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public Integer get(int index) { - // return (Integer) realList.get(index); - // } - // - // public int indexOf(Object o) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public boolean isEmpty() { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public Iterator iterator() { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public int lastIndexOf(Object o) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public ListIterator listIterator() { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public ListIterator listIterator(int index) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public Integer remove(int index) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public boolean remove(Object o) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public boolean removeAll(Collection c) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public boolean retainAll(Collection c) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public Integer set(int index, Integer element) { - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public int size() { // TODO Auto-generated method stu - // throw new UnsupportedOperationException("Auto-generatedmethod stub"); - // } - // - // public List subList(int fromIndex, int toIndex) { // TODO Auto-generated method stub throw new - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public Object[] toArray() { // TODO Auto-generated method stub throw new - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // - // public T[] toArray(T[] a) { // TODO Auto-generated method stub throw new - // throw new UnsupportedOperationException("Auto-generated method stub"); - // } - // } + public Object convertSourceToTargetClass(Object source, Class targetClass) throws Exception { + return ((String) source).trim(); + } + + public Class getSourceClass() { + return String.class; + } + + public Class getTargetClass() { + return String.class; + } + + } } \ No newline at end of file diff --git a/spring-binding/src/test/java/org/springframework/binding/convert/service/StaticConversionExecutorImplTests.java b/spring-binding/src/test/java/org/springframework/binding/convert/service/StaticConversionExecutorImplTests.java index aca83925..61db4646 100644 --- a/spring-binding/src/test/java/org/springframework/binding/convert/service/StaticConversionExecutorImplTests.java +++ b/spring-binding/src/test/java/org/springframework/binding/convert/service/StaticConversionExecutorImplTests.java @@ -37,7 +37,12 @@ public class StaticConversionExecutorImplTests extends TestCase { public void testAssignmentCompatibleTypeConversion() { java.sql.Date date = new java.sql.Date(123L); - assertSame(date, conversionExecutor.execute(date)); + try { + assertSame(date, conversionExecutor.execute(date)); + fail("Should have failed"); + } catch (ConversionExecutionException e) { + + } } public void testConvertNull() {