#464/#465 Collection fixes for forward compatibility.

This commit is contained in:
Harald Kuhr 2020-04-15 16:03:12 +02:00
parent 96cb3a07f4
commit 8bc952ba66
2 changed files with 8 additions and 7 deletions

View File

@ -1117,18 +1117,19 @@ public abstract class CollectionAbstractTest extends ObjectAbstractTest {
/** /**
* Tests {@link Collection#toArray(Object[])}. * Tests {@link Collection#toArray(Object[])}.
*/ */
@SuppressWarnings({"SuspiciousToArrayCall", "RedundantCast"})
@Test @Test
public void testCollectionToArray2() { public void testCollectionToArray2() {
resetEmpty(); resetEmpty();
Object[] a = new Object[] { new Object(), null, null }; Object[] a = new Object[] { new Object(), null, null };
Object[] array = collection.toArray(a); Object[] array = collection.toArray(a);
assertArrayEquals("Given array shouldn't shrink", array, a); assertArrayEquals("Given array shouldn't shrink", array, a);
assertEquals("Last element should be set to null", a[0], null); assertNull("Last element should be set to null", a[0]);
verifyAll(); verifyAll();
resetFull(); resetFull();
try { try {
array = collection.toArray(new Void[0]); collection.toArray(new Void[0]);
fail("toArray(new Void[0]) should raise ArrayStore"); fail("toArray(new Void[0]) should raise ArrayStore");
} catch (ArrayStoreException e) { } catch (ArrayStoreException e) {
// expected // expected
@ -1136,7 +1137,7 @@ public abstract class CollectionAbstractTest extends ObjectAbstractTest {
verifyAll(); verifyAll();
try { try {
array = collection.toArray(null); collection.toArray((Object[]) null);
fail("toArray(null) should raise NPE"); fail("toArray(null) should raise NPE");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// expected // expected
@ -1150,13 +1151,13 @@ public abstract class CollectionAbstractTest extends ObjectAbstractTest {
// Figure out if they're all the same class // Figure out if they're all the same class
// TODO: It'd be nicer to detect a common superclass // TODO: It'd be nicer to detect a common superclass
HashSet classes = new HashSet(); HashSet<Class<?>> classes = new HashSet<>();
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
classes.add((array[i] == null) ? null : array[i].getClass()); classes.add((array[i] == null) ? null : array[i].getClass());
} }
if (classes.size() > 1) return; if (classes.size() > 1) return;
Class cl = (Class)classes.iterator().next(); Class<?> cl = (Class<?>)classes.iterator().next();
if (Map.Entry.class.isAssignableFrom(cl)) { // check needed for protective cases like Predicated/Unmod map entrySet if (Map.Entry.class.isAssignableFrom(cl)) { // check needed for protective cases like Predicated/Unmod map entrySet
cl = Map.Entry.class; cl = Map.Entry.class;
} }

View File

@ -250,12 +250,12 @@ public class CollectionUtilTest {
assertCorrectListIterator(new ArrayList<String>(Arrays.asList(new String[] {"foo", "bar", "baz", "boo"})).subList(1, 3).listIterator(0), new String[] {"bar", "baz"}, true, true); assertCorrectListIterator(new ArrayList<String>(Arrays.asList(new String[] {"foo", "bar", "baz", "boo"})).subList(1, 3).listIterator(0), new String[] {"bar", "baz"}, true, true);
} }
private void assertCorrectListIterator(ListIterator<String> iterator, final Object[] elements) { private static void assertCorrectListIterator(ListIterator<String> iterator, final Object[] elements) {
assertCorrectListIterator(iterator, elements, false, false); assertCorrectListIterator(iterator, elements, false, false);
} }
// NOTE: The test is can only test list iterators with a starting index == 0 // NOTE: The test is can only test list iterators with a starting index == 0
private void assertCorrectListIterator(ListIterator<String> iterator, final Object[] elements, boolean skipRemove, boolean skipAdd) { private static void assertCorrectListIterator(ListIterator<String> iterator, final Object[] elements, boolean skipRemove, boolean skipAdd) {
// Index is now "before 0" // Index is now "before 0"
assertEquals(-1, iterator.previousIndex()); assertEquals(-1, iterator.previousIndex());
assertEquals(0, iterator.nextIndex()); assertEquals(0, iterator.nextIndex());