Converter system implementation cleanup and tidying; wip
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* 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.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class ArrayToArrayTests {
|
||||
|
||||
@Test
|
||||
public void testArrayToArrayConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToArray c = new ArrayToArray(TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(Integer[].class), service);
|
||||
Integer[] result = (Integer[]) c.execute(new String[] { "1", "2", "3" });
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class ArrayToCollectionTests {
|
||||
|
||||
@Test
|
||||
public void testArrayToCollectionConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("bindTarget")), service);
|
||||
Collection result = (Collection) c.execute(new String[] { "1", "2", "3" });
|
||||
assertEquals(3, result.size());
|
||||
assertTrue(result.contains(1));
|
||||
assertTrue(result.contains(2));
|
||||
assertTrue(result.contains(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayToSetConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("setTarget")), service);
|
||||
Set result = (Set) c.execute(new String[] { "1" });
|
||||
assertEquals("1", result.iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayToSortedSetConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("sortedSetTarget")), service);
|
||||
SortedSet result = (SortedSet) c.execute(new String[] { "1" });
|
||||
assertEquals(new Integer(1), result.iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayToCollectionImplConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("implTarget")), service);
|
||||
LinkedList result = (LinkedList) c.execute(new String[] { "1" });
|
||||
assertEquals("1", result.iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayToNonGenericCollectionConversionNullElement() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("listTarget")), service);
|
||||
List result = (List) c.execute(new Integer[] { null, new Integer(1) });
|
||||
assertEquals(null, result.get(0));
|
||||
assertEquals(new Integer(1), result.get(1));
|
||||
}
|
||||
|
||||
public Collection<Integer> bindTarget;
|
||||
public List listTarget;
|
||||
public Set setTarget;
|
||||
public SortedSet<Integer> sortedSetTarget;
|
||||
public LinkedList<String> implTarget;
|
||||
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class CollectionToArrayTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToArrayConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToArray c = new CollectionToArray(new TypeDescriptor(getClass().getField("bindTarget")),
|
||||
TypeDescriptor.valueOf(Integer[].class), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
Integer[] result = (Integer[]) c.execute(bindTarget);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionToArrayConversionNoGenericInfo() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor
|
||||
.valueOf(Integer[].class), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
Integer[] result = (Integer[]) c.execute(bindTarget);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionToArrayConversionNoGenericInfoNullElement() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor
|
||||
.valueOf(Integer[].class), service);
|
||||
bindTarget.add(null);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
Integer[] result = (Integer[]) c.execute(bindTarget);
|
||||
assertEquals(null, result[0]);
|
||||
assertEquals(new Integer(1), result[1]);
|
||||
assertEquals(new Integer(2), result[2]);
|
||||
assertEquals(new Integer(3), result[3]);
|
||||
}
|
||||
|
||||
public Collection<String> bindTarget = new ArrayList<String>();
|
||||
|
||||
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class CollectionToCollectionTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(new TypeDescriptor(getClass().getField("bindTarget")),
|
||||
new TypeDescriptor(getClass().getField("integerTarget")), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
Collection result = (Collection) c.execute(bindTarget);
|
||||
assertEquals(3, result.size());
|
||||
assertTrue(result.contains(1));
|
||||
assertTrue(result.contains(2));
|
||||
assertTrue(result.contains(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversionNoGenericInfo() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
|
||||
TypeDescriptor.valueOf(List.class), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
Collection result = (Collection) c.execute(bindTarget);
|
||||
assertEquals(3, result.size());
|
||||
assertTrue(result.contains("1"));
|
||||
assertTrue(result.contains("2"));
|
||||
assertTrue(result.contains("3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversionNoGenericInfoSource() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
|
||||
new TypeDescriptor(getClass().getField("integerTarget")), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
Collection result = (Collection) c.execute(bindTarget);
|
||||
assertEquals(3, result.size());
|
||||
assertTrue(result.contains(1));
|
||||
assertTrue(result.contains(2));
|
||||
assertTrue(result.contains(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversionNoGenericInfoSourceNullValue() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
|
||||
new TypeDescriptor(getClass().getField("integerTarget")), service);
|
||||
bindTarget.add(null);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
Collection result = (Collection) c.execute(bindTarget);
|
||||
Iterator it = result.iterator();
|
||||
assertEquals(null, it.next());
|
||||
assertEquals(new Integer(1), it.next());
|
||||
assertEquals(new Integer(2), it.next());
|
||||
assertEquals(new Integer(3), it.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversionNoGenericInfoSourceEmpty() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
|
||||
new TypeDescriptor(getClass().getField("integerTarget")), service);
|
||||
Collection result = (Collection) c.execute(bindTarget);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
public Collection<String> bindTarget = new ArrayList<String>();
|
||||
public List<Integer> integerTarget = new ArrayList<Integer>();
|
||||
|
||||
}
|
||||
@@ -25,21 +25,6 @@ import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.NumberToCharacter;
|
||||
import org.springframework.core.convert.support.NumberToNumberFactory;
|
||||
import org.springframework.core.convert.support.ObjectToString;
|
||||
import org.springframework.core.convert.support.StringToBigDecimal;
|
||||
import org.springframework.core.convert.support.StringToBigInteger;
|
||||
import org.springframework.core.convert.support.StringToBoolean;
|
||||
import org.springframework.core.convert.support.StringToByte;
|
||||
import org.springframework.core.convert.support.StringToCharacter;
|
||||
import org.springframework.core.convert.support.StringToDouble;
|
||||
import org.springframework.core.convert.support.StringToEnumFactory;
|
||||
import org.springframework.core.convert.support.StringToFloat;
|
||||
import org.springframework.core.convert.support.StringToInteger;
|
||||
import org.springframework.core.convert.support.StringToLocale;
|
||||
import org.springframework.core.convert.support.StringToLong;
|
||||
import org.springframework.core.convert.support.StringToShort;
|
||||
|
||||
/**
|
||||
* Tests for the default converters in the converters package.
|
||||
@@ -48,70 +33,64 @@ import org.springframework.core.convert.support.StringToShort;
|
||||
*/
|
||||
public class DefaultConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void testStringToByte() throws Exception {
|
||||
StringToByte b = new StringToByte();
|
||||
assertEquals(Byte.valueOf("1"), b.convert("1"));
|
||||
}
|
||||
|
||||
private StringToNumberConverterFactory c = new StringToNumberConverterFactory();
|
||||
|
||||
@Test
|
||||
public void testStringToCharacter() {
|
||||
StringToCharacter c = new StringToCharacter();
|
||||
StringToCharacterConverter c = new StringToCharacterConverter();
|
||||
assertEquals(Character.valueOf('1'), c.convert("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBoolean() {
|
||||
StringToBoolean c = new StringToBoolean();
|
||||
StringToBooleanConverter c = new StringToBooleanConverter();
|
||||
assertEquals(Boolean.valueOf(true), c.convert("true"));
|
||||
assertEquals(Boolean.valueOf(false), c.convert("false"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testStringToByte() throws Exception {
|
||||
assertEquals(Byte.valueOf("1"), c.getConverter(Byte.class).convert("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToShort() {
|
||||
StringToShort c = new StringToShort();
|
||||
assertEquals(Short.valueOf("1"), c.convert("1"));
|
||||
assertEquals(Short.valueOf("1"), c.getConverter(Short.class).convert("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToInteger() {
|
||||
StringToInteger c = new StringToInteger();
|
||||
assertEquals(Integer.valueOf("1"), c.convert("1"));
|
||||
assertEquals(Integer.valueOf("1"), c.getConverter(Integer.class).convert("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToLong() {
|
||||
StringToLong c = new StringToLong();
|
||||
assertEquals(Long.valueOf("1"), c.convert("1"));
|
||||
assertEquals(Long.valueOf("1"), c.getConverter(Long.class).convert("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToFloat() {
|
||||
StringToFloat c = new StringToFloat();
|
||||
assertEquals(Float.valueOf("1.0"), c.convert("1.0"));
|
||||
assertEquals(Float.valueOf("1.0"), c.getConverter(Float.class).convert("1.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToDouble() {
|
||||
StringToDouble c = new StringToDouble();
|
||||
assertEquals(Double.valueOf("1.0"), c.convert("1.0"));
|
||||
assertEquals(Double.valueOf("1.0"), c.getConverter(Double.class).convert("1.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBigInteger() {
|
||||
StringToBigInteger c = new StringToBigInteger();
|
||||
assertEquals(new BigInteger("1"), c.convert("1"));
|
||||
assertEquals(new BigInteger("1"), c.getConverter(BigInteger.class).convert("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBigDouble() {
|
||||
StringToBigDecimal c = new StringToBigDecimal();
|
||||
assertEquals(new BigDecimal("1.0"), c.convert("1.0"));
|
||||
assertEquals(new BigDecimal("1.0"), c.getConverter(BigDecimal.class).convert("1.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToEnum() throws Exception {
|
||||
Converter<String, Foo> c = new StringToEnumFactory().getConverter(Foo.class);
|
||||
Converter<String, Foo> c = new StringToEnumConverterFactory().getConverter(Foo.class);
|
||||
assertEquals(Foo.BAR, c.convert("BAR"));
|
||||
}
|
||||
|
||||
@@ -121,19 +100,19 @@ public class DefaultConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void testStringToLocale() {
|
||||
StringToLocale c = new StringToLocale();
|
||||
StringToLocaleConverter c = new StringToLocaleConverter();
|
||||
assertEquals(Locale.ENGLISH, c.convert("en"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberToNumber() throws Exception {
|
||||
Converter<Number, Long> c = new NumberToNumberFactory().getConverter(Long.class);
|
||||
Converter<Number, Long> c = new NumberToNumberConverterFactory().getConverter(Long.class);
|
||||
assertEquals(Long.valueOf(1), c.convert(Integer.valueOf(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberToNumberNotSupportedNumber() throws Exception {
|
||||
Converter<Number, CustomNumber> c = new NumberToNumberFactory().getConverter(CustomNumber.class);
|
||||
Converter<Number, CustomNumber> c = new NumberToNumberConverterFactory().getConverter(CustomNumber.class);
|
||||
try {
|
||||
c.convert(Integer.valueOf(1));
|
||||
fail("Should have failed");
|
||||
@@ -144,13 +123,13 @@ public class DefaultConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void testNumberToCharacter() {
|
||||
NumberToCharacter n = new NumberToCharacter();
|
||||
NumberToCharacterConverter n = new NumberToCharacterConverter();
|
||||
assertEquals(Character.valueOf('A'), n.convert(Integer.valueOf(65)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectToString() {
|
||||
ObjectToString o = new ObjectToString();
|
||||
ObjectToStringConverter o = new ObjectToStringConverter();
|
||||
assertEquals("3", o.convert(3));
|
||||
}
|
||||
|
||||
|
||||
@@ -23,10 +23,13 @@ import static junit.framework.Assert.fail;
|
||||
import java.util.AbstractList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
@@ -42,7 +45,7 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void executeConversion() {
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
assertEquals(new Integer(3), converter.convert("3", Integer.class));
|
||||
}
|
||||
|
||||
@@ -69,7 +72,7 @@ public class GenericConversionServiceTests {
|
||||
public void addConverterNoSourceTargetClassInfoAvailable() {
|
||||
try {
|
||||
converter.addConverter(new Converter() {
|
||||
public Object convert(Object source) throws Exception {
|
||||
public Object convert(Object source) {
|
||||
return source;
|
||||
}
|
||||
});
|
||||
@@ -84,20 +87,20 @@ public class GenericConversionServiceTests {
|
||||
assertNull(converter.convert(null, Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void convertNullTargetClass() {
|
||||
assertEquals("3", converter.convert("3", (Class<?>)null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertNullConversionPointType() {
|
||||
assertEquals("3", converter.convert("3", TypeDescriptor.NULL));
|
||||
assertEquals(null, converter.convert("3", TypeDescriptor.NULL));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void convertWrongTypeArgument() {
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
try {
|
||||
converter.convert("BOGUS", Integer.class);
|
||||
fail("Should have failed");
|
||||
@@ -109,7 +112,7 @@ public class GenericConversionServiceTests {
|
||||
@Test
|
||||
public void convertSuperSourceType() {
|
||||
converter.addConverter(new Converter<CharSequence, Integer>() {
|
||||
public Integer convert(CharSequence source) throws Exception {
|
||||
public Integer convert(CharSequence source) {
|
||||
return Integer.valueOf(source.toString());
|
||||
}
|
||||
});
|
||||
@@ -119,14 +122,15 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertObjectToPrimitive() {
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
Integer three = converter.convert("3", int.class);
|
||||
assertEquals(3, three.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertArrayToArray() {
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
Integer[] result = converter.convert(new String[] { "1", "2", "3" }, Integer[].class);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
@@ -134,8 +138,9 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertArrayToPrimitiveArray() {
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
int[] result = converter.convert(new String[] { "1", "2", "3" }, int[].class);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
@@ -143,6 +148,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertArrayToListInterface() {
|
||||
List<?> result = converter.convert(new String[] { "1", "2", "3" }, List.class);
|
||||
assertEquals("1", result.get(0));
|
||||
@@ -153,8 +159,9 @@ public class GenericConversionServiceTests {
|
||||
public List<Integer> genericList = new ArrayList<Integer>();
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertArrayToListGenericTypeConversion() throws Exception {
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
List<Integer> result = (List<Integer>) converter.convert(new String[] { "1", "2", "3" }, new TypeDescriptor(getClass().getDeclaredField("genericList")));
|
||||
assertEquals(new Integer("1"), result.get(0));
|
||||
assertEquals(new Integer("2"), result.get(1));
|
||||
@@ -162,6 +169,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertArrayToListImpl() {
|
||||
LinkedList<?> result = converter.convert(new String[] { "1", "2", "3" }, LinkedList.class);
|
||||
assertEquals("1", result.get(0));
|
||||
@@ -170,6 +178,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertArrayToAbstractList() {
|
||||
try {
|
||||
converter.convert(new String[] { "1", "2", "3" }, AbstractList.class);
|
||||
@@ -179,6 +188,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertListToArray() {
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add("1");
|
||||
@@ -191,8 +201,9 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertListToArrayWithComponentConversion() {
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add("1");
|
||||
list.add("2");
|
||||
@@ -203,21 +214,36 @@ public class GenericConversionServiceTests {
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToCollection() throws Exception {
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
Set<String> foo = new LinkedHashSet<String>();
|
||||
foo.add("1");
|
||||
foo.add("2");
|
||||
foo.add("3");
|
||||
List<Integer> bar = (List<Integer>)converter.convert(foo, new TypeDescriptor(getClass().getField("genericList")));
|
||||
assertEquals(new Integer(1), bar.get(0));
|
||||
assertEquals(new Integer(2), bar.get(1));
|
||||
assertEquals(new Integer(3), bar.get(2));
|
||||
}
|
||||
|
||||
public Map<Integer, FooEnum> genericMap = new HashMap<Integer, FooEnum>();
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertMapToMap() throws Exception {
|
||||
Map<String, String> foo = new HashMap<String, String>();
|
||||
foo.put("1", "BAR");
|
||||
foo.put("2", "BAZ");
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverter(new StringToEnumFactory().getConverter(FooEnum.class));
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
converter.addConverterFactory(new StringToEnumConverterFactory());
|
||||
Map<String, FooEnum> map = (Map<String, FooEnum>) converter.convert(foo, new TypeDescriptor(getClass().getField("genericMap")));
|
||||
assertEquals(map.get(1), FooEnum.BAR);
|
||||
assertEquals(map.get(2), FooEnum.BAZ);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertStringToArray() {
|
||||
String[] result = (String[]) converter.convert("1,2,3", String[].class);
|
||||
assertEquals(3, result.length);
|
||||
@@ -227,8 +253,9 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertStringToArrayWithElementConversion() {
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||
Integer[] result = converter.convert("1,2,3", Integer[].class);
|
||||
assertEquals(3, result.length);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
@@ -238,7 +265,6 @@ public class GenericConversionServiceTests {
|
||||
|
||||
|
||||
public static enum FooEnum {
|
||||
|
||||
BAR, BAZ
|
||||
}
|
||||
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* 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.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.MapToMap;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class MapToMapTests {
|
||||
|
||||
@Test
|
||||
public void testMapToMapConversion() throws Exception {
|
||||
DefaultConversionService converter = new DefaultConversionService();
|
||||
MapToMap c = new MapToMap(new TypeDescriptor(getClass().getField("source")),
|
||||
new TypeDescriptor(getClass().getField("bindTarget")), converter);
|
||||
source.put("1", "BAR");
|
||||
source.put("2", "BAZ");
|
||||
Map<String, FooEnum> result = (Map<String, FooEnum>) c.execute(source);
|
||||
assertEquals(FooEnum.BAR, result.get(1));
|
||||
assertEquals(FooEnum.BAZ, result.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapToMapConversionNoGenericInfoOnSource() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class),
|
||||
new TypeDescriptor(getClass().getField("bindTarget")), service);
|
||||
source.put("1", "BAR");
|
||||
source.put("2", "BAZ");
|
||||
Map result = (Map) c.execute(source);
|
||||
assertEquals(FooEnum.BAR, result.get(1));
|
||||
assertEquals(FooEnum.BAZ, result.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapToMapConversionNoGenericInfo() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class),
|
||||
TypeDescriptor.valueOf(Map.class), service);
|
||||
source.put("1", "BAR");
|
||||
source.put("2", "BAZ");
|
||||
Map result = (Map) c.execute(source);
|
||||
assertEquals("BAR", result.get("1"));
|
||||
assertEquals("BAZ", result.get("2"));
|
||||
}
|
||||
|
||||
|
||||
public Map<String, String> source = new HashMap<String, String>();
|
||||
public Map<Integer, FooEnum> bindTarget = new HashMap<Integer, FooEnum>();
|
||||
|
||||
public static enum FooEnum {
|
||||
BAR, BAZ;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user