提交 ffbfa299 编写于 作者: K Keith Donald

polish

上级 0a78287a
...@@ -82,7 +82,7 @@ public class FormattingConversionService implements FormatterRegistry, Conversio ...@@ -82,7 +82,7 @@ public class FormattingConversionService implements FormatterRegistry, Conversio
+ "]; does the factory parameterize the <A extends Annotation> generic type?"); + "]; does the factory parameterize the <A extends Annotation> generic type?");
} }
Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes(); Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes();
for (Class<?> fieldType : fieldTypes) { for (final Class<?> fieldType : fieldTypes) {
this.conversionService.addGenericConverter(fieldType, String.class, new ConditionalGenericConverter() { this.conversionService.addGenericConverter(fieldType, String.class, new ConditionalGenericConverter() {
public boolean matches(TypeDescriptor sourceFieldType, TypeDescriptor targetFieldType) { public boolean matches(TypeDescriptor sourceFieldType, TypeDescriptor targetFieldType) {
return sourceFieldType.getAnnotation(annotationType) != null; return sourceFieldType.getAnnotation(annotationType) != null;
...@@ -90,7 +90,10 @@ public class FormattingConversionService implements FormatterRegistry, Conversio ...@@ -90,7 +90,10 @@ public class FormattingConversionService implements FormatterRegistry, Conversio
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
Printer<?> printer = annotationFormatterFactory.getPrinter(sourceType.getAnnotation(annotationType), targetType.getType()); Printer<?> printer = annotationFormatterFactory.getPrinter(sourceType.getAnnotation(annotationType), targetType.getType());
return new PrinterConverter(printer, conversionService).convert(source, sourceType, targetType); return new PrinterConverter(printer, conversionService).convert(source, sourceType, targetType);
} }
public String toString() {
return "@" + annotationType.getName() + " " + fieldType.getName() + " -> " + String.class.getName();
}
}); });
this.conversionService.addGenericConverter(String.class, fieldType, new ConditionalGenericConverter() { this.conversionService.addGenericConverter(String.class, fieldType, new ConditionalGenericConverter() {
public boolean matches(TypeDescriptor sourceFieldType, TypeDescriptor targetFieldType) { public boolean matches(TypeDescriptor sourceFieldType, TypeDescriptor targetFieldType) {
...@@ -99,6 +102,9 @@ public class FormattingConversionService implements FormatterRegistry, Conversio ...@@ -99,6 +102,9 @@ public class FormattingConversionService implements FormatterRegistry, Conversio
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
Parser<?> parser = annotationFormatterFactory.getParser(targetType.getAnnotation(annotationType), targetType.getType()); Parser<?> parser = annotationFormatterFactory.getParser(targetType.getAnnotation(annotationType), targetType.getType());
return new ParserConverter(parser, conversionService).convert(source, sourceType, targetType); return new ParserConverter(parser, conversionService).convert(source, sourceType, targetType);
}
public String toString() {
return String.class.getName() + " -> @" + annotationType.getName() + " " + fieldType.getName();
} }
}); });
} }
...@@ -127,6 +133,10 @@ public class FormattingConversionService implements FormatterRegistry, Conversio ...@@ -127,6 +133,10 @@ public class FormattingConversionService implements FormatterRegistry, Conversio
return this.conversionService.convert(source, sourceType, targetType); return this.conversionService.convert(source, sourceType, targetType);
} }
public String toString() {
return this.conversionService.toString();
}
// internal helpers // internal helpers
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
......
...@@ -41,7 +41,7 @@ import org.springframework.ui.format.number.IntegerFormatter; ...@@ -41,7 +41,7 @@ import org.springframework.ui.format.number.IntegerFormatter;
* @author Keith Donald * @author Keith Donald
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
public class GenericFormattingServiceTests { public class FormattingConversionServiceTests {
private FormattingConversionService formattingService; private FormattingConversionService formattingService;
...@@ -93,6 +93,7 @@ public class GenericFormattingServiceTests { ...@@ -93,6 +93,7 @@ public class GenericFormattingServiceTests {
} }
}); });
formattingService.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); formattingService.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
System.out.println(this.formattingService);
String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime() String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime()
.toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.valueOf(String.class)); .toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.valueOf(String.class));
assertEquals("10/31/09", formatted); assertEquals("10/31/09", formatted);
......
...@@ -15,23 +15,13 @@ ...@@ -15,23 +15,13 @@
*/ */
package org.springframework.core.convert.support; package org.springframework.core.convert.support;
import org.springframework.core.convert.TypeDescriptor;
/** /**
* A generic converter that conditionally executes. * A generic converter that, as a ConverterMatcher, conditionally executes.
* Often used when selectively matching custom conversion logic based on the presence of a field or class-level annotation. * Often used when selectively matching custom conversion logic based on the presence of a field or class-level annotation.
* For example, when converting from a String to a Date field, an implementation might return true if the target field has also been annotated with <code>@DateTimeFormat</code>. * For example, when converting from a String to a Date field, an implementation might return true if the target field has also been annotated with <code>@DateTimeFormat</code>.
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
public interface ConditionalGenericConverter extends GenericConverter { public interface ConditionalGenericConverter extends GenericConverter, ConverterMatcher {
/**
* Should the conversion between <code>sourceFieldType</code> and <code>targetFieldType</code> be performed?
* @param sourceFieldType the type descriptor of the field we are converting from
* @param targetFieldType the type descriptor of the field we are converting to
* @return true if conversion should be performed, false otherwise
*/
boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType);
} }
\ No newline at end of file
/*
* 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 org.springframework.core.convert.TypeDescriptor;
/**
* A rule that determines if a particular Converter of S to T matches given a request to convert between a field of type S and a field of type T.
* Often used to selectively apply custom type conversion logic based on the presence of a field annotation.
* For example, when converting from a String to a Date field, an implementation might return true only if the target Date field has also been annotated with <code>@DateTimeFormat</code>.
* @author Keith Donald
* @since 3.0
*/
public interface ConverterMatcher {
/**
* Should the Converter from <code>sourceType</code> to <code>targetType</code> currently under consideration be selected?
* @param sourceType the type descriptor of the field we are converting from
* @param targetType the type descriptor of the field we are converting to
* @return true if conversion should be performed, false otherwise
*/
boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType);
}
...@@ -16,7 +16,9 @@ ...@@ -16,7 +16,9 @@
package org.springframework.core.convert.support; package org.springframework.core.convert.support;
import java.util.Collection;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
/** /**
* Default implementation of a conversion service. Will automatically register <i>from string</i> * Default implementation of a conversion service. Will automatically register <i>from string</i>
...@@ -32,6 +34,21 @@ public class DefaultConversionService extends GenericConversionService { ...@@ -32,6 +34,21 @@ public class DefaultConversionService extends GenericConversionService {
* Create a new default conversion service, installing the default converters. * Create a new default conversion service, installing the default converters.
*/ */
public DefaultConversionService() { public DefaultConversionService() {
addGenericConverter(Object[].class, Object[].class, new ArrayToArrayConverter(this));
addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter(this));
addGenericConverter(Object[].class, Map.class, new ArrayToMapConverter(this));
addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(this));
addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter(this));
addGenericConverter(Collection.class, Object[].class, new CollectionToArrayConverter(this));
addGenericConverter(Collection.class, Map.class, new CollectionToMapConverter(this));
addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter(this));
addGenericConverter(Map.class, Map.class, new MapToMapConverter(this));
addGenericConverter(Map.class, Object[].class, new MapToArrayConverter(this));
addGenericConverter(Map.class, Collection.class, new MapToCollectionConverter(this));
addGenericConverter(Map.class, Object.class, new MapToObjectConverter(this));
addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(this));
addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(this));
addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(this));
addConverter(String.class, Boolean.class, new StringToBooleanConverter()); addConverter(String.class, Boolean.class, new StringToBooleanConverter());
addConverter(String.class, Character.class, new StringToCharacterConverter()); addConverter(String.class, Character.class, new StringToCharacterConverter());
addConverter(String.class, Locale.class, new StringToLocaleConverter()); addConverter(String.class, Locale.class, new StringToLocaleConverter());
......
...@@ -61,28 +61,6 @@ public class GenericConversionService implements ConversionService, ConverterReg ...@@ -61,28 +61,6 @@ public class GenericConversionService implements ConversionService, ConverterReg
} }
}; };
/**
* Create a new GenericConversionService.
* Generic converters for Collection types are registered.
*/
public GenericConversionService() {
addGenericConverter(Object[].class, Object[].class, new ArrayToArrayConverter(this));
addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter(this));
addGenericConverter(Object[].class, Map.class, new ArrayToMapConverter(this));
addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(this));
addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter(this));
addGenericConverter(Collection.class, Object[].class, new CollectionToArrayConverter(this));
addGenericConverter(Collection.class, Map.class, new CollectionToMapConverter(this));
addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter(this));
addGenericConverter(Map.class, Map.class, new MapToMapConverter(this));
addGenericConverter(Map.class, Object[].class, new MapToArrayConverter(this));
addGenericConverter(Map.class, Collection.class, new MapToCollectionConverter(this));
addGenericConverter(Map.class, Object.class, new MapToObjectConverter(this));
addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(this));
addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(this));
addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(this));
}
/** /**
* Registers the converters in the set provided. * Registers the converters in the set provided.
* JavaBean-friendly alternative to calling {@link #addConverter(Converter)}. * JavaBean-friendly alternative to calling {@link #addConverter(Converter)}.
...@@ -191,7 +169,18 @@ public class GenericConversionService implements ConversionService, ConverterReg ...@@ -191,7 +169,18 @@ public class GenericConversionService implements ConversionService, ConverterReg
public void addGenericConverter(Class<?> sourceType, Class<?> targetType, GenericConverter converter) { public void addGenericConverter(Class<?> sourceType, Class<?> targetType, GenericConverter converter) {
getMatchableConvertersList(sourceType, targetType).add(converter); getMatchableConvertersList(sourceType, targetType).add(converter);
} }
/**
* Registers a GenericConverter for the source/target type pair that will only be matched if the provided matcher returns true.
* @param sourceType the source type to convert from
* @param targetType the target type to convert to
* @param matcher a matcher can restrict a match of the converter based on source and target runtime field types
* @param converter the generic converter.
*/
public void addGenericConverter(Class<?> sourceType, Class<?> targetType, GenericConverter converter, ConverterMatcher matcher) {
getMatchableConvertersList(sourceType, targetType).add(matcher, converter);
}
/** /**
* Registers a Converter with the sourceType and targetType to index on specified explicitly. * Registers a Converter with the sourceType and targetType to index on specified explicitly.
* This method performs better than {@link #addConverter(Converter)} because there parameterized types S and T don't have to be discovered. * This method performs better than {@link #addConverter(Converter)} because there parameterized types S and T don't have to be discovered.
...@@ -214,6 +203,22 @@ public class GenericConversionService implements ConversionService, ConverterReg ...@@ -214,6 +203,22 @@ public class GenericConversionService implements ConversionService, ConverterReg
addGenericConverter(sourceType, targetType, new ConverterFactoryAdapter(converterFactory)); addGenericConverter(sourceType, targetType, new ConverterFactoryAdapter(converterFactory));
} }
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ConversionService converters = ").append("\n");
for (Map<Class<?>, MatchableConverters> targetConverters : this.converters.values()) {
for (MatchableConverters matchable : targetConverters.values()) {
builder.append("\t");
builder.append(matchable);
builder.append("\n");
}
}
if (this.parent != null) {
builder.append("parent = ").append(this.parent);
}
return builder.toString();
}
// subclassing hooks // subclassing hooks
/** /**
...@@ -402,7 +407,8 @@ public class GenericConversionService implements ConversionService, ConverterReg ...@@ -402,7 +407,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
} }
} }
private GenericConverter matchConverter(MatchableConverters matchable, TypeDescriptor sourceFieldType, TypeDescriptor targetFieldType) { private GenericConverter matchConverter(MatchableConverters matchable, TypeDescriptor sourceFieldType,
TypeDescriptor targetFieldType) {
return matchable != null ? matchable.matchConverter(sourceFieldType, targetFieldType) : null; return matchable != null ? matchable.matchConverter(sourceFieldType, targetFieldType) : null;
} }
...@@ -421,6 +427,10 @@ public class GenericConversionService implements ConversionService, ConverterReg ...@@ -421,6 +427,10 @@ public class GenericConversionService implements ConversionService, ConverterReg
} }
return this.converter.convert(source); return this.converter.convert(source);
} }
public String toString() {
return this.converter.toString();
}
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
...@@ -438,29 +448,97 @@ public class GenericConversionService implements ConversionService, ConverterReg ...@@ -438,29 +448,97 @@ public class GenericConversionService implements ConversionService, ConverterReg
} }
return this.converterFactory.getConverter(targetType.getObjectType()).convert(source); return this.converterFactory.getConverter(targetType.getObjectType()).convert(source);
} }
public String toString() {
return this.converterFactory.toString();
}
} }
private static class MatchableConverters { private static class MatchableConverters {
private LinkedList<GenericConverter> matchableConverters = new LinkedList<GenericConverter>(); private static final ConverterMatcher ALWAYS_MATCHES = new ConverterMatcher() {
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
};
private LinkedList<MatchableConverter> matchableConverters = new LinkedList<MatchableConverter>();
public void add(GenericConverter converter) { public void add(GenericConverter converter) {
this.matchableConverters.addFirst(converter); if (converter instanceof ConverterMatcher) {
add((ConverterMatcher) converter, converter);
} else {
add(ALWAYS_MATCHES, converter);
}
}
public void add(ConverterMatcher matcher, GenericConverter converter) {
MatchableConverter matchable = new MatchableConverter(matcher, converter);
int index = this.matchableConverters.indexOf(matchable);
if (index == -1) {
this.matchableConverters.addFirst(new MatchableConverter(matcher, converter));
} else {
this.matchableConverters.set(index, matchable);
}
} }
public GenericConverter matchConverter(TypeDescriptor sourceType, TypeDescriptor targetType) { public GenericConverter matchConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
for (GenericConverter matchable : this.matchableConverters) { for (MatchableConverter matchable : this.matchableConverters) {
if (!(matchable instanceof ConditionalGenericConverter)) { if (matchable.matches(sourceType, targetType)) {
return matchable; return matchable.getConverter();
}
ConditionalGenericConverter conditional = (ConditionalGenericConverter) matchable;
if (conditional.matches(sourceType, targetType)) {
return matchable;
} }
} }
return null; return null;
} }
public String toString() {
if (this.matchableConverters.size() == 1) {
return this.matchableConverters.get(0).toString();
} else {
return "[MatchableConverters = " + this.matchableConverters + "]";
}
}
private static class MatchableConverter {
private ConverterMatcher matcher;
private GenericConverter converter;
public MatchableConverter(ConverterMatcher matcher, GenericConverter converter) {
this.matcher = matcher;
this.converter = converter;
}
public GenericConverter getConverter() {
return this.converter;
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.matcher.matches(sourceType, targetType);
}
public int hashCode() {
return this.matcher.hashCode();
}
public boolean equals(Object o) {
if (!(o instanceof MatchableConverter)) {
return false;
}
MatchableConverter matchable = (MatchableConverter) o;
return this.matcher.equals(matchable.matcher);
}
public String toString() {
if (matcher == ALWAYS_MATCHES || matcher == converter) {
return this.converter.toString();
} else {
return "if (" + this.matcher + ") " + this.converter;
}
}
}
} }
} }
\ No newline at end of file
...@@ -157,6 +157,8 @@ public class GenericConversionServiceTests { ...@@ -157,6 +157,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToPrimitiveArray() { public void convertArrayToPrimitiveArray() {
conversionService.addGenericConverter(Object[].class, Object[].class, new ArrayToArrayConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
int[] result = conversionService.convert(new String[] { "1", "2", "3" }, int[].class); int[] result = conversionService.convert(new String[] { "1", "2", "3" }, int[].class);
assertEquals(1, result[0]); assertEquals(1, result[0]);
...@@ -174,6 +176,8 @@ public class GenericConversionServiceTests { ...@@ -174,6 +176,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToListInterface() { public void convertArrayToListInterface() {
conversionService.addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter(
conversionService));
List<?> result = conversionService.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("1", result.get(0));
assertEquals("2", result.get(1)); assertEquals("2", result.get(1));
...@@ -184,6 +188,8 @@ public class GenericConversionServiceTests { ...@@ -184,6 +188,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToListGenericTypeConversion() throws Exception { public void convertArrayToListGenericTypeConversion() throws Exception {
conversionService.addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList"))); .valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
...@@ -194,6 +200,8 @@ public class GenericConversionServiceTests { ...@@ -194,6 +200,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToListImpl() { public void convertArrayToListImpl() {
conversionService.addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter(
conversionService));
LinkedList<?> result = conversionService.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("1", result.get(0));
assertEquals("2", result.get(1)); assertEquals("2", result.get(1));
...@@ -202,17 +210,43 @@ public class GenericConversionServiceTests { ...@@ -202,17 +210,43 @@ public class GenericConversionServiceTests {
@Test(expected = ConversionFailedException.class) @Test(expected = ConversionFailedException.class)
public void convertArrayToAbstractList() { public void convertArrayToAbstractList() {
conversionService.addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter(
conversionService));
conversionService.convert(new String[] { "1", "2", "3" }, AbstractList.class); conversionService.convert(new String[] { "1", "2", "3" }, AbstractList.class);
} }
@Test
public void convertArrayToMap() {
conversionService.addGenericConverter(Object[].class, Map.class, new ArrayToMapConverter(conversionService));
Map result = conversionService.convert(new String[] { "foo=bar", "bar=baz", "baz=boop" }, Map.class);
assertEquals("bar", result.get("foo"));
assertEquals("baz", result.get("bar"));
assertEquals("boop", result.get("baz"));
}
@Test
public void convertArrayToMapWithElementConversion() throws Exception {
conversionService.addGenericConverter(Object[].class, Map.class, new ArrayToMapConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverterFactory(new StringToEnumConverterFactory());
Map result = (Map) conversionService.convert(new String[] { "1=BAR", "2=BAZ" }, TypeDescriptor
.valueOf(String[].class), new TypeDescriptor(getClass().getField("genericMap")));
assertEquals(FooEnum.BAR, result.get(1));
assertEquals(FooEnum.BAZ, result.get(2));
}
@Test @Test
public void convertArrayToString() { public void convertArrayToString() {
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(
conversionService));
String result = conversionService.convert(new String[] { "1", "2", "3" }, String.class); String result = conversionService.convert(new String[] { "1", "2", "3" }, String.class);
assertEquals("1,2,3", result); assertEquals("1,2,3", result);
} }
@Test @Test
public void convertArrayToStringWithElementConversion() { public void convertArrayToStringWithElementConversion() {
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(
conversionService));
conversionService.addConverter(new ObjectToStringConverter()); conversionService.addConverter(new ObjectToStringConverter());
String result = conversionService.convert(new Integer[] { 1, 2, 3 }, String.class); String result = conversionService.convert(new Integer[] { 1, 2, 3 }, String.class);
assertEquals("1,2,3", result); assertEquals("1,2,3", result);
...@@ -220,12 +254,16 @@ public class GenericConversionServiceTests { ...@@ -220,12 +254,16 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertEmptyArrayToString() { public void convertEmptyArrayToString() {
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(
conversionService));
String result = conversionService.convert(new String[0], String.class); String result = conversionService.convert(new String[0], String.class);
assertEquals("", result); assertEquals("", result);
} }
@Test @Test
public void convertArrayToObject() { public void convertArrayToObject() {
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(
conversionService));
Object[] array = new Object[] { 3L }; Object[] array = new Object[] { 3L };
Object result = conversionService.convert(array, Object.class); Object result = conversionService.convert(array, Object.class);
assertEquals(3L, result); assertEquals(3L, result);
...@@ -233,6 +271,8 @@ public class GenericConversionServiceTests { ...@@ -233,6 +271,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToObjectWithElementConversion() { public void convertArrayToObjectWithElementConversion() {
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
String[] array = new String[] { "3" }; String[] array = new String[] { "3" };
Integer result = conversionService.convert(array, Integer.class); Integer result = conversionService.convert(array, Integer.class);
...@@ -241,6 +281,8 @@ public class GenericConversionServiceTests { ...@@ -241,6 +281,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertCollectionToArray() { public void convertCollectionToArray() {
conversionService.addGenericConverter(Collection.class, Object[].class, new CollectionToArrayConverter(
conversionService));
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
list.add("1"); list.add("1");
list.add("2"); list.add("2");
...@@ -253,6 +295,8 @@ public class GenericConversionServiceTests { ...@@ -253,6 +295,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertCollectionToArrayWithElementConversion() { public void convertCollectionToArrayWithElementConversion() {
conversionService.addGenericConverter(Collection.class, Object[].class, new CollectionToArrayConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
list.add("1"); list.add("1");
...@@ -266,6 +310,8 @@ public class GenericConversionServiceTests { ...@@ -266,6 +310,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertCollectionToCollection() throws Exception { public void convertCollectionToCollection() throws Exception {
conversionService.addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
Set<String> foo = new LinkedHashSet<String>(); Set<String> foo = new LinkedHashSet<String>();
foo.add("1"); foo.add("1");
...@@ -280,6 +326,8 @@ public class GenericConversionServiceTests { ...@@ -280,6 +326,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertCollectionToCollectionNull() throws Exception { public void convertCollectionToCollectionNull() throws Exception {
conversionService.addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter(
conversionService));
List<Integer> bar = (List<Integer>) conversionService.convert(null, List<Integer> bar = (List<Integer>) conversionService.convert(null,
TypeDescriptor.valueOf(LinkedHashSet.class), new TypeDescriptor(getClass().getField("genericList"))); TypeDescriptor.valueOf(LinkedHashSet.class), new TypeDescriptor(getClass().getField("genericList")));
assertNull(bar); assertNull(bar);
...@@ -287,6 +335,8 @@ public class GenericConversionServiceTests { ...@@ -287,6 +335,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertCollectionToCollectionNotGeneric() throws Exception { public void convertCollectionToCollectionNotGeneric() throws Exception {
conversionService.addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
Set<String> foo = new LinkedHashSet<String>(); Set<String> foo = new LinkedHashSet<String>();
foo.add("1"); foo.add("1");
...@@ -301,6 +351,8 @@ public class GenericConversionServiceTests { ...@@ -301,6 +351,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertCollectionToCollectionSpecialCaseSourceImpl() throws Exception { public void convertCollectionToCollectionSpecialCaseSourceImpl() throws Exception {
conversionService.addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
Map map = new LinkedHashMap(); Map map = new LinkedHashMap();
map.put("1", "1"); map.put("1", "1");
...@@ -315,8 +367,39 @@ public class GenericConversionServiceTests { ...@@ -315,8 +367,39 @@ public class GenericConversionServiceTests {
assertEquals(new Integer(3), bar.get(2)); assertEquals(new Integer(3), bar.get(2));
} }
@Test
public void convertCollectionToMap() {
conversionService.addGenericConverter(Collection.class, Map.class, new CollectionToMapConverter(
conversionService));
List<String> list = new ArrayList<String>();
list.add("foo=bar");
list.add("bar=baz");
list.add("baz=boop");
Map result = conversionService.convert(list, Map.class);
assertEquals("bar", result.get("foo"));
assertEquals("baz", result.get("bar"));
assertEquals("boop", result.get("baz"));
}
@Test
public void convertCollectionToMapWithElementConversion() throws Exception {
conversionService.addGenericConverter(Collection.class, Map.class, new CollectionToMapConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverterFactory(new StringToEnumConverterFactory());
List<String> list = new ArrayList<String>();
list.add("1=BAR");
list.add("2=BAZ");
Map result = (Map) conversionService.convert(list, TypeDescriptor.valueOf(List.class), new TypeDescriptor(
getClass().getField("genericMap")));
assertEquals(FooEnum.BAR, result.get(1));
assertEquals(FooEnum.BAZ, result.get(2));
}
@Test @Test
public void convertCollectionToString() { public void convertCollectionToString() {
conversionService.addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter(
conversionService));
List<String> list = Arrays.asList(new String[] { "foo", "bar" }); List<String> list = Arrays.asList(new String[] { "foo", "bar" });
String result = conversionService.convert(list, String.class); String result = conversionService.convert(list, String.class);
assertEquals("foo,bar", result); assertEquals("foo,bar", result);
...@@ -324,6 +407,8 @@ public class GenericConversionServiceTests { ...@@ -324,6 +407,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertCollectionToStringWithElementConversion() throws Exception { public void convertCollectionToStringWithElementConversion() throws Exception {
conversionService.addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter(
conversionService));
conversionService.addConverter(new ObjectToStringConverter()); conversionService.addConverter(new ObjectToStringConverter());
List<Integer> list = Arrays.asList(new Integer[] { 3, 5 }); List<Integer> list = Arrays.asList(new Integer[] { 3, 5 });
String result = (String) conversionService.convert(list, String result = (String) conversionService.convert(list,
...@@ -333,6 +418,8 @@ public class GenericConversionServiceTests { ...@@ -333,6 +418,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertCollectionToObject() { public void convertCollectionToObject() {
conversionService.addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter(
conversionService));
List<Long> list = Collections.singletonList(3L); List<Long> list = Collections.singletonList(3L);
Long result = conversionService.convert(list, Long.class); Long result = conversionService.convert(list, Long.class);
assertEquals(new Long(3), result); assertEquals(new Long(3), result);
...@@ -340,6 +427,8 @@ public class GenericConversionServiceTests { ...@@ -340,6 +427,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertCollectionToObjectWithElementConversion() { public void convertCollectionToObjectWithElementConversion() {
conversionService.addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = Collections.singletonList("3"); List<String> list = Collections.singletonList("3");
Integer result = conversionService.convert(list, Integer.class); Integer result = conversionService.convert(list, Integer.class);
...@@ -350,11 +439,12 @@ public class GenericConversionServiceTests { ...@@ -350,11 +439,12 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertMapToMap() throws Exception { public void convertMapToMap() throws Exception {
conversionService.addGenericConverter(Map.class, Map.class, new MapToMapConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverterFactory(new StringToEnumConverterFactory());
Map<String, String> foo = new HashMap<String, String>(); Map<String, String> foo = new HashMap<String, String>();
foo.put("1", "BAR"); foo.put("1", "BAR");
foo.put("2", "BAZ"); foo.put("2", "BAZ");
conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverterFactory(new StringToEnumConverterFactory());
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, TypeDescriptor Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, TypeDescriptor
.valueOf(Map.class), new TypeDescriptor(getClass().getField("genericMap"))); .valueOf(Map.class), new TypeDescriptor(getClass().getField("genericMap")));
assertEquals(FooEnum.BAR, map.get(1)); assertEquals(FooEnum.BAR, map.get(1));
...@@ -363,10 +453,10 @@ public class GenericConversionServiceTests { ...@@ -363,10 +453,10 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertMapToStringArray() throws Exception { public void convertMapToStringArray() throws Exception {
conversionService.addGenericConverter(Map.class, Object[].class, new MapToArrayConverter(conversionService));
Map<String, String> foo = new LinkedHashMap<String, String>(); Map<String, String> foo = new LinkedHashMap<String, String>();
foo.put("1", "BAR"); foo.put("1", "BAR");
foo.put("2", "BAZ"); foo.put("2", "BAZ");
conversionService.addConverterFactory(new StringToNumberConverterFactory());
String[] result = conversionService.convert(foo, String[].class); String[] result = conversionService.convert(foo, String[].class);
assertEquals("1=BAR", result[0]); assertEquals("1=BAR", result[0]);
assertEquals("2=BAZ", result[1]); assertEquals("2=BAZ", result[1]);
...@@ -374,19 +464,67 @@ public class GenericConversionServiceTests { ...@@ -374,19 +464,67 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertMapToStringArrayWithElementConversion() throws Exception { public void convertMapToStringArrayWithElementConversion() throws Exception {
conversionService.addGenericConverter(Map.class, Object[].class, new MapToArrayConverter(conversionService));
conversionService.addConverter(new ObjectToStringConverter());
Map<Integer, FooEnum> foo = new LinkedHashMap<Integer, FooEnum>(); Map<Integer, FooEnum> foo = new LinkedHashMap<Integer, FooEnum>();
foo.put(1, FooEnum.BAR); foo.put(1, FooEnum.BAR);
foo.put(2, FooEnum.BAZ); foo.put(2, FooEnum.BAZ);
conversionService.addConverter(new ObjectToStringConverter());
String[] result = (String[]) conversionService.convert(foo, new TypeDescriptor(getClass() String[] result = (String[]) conversionService.convert(foo, new TypeDescriptor(getClass()
.getField("genericMap")), TypeDescriptor.valueOf(String[].class)); .getField("genericMap")), TypeDescriptor.valueOf(String[].class));
assertEquals("1=BAR", result[0]); assertEquals("1=BAR", result[0]);
assertEquals("2=BAZ", result[1]); assertEquals("2=BAZ", result[1]);
} }
@Test
public void convertMapToString() {
conversionService.addGenericConverter(Map.class, Object.class, new MapToObjectConverter(conversionService));
Map<String, String> foo = new LinkedHashMap<String, String>();
foo.put("1", "BAR");
foo.put("2", "BAZ");
String result = conversionService.convert(foo, String.class);
assertEquals("1=BAR 2=BAZ", result);
}
@Test
public void convertMapToStringWithConversion() throws Exception {
conversionService.addGenericConverter(Map.class, Object.class, new MapToObjectConverter(conversionService));
Map<Integer, FooEnum> foo = new LinkedHashMap<Integer, FooEnum>();
foo.put(1, FooEnum.BAR);
foo.put(2, FooEnum.BAZ);
conversionService.addConverter(new ObjectToStringConverter());
String result = (String) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericMap")),
TypeDescriptor.valueOf(String.class));
assertEquals("1=BAR 2=BAZ", result);
}
@Test
public void convertMapToObject() {
conversionService.addGenericConverter(Map.class, Object.class, new MapToObjectConverter(conversionService));
Map<Long, Long> foo = new LinkedHashMap<Long, Long>();
foo.put(1L, 1L);
foo.put(2L, 2L);
Long result = conversionService.convert(foo, Long.class);
assertEquals(new Long(1), result);
}
public Map<Long, Long> genericMap2 = new HashMap<Long, Long>();
@Test
public void convertMapToObjectWithConversion() throws Exception {
conversionService.addGenericConverter(Map.class, Object.class, new MapToObjectConverter(conversionService));
conversionService.addConverterFactory(new NumberToNumberConverterFactory());
Map<Long, Long> foo = new LinkedHashMap<Long, Long>();
foo.put(1L, 1L);
foo.put(2L, 2L);
Integer result = (Integer) conversionService.convert(foo,
new TypeDescriptor(getClass().getField("genericMap2")), TypeDescriptor.valueOf(Integer.class));
assertEquals(new Integer(1), result);
}
@Test @Test
public void convertStringToArray() { public void convertStringToArray() {
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(
conversionService));
String[] result = conversionService.convert("1,2,3", String[].class); String[] result = conversionService.convert("1,2,3", String[].class);
assertEquals(3, result.length); assertEquals(3, result.length);
assertEquals("1", result[0]); assertEquals("1", result[0]);
...@@ -396,6 +534,8 @@ public class GenericConversionServiceTests { ...@@ -396,6 +534,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertStringToArrayWithElementConversion() { public void convertStringToArrayWithElementConversion() {
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
Integer[] result = conversionService.convert("1,2,3", Integer[].class); Integer[] result = conversionService.convert("1,2,3", Integer[].class);
assertEquals(3, result.length); assertEquals(3, result.length);
...@@ -406,14 +546,35 @@ public class GenericConversionServiceTests { ...@@ -406,14 +546,35 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertEmptyStringToArray() { public void convertEmptyStringToArray() {
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(
conversionService));
String[] result = conversionService.convert("", String[].class); String[] result = conversionService.convert("", String[].class);
assertEquals(0, result.length); assertEquals(0, result.length);
} }
@Test
public void convertObjectToArray() {
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(
conversionService));
Object[] result = conversionService.convert(3L, Object[].class);
assertEquals(1, result.length);
assertEquals(3L, result[0]);
}
@Test
public void convertObjectToArrayWithElementConversion() {
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(
conversionService));
conversionService.addConverterFactory(new NumberToNumberConverterFactory());
Integer[] result = conversionService.convert(3L, Integer[].class);
assertEquals(1, result.length);
assertEquals(new Integer(3), result[0]);
}
@Test @Test
public void convertStringToCollection() { public void convertStringToCollection() {
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(
conversionService));
List result = conversionService.convert("1,2,3", List.class); List result = conversionService.convert("1,2,3", List.class);
assertEquals(3, result.size()); assertEquals(3, result.size());
assertEquals("1", result.get(0)); assertEquals("1", result.get(0));
...@@ -423,6 +584,8 @@ public class GenericConversionServiceTests { ...@@ -423,6 +584,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertStringToCollectionWithElementConversion() throws Exception { public void convertStringToCollectionWithElementConversion() throws Exception {
conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(
conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class),
new TypeDescriptor(getClass().getField("genericList"))); new TypeDescriptor(getClass().getField("genericList")));
...@@ -434,13 +597,16 @@ public class GenericConversionServiceTests { ...@@ -434,13 +597,16 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertEmptyStringToCollection() { public void convertEmptyStringToCollection() {
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(
String[] result = conversionService.convert("", String[].class); conversionService));
assertEquals(0, result.length); Collection result = conversionService.convert("", Collection.class);
assertEquals(0, result.size());
} }
@Test @Test
public void convertObjectToCollection() { public void convertObjectToCollection() {
conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(
conversionService));
List<String> result = (List<String>) conversionService.convert(3L, List.class); List<String> result = (List<String>) conversionService.convert(3L, List.class);
assertEquals(1, result.size()); assertEquals(1, result.size());
assertEquals(3L, result.get(0)); assertEquals(3L, result.get(0));
...@@ -448,6 +614,8 @@ public class GenericConversionServiceTests { ...@@ -448,6 +614,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertObjectToCollectionWithElementConversion() throws Exception { public void convertObjectToCollectionWithElementConversion() throws Exception {
conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(
conversionService));
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); conversionService.addConverterFactory(new NumberToNumberConverterFactory());
List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class), List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class),
new TypeDescriptor(getClass().getField("genericList"))); new TypeDescriptor(getClass().getField("genericList")));
...@@ -455,56 +623,9 @@ public class GenericConversionServiceTests { ...@@ -455,56 +623,9 @@ public class GenericConversionServiceTests {
assertEquals(new Integer(3), result.get(0)); assertEquals(new Integer(3), result.get(0));
} }
@Test
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 convertObjectToMap() {
Map result = conversionService.convert("foo=bar bar=baz", Map.class);
assertEquals("bar", result.get("foo"));
assertEquals("baz", result.get("bar"));
}
@Test
public void convertObjectToMapWithConversion() throws Exception {
conversionService.addConverterFactory(new NumberToNumberConverterFactory());
Map result = (Map) conversionService.convert(1L, TypeDescriptor.valueOf(Integer.class), new TypeDescriptor(
getClass().getField("genericMap2")));
assertEquals(new Long(1), result.get(1L));
}
@Test
public void convertStringArrayToMap() {
Map result = conversionService.convert(new String[] { "foo=bar", "bar=baz", "baz=boop" }, Map.class);
assertEquals("bar", result.get("foo"));
assertEquals("baz", result.get("bar"));
assertEquals("boop", result.get("baz"));
}
@Test
public void convertStringArrayToMapWithElementConversion() throws Exception {
conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverterFactory(new StringToEnumConverterFactory());
Map result = (Map) conversionService.convert(new String[] { "1=BAR", "2=BAZ" }, TypeDescriptor
.valueOf(String[].class), new TypeDescriptor(getClass().getField("genericMap")));
assertEquals(FooEnum.BAR, result.get(1));
assertEquals(FooEnum.BAZ, result.get(2));
}
@Test @Test
public void convertStringToMap() { public void convertStringToMap() {
conversionService.addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(conversionService));
Map result = conversionService.convert("foo=bar bar=baz baz=boop", Map.class); Map result = conversionService.convert("foo=bar bar=baz baz=boop", Map.class);
assertEquals("bar", result.get("foo")); assertEquals("bar", result.get("foo"));
assertEquals("baz", result.get("bar")); assertEquals("baz", result.get("bar"));
...@@ -513,6 +634,7 @@ public class GenericConversionServiceTests { ...@@ -513,6 +634,7 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertStringToMapWithElementConversion() throws Exception { public void convertStringToMapWithElementConversion() throws Exception {
conversionService.addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverterFactory(new StringToEnumConverterFactory()); conversionService.addConverterFactory(new StringToEnumConverterFactory());
Map result = (Map) conversionService.convert("1=BAR 2=BAZ", TypeDescriptor.valueOf(String.class), Map result = (Map) conversionService.convert("1=BAR 2=BAZ", TypeDescriptor.valueOf(String.class),
...@@ -522,49 +644,26 @@ public class GenericConversionServiceTests { ...@@ -522,49 +644,26 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void convertMapToString() { public void convertObjectToMap() {
Map<String, String> foo = new LinkedHashMap<String, String>(); conversionService.addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(conversionService));
foo.put("1", "BAR"); Map result = conversionService.convert("foo=bar bar=baz", Map.class);
foo.put("2", "BAZ"); assertEquals("bar", result.get("foo"));
String result = conversionService.convert(foo, String.class); assertEquals("baz", result.get("bar"));
assertEquals("1=BAR 2=BAZ", result);
}
@Test
public void convertMapToStringWithConversion() throws Exception {
Map<Integer, FooEnum> foo = new LinkedHashMap<Integer, FooEnum>();
foo.put(1, FooEnum.BAR);
foo.put(2, FooEnum.BAZ);
conversionService.addConverter(new ObjectToStringConverter());
String result = (String) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericMap")),
TypeDescriptor.valueOf(String.class));
assertEquals("1=BAR 2=BAZ", result);
}
@Test
public void convertMapToObject() {
Map<Long, Long> foo = new LinkedHashMap<Long, Long>();
foo.put(1L, 1L);
foo.put(2L, 2L);
Long result = conversionService.convert(foo, Long.class);
assertEquals(new Long(1), result);
} }
public Map<Long, Long> genericMap2 = new HashMap<Long, Long>();
@Test @Test
public void convertMapToObjectWithConversion() throws Exception { public void convertObjectToMapWithConversion() throws Exception {
Map<Long, Long> foo = new LinkedHashMap<Long, Long>(); conversionService.addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(conversionService));
foo.put(1L, 1L);
foo.put(2L, 2L);
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); conversionService.addConverterFactory(new NumberToNumberConverterFactory());
Integer result = (Integer) conversionService.convert(foo, Map result = (Map) conversionService.convert(1L, TypeDescriptor.valueOf(Integer.class), new TypeDescriptor(
new TypeDescriptor(getClass().getField("genericMap2")), TypeDescriptor.valueOf(Integer.class)); getClass().getField("genericMap2")));
assertEquals(new Integer(1), result); assertEquals(new Long(1), result.get(1L));
} }
@Test @Test
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() { public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(
conversionService));
try { try {
conversionService.convert("1", Integer[].class); conversionService.convert("1", Integer[].class);
} catch (ConversionFailedException e) { } catch (ConversionFailedException e) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册