Polish ConversionService tests
- Now correctly using @Test(expected=...) where appropriate. - Renamed DefaultConversionTests to DefaultConversionServiceTests. - Moved all tests related to DefaultConversionService from GenericConversionServiceTests to DefaultConversionServiceTests. - No longer printing to System.out. - Removed all duplicate instantiation of conversion services. - Now using Java 8 streams to simplify implementations of custom test converters. Also using streams in tests where appropriate.
This commit is contained in:
@@ -26,6 +26,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -36,6 +37,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -46,17 +48,27 @@ import org.springframework.core.convert.ConverterNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterRegistry;
|
||||
import org.springframework.tests.Assume;
|
||||
import org.springframework.tests.TestGroup;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link DefaultConversionService}.
|
||||
*
|
||||
* <p>For tests involving the {@link GenericConversionService}, see
|
||||
* {@link GenericConversionServiceTests}.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
* @author Stephane Nicoll
|
||||
* @author Sam Brannen
|
||||
* @see GenericConversionServiceTests
|
||||
*/
|
||||
public class DefaultConversionTests {
|
||||
public class DefaultConversionServiceTests {
|
||||
|
||||
private final DefaultConversionService conversionService = new DefaultConversionService();
|
||||
|
||||
@@ -229,6 +241,15 @@ public class DefaultConversionTests {
|
||||
assertEquals("BAR", conversionService.convert(Foo.BAR, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToEnumSet() throws Exception {
|
||||
assertEquals(EnumSet.of(Foo.BAR), conversionService.convert("BAR", TypeDescriptor.valueOf(String.class),
|
||||
new TypeDescriptor(getClass().getField("enumSet"))));
|
||||
}
|
||||
|
||||
public EnumSet<Foo> enumSet;
|
||||
|
||||
|
||||
public enum Foo {
|
||||
BAR, BAZ
|
||||
}
|
||||
@@ -262,6 +283,14 @@ public class DefaultConversionTests {
|
||||
assertSame(str, conversionService.convert(str, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUuidToStringAndStringToUuid() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
String convertToString = conversionService.convert(uuid, String.class);
|
||||
UUID convertToUUID = conversionService.convert(convertToString, UUID.class);
|
||||
assertEquals(uuid, convertToUUID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberToNumber() {
|
||||
assertEquals(Long.valueOf(1), conversionService.convert(1, Long.class));
|
||||
@@ -575,6 +604,13 @@ public class DefaultConversionTests {
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToWrapperArray() {
|
||||
byte[] byteArray = new byte[] { 1, 2, 3 };
|
||||
Byte[] converted = conversionService.convert(byteArray, Byte[].class);
|
||||
assertTrue(Arrays.equals(converted, new Byte[] { 1, 2, 3 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToArrayAssignable() {
|
||||
int[] result = conversionService.convert(new int[] { 1, 2, 3 }, int[].class);
|
||||
@@ -583,6 +619,16 @@ public class DefaultConversionTests {
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertListOfListToString() {
|
||||
List<String> list1 = Arrays.asList("Foo", "Bar");
|
||||
List<String> list2 = Arrays.asList("Baz", "Boop");
|
||||
List<List<String>> list = Arrays.asList(list1, list2);
|
||||
String result = conversionService.convert(list, String.class);
|
||||
assertNotNull(result);
|
||||
assertEquals("Foo,Bar,Baz,Boop", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToCollection() throws Exception {
|
||||
Set<String> foo = new LinkedHashSet<String>();
|
||||
@@ -660,6 +706,16 @@ public class DefaultConversionTests {
|
||||
assertEquals(FooEnum.BAZ, map.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public void convertHashMapValuesToList() {
|
||||
Map<String, Integer> hashMap = new LinkedHashMap<String, Integer>();
|
||||
hashMap.put("1", 1);
|
||||
hashMap.put("2", 2);
|
||||
List converted = conversionService.convert(hashMap.values(), List.class);
|
||||
assertEquals(Arrays.asList(1, 2), converted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void map() {
|
||||
Map<String, String> strings = new HashMap<String, String>();
|
||||
@@ -794,6 +850,21 @@ public class DefaultConversionTests {
|
||||
assertArrayEquals(grid, convertedBack);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCannotOptimizeArray() {
|
||||
conversionService.addConverter(new Converter<Byte, Byte>() {
|
||||
|
||||
@Override
|
||||
public Byte convert(Byte source) {
|
||||
return (byte) (source + 1);
|
||||
}
|
||||
});
|
||||
byte[] byteArray = new byte[] { 1, 2, 3 };
|
||||
byte[] converted = conversionService.convert(byteArray, byte[].class);
|
||||
assertNotSame(byteArray, converted);
|
||||
assertTrue(Arrays.equals(new byte[] { 2, 3, 4 }, converted));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void convertObjectToOptional() {
|
||||
@@ -819,6 +890,22 @@ public class DefaultConversionTests {
|
||||
assertSame(Optional.empty(), conversionService.convert(Optional.empty(), Optional.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformance1() {
|
||||
Assume.group(TestGroup.PERFORMANCE);
|
||||
StopWatch watch = new StopWatch("integer->string conversionPerformance");
|
||||
watch.start("convert 4,000,000 with conversion service");
|
||||
for (int i = 0; i < 4000000; i++) {
|
||||
conversionService.convert(3, String.class);
|
||||
}
|
||||
watch.stop();
|
||||
watch.start("convert 4,000,000 manually");
|
||||
for (int i = 0; i < 4000000; i++) {
|
||||
new Integer(3).toString();
|
||||
}
|
||||
watch.stop();
|
||||
// System.out.println(watch.prettyPrint());
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class CustomNumber extends Number {
|
||||
@@ -24,16 +24,13 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -51,18 +48,27 @@ import org.springframework.tests.TestGroup;
|
||||
import org.springframework.util.StopWatch;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static java.util.Comparator.*;
|
||||
import static java.util.stream.Collectors.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link GenericConversionService}.
|
||||
*
|
||||
* <p>For tests involving the {@link DefaultConversionService}, see
|
||||
* {@link DefaultConversionServiceTests}.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
* @author Phillip Webb
|
||||
* @author David Haraburda
|
||||
* @author Sam Brannen
|
||||
* @see DefaultConversionServiceTests
|
||||
*/
|
||||
public class GenericConversionServiceTests {
|
||||
|
||||
private GenericConversionService conversionService = new GenericConversionService();
|
||||
private final GenericConversionService conversionService = new GenericConversionService();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -80,20 +86,14 @@ public class GenericConversionServiceTests {
|
||||
assertTrue(conversionService.canConvert(boolean.class, Boolean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canConvertIllegalArgumentNullTargetType() {
|
||||
try {
|
||||
assertFalse(conversionService.canConvert(String.class, null));
|
||||
fail("Should have failed");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
}
|
||||
try {
|
||||
assertFalse(conversionService.canConvert(TypeDescriptor.valueOf(String.class), null));
|
||||
fail("Should have failed");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
}
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void canConvertFromClassSourceTypeToNullTargetType() {
|
||||
conversionService.canConvert(String.class, null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void canConvertFromTypeDescriptorSourceTypeToNullTargetType() {
|
||||
conversionService.canConvert(TypeDescriptor.valueOf(String.class), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -115,7 +115,7 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test(expected = ConversionFailedException.class)
|
||||
public void convertNullSourcePrimitiveTarget() {
|
||||
assertEquals(null, conversionService.convert(null, int.class));
|
||||
conversionService.convert(null, int.class);
|
||||
}
|
||||
|
||||
@Test(expected = ConversionFailedException.class)
|
||||
@@ -134,41 +134,23 @@ public class GenericConversionServiceTests {
|
||||
assertEquals(Boolean.FALSE, conversionService.convert(false, Boolean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = ConverterNotFoundException.class)
|
||||
public void converterNotFound() {
|
||||
try {
|
||||
conversionService.convert("3", Integer.class);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (ConverterNotFoundException e) {
|
||||
}
|
||||
conversionService.convert("3", Integer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void addConverterNoSourceTargetClassInfoAvailable() {
|
||||
try {
|
||||
conversionService.addConverter(new Converter() {
|
||||
@Override
|
||||
public Object convert(Object source) {
|
||||
return source;
|
||||
}
|
||||
});
|
||||
fail("Should have failed");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
}
|
||||
conversionService.addConverter(new UntypedConverter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sourceTypeIsVoid() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
assertFalse(conversionService.canConvert(void.class, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void targetTypeIsVoid() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
assertFalse(conversionService.canConvert(String.class, void.class));
|
||||
}
|
||||
|
||||
@@ -178,14 +160,13 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void convertNullTargetClass() {
|
||||
assertNull(conversionService.convert("3", (Class<?>) null));
|
||||
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), null));
|
||||
public void convertToNullTargetClass() {
|
||||
conversionService.convert("3", (Class<?>) null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void convertNullTypeDescriptor() {
|
||||
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), null));
|
||||
public void convertToNullTargetTypeDescriptor() {
|
||||
conversionService.convert("3", TypeDescriptor.valueOf(String.class), null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -193,16 +174,10 @@ public class GenericConversionServiceTests {
|
||||
conversionService.convert("3", TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(Long.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = ConversionFailedException.class)
|
||||
public void convertWrongTypeArgument() {
|
||||
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||
try {
|
||||
conversionService.convert("BOGUS", Integer.class);
|
||||
fail("Should have failed");
|
||||
}
|
||||
catch (ConversionFailedException e) {
|
||||
|
||||
}
|
||||
conversionService.convert("BOGUS", Integer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -224,21 +199,16 @@ public class GenericConversionServiceTests {
|
||||
conversionService.convert("#000000", SystemColor.class);
|
||||
}
|
||||
|
||||
public class ColorConverter implements Converter<String, Color> {
|
||||
@Override
|
||||
public Color convert(String source) { if (!source.startsWith("#")) source = "#" + source; return Color.decode(source); }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToPrimitive() {
|
||||
assertFalse(conversionService.canConvert(String.class, boolean.class));
|
||||
conversionService.addConverter(new StringToBooleanConverter());
|
||||
assertTrue(conversionService.canConvert(String.class, boolean.class));
|
||||
Boolean b = conversionService.convert("true", boolean.class);
|
||||
assertEquals(Boolean.TRUE, b);
|
||||
assertTrue(b);
|
||||
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class)));
|
||||
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class));
|
||||
assertEquals(Boolean.TRUE, b);
|
||||
assertTrue(b);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -250,21 +220,15 @@ public class GenericConversionServiceTests {
|
||||
assertEquals(3, three.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = ConverterNotFoundException.class)
|
||||
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
|
||||
conversionService.addConverter(new ObjectToArrayConverter(conversionService));
|
||||
assertFalse(conversionService.canConvert(String.class, Integer[].class));
|
||||
try {
|
||||
conversionService.convert("3,4,5", Integer[].class);
|
||||
fail("should have failed");
|
||||
}
|
||||
catch (ConverterNotFoundException ex) {
|
||||
}
|
||||
conversionService.convert("3,4,5", Integer[].class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListToIterableConversion() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
List<Object> raw = new ArrayList<Object>();
|
||||
raw.add("one");
|
||||
raw.add("two");
|
||||
@@ -274,7 +238,6 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void testListToObjectConversion() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
List<Object> raw = new ArrayList<Object>();
|
||||
raw.add("one");
|
||||
raw.add("two");
|
||||
@@ -284,7 +247,6 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void testMapToObjectConversion() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
Map<Object, Object> raw = new HashMap<Object, Object>();
|
||||
raw.put("key", "value");
|
||||
Object converted = conversionService.convert(raw, Object.class);
|
||||
@@ -293,7 +255,6 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void testInterfaceToString() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
conversionService.addConverter(new MyBaseInterfaceToStringConverter());
|
||||
conversionService.addConverter(new ObjectToStringConverter());
|
||||
Object converted = conversionService.convert(new MyInterfaceImplementer(), String.class);
|
||||
@@ -302,7 +263,6 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void testInterfaceArrayToStringArray() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
conversionService.addConverter(new MyBaseInterfaceToStringConverter());
|
||||
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
|
||||
String[] converted = conversionService.convert(new MyInterface[] {new MyInterfaceImplementer()}, String[].class);
|
||||
@@ -311,7 +271,6 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void testObjectArrayToStringArray() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
conversionService.addConverter(new MyBaseInterfaceToStringConverter());
|
||||
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
|
||||
String[] converted = conversionService.convert(new MyInterfaceImplementer[] {new MyInterfaceImplementer()}, String[].class);
|
||||
@@ -320,109 +279,58 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void testStringArrayToResourceArray() {
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
conversionService.addConverter(new MyStringArrayToResourceArrayConverter());
|
||||
Resource[] converted = conversionService.convert(new String[] {"x1", "z3"}, Resource[].class);
|
||||
assertEquals(2, converted.length);
|
||||
assertEquals("1", converted[0].getDescription());
|
||||
assertEquals("3", converted[1].getDescription());
|
||||
Resource[] converted = conversionService.convert(new String[] { "x1", "z3" }, Resource[].class);
|
||||
List<String> descriptions = Arrays.stream(converted).map(Resource::getDescription).sorted(naturalOrder()).collect(toList());
|
||||
assertEquals(Arrays.asList("1", "3"), descriptions);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringArrayToIntegerArray() {
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
conversionService.addConverter(new MyStringArrayToIntegerArrayConverter());
|
||||
Integer[] converted = conversionService.convert(new String[] {"x1", "z3"}, Integer[].class);
|
||||
assertEquals(2, converted.length);
|
||||
assertEquals(1, converted[0].intValue());
|
||||
assertEquals(3, converted[1].intValue());
|
||||
assertArrayEquals(new Integer[] { 1, 3 }, converted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToIntegerArray() {
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
conversionService.addConverter(new MyStringToIntegerArrayConverter());
|
||||
Integer[] converted = conversionService.convert("x1,z3", Integer[].class);
|
||||
assertEquals(2, converted.length);
|
||||
assertEquals(1, converted[0].intValue());
|
||||
assertEquals(3, converted[1].intValue());
|
||||
assertArrayEquals(new Integer[] { 1, 3 }, converted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWildcardMap() throws Exception {
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
Map<String, String> input = new LinkedHashMap<String, String>();
|
||||
input.put("key", "value");
|
||||
Object converted = conversionService.convert(input, TypeDescriptor.forObject(input), new TypeDescriptor(getClass().getField("wildcardMap")));
|
||||
assertEquals(input, converted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListOfList() {
|
||||
GenericConversionService service = new DefaultConversionService();
|
||||
List<String> list1 = Arrays.asList("Foo", "Bar");
|
||||
List<String> list2 = Arrays.asList("Baz", "Boop");
|
||||
List<List<String>> list = Arrays.asList(list1, list2);
|
||||
String result = service.convert(list, String.class);
|
||||
assertNotNull(result);
|
||||
assertEquals("Foo,Bar,Baz,Boop", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToString() {
|
||||
GenericConversionService service = new DefaultConversionService();
|
||||
String value = "myValue";
|
||||
String result = service.convert(value, String.class);
|
||||
String result = conversionService.convert(value, String.class);
|
||||
assertSame(value, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToObject() {
|
||||
GenericConversionService service = new DefaultConversionService();
|
||||
String value = "myValue";
|
||||
Object result = service.convert(value, Object.class);
|
||||
Object result = conversionService.convert(value, Object.class);
|
||||
assertSame(value, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreCopyConstructor() {
|
||||
GenericConversionService service = new DefaultConversionService();
|
||||
WithCopyConstructor value = new WithCopyConstructor();
|
||||
Object result = service.convert(value, WithCopyConstructor.class);
|
||||
Object result = conversionService.convert(value, WithCopyConstructor.class);
|
||||
assertSame(value, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertUUID() {
|
||||
GenericConversionService service = new DefaultConversionService();
|
||||
UUID uuid = UUID.randomUUID();
|
||||
String convertToString = service.convert(uuid, String.class);
|
||||
UUID convertToUUID = service.convert(convertToString, UUID.class);
|
||||
assertEquals(uuid, convertToUUID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformance1() {
|
||||
Assume.group(TestGroup.PERFORMANCE);
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
StopWatch watch = new StopWatch("integer->string conversionPerformance");
|
||||
watch.start("convert 4,000,000 with conversion service");
|
||||
for (int i = 0; i < 4000000; i++) {
|
||||
conversionService.convert(3, String.class);
|
||||
}
|
||||
watch.stop();
|
||||
watch.start("convert 4,000,000 manually");
|
||||
for (int i = 0; i < 4000000; i++) {
|
||||
new Integer(3).toString();
|
||||
}
|
||||
watch.stop();
|
||||
System.out.println(watch.prettyPrint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformance2() throws Exception {
|
||||
Assume.group(TestGroup.PERFORMANCE);
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
StopWatch watch = new StopWatch("list<string> -> list<integer> conversionPerformance");
|
||||
watch.start("convert 4,000,000 with conversion service");
|
||||
List<String> source = new LinkedList<String>();
|
||||
@@ -442,13 +350,12 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
watch.stop();
|
||||
System.out.println(watch.prettyPrint());
|
||||
// System.out.println(watch.prettyPrint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformance3() throws Exception {
|
||||
Assume.group(TestGroup.PERFORMANCE);
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
StopWatch watch = new StopWatch("map<string, string> -> map<string, integer> conversionPerformance");
|
||||
watch.start("convert 4,000,000 with conversion service");
|
||||
Map<String, String> source = new HashMap<String, String>();
|
||||
@@ -468,7 +375,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
watch.stop();
|
||||
System.out.println(watch.prettyPrint());
|
||||
// System.out.println(watch.prettyPrint());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -534,43 +441,14 @@ public class GenericConversionServiceTests {
|
||||
assertFalse(pair.hashCode() == pairOpposite.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertPrimitiveArray() {
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
byte[] byteArray = new byte[] { 1, 2, 3 };
|
||||
Byte[] converted = conversionService.convert(byteArray, Byte[].class);
|
||||
assertTrue(Arrays.equals(converted, new Byte[] {1, 2, 3}));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void canConvertIllegalArgumentNullTargetTypeFromClass() {
|
||||
try {
|
||||
conversionService.canConvert(String.class, null);
|
||||
fail("Did not thow IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
}
|
||||
conversionService.canConvert(String.class, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void canConvertIllegalArgumentNullTargetTypeFromTypeDescriptor() {
|
||||
try {
|
||||
conversionService.canConvert(TypeDescriptor.valueOf(String.class), null);
|
||||
fail("Did not thow IllegalArgumentException");
|
||||
}
|
||||
catch(IllegalArgumentException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public void convertHashMapValuesToList() {
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
Map<String, Integer> hashMap = new LinkedHashMap<String, Integer>();
|
||||
hashMap.put("1", 1);
|
||||
hashMap.put("2", 2);
|
||||
List converted = conversionService.convert(hashMap.values(), List.class);
|
||||
assertEquals(Arrays.asList(1, 2), converted);
|
||||
conversionService.canConvert(TypeDescriptor.valueOf(String.class), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -583,7 +461,6 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void conditionalConverter() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
MyConditionalConverter converter = new MyConditionalConverter();
|
||||
conversionService.addConverter(new ColorConverter());
|
||||
conversionService.addConverter(converter);
|
||||
@@ -593,7 +470,6 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void conditionalConverterFactory() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
MyConditionalConverterFactory converter = new MyConditionalConverterFactory();
|
||||
conversionService.addConverter(new ColorConverter());
|
||||
conversionService.addConverterFactory(converter);
|
||||
@@ -604,20 +480,10 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void shouldNotSupportNullConvertibleTypesFromNonConditionalGenericConverter() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
GenericConverter converter = new GenericConverter() {
|
||||
@Override
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
GenericConverter converter = new NonConditionalGenericConverter();
|
||||
try {
|
||||
conversionService.addConverter(converter);
|
||||
fail("Did not throw");
|
||||
fail("Did not throw IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
assertEquals("Only conditional converters may return null convertible types", ex.getMessage());
|
||||
@@ -626,54 +492,31 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void conditionalConversionForAllTypes() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
MyConditionalGenericConverter converter = new MyConditionalGenericConverter();
|
||||
conversionService.addConverter(converter);
|
||||
assertEquals((Integer) 3, conversionService.convert(3, Integer.class));
|
||||
assertThat(converter.getSourceTypes().size(), greaterThan(2));
|
||||
Iterator<TypeDescriptor> iterator = converter.getSourceTypes().iterator();
|
||||
while(iterator.hasNext()) {
|
||||
assertEquals(Integer.class, iterator.next().getType());
|
||||
}
|
||||
assertTrue(converter.getSourceTypes().stream().allMatch(td -> Integer.class.equals(td.getType())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertOptimizeArray() {
|
||||
// SPR-9566
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
byte[] byteArray = new byte[] { 1, 2, 3 };
|
||||
byte[] converted = conversionService.convert(byteArray, byte[].class);
|
||||
assertSame(byteArray, converted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCannotOptimizeArray() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
conversionService.addConverter(new Converter<Byte, Byte>() {
|
||||
@Override
|
||||
public Byte convert(Byte source) {
|
||||
return (byte) (source + 1);
|
||||
}
|
||||
});
|
||||
DefaultConversionService.addDefaultConverters(conversionService);
|
||||
byte[] byteArray = new byte[] { 1, 2, 3 };
|
||||
byte[] converted = conversionService.convert(byteArray, byte[].class);
|
||||
assertNotSame(byteArray, converted);
|
||||
assertTrue(Arrays.equals(new byte[] {2, 3, 4}, converted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumToStringConversion() {
|
||||
conversionService.addConverter(new EnumToStringConverter(conversionService));
|
||||
String result = conversionService.convert(MyEnum.A, String.class);
|
||||
assertEquals("A", result);
|
||||
assertEquals("A", conversionService.convert(MyEnum.A, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubclassOfEnumToString() throws Exception {
|
||||
conversionService.addConverter(new EnumToStringConverter(conversionService));
|
||||
String result = conversionService.convert(EnumWithSubclass.FIRST, String.class);
|
||||
assertEquals("FIRST", result);
|
||||
assertEquals("FIRST", conversionService.convert(EnumWithSubclass.FIRST, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -681,8 +524,7 @@ public class GenericConversionServiceTests {
|
||||
// SPR-9692
|
||||
conversionService.addConverter(new EnumToStringConverter(conversionService));
|
||||
conversionService.addConverter(new MyEnumInterfaceToStringConverter<MyEnum>());
|
||||
String result = conversionService.convert(MyEnum.A, String.class);
|
||||
assertEquals("1", result);
|
||||
assertEquals("1", conversionService.convert(MyEnum.A, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -699,16 +541,8 @@ public class GenericConversionServiceTests {
|
||||
assertEquals(MyEnum.A, conversionService.convert("base1", MyEnum.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToEnumSet() throws Exception {
|
||||
DefaultConversionService.addDefaultConverters(conversionService);
|
||||
assertEquals(EnumSet.of(MyEnum.A),
|
||||
conversionService.convert("A", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("enumSet"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertNullAnnotatedStringToString() throws Exception {
|
||||
DefaultConversionService.addDefaultConverters(conversionService);
|
||||
String source = null;
|
||||
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("annotatedString"));
|
||||
TypeDescriptor targetType = TypeDescriptor.valueOf(String.class);
|
||||
@@ -796,21 +630,13 @@ public class GenericConversionServiceTests {
|
||||
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public static @interface ExampleAnnotation {
|
||||
}
|
||||
private static @interface ExampleAnnotation {}
|
||||
|
||||
private static interface MyBaseInterface {}
|
||||
|
||||
private interface MyBaseInterface {
|
||||
}
|
||||
|
||||
|
||||
private interface MyInterface extends MyBaseInterface {
|
||||
}
|
||||
|
||||
|
||||
private static class MyInterfaceImplementer implements MyInterface {
|
||||
}
|
||||
private static interface MyInterface extends MyBaseInterface {}
|
||||
|
||||
private static class MyInterfaceImplementer implements MyInterface {}
|
||||
|
||||
private static class MyBaseInterfaceToStringConverter implements Converter<MyBaseInterface, String> {
|
||||
|
||||
@@ -820,55 +646,39 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class MyStringArrayToResourceArrayConverter implements Converter<String[], Resource[]> {
|
||||
private static class MyStringArrayToResourceArrayConverter implements Converter<String[], Resource[]> {
|
||||
|
||||
@Override
|
||||
public Resource[] convert(String[] source) {
|
||||
Resource[] result = new Resource[source.length];
|
||||
for (int i = 0; i < source.length; i++) {
|
||||
result[i] = new DescriptiveResource(source[i].substring(1));
|
||||
}
|
||||
return result;
|
||||
return Arrays.stream(source).map(s -> s.substring(1)).map(DescriptiveResource::new).toArray(Resource[]::new);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class MyStringArrayToIntegerArrayConverter implements Converter<String[], Integer[]> {
|
||||
private static class MyStringArrayToIntegerArrayConverter implements Converter<String[], Integer[]> {
|
||||
|
||||
@Override
|
||||
public Integer[] convert(String[] source) {
|
||||
Integer[] result = new Integer[source.length];
|
||||
for (int i = 0; i < source.length; i++) {
|
||||
result[i] = Integer.parseInt(source[i].substring(1));
|
||||
}
|
||||
return result;
|
||||
return Arrays.stream(source).map(s -> s.substring(1)).map(Integer::valueOf).toArray(Integer[]::new);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class MyStringToIntegerArrayConverter implements Converter<String, Integer[]> {
|
||||
|
||||
@Override
|
||||
public Integer[] convert(String source) {
|
||||
String[] srcArray = StringUtils.commaDelimitedListToStringArray(source);
|
||||
Integer[] result = new Integer[srcArray.length];
|
||||
for (int i = 0; i < srcArray.length; i++) {
|
||||
result[i] = Integer.parseInt(srcArray[i].substring(1));
|
||||
}
|
||||
return result;
|
||||
return Arrays.stream(srcArray).map(s -> s.substring(1)).map(Integer::valueOf).toArray(Integer[]::new);
|
||||
}
|
||||
}
|
||||
|
||||
private static class WithCopyConstructor {
|
||||
|
||||
public static class WithCopyConstructor {
|
||||
WithCopyConstructor() {}
|
||||
|
||||
public WithCopyConstructor() {
|
||||
}
|
||||
|
||||
public WithCopyConstructor(WithCopyConstructor value) {
|
||||
}
|
||||
@SuppressWarnings("unused")
|
||||
WithCopyConstructor(WithCopyConstructor value) {}
|
||||
}
|
||||
|
||||
private static class MyConditionalConverter implements Converter<String, Color>, ConditionalConverter {
|
||||
|
||||
private int matchAttempts = 0;
|
||||
@@ -889,10 +699,22 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class NonConditionalGenericConverter implements GenericConverter {
|
||||
|
||||
@Override
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyConditionalGenericConverter implements GenericConverter, ConditionalConverter {
|
||||
|
||||
private List<TypeDescriptor> sourceTypes = new ArrayList<TypeDescriptor>();
|
||||
private final List<TypeDescriptor> sourceTypes = new ArrayList<TypeDescriptor>();
|
||||
|
||||
@Override
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
@@ -915,7 +737,6 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class MyConditionalConverterFactory implements ConverterFactory<String, Color>, ConditionalConverter {
|
||||
|
||||
private MyConditionalConverter converter = new MyConditionalConverter();
|
||||
@@ -943,26 +764,21 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface MyEnumBaseInterface {
|
||||
|
||||
private static interface MyEnumBaseInterface {
|
||||
String getBaseCode();
|
||||
}
|
||||
|
||||
|
||||
interface MyEnumInterface extends MyEnumBaseInterface {
|
||||
|
||||
private static interface MyEnumInterface extends MyEnumBaseInterface {
|
||||
String getCode();
|
||||
}
|
||||
|
||||
|
||||
public static enum MyEnum implements MyEnumInterface {
|
||||
private static enum MyEnum implements MyEnumInterface {
|
||||
|
||||
A("1"),
|
||||
B("2"),
|
||||
C("3");
|
||||
|
||||
private String code;
|
||||
private final String code;
|
||||
|
||||
MyEnum(String code) {
|
||||
this.code = code;
|
||||
@@ -979,8 +795,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public enum EnumWithSubclass {
|
||||
private static enum EnumWithSubclass {
|
||||
|
||||
FIRST {
|
||||
@Override
|
||||
@@ -990,8 +805,8 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyStringToRawCollectionConverter implements Converter<String, Collection> {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static class MyStringToRawCollectionConverter implements Converter<String, Collection> {
|
||||
|
||||
@Override
|
||||
public Collection convert(String source) {
|
||||
@@ -999,8 +814,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyStringToGenericCollectionConverter implements Converter<String, Collection<?>> {
|
||||
private static class MyStringToGenericCollectionConverter implements Converter<String, Collection<?>> {
|
||||
|
||||
@Override
|
||||
public Collection<?> convert(String source) {
|
||||
@@ -1008,7 +822,6 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class MyEnumInterfaceToStringConverter<T extends MyEnumInterface> implements Converter<T, String> {
|
||||
|
||||
@Override
|
||||
@@ -1017,10 +830,9 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class StringToMyEnumInterfaceConverterFactory implements ConverterFactory<String, MyEnumInterface> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public <T extends MyEnumInterface> Converter<String, T> getConverter(Class<T> targetType) {
|
||||
return new StringToMyEnumInterfaceConverter(targetType);
|
||||
}
|
||||
@@ -1043,10 +855,9 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class StringToMyEnumBaseInterfaceConverterFactory implements ConverterFactory<String, MyEnumBaseInterface> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public <T extends MyEnumBaseInterface> Converter<String, T> getConverter(Class<T> targetType) {
|
||||
return new StringToMyEnumBaseInterfaceConverter(targetType);
|
||||
}
|
||||
@@ -1070,8 +881,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyStringToStringCollectionConverter implements Converter<String, Collection<String>> {
|
||||
private static class MyStringToStringCollectionConverter implements Converter<String, Collection<String>> {
|
||||
|
||||
@Override
|
||||
public Collection<String> convert(String source) {
|
||||
@@ -1079,8 +889,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyStringToIntegerCollectionConverter implements Converter<String, Collection<Integer>> {
|
||||
private static class MyStringToIntegerCollectionConverter implements Converter<String, Collection<Integer>> {
|
||||
|
||||
@Override
|
||||
public Collection<Integer> convert(String source) {
|
||||
@@ -1088,6 +897,20 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static class UntypedConverter implements Converter {
|
||||
|
||||
@Override
|
||||
public Object convert(Object source) {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ColorConverter implements Converter<String, Color> {
|
||||
@Override
|
||||
public Color convert(String source) { if (!source.startsWith("#")) source = "#" + source; return Color.decode(source); }
|
||||
}
|
||||
|
||||
|
||||
@ExampleAnnotation
|
||||
public String annotatedString;
|
||||
@@ -1098,8 +921,7 @@ public class GenericConversionServiceTests {
|
||||
|
||||
public Map<String, ?> wildcardMap;
|
||||
|
||||
public EnumSet<MyEnum> enumSet;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Collection rawCollection;
|
||||
|
||||
public Collection<?> genericCollection;
|
||||
|
||||
Reference in New Issue
Block a user