more generic converters
This commit is contained in:
parent
ed611be838
commit
e6c3227d2d
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
package org.springframework.core.convert.support;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
@ -6,16 +21,16 @@ import org.springframework.core.convert.ConversionFailedException;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
||||||
final class ConversionUtils {
|
final class ConversionUtils {
|
||||||
|
|
||||||
public static Object invokeConverter(GenericConverter converter, Object source, TypeDescriptor sourceType,
|
public static Object invokeConverter(GenericConverter converter, Object source, TypeDescriptor sourceType,
|
||||||
TypeDescriptor targetType) {
|
TypeDescriptor targetType) {
|
||||||
try {
|
try {
|
||||||
return converter.convert(source, sourceType, targetType);
|
return converter.convert(source, sourceType, targetType);
|
||||||
}
|
} catch (Exception ex) {
|
||||||
catch (Exception ex) {
|
|
||||||
throw new ConversionFailedException(sourceType, targetType, source, ex);
|
throw new ConversionFailedException(sourceType, targetType, source, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TypeDescriptor getElementType(Collection collection) {
|
public static TypeDescriptor getElementType(Collection collection) {
|
||||||
for (Object element : collection) {
|
for (Object element : collection) {
|
||||||
if (element != null) {
|
if (element != null) {
|
||||||
|
|
|
||||||
|
|
@ -184,14 +184,17 @@ public class GenericConversionService implements ConversionService, ConverterReg
|
||||||
protected void initGenericConverters() {
|
protected void initGenericConverters() {
|
||||||
addGenericConverter(Object[].class, Object[].class, new ArrayToArrayGenericConverter(this));
|
addGenericConverter(Object[].class, Object[].class, new ArrayToArrayGenericConverter(this));
|
||||||
addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionGenericConverter(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(Object[].class, Object.class, new ArrayToObjectGenericConverter(this));
|
||||||
addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionGenericConverter(this));
|
addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionGenericConverter(this));
|
||||||
addGenericConverter(Collection.class, Object[].class, new CollectionToArrayGenericConverter(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(Collection.class, Object.class, new CollectionToObjectGenericConverter(this));
|
||||||
addGenericConverter(Map.class, Map.class, new MapToMapGenericConverter(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, Object[].class, new ObjectToArrayGenericConverter(this));
|
||||||
addGenericConverter(Object.class, Collection.class, new ObjectToCollectionGenericConverter(this));
|
addGenericConverter(Object.class, Collection.class, new ObjectToCollectionGenericConverter(this));
|
||||||
addGenericConverter(String.class, Object[].class, new StringToArrayGenericConverter(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.AbstractList;
|
import java.util.AbstractList;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
|
@ -32,7 +33,6 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.core.convert.ConversionFailedException;
|
import org.springframework.core.convert.ConversionFailedException;
|
||||||
import org.springframework.core.convert.ConverterNotFoundException;
|
import org.springframework.core.convert.ConverterNotFoundException;
|
||||||
|
|
@ -44,28 +44,28 @@ import org.springframework.core.convert.converter.Converter;
|
||||||
*/
|
*/
|
||||||
public class GenericConversionServiceTests {
|
public class GenericConversionServiceTests {
|
||||||
|
|
||||||
private GenericConversionService converter = new GenericConversionService();
|
private GenericConversionService conversionService = new GenericConversionService();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void executeConversion() {
|
public void executeConversion() {
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
assertEquals(new Integer(3), converter.convert("3", Integer.class));
|
assertEquals(new Integer(3), conversionService.convert("3", Integer.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void executeConversionNullSource() {
|
public void executeConversionNullSource() {
|
||||||
assertEquals(null, converter.convert(null, Integer.class));
|
assertEquals(null, conversionService.convert(null, Integer.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void executeCompatibleSource() {
|
public void executeCompatibleSource() {
|
||||||
assertEquals(Boolean.FALSE, converter.convert(false, boolean.class));
|
assertEquals(Boolean.FALSE, conversionService.convert(false, boolean.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void converterNotFound() {
|
public void converterNotFound() {
|
||||||
try {
|
try {
|
||||||
converter.convert("3", Integer.class);
|
conversionService.convert("3", Integer.class);
|
||||||
fail("Should have thrown an exception");
|
fail("Should have thrown an exception");
|
||||||
} catch (ConverterNotFoundException e) {
|
} catch (ConverterNotFoundException e) {
|
||||||
}
|
}
|
||||||
|
|
@ -74,7 +74,7 @@ public class GenericConversionServiceTests {
|
||||||
@Test
|
@Test
|
||||||
public void addConverterNoSourceTargetClassInfoAvailable() {
|
public void addConverterNoSourceTargetClassInfoAvailable() {
|
||||||
try {
|
try {
|
||||||
converter.addConverter(new Converter() {
|
conversionService.addConverter(new Converter() {
|
||||||
public Object convert(Object source) {
|
public Object convert(Object source) {
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
@ -87,24 +87,24 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertNull() {
|
public void convertNull() {
|
||||||
assertNull(converter.convert(null, Integer.class));
|
assertNull(conversionService.convert(null, Integer.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void convertNullTargetClass() {
|
public void convertNullTargetClass() {
|
||||||
assertEquals("3", converter.convert("3", (Class<?>) null));
|
assertEquals("3", conversionService.convert("3", (Class<?>) null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertNullTypeDescriptor() {
|
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
|
@Test
|
||||||
public void convertWrongTypeArgument() {
|
public void convertWrongTypeArgument() {
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
try {
|
try {
|
||||||
converter.convert("BOGUS", Integer.class);
|
conversionService.convert("BOGUS", Integer.class);
|
||||||
fail("Should have failed");
|
fail("Should have failed");
|
||||||
} catch (ConversionFailedException e) {
|
} catch (ConversionFailedException e) {
|
||||||
|
|
||||||
|
|
@ -113,26 +113,26 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertSuperSourceType() {
|
public void convertSuperSourceType() {
|
||||||
converter.addConverter(new Converter<CharSequence, Integer>() {
|
conversionService.addConverter(new Converter<CharSequence, Integer>() {
|
||||||
public Integer convert(CharSequence source) {
|
public Integer convert(CharSequence source) {
|
||||||
return Integer.valueOf(source.toString());
|
return Integer.valueOf(source.toString());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Integer result = converter.convert("3", Integer.class);
|
Integer result = conversionService.convert("3", Integer.class);
|
||||||
assertEquals(new Integer(3), result);
|
assertEquals(new Integer(3), result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertObjectToPrimitive() {
|
public void convertObjectToPrimitive() {
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
Integer three = converter.convert("3", int.class);
|
Integer three = conversionService.convert("3", int.class);
|
||||||
assertEquals(3, three.intValue());
|
assertEquals(3, three.intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertArrayToArray() {
|
public void convertArrayToArray() {
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
Integer[] result = converter.convert(new String[] { "1", "2", "3" }, Integer[].class);
|
Integer[] result = conversionService.convert(new String[] { "1", "2", "3" }, Integer[].class);
|
||||||
assertEquals(new Integer(1), result[0]);
|
assertEquals(new Integer(1), result[0]);
|
||||||
assertEquals(new Integer(2), result[1]);
|
assertEquals(new Integer(2), result[1]);
|
||||||
assertEquals(new Integer(3), result[2]);
|
assertEquals(new Integer(3), result[2]);
|
||||||
|
|
@ -140,8 +140,8 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertArrayToPrimitiveArray() {
|
public void convertArrayToPrimitiveArray() {
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
int[] result = converter.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]);
|
||||||
assertEquals(2, result[1]);
|
assertEquals(2, result[1]);
|
||||||
assertEquals(3, result[2]);
|
assertEquals(3, result[2]);
|
||||||
|
|
@ -149,7 +149,7 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertArrayToArrayAssignable() {
|
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(1, result[0]);
|
||||||
assertEquals(2, result[1]);
|
assertEquals(2, result[1]);
|
||||||
assertEquals(3, result[2]);
|
assertEquals(3, result[2]);
|
||||||
|
|
@ -157,7 +157,7 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertArrayToListInterface() {
|
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("1", result.get(0));
|
||||||
assertEquals("2", result.get(1));
|
assertEquals("2", result.get(1));
|
||||||
assertEquals("3", result.get(2));
|
assertEquals("3", result.get(2));
|
||||||
|
|
@ -167,8 +167,8 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertArrayToListGenericTypeConversion() throws Exception {
|
public void convertArrayToListGenericTypeConversion() throws Exception {
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
List<Integer> result = (List<Integer>) converter.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")));
|
||||||
assertEquals(new Integer("1"), result.get(0));
|
assertEquals(new Integer("1"), result.get(0));
|
||||||
assertEquals(new Integer("2"), result.get(1));
|
assertEquals(new Integer("2"), result.get(1));
|
||||||
|
|
@ -177,7 +177,7 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertArrayToListImpl() {
|
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("1", result.get(0));
|
||||||
assertEquals("2", result.get(1));
|
assertEquals("2", result.get(1));
|
||||||
assertEquals("3", result.get(2));
|
assertEquals("3", result.get(2));
|
||||||
|
|
@ -185,29 +185,63 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
@Test(expected = ConversionFailedException.class)
|
@Test(expected = ConversionFailedException.class)
|
||||||
public void convertArrayToAbstractList() {
|
public void convertArrayToAbstractList() {
|
||||||
converter.convert(new String[] { "1", "2", "3" }, AbstractList.class);
|
conversionService.convert(new String[] { "1", "2", "3" }, AbstractList.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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<String> list = new ArrayList<String>();
|
List<String> list = new ArrayList<String>();
|
||||||
list.add("1");
|
list.add("1");
|
||||||
list.add("2");
|
list.add("2");
|
||||||
list.add("3");
|
list.add("3");
|
||||||
String[] result = converter.convert(list, String[].class);
|
String[] result = conversionService.convert(list, String[].class);
|
||||||
assertEquals("1", result[0]);
|
assertEquals("1", result[0]);
|
||||||
assertEquals("2", result[1]);
|
assertEquals("2", result[1]);
|
||||||
assertEquals("3", result[2]);
|
assertEquals("3", result[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertListToArrayWithComponentConversion() {
|
public void convertCollectionToArrayWithElementConversion() {
|
||||||
converter.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");
|
||||||
list.add("2");
|
list.add("2");
|
||||||
list.add("3");
|
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(1), result[0]);
|
||||||
assertEquals(new Integer(2), result[1]);
|
assertEquals(new Integer(2), result[1]);
|
||||||
assertEquals(new Integer(3), result[2]);
|
assertEquals(new Integer(3), result[2]);
|
||||||
|
|
@ -215,18 +249,48 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertCollectionToCollection() throws Exception {
|
public void convertCollectionToCollection() throws Exception {
|
||||||
converter.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");
|
||||||
foo.add("2");
|
foo.add("2");
|
||||||
foo.add("3");
|
foo.add("3");
|
||||||
List<Integer> bar = (List<Integer>) converter.convert(foo, TypeDescriptor.valueOf(List.class),
|
List<Integer> bar = (List<Integer>) conversionService.convert(foo, TypeDescriptor.valueOf(List.class),
|
||||||
new TypeDescriptor(getClass().getField("genericList")));
|
new TypeDescriptor(getClass().getField("genericList")));
|
||||||
assertEquals(new Integer(1), bar.get(0));
|
assertEquals(new Integer(1), bar.get(0));
|
||||||
assertEquals(new Integer(2), bar.get(1));
|
assertEquals(new Integer(2), bar.get(1));
|
||||||
assertEquals(new Integer(3), bar.get(2));
|
assertEquals(new Integer(3), bar.get(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertCollectionToString() {
|
||||||
|
List<String> 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<Integer> 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<Long> 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<String> list = Collections.singletonList("3");
|
||||||
|
Integer result = conversionService.convert(list, Integer.class);
|
||||||
|
assertEquals(new Integer(3), result);
|
||||||
|
}
|
||||||
|
|
||||||
public Map<Integer, FooEnum> genericMap = new HashMap<Integer, FooEnum>();
|
public Map<Integer, FooEnum> genericMap = new HashMap<Integer, FooEnum>();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -234,89 +298,103 @@ public class GenericConversionServiceTests {
|
||||||
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");
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
converter.addConverterFactory(new StringToEnumConverterFactory());
|
conversionService.addConverterFactory(new StringToEnumConverterFactory());
|
||||||
Map<String, FooEnum> map = (Map<String, FooEnum>) converter.convert(foo, TypeDescriptor.valueOf(Map.class),
|
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, TypeDescriptor
|
||||||
new TypeDescriptor(getClass().getField("genericMap")));
|
.valueOf(Map.class), new TypeDescriptor(getClass().getField("genericMap")));
|
||||||
assertEquals(map.get(1), FooEnum.BAR);
|
assertEquals(map.get(1), FooEnum.BAR);
|
||||||
assertEquals(map.get(2), FooEnum.BAZ);
|
assertEquals(map.get(2), FooEnum.BAZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertObjectToCollection() {
|
public void convertStringToArray() {
|
||||||
List<String> result = (List<String>) converter.convert("test", List.class);
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
assertEquals(1, result.size());
|
String[] result = conversionService.convert("1,2,3", String[].class);
|
||||||
assertEquals("test", result.get(0));
|
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<Integer> result = (List<Integer>) converter.convert("3", TypeDescriptor.valueOf(String.class),
|
|
||||||
new TypeDescriptor(getClass().getField("genericList")));
|
|
||||||
assertEquals(1, result.size());
|
|
||||||
assertEquals(new Integer(3), result.get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void convertCollectionToObject() {
|
|
||||||
List<String> list = Collections.singletonList("test");
|
|
||||||
String result = converter.convert(list, String.class);
|
|
||||||
assertEquals("test", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void convertCollectionToObjectWithElementConversion() {
|
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
|
||||||
List<String> list = Collections.singletonList("3");
|
|
||||||
Integer result = converter.convert(list, Integer.class);
|
|
||||||
assertEquals(new Integer(3), result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void convertObjectToArray() {
|
|
||||||
String[] result = converter.convert("test", String[].class);
|
|
||||||
assertEquals(1, result.length);
|
|
||||||
assertEquals("test", result[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void convertArrayToObject() {
|
|
||||||
String[] array = new String[] { "test" };
|
|
||||||
String result = converter.convert(array, String.class);
|
|
||||||
assertEquals("test", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertStringToArrayWithElementConversion() {
|
public void convertStringToArrayWithElementConversion() {
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
Integer[] result = converter.convert("1,2,3", Integer[].class);
|
Integer[] result = conversionService.convert("1,2,3", Integer[].class);
|
||||||
assertEquals(3, result.length);
|
assertEquals(3, result.length);
|
||||||
assertEquals(new Integer(1), result[0]);
|
assertEquals(new Integer(1), result[0]);
|
||||||
assertEquals(new Integer(2), result[1]);
|
assertEquals(new Integer(2), result[1]);
|
||||||
assertEquals(new Integer(3), result[2]);
|
assertEquals(new Integer(3), result[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertEmptyStringToArray() {
|
||||||
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
|
String[] result = conversionService.convert("", String[].class);
|
||||||
|
assertEquals(0, result.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
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 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 convertEmptyStringToCollection() {
|
||||||
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
|
String[] result = conversionService.convert("", String[].class);
|
||||||
|
assertEquals(0, result.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertObjectToCollection() {
|
||||||
|
List<String> result = (List<String>) conversionService.convert(3L, List.class);
|
||||||
|
assertEquals(1, result.size());
|
||||||
|
assertEquals(3L, result.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertObjectToCollectionWithElementConversion() throws Exception {
|
||||||
|
conversionService.addConverterFactory(new NumberToNumberConverterFactory());
|
||||||
|
List<Integer> result = (List<Integer>) 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 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
|
@Test
|
||||||
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
|
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
|
||||||
try {
|
try {
|
||||||
converter.convert("1", Integer[].class);
|
conversionService.convert("1", Integer[].class);
|
||||||
} catch (ConversionFailedException e) {
|
} catch (ConversionFailedException e) {
|
||||||
assertTrue(e.getCause() instanceof ConverterNotFoundException);
|
assertTrue(e.getCause() instanceof ConverterNotFoundException);
|
||||||
}
|
}
|
||||||
|
|
@ -325,10 +403,10 @@ public class GenericConversionServiceTests {
|
||||||
@Test
|
@Test
|
||||||
public void parent() {
|
public void parent() {
|
||||||
GenericConversionService parent = new GenericConversionService();
|
GenericConversionService parent = new GenericConversionService();
|
||||||
converter.setParent(parent);
|
conversionService.setParent(parent);
|
||||||
assertFalse(converter.canConvert(String.class, Integer.class));
|
assertFalse(conversionService.canConvert(String.class, Integer.class));
|
||||||
try {
|
try {
|
||||||
converter.convert("3", Integer.class);
|
conversionService.convert("3", Integer.class);
|
||||||
} catch (ConverterNotFoundException e) {
|
} catch (ConverterNotFoundException e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue