|
32 | 32 | import org.springframework.core.convert.TypeDescriptor;
|
33 | 33 | import org.springframework.core.convert.converter.Converter;
|
34 | 34 | import org.springframework.util.StopWatch;
|
| 35 | +import org.springframework.util.StringUtils; |
35 | 36 |
|
36 | 37 | /**
|
37 | 38 | * @author Keith Donald
|
@@ -216,6 +217,26 @@ public void testObjectArrayToStringArray() {
|
216 | 217 | assertEquals("RESULT", converted[0]);
|
217 | 218 | }
|
218 | 219 |
|
| 220 | + @Test |
| 221 | + public void testStringArrayToIntegerArray() { |
| 222 | + GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService(); |
| 223 | + conversionService.addConverter(new MyStringArrayToIntegerArrayConverter()); |
| 224 | + Integer[] converted = conversionService.convert(new String[] {"x1", "z3"}, Integer[].class); |
| 225 | + assertEquals(2, converted.length); |
| 226 | + assertEquals(1, converted[0].intValue()); |
| 227 | + assertEquals(3, converted[1].intValue()); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void testStringToIntegerArray() { |
| 232 | + GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService(); |
| 233 | + conversionService.addConverter(new MyStringToIntegerArrayConverter()); |
| 234 | + Integer[] converted = conversionService.convert("x1,z3", Integer[].class); |
| 235 | + assertEquals(2, converted.length); |
| 236 | + assertEquals(1, converted[0].intValue()); |
| 237 | + assertEquals(3, converted[1].intValue()); |
| 238 | + } |
| 239 | + |
219 | 240 | @Test
|
220 | 241 | public void testWildcardMap() throws Exception {
|
221 | 242 | GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
|
@@ -372,6 +393,31 @@ public String convert(MyBaseInterface source) {
|
372 | 393 | }
|
373 | 394 |
|
374 | 395 |
|
| 396 | + private static class MyStringArrayToIntegerArrayConverter implements Converter<String[], Integer[]> { |
| 397 | + |
| 398 | + public Integer[] convert(String[] source) { |
| 399 | + Integer[] result = new Integer[source.length]; |
| 400 | + for (int i = 0; i < source.length; i++) { |
| 401 | + result[i] = Integer.parseInt(source[i].substring(1)); |
| 402 | + } |
| 403 | + return result; |
| 404 | + } |
| 405 | + } |
| 406 | + |
| 407 | + |
| 408 | + private static class MyStringToIntegerArrayConverter implements Converter<String, Integer[]> { |
| 409 | + |
| 410 | + public Integer[] convert(String source) { |
| 411 | + String[] srcArray = StringUtils.commaDelimitedListToStringArray(source); |
| 412 | + Integer[] result = new Integer[srcArray.length]; |
| 413 | + for (int i = 0; i < srcArray.length; i++) { |
| 414 | + result[i] = Integer.parseInt(srcArray[i].substring(1)); |
| 415 | + } |
| 416 | + return result; |
| 417 | + } |
| 418 | + } |
| 419 | + |
| 420 | + |
375 | 421 | public static class WithCopyConstructor {
|
376 | 422 |
|
377 | 423 | public WithCopyConstructor() {
|
|
0 commit comments