support for transfomer custom converters
This commit is contained in:
@@ -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<Integer> integerList;
|
||||
//
|
||||
// private int primitive;
|
||||
//
|
||||
// private int[] primitiveArray;
|
||||
//
|
||||
// private List<Authority> authorityList;
|
||||
//
|
||||
// private List<Map<Integer, Authority>> nested;
|
||||
//
|
||||
// public TestBean() {
|
||||
// nested = new ArrayList<Map<Integer, Authority>>();
|
||||
// nested.add(new HashMap<Integer, Authority>());
|
||||
// nested.get(0).put(0, new Authority("bubba"));
|
||||
// }
|
||||
//
|
||||
// public List<Integer> getIntegerList() {
|
||||
// return integerList;
|
||||
// }
|
||||
//
|
||||
// public void setIntegerList(List<Integer> 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<Authority> getAuthorityList() {
|
||||
// return authorityList;
|
||||
// }
|
||||
//
|
||||
// public void setAuthorityList(List<Authority> authorityList) {
|
||||
// this.authorityList = authorityList;
|
||||
// }
|
||||
//
|
||||
// // nested[0][1]
|
||||
// public List<Map<Integer, Authority>> getNested() {
|
||||
// return nested;
|
||||
// }
|
||||
//
|
||||
// public void setNested(List<Map<Integer, Authority>> 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<Integer> {
|
||||
//
|
||||
// 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<? extends Integer> c) {
|
||||
// throw new UnsupportedOperationException("Auto-generated method stub");
|
||||
// }
|
||||
//
|
||||
// public boolean addAll(int index, Collection<? extends Integer> 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<Integer> iterator() {
|
||||
// throw new UnsupportedOperationException("Auto-generated method stub");
|
||||
// }
|
||||
//
|
||||
// public int lastIndexOf(Object o) {
|
||||
// throw new UnsupportedOperationException("Auto-generated method stub");
|
||||
// }
|
||||
//
|
||||
// public ListIterator<Integer> listIterator() {
|
||||
// throw new UnsupportedOperationException("Auto-generated method stub");
|
||||
// }
|
||||
//
|
||||
// public ListIterator<Integer> 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<Integer> 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> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user