|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
|
* Copyright 2002-2019 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. |
|
|
|
@ -134,7 +134,7 @@ public class DefaultConversionServiceTests {
@@ -134,7 +134,7 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testStringToByte() throws Exception { |
|
|
|
|
public void testStringToByte() { |
|
|
|
|
assertEquals(Byte.valueOf("1"), conversionService.convert("1", Byte.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -225,12 +225,12 @@ public class DefaultConversionServiceTests {
@@ -225,12 +225,12 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testStringToEnum() throws Exception { |
|
|
|
|
public void testStringToEnum() { |
|
|
|
|
assertEquals(Foo.BAR, conversionService.convert("BAR", Foo.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testStringToEnumWithSubclass() throws Exception { |
|
|
|
|
public void testStringToEnumWithSubclass() { |
|
|
|
|
assertEquals(SubFoo.BAZ, conversionService.convert("BAZ", SubFoo.BAR.getClass())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -245,12 +245,12 @@ public class DefaultConversionServiceTests {
@@ -245,12 +245,12 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testIntegerToEnum() throws Exception { |
|
|
|
|
public void testIntegerToEnum() { |
|
|
|
|
assertEquals(Foo.BAR, conversionService.convert(0, Foo.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testIntegerToEnumWithSubclass() throws Exception { |
|
|
|
|
public void testIntegerToEnumWithSubclass() { |
|
|
|
|
assertEquals(SubFoo.BAZ, conversionService.convert(1, SubFoo.BAR.getClass())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -395,10 +395,6 @@ public class DefaultConversionServiceTests {
@@ -395,10 +395,6 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
conversionService.convert(new String[]{"1", "2", "3"}, AbstractList.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static enum FooEnum { |
|
|
|
|
BAR, BAZ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertArrayToString() { |
|
|
|
|
String result = conversionService.convert(new String[] {"1", "2", "3"}, String.class); |
|
|
|
@ -525,9 +521,8 @@ public class DefaultConversionServiceTests {
@@ -525,9 +521,8 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
|
public void convertStringToCollection() { |
|
|
|
|
List result = conversionService.convert("1,2,3", List.class); |
|
|
|
|
List<?> result = conversionService.convert("1,2,3", List.class); |
|
|
|
|
assertEquals(3, result.size()); |
|
|
|
|
assertEquals("1", result.get(0)); |
|
|
|
|
assertEquals("2", result.get(1)); |
|
|
|
@ -535,9 +530,8 @@ public class DefaultConversionServiceTests {
@@ -535,9 +530,8 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
|
public void convertStringToCollectionWithElementConversion() throws Exception { |
|
|
|
|
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), |
|
|
|
|
List<?> result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), |
|
|
|
|
new TypeDescriptor(getClass().getField("genericList"))); |
|
|
|
|
assertEquals(3, result.size()); |
|
|
|
|
assertEquals(1, result.get(0)); |
|
|
|
@ -546,9 +540,8 @@ public class DefaultConversionServiceTests {
@@ -546,9 +540,8 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
|
public void convertEmptyStringToCollection() { |
|
|
|
|
Collection result = conversionService.convert("", Collection.class); |
|
|
|
|
Collection<?> result = conversionService.convert("", Collection.class); |
|
|
|
|
assertEquals(0, result.size()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -575,25 +568,18 @@ public class DefaultConversionServiceTests {
@@ -575,25 +568,18 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
|
public void convertCollectionToObjectWithCustomConverter() throws Exception { |
|
|
|
|
public void convertCollectionToObjectWithCustomConverter() { |
|
|
|
|
List<String> source = new ArrayList<>(); |
|
|
|
|
source.add("A"); |
|
|
|
|
source.add("B"); |
|
|
|
|
conversionService.addConverter(new Converter<List, ListWrapper>() { |
|
|
|
|
@Override |
|
|
|
|
public ListWrapper convert(List source) { |
|
|
|
|
return new ListWrapper(source); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
conversionService.addConverter(List.class, ListWrapper.class, ListWrapper::new); |
|
|
|
|
ListWrapper result = conversionService.convert(source, ListWrapper.class); |
|
|
|
|
assertSame(source, result.getList()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
|
public void convertObjectToCollection() { |
|
|
|
|
List result = conversionService.convert(3L, List.class); |
|
|
|
|
List<?> result = conversionService.convert(3L, List.class); |
|
|
|
|
assertEquals(1, result.size()); |
|
|
|
|
assertEquals(3L, result.get(0)); |
|
|
|
|
} |
|
|
|
@ -608,7 +594,7 @@ public class DefaultConversionServiceTests {
@@ -608,7 +594,7 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertArrayToArray() { |
|
|
|
|
public void convertStringArrayToIntegerArray() { |
|
|
|
|
Integer[] result = conversionService.convert(new String[] {"1", "2", "3"}, Integer[].class); |
|
|
|
|
assertEquals(Integer.valueOf(1), result[0]); |
|
|
|
|
assertEquals(Integer.valueOf(2), result[1]); |
|
|
|
@ -616,7 +602,7 @@ public class DefaultConversionServiceTests {
@@ -616,7 +602,7 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertArrayToPrimitiveArray() { |
|
|
|
|
public void convertStringArrayToIntArray() { |
|
|
|
|
int[] result = conversionService.convert(new String[] {"1", "2", "3"}, int[].class); |
|
|
|
|
assertEquals(1, result[0]); |
|
|
|
|
assertEquals(2, result[1]); |
|
|
|
@ -624,7 +610,39 @@ public class DefaultConversionServiceTests {
@@ -624,7 +610,39 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertArrayToWrapperArray() { |
|
|
|
|
public void convertIntegerArrayToIntegerArray() { |
|
|
|
|
Integer[] result = conversionService.convert(new Integer[] {1, 2, 3}, Integer[].class); |
|
|
|
|
assertEquals(Integer.valueOf(1), result[0]); |
|
|
|
|
assertEquals(Integer.valueOf(2), result[1]); |
|
|
|
|
assertEquals(Integer.valueOf(3), result[2]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertIntegerArrayToIntArray() { |
|
|
|
|
int[] result = conversionService.convert(new Integer[] {1, 2, 3}, int[].class); |
|
|
|
|
assertEquals(1, result[0]); |
|
|
|
|
assertEquals(2, result[1]); |
|
|
|
|
assertEquals(3, result[2]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertObjectArrayToIntegerArray() { |
|
|
|
|
Integer[] result = conversionService.convert(new Object[] {1, 2, 3}, Integer[].class); |
|
|
|
|
assertEquals(Integer.valueOf(1), result[0]); |
|
|
|
|
assertEquals(Integer.valueOf(2), result[1]); |
|
|
|
|
assertEquals(Integer.valueOf(3), result[2]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertObjectArrayToIntArray() { |
|
|
|
|
int[] result = conversionService.convert(new Object[] {1, 2, 3}, int[].class); |
|
|
|
|
assertEquals(1, result[0]); |
|
|
|
|
assertEquals(2, result[1]); |
|
|
|
|
assertEquals(3, result[2]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertByteArrayToWrapperArray() { |
|
|
|
|
byte[] byteArray = new byte[] {1, 2, 3}; |
|
|
|
|
Byte[] converted = conversionService.convert(byteArray, Byte[].class); |
|
|
|
|
assertThat(converted, equalTo(new Byte[]{1, 2, 3})); |
|
|
|
@ -694,7 +712,7 @@ public class DefaultConversionServiceTests {
@@ -694,7 +712,7 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
|
public void convertCollectionToCollectionNotGeneric() throws Exception { |
|
|
|
|
public void convertCollectionToCollectionNotGeneric() { |
|
|
|
|
Set<String> foo = new LinkedHashSet<>(); |
|
|
|
|
foo.add("1"); |
|
|
|
|
foo.add("2"); |
|
|
|
@ -740,10 +758,10 @@ public class DefaultConversionServiceTests {
@@ -740,10 +758,10 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
foo.put("1", "BAR"); |
|
|
|
|
foo.put("2", "BAZ"); |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
Map<Integer, FooEnum> map = (Map<Integer, FooEnum>) conversionService.convert(foo, |
|
|
|
|
Map<Integer, Foo> map = (Map<Integer, Foo>) conversionService.convert(foo, |
|
|
|
|
TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericMap"))); |
|
|
|
|
assertEquals(FooEnum.BAR, map.get(1)); |
|
|
|
|
assertEquals(FooEnum.BAZ, map.get(2)); |
|
|
|
|
assertEquals(Foo.BAR, map.get(1)); |
|
|
|
|
assertEquals(Foo.BAZ, map.get(2)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -881,25 +899,20 @@ public class DefaultConversionServiceTests {
@@ -881,25 +899,20 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertCharArrayToString() throws Exception { |
|
|
|
|
public void convertCharArrayToString() { |
|
|
|
|
String converted = conversionService.convert(new char[] {'a', 'b', 'c'}, String.class); |
|
|
|
|
assertThat(converted, equalTo("a,b,c")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertStringToCharArray() throws Exception { |
|
|
|
|
public void convertStringToCharArray() { |
|
|
|
|
char[] converted = conversionService.convert("a,b,c", char[].class); |
|
|
|
|
assertThat(converted, equalTo(new char[]{'a', 'b', 'c'})); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertStringToCustomCharArray() throws Exception { |
|
|
|
|
conversionService.addConverter(new Converter<String, char[]>() { |
|
|
|
|
@Override |
|
|
|
|
public char[] convert(String source) { |
|
|
|
|
return source.toCharArray(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
public void convertStringToCustomCharArray() { |
|
|
|
|
conversionService.addConverter(String.class, char[].class, String::toCharArray); |
|
|
|
|
char[] converted = conversionService.convert("abc", char[].class); |
|
|
|
|
assertThat(converted, equalTo(new char[] {'a', 'b', 'c'})); |
|
|
|
|
} |
|
|
|
@ -916,16 +929,11 @@ public class DefaultConversionServiceTests {
@@ -916,16 +929,11 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void convertCannotOptimizeArray() { |
|
|
|
|
conversionService.addConverter(new Converter<Byte, Byte>() { |
|
|
|
|
@Override |
|
|
|
|
public Byte convert(Byte source) { |
|
|
|
|
return (byte) (source + 1); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
conversionService.addConverter(Byte.class, Byte.class, source -> (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)); |
|
|
|
|
assertArrayEquals(new byte[]{2, 3, 4}, converted); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -977,7 +985,7 @@ public class DefaultConversionServiceTests {
@@ -977,7 +985,7 @@ public class DefaultConversionServiceTests {
|
|
|
|
|
|
|
|
|
|
public Stream<Integer> genericStream; |
|
|
|
|
|
|
|
|
|
public Map<Integer, FooEnum> genericMap = new HashMap<>(); |
|
|
|
|
public Map<Integer, Foo> genericMap = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
public EnumSet<Foo> enumSet; |
|
|
|
|
|
|
|
|
|