Skip to content

Commit 1e07c36

Browse files
committed
Fix NullPointerException in Enumeration to Iterable tests.
The NPE was caused by API changes in Java 17 and Spring Framework 6.
1 parent a2df58f commit 1e07c36

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

spring-data-geode/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsUnitTests.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Map;
3434
import java.util.NoSuchElementException;
3535
import java.util.Set;
36+
import java.util.Vector;
3637
import java.util.stream.Collectors;
3738
import java.util.stream.StreamSupport;
3839

@@ -46,6 +47,8 @@
4647
* @author John Blum
4748
* @see java.lang.Iterable
4849
* @see java.util.Collection
50+
* @see java.util.Arrays
51+
* @see java.util.Collection
4952
* @see java.util.Collections
5053
* @see java.util.Enumeration
5154
* @see java.util.Iterator
@@ -59,6 +62,17 @@
5962
*/
6063
public class CollectionUtilsUnitTests {
6164

65+
@SuppressWarnings("unchecked")
66+
private <T> Vector<T> vectorOf(T... elements) {
67+
68+
T[] nullSafeElements = (T[]) ArrayUtils.nullSafeArray(elements, Object.class);
69+
70+
Vector<T> vector = new Vector<>(nullSafeElements.length);
71+
72+
Collections.addAll(vector, nullSafeElements);
73+
74+
return vector;
75+
}
6276
@Test
6377
public void addAllIterableElementsToList() {
6478

@@ -125,6 +139,7 @@ public void addNullIterableToCollection() {
125139
}
126140

127141
@Test
142+
@SuppressWarnings("all")
128143
public void asSetContainsAllArrayElements() {
129144

130145
Object[] elements = { "a", "b", "c" };
@@ -137,6 +152,7 @@ public void asSetContainsAllArrayElements() {
137152
}
138153

139154
@Test
155+
@SuppressWarnings("all")
140156
public void asSetContainsUniqueArrayElements() {
141157

142158
Object[] elements = { 1, 2, 1 };
@@ -203,45 +219,29 @@ public void emptyIterableReturnsEmptyIterable() {
203219
}
204220

205221
@Test
206-
@SuppressWarnings("unchecked")
207222
public void iterableOfEnumeration() {
208223

209-
Enumeration<Object> mockEnumeration = mock(Enumeration.class, "MockEnumeration");
224+
Enumeration<Integer> enumeration = vectorOf(1, 2, 3).elements();
210225

211-
when(mockEnumeration.hasMoreElements()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
212-
when(mockEnumeration.nextElement()).thenReturn(1).thenReturn(2).thenReturn(3)
213-
.thenThrow(new NoSuchElementException("Enumeration exhausted"));
214-
215-
Iterable<Object> iterable = CollectionUtils.iterable(mockEnumeration);
226+
Iterable<Integer> iterable = CollectionUtils.iterable(enumeration);
216227

217228
assertThat(iterable).isNotNull();
218229
//assertThat(iterable).containsExactly(1, 2, 3);
219230
assertThat(StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toSet()))
220231
.containsExactly(1, 2, 3);
221-
222-
verify(mockEnumeration, times(4)).hasMoreElements();
223-
verify(mockEnumeration, times(3)).nextElement();
224232
}
225233

226234
@Test
227-
@SuppressWarnings("unchecked")
228235
public void iterableOfSingleEnumeration() {
229236

230-
Enumeration<Object> mockEnumeration = mock(Enumeration.class);
231-
232-
when(mockEnumeration.hasMoreElements()).thenReturn(true).thenReturn(false);
233-
when(mockEnumeration.nextElement()).thenReturn(1)
234-
.thenThrow(new NoSuchElementException("Enumeration exhausted"));
237+
Enumeration<Integer> enumeration = vectorOf(1).elements();
235238

236-
Iterable<Object> iterable = CollectionUtils.iterable(mockEnumeration);
239+
Iterable<Integer> iterable = CollectionUtils.iterable(enumeration);
237240

238241
assertThat(iterable).isNotNull();
239242
//assertThat(iterable).containsExactly(1);
240243
assertThat(StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toSet()))
241244
.containsExactly(1);
242-
243-
verify(mockEnumeration, times(2)).hasMoreElements();
244-
verify(mockEnumeration, times(1)).nextElement();
245245
}
246246

247247
@Test

0 commit comments

Comments
 (0)