Added explicit tests for generic and raw collection converters
Issue: SPR-11369
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -19,9 +19,9 @@ package org.springframework.core.convert.converter;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
/**
|
||||
* A {@link GenericConverter} that may conditionally execute based on attributes of the
|
||||
* {@code source} and {@code target} {@link TypeDescriptor}. See
|
||||
* {@link ConditionalConverter} for details.
|
||||
* A {@link GenericConverter} that may conditionally execute based on attributes
|
||||
* of the {@code source} and {@code target} {@link TypeDescriptor}.
|
||||
* See {@link ConditionalConverter} for details.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Phillip Webb
|
||||
@@ -29,7 +29,6 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||
* @see GenericConverter
|
||||
* @see ConditionalConverter
|
||||
*/
|
||||
public interface ConditionalGenericConverter extends GenericConverter,
|
||||
ConditionalConverter {
|
||||
public interface ConditionalGenericConverter extends GenericConverter, ConditionalConverter {
|
||||
|
||||
}
|
||||
|
||||
@@ -619,7 +619,7 @@ public class GenericConversionServiceTests {
|
||||
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 }));
|
||||
assertTrue(Arrays.equals(converted, new Byte[] {1, 2, 3}));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -739,7 +739,7 @@ public class GenericConversionServiceTests {
|
||||
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));
|
||||
assertTrue(Arrays.equals(new byte[] {2, 3, 4}, converted));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -769,8 +769,11 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void multipleCollectionTypesFromSameSourceType() throws Exception {
|
||||
conversionService.addConverter(new MyStringToRawCollectionConverter());
|
||||
conversionService.addConverter(new MyStringToGenericCollectionConverter());
|
||||
conversionService.addConverter(new MyStringToStringCollectionConverter());
|
||||
conversionService.addConverter(new MyStringToIntegerCollectionConverter());
|
||||
|
||||
assertEquals(Collections.singleton(4), // should be "testX" from MyStringToStringCollectionConverter, ideally
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
|
||||
assertEquals(Collections.singleton(4),
|
||||
@@ -788,6 +791,7 @@ public class GenericConversionServiceTests {
|
||||
@Test
|
||||
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
|
||||
conversionService.addConverter(new MyStringToStringCollectionConverter());
|
||||
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
@@ -800,6 +804,42 @@ public class GenericConversionServiceTests {
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
|
||||
|
||||
// The following is unpleasant but simply a consequence of the raw type matching algorithm in Spring 3.x
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void genericCollectionAsSource() throws Exception {
|
||||
conversionService.addConverter(new MyStringToGenericCollectionConverter());
|
||||
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
|
||||
|
||||
// The following is unpleasant but a consequence of the generic collection converter above...
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rawCollectionAsSource() throws Exception {
|
||||
conversionService.addConverter(new MyStringToRawCollectionConverter());
|
||||
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
|
||||
|
||||
// The following is unpleasant but a consequence of the raw collection converter above...
|
||||
assertEquals(Collections.singleton("testX"),
|
||||
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection"))));
|
||||
}
|
||||
|
||||
|
||||
@@ -810,6 +850,7 @@ public class GenericConversionServiceTests {
|
||||
public static @interface ExampleAnnotation {
|
||||
}
|
||||
|
||||
|
||||
private static class MyConditionalConverter implements Converter<String, Color>, ConditionalConverter {
|
||||
|
||||
private int matchAttempts = 0;
|
||||
@@ -830,8 +871,8 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyConditionalGenericConverter implements GenericConverter,
|
||||
ConditionalConverter {
|
||||
|
||||
private static class MyConditionalGenericConverter implements GenericConverter, ConditionalConverter {
|
||||
|
||||
private List<TypeDescriptor> sourceTypes = new ArrayList<TypeDescriptor>();
|
||||
|
||||
@@ -857,8 +898,8 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyConditionalConverterFactory implements
|
||||
ConverterFactory<String, Color>, ConditionalConverter {
|
||||
|
||||
private static class MyConditionalConverterFactory implements ConverterFactory<String, Color>, ConditionalConverter {
|
||||
|
||||
private MyConditionalConverter converter = new MyConditionalConverter();
|
||||
|
||||
@@ -885,6 +926,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface MyEnumInterface {
|
||||
|
||||
String getCode();
|
||||
@@ -900,6 +942,23 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyStringToRawCollectionConverter implements Converter<String, Collection> {
|
||||
|
||||
@Override
|
||||
public Collection convert(String source) {
|
||||
return Collections.singleton(source + "X");
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyStringToGenericCollectionConverter implements Converter<String, Collection<?>> {
|
||||
|
||||
@Override
|
||||
public Collection<?> convert(String source) {
|
||||
return Collections.singleton(source + "X");
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyEnumInterfaceToStringConverter<T extends MyEnumInterface> implements Converter<T, String> {
|
||||
|
||||
@Override
|
||||
@@ -924,12 +983,13 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<String> stringCollection;
|
||||
|
||||
public Collection<Integer> integerCollection;
|
||||
|
||||
public Collection rawCollection;
|
||||
|
||||
public Collection<?> genericCollection;
|
||||
|
||||
public Collection<String> stringCollection;
|
||||
|
||||
public Collection<Integer> integerCollection;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user