From 333e06c3e0504263d1714dc5b775bc8ee3c413b2 Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 26 Oct 2021 10:49:00 -0700 Subject: [PATCH] Fix NullPointerException in Enumeration to Iterable tests. The NPE was caused by API changes in Java 17 and Spring Framework 6. --- .../util/CollectionUtilsUnitTests.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsUnitTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsUnitTests.java index 16198911..dac31814 100644 --- a/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsUnitTests.java +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsUnitTests.java @@ -33,6 +33,7 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; +import java.util.Vector; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -46,6 +47,8 @@ import org.springframework.data.gemfire.test.support.MapBuilder; * @author John Blum * @see java.lang.Iterable * @see java.util.Collection + * @see java.util.Arrays + * @see java.util.Collection * @see java.util.Collections * @see java.util.Enumeration * @see java.util.Iterator @@ -59,6 +62,17 @@ import org.springframework.data.gemfire.test.support.MapBuilder; */ public class CollectionUtilsUnitTests { + @SuppressWarnings("unchecked") + private Vector vectorOf(T... elements) { + + T[] nullSafeElements = (T[]) ArrayUtils.nullSafeArray(elements, Object.class); + + Vector vector = new Vector<>(nullSafeElements.length); + + Collections.addAll(vector, nullSafeElements); + + return vector; + } @Test public void addAllIterableElementsToList() { @@ -125,6 +139,7 @@ public class CollectionUtilsUnitTests { } @Test + @SuppressWarnings("all") public void asSetContainsAllArrayElements() { Object[] elements = { "a", "b", "c" }; @@ -137,6 +152,7 @@ public class CollectionUtilsUnitTests { } @Test + @SuppressWarnings("all") public void asSetContainsUniqueArrayElements() { Object[] elements = { 1, 2, 1 }; @@ -203,45 +219,29 @@ public class CollectionUtilsUnitTests { } @Test - @SuppressWarnings("unchecked") public void iterableOfEnumeration() { - Enumeration mockEnumeration = mock(Enumeration.class, "MockEnumeration"); + Enumeration enumeration = vectorOf(1, 2, 3).elements(); - when(mockEnumeration.hasMoreElements()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false); - when(mockEnumeration.nextElement()).thenReturn(1).thenReturn(2).thenReturn(3) - .thenThrow(new NoSuchElementException("Enumeration exhausted")); - - Iterable iterable = CollectionUtils.iterable(mockEnumeration); + Iterable iterable = CollectionUtils.iterable(enumeration); assertThat(iterable).isNotNull(); //assertThat(iterable).containsExactly(1, 2, 3); assertThat(StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toSet())) .containsExactly(1, 2, 3); - - verify(mockEnumeration, times(4)).hasMoreElements(); - verify(mockEnumeration, times(3)).nextElement(); } @Test - @SuppressWarnings("unchecked") public void iterableOfSingleEnumeration() { - Enumeration mockEnumeration = mock(Enumeration.class); + Enumeration enumeration = vectorOf(1).elements(); - when(mockEnumeration.hasMoreElements()).thenReturn(true).thenReturn(false); - when(mockEnumeration.nextElement()).thenReturn(1) - .thenThrow(new NoSuchElementException("Enumeration exhausted")); - - Iterable iterable = CollectionUtils.iterable(mockEnumeration); + Iterable iterable = CollectionUtils.iterable(enumeration); assertThat(iterable).isNotNull(); //assertThat(iterable).containsExactly(1); assertThat(StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toSet())) .containsExactly(1); - - verify(mockEnumeration, times(2)).hasMoreElements(); - verify(mockEnumeration, times(1)).nextElement(); } @Test