Skip to content

Commit a53df0b

Browse files
committed
Added explicit tests for generic and raw collection converters
Issue: SPR-11369
1 parent 1a01161 commit a53df0b

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

spring-core/src/main/java/org/springframework/core/convert/converter/ConditionalGenericConverter.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,17 +19,16 @@
1919
import org.springframework.core.convert.TypeDescriptor;
2020

2121
/**
22-
* A {@link GenericConverter} that may conditionally execute based on attributes of the
23-
* {@code source} and {@code target} {@link TypeDescriptor}. See
24-
* {@link ConditionalConverter} for details.
22+
* A {@link GenericConverter} that may conditionally execute based on attributes
23+
* of the {@code source} and {@code target} {@link TypeDescriptor}.
24+
* See {@link ConditionalConverter} for details.
2525
*
2626
* @author Keith Donald
2727
* @author Phillip Webb
2828
* @since 3.0
2929
* @see GenericConverter
3030
* @see ConditionalConverter
3131
*/
32-
public interface ConditionalGenericConverter extends GenericConverter,
33-
ConditionalConverter {
32+
public interface ConditionalGenericConverter extends GenericConverter, ConditionalConverter {
3433

3534
}

spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ public void convertPrimitiveArray() {
619619
GenericConversionService conversionService = new DefaultConversionService();
620620
byte[] byteArray = new byte[] { 1, 2, 3 };
621621
Byte[] converted = conversionService.convert(byteArray, Byte[].class);
622-
assertTrue(Arrays.equals(converted, new Byte[] { 1, 2, 3 }));
622+
assertTrue(Arrays.equals(converted, new Byte[] {1, 2, 3}));
623623
}
624624

625625
@Test
@@ -739,7 +739,7 @@ public Byte convert(Byte source) {
739739
byte[] byteArray = new byte[] { 1, 2, 3 };
740740
byte[] converted = conversionService.convert(byteArray, byte[].class);
741741
assertNotSame(byteArray, converted);
742-
assertTrue(Arrays.equals(new byte[] { 2, 3, 4 }, converted));
742+
assertTrue(Arrays.equals(new byte[] {2, 3, 4}, converted));
743743
}
744744

745745
@Test
@@ -769,8 +769,11 @@ public void convertNullAnnotatedStringToString() throws Exception {
769769

770770
@Test
771771
public void multipleCollectionTypesFromSameSourceType() throws Exception {
772+
conversionService.addConverter(new MyStringToRawCollectionConverter());
773+
conversionService.addConverter(new MyStringToGenericCollectionConverter());
772774
conversionService.addConverter(new MyStringToStringCollectionConverter());
773775
conversionService.addConverter(new MyStringToIntegerCollectionConverter());
776+
774777
assertEquals(Collections.singleton(4), // should be "testX" from MyStringToStringCollectionConverter, ideally
775778
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
776779
assertEquals(Collections.singleton(4),
@@ -788,6 +791,7 @@ public void multipleCollectionTypesFromSameSourceType() throws Exception {
788791
@Test
789792
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
790793
conversionService.addConverter(new MyStringToStringCollectionConverter());
794+
791795
assertEquals(Collections.singleton("testX"),
792796
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
793797
assertEquals(Collections.singleton("testX"),
@@ -800,6 +804,42 @@ public void adaptedCollectionTypesFromSameSourceType() throws Exception {
800804
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
801805
assertEquals(Collections.singleton("testX"),
802806
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
807+
808+
// The following is unpleasant but simply a consequence of the raw type matching algorithm in Spring 3.x
809+
assertEquals(Collections.singleton("testX"),
810+
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection"))));
811+
}
812+
813+
@Test
814+
public void genericCollectionAsSource() throws Exception {
815+
conversionService.addConverter(new MyStringToGenericCollectionConverter());
816+
817+
assertEquals(Collections.singleton("testX"),
818+
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
819+
assertEquals(Collections.singleton("testX"),
820+
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
821+
assertEquals(Collections.singleton("testX"),
822+
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
823+
824+
// The following is unpleasant but a consequence of the generic collection converter above...
825+
assertEquals(Collections.singleton("testX"),
826+
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection"))));
827+
}
828+
829+
@Test
830+
public void rawCollectionAsSource() throws Exception {
831+
conversionService.addConverter(new MyStringToRawCollectionConverter());
832+
833+
assertEquals(Collections.singleton("testX"),
834+
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
835+
assertEquals(Collections.singleton("testX"),
836+
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
837+
assertEquals(Collections.singleton("testX"),
838+
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
839+
840+
// The following is unpleasant but a consequence of the raw collection converter above...
841+
assertEquals(Collections.singleton("testX"),
842+
conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection"))));
803843
}
804844

805845

@@ -810,6 +850,7 @@ public void adaptedCollectionTypesFromSameSourceType() throws Exception {
810850
public static @interface ExampleAnnotation {
811851
}
812852

853+
813854
private static class MyConditionalConverter implements Converter<String, Color>, ConditionalConverter {
814855

815856
private int matchAttempts = 0;
@@ -830,8 +871,8 @@ public int getMatchAttempts() {
830871
}
831872
}
832873

833-
private static class MyConditionalGenericConverter implements GenericConverter,
834-
ConditionalConverter {
874+
875+
private static class MyConditionalGenericConverter implements GenericConverter, ConditionalConverter {
835876

836877
private List<TypeDescriptor> sourceTypes = new ArrayList<TypeDescriptor>();
837878

@@ -857,8 +898,8 @@ public List<TypeDescriptor> getSourceTypes() {
857898
}
858899
}
859900

860-
private static class MyConditionalConverterFactory implements
861-
ConverterFactory<String, Color>, ConditionalConverter {
901+
902+
private static class MyConditionalConverterFactory implements ConverterFactory<String, Color>, ConditionalConverter {
862903

863904
private MyConditionalConverter converter = new MyConditionalConverter();
864905

@@ -885,6 +926,7 @@ public int getNestedMatchAttempts() {
885926
}
886927
}
887928

929+
888930
interface MyEnumInterface {
889931

890932
String getCode();
@@ -900,6 +942,23 @@ public String getCode() {
900942
}
901943
}
902944

945+
946+
public static class MyStringToRawCollectionConverter implements Converter<String, Collection> {
947+
948+
@Override
949+
public Collection convert(String source) {
950+
return Collections.singleton(source + "X");
951+
}
952+
}
953+
954+
public static class MyStringToGenericCollectionConverter implements Converter<String, Collection<?>> {
955+
956+
@Override
957+
public Collection<?> convert(String source) {
958+
return Collections.singleton(source + "X");
959+
}
960+
}
961+
903962
private static class MyEnumInterfaceToStringConverter<T extends MyEnumInterface> implements Converter<T, String> {
904963

905964
@Override
@@ -924,12 +983,13 @@ public Collection<Integer> convert(String source) {
924983
}
925984
}
926985

927-
public Collection<String> stringCollection;
928-
929-
public Collection<Integer> integerCollection;
930986

931987
public Collection rawCollection;
932988

933989
public Collection<?> genericCollection;
934990

991+
public Collection<String> stringCollection;
992+
993+
public Collection<Integer> integerCollection;
994+
935995
}

0 commit comments

Comments
 (0)