33
33
import java .util .Map ;
34
34
import java .util .NoSuchElementException ;
35
35
import java .util .Set ;
36
+ import java .util .Vector ;
36
37
import java .util .stream .Collectors ;
37
38
import java .util .stream .StreamSupport ;
38
39
46
47
* @author John Blum
47
48
* @see java.lang.Iterable
48
49
* @see java.util.Collection
50
+ * @see java.util.Arrays
51
+ * @see java.util.Collection
49
52
* @see java.util.Collections
50
53
* @see java.util.Enumeration
51
54
* @see java.util.Iterator
59
62
*/
60
63
public class CollectionUtilsUnitTests {
61
64
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
+ }
62
76
@ Test
63
77
public void addAllIterableElementsToList () {
64
78
@@ -125,6 +139,7 @@ public void addNullIterableToCollection() {
125
139
}
126
140
127
141
@ Test
142
+ @ SuppressWarnings ("all" )
128
143
public void asSetContainsAllArrayElements () {
129
144
130
145
Object [] elements = { "a" , "b" , "c" };
@@ -137,6 +152,7 @@ public void asSetContainsAllArrayElements() {
137
152
}
138
153
139
154
@ Test
155
+ @ SuppressWarnings ("all" )
140
156
public void asSetContainsUniqueArrayElements () {
141
157
142
158
Object [] elements = { 1 , 2 , 1 };
@@ -203,45 +219,29 @@ public void emptyIterableReturnsEmptyIterable() {
203
219
}
204
220
205
221
@ Test
206
- @ SuppressWarnings ("unchecked" )
207
222
public void iterableOfEnumeration () {
208
223
209
- Enumeration <Object > mockEnumeration = mock ( Enumeration . class , "MockEnumeration" );
224
+ Enumeration <Integer > enumeration = vectorOf ( 1 , 2 , 3 ). elements ( );
210
225
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 );
216
227
217
228
assertThat (iterable ).isNotNull ();
218
229
//assertThat(iterable).containsExactly(1, 2, 3);
219
230
assertThat (StreamSupport .stream (iterable .spliterator (), false ).collect (Collectors .toSet ()))
220
231
.containsExactly (1 , 2 , 3 );
221
-
222
- verify (mockEnumeration , times (4 )).hasMoreElements ();
223
- verify (mockEnumeration , times (3 )).nextElement ();
224
232
}
225
233
226
234
@ Test
227
- @ SuppressWarnings ("unchecked" )
228
235
public void iterableOfSingleEnumeration () {
229
236
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 ();
235
238
236
- Iterable <Object > iterable = CollectionUtils .iterable (mockEnumeration );
239
+ Iterable <Integer > iterable = CollectionUtils .iterable (enumeration );
237
240
238
241
assertThat (iterable ).isNotNull ();
239
242
//assertThat(iterable).containsExactly(1);
240
243
assertThat (StreamSupport .stream (iterable .spliterator (), false ).collect (Collectors .toSet ()))
241
244
.containsExactly (1 );
242
-
243
- verify (mockEnumeration , times (2 )).hasMoreElements ();
244
- verify (mockEnumeration , times (1 )).nextElement ();
245
245
}
246
246
247
247
@ Test
0 commit comments