Skip to content

Commit 64bd8b7

Browse files
committed
Polishing
1 parent 387c8a8 commit 64bd8b7

File tree

4 files changed

+103
-63
lines changed

4 files changed

+103
-63
lines changed

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

+20-20
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,25 @@ public void testStringToEnumSet() throws Exception {
262262
}
263263

264264

265+
public ArrayList<Integer> scalarListTarget;
266+
267+
public List<Integer> emptyListTarget;
268+
269+
public LinkedList<Integer> emptyListDifferentTarget;
270+
271+
public List<List<List<Integer>>> objectToCollection;
272+
273+
public List<String> strings;
274+
275+
public List<?> list = Collections.emptyList();
276+
277+
public Collection<?> wildcardCollection = Collections.emptyList();
278+
279+
public List<Resource> resources;
280+
281+
public EnumSet<MyEnum> enumSet;
282+
283+
265284
public static abstract class BaseResource implements Resource {
266285

267286
@Override
@@ -330,25 +349,6 @@ public static class TestResource extends BaseResource {
330349
}
331350

332351

333-
public static enum MyEnum {A, B, C}
334-
335-
336-
public ArrayList<Integer> scalarListTarget;
337-
338-
public List<Integer> emptyListTarget;
339-
340-
public LinkedList<Integer> emptyListDifferentTarget;
341-
342-
public List<List<List<Integer>>> objectToCollection;
343-
344-
public List<String> strings;
345-
346-
public List<?> list = Collections.emptyList();
347-
348-
public Collection<?> wildcardCollection = Collections.emptyList();
349-
350-
public List<Resource> resources;
351-
352-
public EnumSet<MyEnum> enumSet;
352+
public enum MyEnum {A, B, C}
353353

354354
}

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

