Fix broken tests, update copyright dates, and polish

See gh-29414
This commit is contained in:
Sam Brannen 2022-11-08 20:03:01 +01:00
parent b2c8546013
commit f26a7dee97
23 changed files with 87 additions and 148 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -28,7 +28,6 @@ import java.net.URL;
import java.time.DayOfWeek; import java.time.DayOfWeek;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -227,8 +226,8 @@ class BeanUtilsTests {
IntegerListHolder2 integerListHolder2 = new IntegerListHolder2(); IntegerListHolder2 integerListHolder2 = new IntegerListHolder2();
BeanUtils.copyProperties(integerListHolder1, integerListHolder2); BeanUtils.copyProperties(integerListHolder1, integerListHolder2);
assertThat(integerListHolder1.getList()).containsOnly(42); assertThat(integerListHolder1.getList()).containsExactly(42);
assertThat(integerListHolder2.getList()).containsOnly(42); assertThat(integerListHolder2.getList()).containsExactly(42);
} }
/** /**
@ -236,7 +235,7 @@ class BeanUtilsTests {
*/ */
@Test @Test
void copyPropertiesHonorsGenericTypeMatchesFromWildcardToWildcard() { void copyPropertiesHonorsGenericTypeMatchesFromWildcardToWildcard() {
List<?> list = Arrays.asList("foo", 42); List<?> list = List.of("foo", 42);
WildcardListHolder1 wildcardListHolder1 = new WildcardListHolder1(); WildcardListHolder1 wildcardListHolder1 = new WildcardListHolder1();
wildcardListHolder1.setList(list); wildcardListHolder1.setList(list);
WildcardListHolder2 wildcardListHolder2 = new WildcardListHolder2(); WildcardListHolder2 wildcardListHolder2 = new WildcardListHolder2();
@ -257,8 +256,8 @@ class BeanUtilsTests {
WildcardListHolder2 wildcardListHolder2 = new WildcardListHolder2(); WildcardListHolder2 wildcardListHolder2 = new WildcardListHolder2();
BeanUtils.copyProperties(integerListHolder1, wildcardListHolder2); BeanUtils.copyProperties(integerListHolder1, wildcardListHolder2);
assertThat(integerListHolder1.getList()).containsOnly(42); assertThat(integerListHolder1.getList()).containsExactly(42);
assertThat(wildcardListHolder2.getList()).isEqualTo(Arrays.asList(42)); assertThat(wildcardListHolder2.getList()).isEqualTo(List.of(42));
} }
/** /**
@ -271,9 +270,8 @@ class BeanUtilsTests {
NumberUpperBoundedWildcardListHolder numberListHolder = new NumberUpperBoundedWildcardListHolder(); NumberUpperBoundedWildcardListHolder numberListHolder = new NumberUpperBoundedWildcardListHolder();
BeanUtils.copyProperties(integerListHolder1, numberListHolder); BeanUtils.copyProperties(integerListHolder1, numberListHolder);
assertThat(integerListHolder1.getList()).containsOnly(42); assertThat(integerListHolder1.getList()).containsExactly(42);
assertThat(numberListHolder.getList()).hasSize(1); assertThat(numberListHolder.getList()).isEqualTo(List.of(42));
assertThat(numberListHolder.getList().contains(42)).isTrue();
} }
/** /**
@ -300,7 +298,7 @@ class BeanUtilsTests {
LongListHolder longListHolder = new LongListHolder(); LongListHolder longListHolder = new LongListHolder();
BeanUtils.copyProperties(integerListHolder, longListHolder); BeanUtils.copyProperties(integerListHolder, longListHolder);
assertThat(integerListHolder.getList()).containsOnly(42); assertThat(integerListHolder.getList()).containsExactly(42);
assertThat(longListHolder.getList()).isEmpty(); assertThat(longListHolder.getList()).isEmpty();
} }
@ -314,13 +312,13 @@ class BeanUtilsTests {
NumberListHolder numberListHolder = new NumberListHolder(); NumberListHolder numberListHolder = new NumberListHolder();
BeanUtils.copyProperties(integerListHolder, numberListHolder); BeanUtils.copyProperties(integerListHolder, numberListHolder);
assertThat(integerListHolder.getList()).containsOnly(42); assertThat(integerListHolder.getList()).containsExactly(42);
assertThat(numberListHolder.getList()).isEmpty(); assertThat(numberListHolder.getList()).isEmpty();
} }
@Test // gh-26531 @Test // gh-26531
void copyPropertiesIgnoresGenericsIfSourceOrTargetHasUnresolvableGenerics() throws Exception { void copyPropertiesIgnoresGenericsIfSourceOrTargetHasUnresolvableGenerics() throws Exception {
Order original = new Order("test", Arrays.asList("foo", "bar")); Order original = new Order("test", List.of("foo", "bar"));
// Create a Proxy that loses the generic type information for the getLineItems() method. // Create a Proxy that loses the generic type information for the getLineItems() method.
OrderSummary proxy = proxyOrder(original); OrderSummary proxy = proxyOrder(original);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -25,7 +25,6 @@ import java.nio.charset.StandardCharsets;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.AbstractList; import java.util.AbstractList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Currency; import java.util.Currency;
@ -154,7 +153,7 @@ class DefaultConversionServiceTests {
@Test @Test
void stringToInteger() { void stringToInteger() {
assertThat(conversionService.convert("1", Integer.class)).isEqualTo((int) 1); assertThat(conversionService.convert("1", Integer.class)).isEqualTo(1);
} }
@Test @Test
@ -259,7 +258,7 @@ class DefaultConversionServiceTests {
@Test @Test
void enumToInteger() { void enumToInteger() {
assertThat(conversionService.convert(Foo.BAR, Integer.class)).isEqualTo((int) 0); assertThat(conversionService.convert(Foo.BAR, Integer.class)).isEqualTo(0);
} }
@Test @Test
@ -343,9 +342,7 @@ class DefaultConversionServiceTests {
@Test @Test
void convertArrayToCollectionInterface() { void convertArrayToCollectionInterface() {
List<?> result = conversionService.convert(new String[] {"1", "2", "3"}, List.class); List<?> result = conversionService.convert(new String[] {"1", "2", "3"}, List.class);
assertThat(result.get(0)).isEqualTo("1"); assertThat(result).isEqualTo(List.of("1", "2", "3"));
assertThat(result.get(1)).isEqualTo("2");
assertThat(result.get(2)).isEqualTo("3");
} }
@Test @Test
@ -353,9 +350,7 @@ class DefaultConversionServiceTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Integer> result = (List<Integer>) conversionService.convert(new String[] {"1", "2", "3"}, TypeDescriptor List<Integer> result = (List<Integer>) conversionService.convert(new String[] {"1", "2", "3"}, TypeDescriptor
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList"))); .valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
assertThat((int) result.get(0)).isEqualTo((int) 1); assertThat(result).isEqualTo(List.of(1, 2, 3));
assertThat((int) result.get(1)).isEqualTo((int) 2);
assertThat((int) result.get(2)).isEqualTo((int) 3);
} }
@Test @Test
@ -365,7 +360,7 @@ class DefaultConversionServiceTests {
Stream<Integer> result = (Stream<Integer>) this.conversionService.convert(source, Stream<Integer> result = (Stream<Integer>) this.conversionService.convert(source,
TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(String[].class),
new TypeDescriptor(getClass().getDeclaredField("genericStream"))); new TypeDescriptor(getClass().getDeclaredField("genericStream")));
assertThat(result.mapToInt(x -> x).sum()).isEqualTo(8); assertThat(result).containsExactly(1, 3, 4);
} }
@Test @Test
@ -376,17 +371,13 @@ class DefaultConversionServiceTests {
List<Color> colors = (List<Color>) conversionService.convert(new String[] {"ffffff", "#000000"}, List<Color> colors = (List<Color>) conversionService.convert(new String[] {"ffffff", "#000000"},
TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(String[].class),
new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0))); new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
assertThat(colors.size()).isEqualTo(2); assertThat(colors).containsExactly(Color.WHITE, Color.BLACK);
assertThat(colors.get(0)).isEqualTo(Color.WHITE);
assertThat(colors.get(1)).isEqualTo(Color.BLACK);
} }
@Test @Test
void convertArrayToCollectionImpl() { void convertArrayToCollectionImpl() {
ArrayList<?> result = conversionService.convert(new String[] {"1", "2", "3"}, ArrayList.class); ArrayList<?> result = conversionService.convert(new String[] {"1", "2", "3"}, ArrayList.class);
assertThat(result.get(0)).isEqualTo("1"); assertThat(result).isEqualTo(List.of("1", "2", "3"));
assertThat(result.get(1)).isEqualTo("2");
assertThat(result.get(2)).isEqualTo("3");
} }
@Test @Test
@ -416,34 +407,25 @@ class DefaultConversionServiceTests {
@Test @Test
void convertStringToArray() { void convertStringToArray() {
String[] result = conversionService.convert("1,2,3", String[].class); String[] result = conversionService.convert("1,2,3", String[].class);
assertThat(result.length).isEqualTo(3); assertThat(result).containsExactly("1", "2", "3");
assertThat(result[0]).isEqualTo("1");
assertThat(result[1]).isEqualTo("2");
assertThat(result[2]).isEqualTo("3");
} }
@Test @Test
void convertStringToArrayWithElementConversion() { void convertStringToArrayWithElementConversion() {
Integer[] result = conversionService.convert("1,2,3", Integer[].class); Integer[] result = conversionService.convert("1,2,3", Integer[].class);
assertThat(result.length).isEqualTo(3); assertThat(result).containsExactly(1, 2, 3);
assertThat((int) result[0]).isEqualTo((int) 1);
assertThat((int) result[1]).isEqualTo((int) 2);
assertThat((int) result[2]).isEqualTo((int) 3);
} }
@Test @Test
void convertStringToPrimitiveArrayWithElementConversion() { void convertStringToPrimitiveArrayWithElementConversion() {
int[] result = conversionService.convert("1,2,3", int[].class); int[] result = conversionService.convert("1,2,3", int[].class);
assertThat(result.length).isEqualTo(3); assertThat(result).containsExactly(1, 2, 3);
assertThat(result[0]).isEqualTo(1);
assertThat(result[1]).isEqualTo(2);
assertThat(result[2]).isEqualTo(3);
} }
@Test @Test
void convertEmptyStringToArray() { void convertEmptyStringToArray() {
String[] result = conversionService.convert("", String[].class); String[] result = conversionService.convert("", String[].class);
assertThat(result.length).isEqualTo(0); assertThat(result).isEmpty();
} }
@Test @Test
@ -457,7 +439,7 @@ class DefaultConversionServiceTests {
void convertArrayToObjectWithElementConversion() { void convertArrayToObjectWithElementConversion() {
String[] array = new String[] {"3"}; String[] array = new String[] {"3"};
Integer result = conversionService.convert(array, Integer.class); Integer result = conversionService.convert(array, Integer.class);
assertThat((int) result).isEqualTo((int) 3); assertThat(result).isEqualTo(3);
} }
@Test @Test
@ -477,44 +459,33 @@ class DefaultConversionServiceTests {
@Test @Test
void convertObjectToArrayWithElementConversion() { void convertObjectToArrayWithElementConversion() {
Integer[] result = conversionService.convert(3L, Integer[].class); Integer[] result = conversionService.convert(3L, Integer[].class);
assertThat(result.length).isEqualTo(1); assertThat(result).containsExactly(3);
assertThat((int) result[0]).isEqualTo((int) 3);
} }
@Test @Test
void convertCollectionToArray() { void convertCollectionToArray() {
List<String> list = new ArrayList<>(); List<String> list = List.of("1", "2", "3");
list.add("1");
list.add("2");
list.add("3");
String[] result = conversionService.convert(list, String[].class); String[] result = conversionService.convert(list, String[].class);
assertThat(result[0]).isEqualTo("1"); assertThat(result).containsExactly("1", "2", "3");
assertThat(result[1]).isEqualTo("2");
assertThat(result[2]).isEqualTo("3");
} }
@Test @Test
void convertCollectionToArrayWithElementConversion() { void convertCollectionToArrayWithElementConversion() {
List<String> list = new ArrayList<>(); List<String> list = List.of("1", "2", "3");
list.add("1");
list.add("2");
list.add("3");
Integer[] result = conversionService.convert(list, Integer[].class); Integer[] result = conversionService.convert(list, Integer[].class);
assertThat((int) result[0]).isEqualTo((int) 1); assertThat(result).containsExactly(1, 2, 3);
assertThat((int) result[1]).isEqualTo((int) 2);
assertThat((int) result[2]).isEqualTo((int) 3);
} }
@Test @Test
void convertCollectionToString() { void convertCollectionToString() {
List<String> list = Arrays.asList("foo", "bar"); List<String> list = List.of("foo", "bar");
String result = conversionService.convert(list, String.class); String result = conversionService.convert(list, String.class);
assertThat(result).isEqualTo("foo,bar"); assertThat(result).isEqualTo("foo,bar");
} }
@Test @Test
void convertCollectionToStringWithElementConversion() throws Exception { void convertCollectionToStringWithElementConversion() throws Exception {
List<Integer> list = Arrays.asList(3, 5); List<Integer> list = List.of(3, 5);
String result = (String) conversionService.convert(list, String result = (String) conversionService.convert(list,
new TypeDescriptor(getClass().getField("genericList")), TypeDescriptor.valueOf(String.class)); new TypeDescriptor(getClass().getField("genericList")), TypeDescriptor.valueOf(String.class));
assertThat(result).isEqualTo("3,5"); assertThat(result).isEqualTo("3,5");
@ -523,40 +494,34 @@ class DefaultConversionServiceTests {
@Test @Test
void convertStringToCollection() { void convertStringToCollection() {
List<?> result = conversionService.convert("1,2,3", List.class); List<?> result = conversionService.convert("1,2,3", List.class);
assertThat(result.size()).isEqualTo(3); assertThat(result).isEqualTo(List.of("1", "2", "3"));
assertThat(result.get(0)).isEqualTo("1");
assertThat(result.get(1)).isEqualTo("2");
assertThat(result.get(2)).isEqualTo("3");
} }
@Test @Test
void convertStringToCollectionWithElementConversion() throws Exception { 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"))); new TypeDescriptor(getClass().getField("genericList")));
assertThat(result.size()).isEqualTo(3); assertThat(result).isEqualTo(List.of(1, 2, 3));
assertThat(result.get(0)).isEqualTo(1);
assertThat(result.get(1)).isEqualTo(2);
assertThat(result.get(2)).isEqualTo(3);
} }
@Test @Test
void convertEmptyStringToCollection() { void convertEmptyStringToCollection() {
Collection<?> result = conversionService.convert("", Collection.class); Collection<?> result = conversionService.convert("", Collection.class);
assertThat(result.size()).isEqualTo(0); assertThat(result).isEmpty();
} }
@Test @Test
void convertCollectionToObject() { void convertCollectionToObject() {
List<Long> list = Collections.singletonList(3L); List<Long> list = Collections.singletonList(3L);
Long result = conversionService.convert(list, Long.class); Long result = conversionService.convert(list, Long.class);
assertThat(result).isEqualTo(Long.valueOf(3)); assertThat(result).isEqualTo(3L);
} }
@Test @Test
void convertCollectionToObjectWithElementConversion() { void convertCollectionToObjectWithElementConversion() {
List<String> list = Collections.singletonList("3"); List<String> list = Collections.singletonList("3");
Integer result = conversionService.convert(list, Integer.class); Integer result = conversionService.convert(list, Integer.class);
assertThat((int) result).isEqualTo((int) 3); assertThat(result).isEqualTo(3);
} }
@Test @Test
@ -580,8 +545,7 @@ class DefaultConversionServiceTests {
@Test @Test
void convertObjectToCollection() { void convertObjectToCollection() {
List<?> result = conversionService.convert(3L, List.class); List<?> result = conversionService.convert(3L, List.class);
assertThat(result.size()).isEqualTo(1); assertThat(result).isEqualTo(List.of(3L));
assertThat(result.get(0)).isEqualTo(3L);
} }
@Test @Test
@ -589,56 +553,43 @@ class DefaultConversionServiceTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class), List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class),
new TypeDescriptor(getClass().getField("genericList"))); new TypeDescriptor(getClass().getField("genericList")));
assertThat(result.size()).isEqualTo(1); assertThat(result).containsExactly(3);
assertThat((int) result.get(0)).isEqualTo((int) 3);
} }
@Test @Test
void convertStringArrayToIntegerArray() { void convertStringArrayToIntegerArray() {
Integer[] result = conversionService.convert(new String[] {"1", "2", "3"}, Integer[].class); Integer[] result = conversionService.convert(new String[] {"1", "2", "3"}, Integer[].class);
assertThat((int) result[0]).isEqualTo((int) 1); assertThat(result).containsExactly(1, 2, 3);
assertThat((int) result[1]).isEqualTo((int) 2);
assertThat((int) result[2]).isEqualTo((int) 3);
} }
@Test @Test
void convertStringArrayToIntArray() { void convertStringArrayToIntArray() {
int[] result = conversionService.convert(new String[] {"1", "2", "3"}, int[].class); int[] result = conversionService.convert(new String[] {"1", "2", "3"}, int[].class);
assertThat(result[0]).isEqualTo(1); assertThat(result).containsExactly(1, 2, 3);
assertThat(result[1]).isEqualTo(2);
assertThat(result[2]).isEqualTo(3);
} }
@Test @Test
void convertIntegerArrayToIntegerArray() { void convertIntegerArrayToIntegerArray() {
Integer[] result = conversionService.convert(new Integer[] {1, 2, 3}, Integer[].class); Integer[] result = conversionService.convert(new Integer[] {1, 2, 3}, Integer[].class);
assertThat((int) result[0]).isEqualTo((int) 1); assertThat(result).containsExactly(1, 2, 3);
assertThat((int) result[1]).isEqualTo((int) 2);
assertThat((int) result[2]).isEqualTo((int) 3);
} }
@Test @Test
void convertIntegerArrayToIntArray() { void convertIntegerArrayToIntArray() {
int[] result = conversionService.convert(new Integer[] {1, 2, 3}, int[].class); int[] result = conversionService.convert(new Integer[] {1, 2, 3}, int[].class);
assertThat(result[0]).isEqualTo(1); assertThat(result).containsExactly(1, 2, 3);
assertThat(result[1]).isEqualTo(2);
assertThat(result[2]).isEqualTo(3);
} }
@Test @Test
void convertObjectArrayToIntegerArray() { void convertObjectArrayToIntegerArray() {
Integer[] result = conversionService.convert(new Object[] {1, 2, 3}, Integer[].class); Integer[] result = conversionService.convert(new Object[] {1, 2, 3}, Integer[].class);
assertThat((int) result[0]).isEqualTo((int) 1); assertThat(result).containsExactly(1, 2, 3);
assertThat((int) result[1]).isEqualTo((int) 2);
assertThat((int) result[2]).isEqualTo((int) 3);
} }
@Test @Test
void convertObjectArrayToIntArray() { void convertObjectArrayToIntArray() {
int[] result = conversionService.convert(new Object[] {1, 2, 3}, int[].class); int[] result = conversionService.convert(new Object[] {1, 2, 3}, int[].class);
assertThat(result[0]).isEqualTo(1); assertThat(result).containsExactly(1, 2, 3);
assertThat(result[1]).isEqualTo(2);
assertThat(result[2]).isEqualTo(3);
} }
@Test @Test
@ -651,28 +602,26 @@ class DefaultConversionServiceTests {
@Test @Test
void convertArrayToArrayAssignable() { void convertArrayToArrayAssignable() {
int[] result = conversionService.convert(new int[] {1, 2, 3}, int[].class); int[] result = conversionService.convert(new int[] {1, 2, 3}, int[].class);
assertThat(result[0]).isEqualTo(1); assertThat(result).containsExactly(1, 2, 3);
assertThat(result[1]).isEqualTo(2);
assertThat(result[2]).isEqualTo(3);
} }
@Test @Test
void convertListOfNonStringifiable() { void convertListOfNonStringifiable() {
List<Object> list = Arrays.asList(new TestEntity(1L), new TestEntity(2L)); List<Object> list = List.of(new TestEntity(1L), new TestEntity(2L));
assertThat(conversionService.canConvert(list.getClass(), String.class)).isTrue(); assertThat(conversionService.canConvert(list.getClass(), String.class)).isTrue();
try { try {
conversionService.convert(list, String.class); conversionService.convert(list, String.class);
} }
catch (ConversionFailedException ex) { catch (ConversionFailedException ex) {
assertThat(ex.getMessage().contains(list.getClass().getName())).isTrue(); assertThat(ex.getMessage()).contains(list.getClass().getName());
assertThat(ex.getCause() instanceof ConverterNotFoundException).isTrue(); assertThat(ex.getCause()).isInstanceOf(ConverterNotFoundException.class);
assertThat(ex.getCause().getMessage().contains(TestEntity.class.getName())).isTrue(); assertThat(ex.getCause().getMessage()).contains(TestEntity.class.getName());
} }
} }
@Test @Test
void convertListOfStringToString() { void convertListOfStringToString() {
List<String> list = Arrays.asList("Foo", "Bar"); List<String> list = List.of("Foo", "Bar");
assertThat(conversionService.canConvert(list.getClass(), String.class)).isTrue(); assertThat(conversionService.canConvert(list.getClass(), String.class)).isTrue();
String result = conversionService.convert(list, String.class); String result = conversionService.convert(list, String.class);
assertThat(result).isEqualTo("Foo,Bar"); assertThat(result).isEqualTo("Foo,Bar");
@ -680,9 +629,9 @@ class DefaultConversionServiceTests {
@Test @Test
void convertListOfListToString() { void convertListOfListToString() {
List<String> list1 = Arrays.asList("Foo", "Bar"); List<String> list1 = List.of("Foo", "Bar");
List<String> list2 = Arrays.asList("Baz", "Boop"); List<String> list2 = List.of("Baz", "Boop");
List<List<String>> list = Arrays.asList(list1, list2); List<List<String>> list = List.of(list1, list2);
assertThat(conversionService.canConvert(list.getClass(), String.class)).isTrue(); assertThat(conversionService.canConvert(list.getClass(), String.class)).isTrue();
String result = conversionService.convert(list, String.class); String result = conversionService.convert(list, String.class);
assertThat(result).isEqualTo("Foo,Bar,Baz,Boop"); assertThat(result).isEqualTo("Foo,Bar,Baz,Boop");
@ -697,9 +646,7 @@ class DefaultConversionServiceTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Integer> bar = (List<Integer>) conversionService.convert(foo, TypeDescriptor.forObject(foo), List<Integer> bar = (List<Integer>) conversionService.convert(foo, TypeDescriptor.forObject(foo),
new TypeDescriptor(getClass().getField("genericList"))); new TypeDescriptor(getClass().getField("genericList")));
assertThat((int) bar.get(0)).isEqualTo((int) 1); assertThat(bar).containsExactly(1, 2, 3);
assertThat((int) bar.get(1)).isEqualTo((int) 2);
assertThat((int) bar.get(2)).isEqualTo((int) 3);
} }
@Test @Test
@ -735,9 +682,9 @@ class DefaultConversionServiceTests {
List<Integer> bar = (List<Integer>) conversionService.convert(values, List<Integer> bar = (List<Integer>) conversionService.convert(values,
TypeDescriptor.forObject(values), new TypeDescriptor(getClass().getField("genericList"))); TypeDescriptor.forObject(values), new TypeDescriptor(getClass().getField("genericList")));
assertThat(bar.size()).isEqualTo(3); assertThat(bar.size()).isEqualTo(3);
assertThat((int) bar.get(0)).isEqualTo((int) 1); assertThat(bar.get(0)).isEqualTo(1);
assertThat((int) bar.get(1)).isEqualTo((int) 2); assertThat(bar.get(1)).isEqualTo(2);
assertThat((int) bar.get(2)).isEqualTo((int) 3); assertThat(bar.get(2)).isEqualTo(3);
} }
@Test @Test
@ -748,8 +695,8 @@ class DefaultConversionServiceTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Integer> integers = (List<Integer>) conversionService.convert(strings, List<Integer> integers = (List<Integer>) conversionService.convert(strings,
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Integer.class))); TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Integer.class)));
assertThat((int) integers.get(0)).isEqualTo((int) 3); assertThat(integers.get(0)).isEqualTo(3);
assertThat((int) integers.get(1)).isEqualTo((int) 9); assertThat(integers.get(1)).isEqualTo(9);
} }
@Test @Test
@ -770,7 +717,7 @@ class DefaultConversionServiceTests {
hashMap.put("1", 1); hashMap.put("1", 1);
hashMap.put("2", 2); hashMap.put("2", 2);
List<?> converted = conversionService.convert(hashMap.values(), List.class); List<?> converted = conversionService.convert(hashMap.values(), List.class);
assertThat(converted).isEqualTo(Arrays.asList(1, 2)); assertThat(converted).isEqualTo(List.of(1, 2));
} }
@Test @Test
@ -781,8 +728,8 @@ class DefaultConversionServiceTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<Integer, Integer> integers = (Map<Integer, Integer>) conversionService.convert(strings, Map<Integer, Integer> integers = (Map<Integer, Integer>) conversionService.convert(strings,
TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(Integer.class))); TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(Integer.class)));
assertThat((int) integers.get(3)).isEqualTo((int) 9); assertThat(integers.get(3)).isEqualTo(9);
assertThat((int) integers.get(6)).isEqualTo((int) 31); assertThat(integers.get(6)).isEqualTo(31);
} }
@Test @Test
@ -947,7 +894,7 @@ class DefaultConversionServiceTests {
TypeDescriptor descriptor = new TypeDescriptor(parameter); TypeDescriptor descriptor = new TypeDescriptor(parameter);
Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor); Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor);
assertThat(actual.getClass()).isEqualTo(Optional.class); assertThat(actual.getClass()).isEqualTo(Optional.class);
assertThat(((Optional<List<Integer>>) actual).get()).isEqualTo(Arrays.asList(1, 2, 3)); assertThat(((Optional<List<Integer>>) actual).get()).isEqualTo(List.of(1, 2, 3));
} }
@Test @Test

View File

@ -24,7 +24,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -104,7 +103,7 @@ class GenericConversionServiceTests {
@Test @Test
void convert() { void convert() {
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertThat(conversionService.convert("3", Integer.class)).isEqualTo((int) 3); assertThat(conversionService.convert("3", Integer.class)).isEqualTo(3);
} }
@Test @Test
@ -192,7 +191,7 @@ class GenericConversionServiceTests {
void convertSuperSourceType() { void convertSuperSourceType() {
conversionService.addConverter(CharSequence.class, Integer.class, source -> Integer.valueOf(source.toString())); conversionService.addConverter(CharSequence.class, Integer.class, source -> Integer.valueOf(source.toString()));
Integer result = conversionService.convert("3", Integer.class); Integer result = conversionService.convert("3", Integer.class);
assertThat((int) result).isEqualTo((int) 3); assertThat(result).isEqualTo(3);
} }
// SPR-8718 // SPR-8718
@ -221,7 +220,7 @@ class GenericConversionServiceTests {
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertThat(conversionService.canConvert(String.class, int.class)).isTrue(); assertThat(conversionService.canConvert(String.class, int.class)).isTrue();
Integer three = conversionService.convert("3", int.class); Integer three = conversionService.convert("3", int.class);
assertThat(three.intValue()).isEqualTo(3); assertThat(three).isEqualTo(3);
} }
@Test @Test
@ -234,26 +233,21 @@ class GenericConversionServiceTests {
@Test @Test
void listToIterableConversion() { void listToIterableConversion() {
List<Object> raw = new ArrayList<>(); List<Object> raw = List.of("one", "two");
raw.add("one");
raw.add("two");
Object converted = conversionService.convert(raw, Iterable.class); Object converted = conversionService.convert(raw, Iterable.class);
assertThat(converted).isSameAs(raw); assertThat(converted).isSameAs(raw);
} }
@Test @Test
void listToObjectConversion() { void listToObjectConversion() {
List<Object> raw = new ArrayList<>(); List<Object> raw = List.of("one", "two");
raw.add("one");
raw.add("two");
Object converted = conversionService.convert(raw, Object.class); Object converted = conversionService.convert(raw, Object.class);
assertThat(converted).isSameAs(raw); assertThat(converted).isSameAs(raw);
} }
@Test @Test
void mapToObjectConversion() { void mapToObjectConversion() {
Map<Object, Object> raw = new HashMap<>(); Map<Object, Object> raw = Map.of("key", "value");
raw.put("key", "value");
Object converted = conversionService.convert(raw, Object.class); Object converted = conversionService.convert(raw, Object.class);
assertThat(converted).isSameAs(raw); assertThat(converted).isSameAs(raw);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.