+22-17
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public void testCharacterToNumber() {
313313

314314
@Test
315315
public void convertArrayToCollectionInterface() {
316-
List<?> result = conversionService.convert(new String[] { "1", "2", "3" }, List.class);
316+
List<?> result = conversionService.convert(new String[] {"1", "2", "3"}, List.class);
317317
assertEquals("1", result.get(0));
318318
assertEquals("2", result.get(1));
319319
assertEquals("3", result.get(2));
@@ -322,7 +322,7 @@ public void convertArrayToCollectionInterface() {
322322
@Test
323323
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
324324
@SuppressWarnings("unchecked")
325-
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor
325+
List<Integer> result = (List<Integer>) conversionService.convert(new String[] {"1", "2", "3"}, TypeDescriptor
326326
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
327327
assertEquals(new Integer("1"), result.get(0));
328328
assertEquals(new Integer("2"), result.get(1));
@@ -344,7 +344,7 @@ public void testSpr7766() throws Exception {
344344
ConverterRegistry registry = (conversionService);
345345
registry.addConverter(new ColorConverter());
346346
@SuppressWarnings("unchecked")
347-
List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" },
347+
List<Color> colors = (List<Color>) conversionService.convert(new String[] {"ffffff", "#000000"},
348348
TypeDescriptor.valueOf(String[].class),
349349
new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
350350
assertEquals(2, colors.size());
@@ -354,7 +354,7 @@ public void testSpr7766() throws Exception {
354354

355355
@Test
356356
public void convertArrayToCollectionImpl() {
357-
LinkedList<?> result = conversionService.convert(new String[] { "1", "2", "3" }, LinkedList.class);
357+
LinkedList<?> result = conversionService.convert(new String[] {"1", "2", "3"}, LinkedList.class);
358358
assertEquals("1", result.get(0));
359359
assertEquals("2", result.get(1));
360360
assertEquals("3", result.get(2));
@@ -371,13 +371,13 @@ public static enum FooEnum {
371371

372372
@Test
373373
public void convertArrayToString() {
374-
String result = conversionService.convert(new String[] { "1", "2", "3" }, String.class);
374+
String result = conversionService.convert(new String[] {"1", "2", "3"}, String.class);
375375
assertEquals("1,2,3", result);
376376
}
377377

378378
@Test
379379
public void convertArrayToStringWithElementConversion() {
380-
String result = conversionService.convert(new Integer[] { 1, 2, 3 }, String.class);
380+
String result = conversionService.convert(new Integer[] {1, 2, 3}, String.class);
381381
assertEquals("1,2,3", result);
382382
}
383383

@@ -422,21 +422,21 @@ public void convertEmptyStringToArray() {
422422

423423
@Test
424424
public void convertArrayToObject() {
425-
Object[] array = new Object[] { 3L };
425+
Object[] array = new Object[] {3L};
426426
Object result = conversionService.convert(array, Long.class);
427427
assertEquals(3L, result);
428428
}
429429

430430
@Test
431431
public void convertArrayToObjectWithElementConversion() {
432-
String[] array = new String[] { "3" };
432+
String[] array = new String[] {"3"};
433433
Integer result = conversionService.convert(array, Integer.class);
434434
assertEquals(new Integer(3), result);
435435
}
436436

437437
@Test
438438
public void convertArrayToObjectAssignableTargetType() {
439-
Long[] array = new Long[] { 3L };
439+
Long[] array = new Long[] {3L};
440440
Long[] result = (Long[]) conversionService.convert(array, Object.class);
441441
assertArrayEquals(array, result);
442442
}
@@ -849,14 +849,14 @@ public char[] convert(String source) {
849849
}
850850
});
851851
char[] converted = conversionService.convert("abc", char[].class);
852-
assertThat(converted, equalTo(new char[] { 'a', 'b', 'c' }));
852+
assertThat(converted, equalTo(new char[] {'a', 'b', 'c'}));
853853
}
854854

855855
@Test
856856
@SuppressWarnings("unchecked")
857857
public void multidimensionalArrayToListConversionShouldConvertEntriesCorrectly() {
858-
String[][] grid = new String[][] { new String[] { "1", "2", "3", "4" }, new String[] { "5", "6", "7", "8" },
859-
new String[] { "9", "10", "11", "12" } };
858+
String[][] grid = new String[][] {new String[] {"1", "2", "3", "4"}, new String[] {"5", "6", "7", "8"},
859+
new String[] {"9", "10", "11", "12"}};
860860
List<String[]> converted = conversionService.convert(grid, List.class);
861861
String[][] convertedBack = conversionService.convert(converted, String[][].class);
862862
assertArrayEquals(grid, convertedBack);
@@ -865,16 +865,15 @@ public void multidimensionalArrayToListConversionShouldConvertEntriesCorrectly()
865865
@Test
866866
public void convertCannotOptimizeArray() {
867867
conversionService.addConverter(new Converter<Byte, Byte>() {
868-
869868
@Override
870869
public Byte convert(Byte source) {
871870
return (byte) (source + 1);
872871
}
873872
});
874-
byte[] byteArray = new byte[] { 1, 2, 3 };
873+
byte[] byteArray = new byte[] {1, 2, 3};
875874
byte[] converted = conversionService.convert(byteArray, byte[].class);
876875
assertNotSame(byteArray, converted);
877-
assertTrue(Arrays.equals(new byte[] { 2, 3, 4 }, converted));
876+
assertTrue(Arrays.equals(new byte[] {2, 3, 4}, converted));
878877
}
879878

880879
@Test
@@ -938,6 +937,7 @@ public void handlerMethod(List<Color> color) {
938937

939938

940939
public enum Foo {
940+
941941
BAR, BAZ
942942
}
943943

@@ -964,7 +964,12 @@ String s() {
964964
public class ColorConverter implements Converter<String, Color> {
965965

966966
@Override
967-
public Color convert(String source) { if (!source.startsWith("#")) source = "#" + source; return Color.decode(source); }
967+
public Color convert(String source) {
968+
if (!source.startsWith("#")) {
969+
source = "#" + source;
970+
}
971+
return Color.decode(source);
972+
}
968973
}
969974

970975

@@ -1031,8 +1036,8 @@ public List<?> getList() {
10311036
private static class SSN {
10321037

10331038
static int constructorCount = 0;
1034-
static int toStringCount = 0;
10351039

1040+
static int toStringCount = 0;
10361041

10371042
static void reset() {
10381043
constructorCount = 0;

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

+46-20
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@
3636
import static org.hamcrest.Matchers.*;
3737
import static org.junit.Assert.*;
3838

39+
/**
40+
* @author Keith Donald
41+
* @author Phil Webb
42+
* @author Juergen Hoeller
43+
*/
3944
public class MapToMapConverterTests {
4045

41-
private GenericConversionService conversionService = new GenericConversionService();
46+
private final GenericConversionService conversionService = new GenericConversionService();
4247

4348

4449
@Before
@@ -54,12 +59,15 @@ public void scalarMap() throws Exception {
5459
map.put("2", "37");
5560
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
5661
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
62+
5763
assertTrue(conversionService.canConvert(sourceType, targetType));
5864
try {
5965
conversionService.convert(map, sourceType, targetType);
60-
} catch (ConversionFailedException e) {
61-
assertTrue(e.getCause() instanceof ConverterNotFoundException);
6266
}
67+
catch (ConversionFailedException ex) {
68+
assertTrue(ex.getCause() instanceof ConverterNotFoundException);
69+
}
70+
6371
conversionService.addConverterFactory(new StringToNumberConverterFactory());
6472
assertTrue(conversionService.canConvert(sourceType, targetType));
6573
@SuppressWarnings("unchecked")
@@ -74,6 +82,7 @@ public void scalarMapNotGenericTarget() throws Exception {
7482
Map<String, String> map = new HashMap<String, String>();
7583
map.put("1", "9");
7684
map.put("2", "37");
85+
7786
assertTrue(conversionService.canConvert(Map.class, Map.class));
7887
assertSame(map, conversionService.convert(map, Map.class));
7988
}
@@ -85,12 +94,15 @@ public void scalarMapNotGenericSourceField() throws Exception {
8594
map.put("2", "37");
8695
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
8796
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
97+
8898
assertTrue(conversionService.canConvert(sourceType, targetType));
8999
try {
90100
conversionService.convert(map, sourceType, targetType);
91-
} catch (ConversionFailedException e) {
92-
assertTrue(e.getCause() instanceof ConverterNotFoundException);
93101
}
102+
catch (ConversionFailedException ex) {
103+
assertTrue(ex.getCause() instanceof ConverterNotFoundException);
104+
}
105+
94106
conversionService.addConverterFactory(new StringToNumberConverterFactory());
95107
assertTrue(conversionService.canConvert(sourceType, targetType));
96108
@SuppressWarnings("unchecked")
@@ -107,12 +119,15 @@ public void collectionMap() throws Exception {
107119
map.put("2", Arrays.asList("37", "23"));
108120
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
109121
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
122+
110123
assertTrue(conversionService.canConvert(sourceType, targetType));
111124
try {
112125
conversionService.convert(map, sourceType, targetType);
113-
} catch (ConversionFailedException e) {
114-
assertTrue(e.getCause() instanceof ConverterNotFoundException);
115126
}
127+
catch (ConversionFailedException ex) {
128+
assertTrue(ex.getCause() instanceof ConverterNotFoundException);
129+
}
130+
116131
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
117132
conversionService.addConverterFactory(new StringToNumberConverterFactory());
118133
assertTrue(conversionService.canConvert(sourceType, targetType));
@@ -130,6 +145,7 @@ public void collectionMapSourceTarget() throws Exception {
130145
map.put("2", Arrays.asList("37", "23"));
131146
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
132147
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
148+
133149
assertFalse(conversionService.canConvert(sourceType, targetType));
134150
try {
135151
conversionService.convert(map, sourceType, targetType);
@@ -138,6 +154,7 @@ public void collectionMapSourceTarget() throws Exception {
138154
catch (ConverterNotFoundException ex) {
139155
// expected
140156
}
157+
141158
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
142159
conversionService.addConverterFactory(new StringToNumberConverterFactory());
143160
assertTrue(conversionService.canConvert(sourceType, targetType));
@@ -153,6 +170,7 @@ public void collectionMapNotGenericTarget() throws Exception {
153170
Map<String, List<String>> map = new HashMap<String, List<String>>();
154171
map.put("1", Arrays.asList("9", "12"));
155172
map.put("2", Arrays.asList("37", "23"));
173+
156174
assertTrue(conversionService.canConvert(Map.class, Map.class));
157175
assertSame(map, conversionService.convert(map, Map.class));
158176
}
@@ -164,6 +182,7 @@ public void collectionMapNotGenericTargetCollectionToObjectInteraction() throws
164182
map.put("2", Arrays.asList("37", "23"));
165183
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
166184
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
185+
167186
assertTrue(conversionService.canConvert(Map.class, Map.class));
168187
assertSame(map, conversionService.convert(map, Map.class));
169188
}
@@ -173,13 +192,15 @@ public void emptyMap() throws Exception {
173192
Map<String, String> map = new HashMap<String, String>();
174193
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
175194
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapTarget"));
195+
176196
assertTrue(conversionService.canConvert(sourceType, targetType));
177197
assertSame(map, conversionService.convert(map, sourceType, targetType));
178198
}
179199

180200
@Test
181201
public void emptyMapNoTargetGenericInfo() throws Exception {
182202
Map<String, String> map = new HashMap<String, String>();
203+
183204
assertTrue(conversionService.canConvert(Map.class, Map.class));
184205
assertSame(map, conversionService.convert(map, Map.class));
185206
}
@@ -189,6 +210,7 @@ public void emptyMapDifferentTargetImplType() throws Exception {
189210
Map<String, String> map = new HashMap<String, String>();
190211
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
191212
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget"));
213+
192214
assertTrue(conversionService.canConvert(sourceType, targetType));
193215
@SuppressWarnings("unchecked")
194216
LinkedHashMap<String, String> result = (LinkedHashMap<String, String>) conversionService.convert(map, sourceType, targetType);
@@ -205,6 +227,7 @@ public void noDefaultConstructorCopyNotRequired() throws Exception {
205227
TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
206228
TypeDescriptor targetType = TypeDescriptor.map(NoDefaultConstructorMap.class,
207229
TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
230+
208231
assertTrue(conversionService.canConvert(sourceType, targetType));
209232
@SuppressWarnings("unchecked")
210233
Map<String, Integer> result = (Map<String, Integer>) conversionService.convert(map, sourceType, targetType);
@@ -220,6 +243,7 @@ public void multiValueMapToMultiValueMap() throws Exception {
220243
source.put("a", Arrays.asList(1, 2, 3));
221244
source.put("b", Arrays.asList(4, 5, 6));
222245
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
246+
223247
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
224248
assertThat(converted.size(), equalTo(2));
225249
assertThat(converted.get("a"), equalTo(Arrays.asList("1", "2", "3")));
@@ -234,6 +258,7 @@ public void mapToMultiValueMap() throws Exception {
234258
source.put("a", 1);
235259
source.put("b", 2);
236260
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
261+
237262
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
238263
assertThat(converted.size(), equalTo(2));
239264
assertThat(converted.get("a"), equalTo(Arrays.asList("1")));
@@ -249,23 +274,12 @@ public void testStringToEnumMap() throws Exception {
249274
EnumMap<MyEnum, Integer> result = new EnumMap<MyEnum, Integer>(MyEnum.class);
250275
result.put(MyEnum.A, 1);
251276
result.put(MyEnum.C, 2);
252-
assertEquals(result,
253-
conversionService.convert(source, TypeDescriptor.forObject(source), new TypeDescriptor(getClass().getField("enumMap"))));
254-
}
255-
256-
257-
@SuppressWarnings("serial")
258-
public static class NoDefaultConstructorMap<K, V> extends HashMap<K, V> {
259277

260-
public NoDefaultConstructorMap(Map<? extends K, ? extends V> map) {
261-
super(map);
262-
}
278+
assertEquals(result, conversionService.convert(source,
279+
TypeDescriptor.forObject(source), new TypeDescriptor(getClass().getField("enumMap"))));
263280
}
264281

265282

266-
public static enum MyEnum {A, B, C}
267-
268-
269283
public Map<Integer, Integer> scalarMapTarget;
270284

271285
public Map<Integer, List<Integer>> collectionMapTarget;
@@ -283,4 +297,16 @@ public static enum MyEnum {A, B, C}
283297

284298
public EnumMap<MyEnum, Integer> enumMap;
285299

300+
301+
@SuppressWarnings("serial")
302+
public static class NoDefaultConstructorMap<K, V> extends HashMap<K, V> {
303+
304+
public NoDefaultConstructorMap(Map<? extends K, ? extends V> map) {
305+
super(map);
306+
}
307+
}
308+
309+
310+
public enum MyEnum {A, B, C}
311+
286312
}

0 commit comments

Comments
 (0)