18
18
19
19
import java .util .ArrayList ;
20
20
import java .util .Arrays ;
21
+ import java .util .Collections ;
21
22
import java .util .Enumeration ;
22
23
import java .util .HashMap ;
23
24
import java .util .HashSet ;
24
- import java .util .Hashtable ;
25
25
import java .util .Iterator ;
26
26
import java .util .List ;
27
27
import java .util .Map ;
42
42
* @author Rob Harrop
43
43
* @author Juergen Hoeller
44
44
* @author Rick Evans
45
+ * @author Sam Brannen
45
46
*/
46
47
class CollectionUtilsTests {
47
48
@@ -100,19 +101,24 @@ void mergePropertiesIntoMap() {
100
101
}
101
102
102
103
@ Test
103
- void contains () {
104
+ void containsWithIterator () {
104
105
assertThat (CollectionUtils .contains ((Iterator <String >) null , "myElement" )).isFalse ();
105
- assertThat (CollectionUtils .contains ((Enumeration <String >) null , "myElement" )).isFalse ();
106
- assertThat (CollectionUtils .contains (new ArrayList <String >().iterator (), "myElement" )).isFalse ();
107
- assertThat (CollectionUtils .contains (new Hashtable <String , Object >().keys (), "myElement" )).isFalse ();
106
+ assertThat (CollectionUtils .contains (List .of ().iterator (), "myElement" )).isFalse ();
108
107
109
- List <String > list = new ArrayList <>();
110
- list .add ("myElement" );
108
+ List <String > list = Arrays .asList ("myElement" , null );
111
109
assertThat (CollectionUtils .contains (list .iterator (), "myElement" )).isTrue ();
110
+ assertThat (CollectionUtils .contains (list .iterator (), null )).isTrue ();
111
+ }
112
112
113
- Hashtable <String , String > ht = new Hashtable <>();
114
- ht .put ("myElement" , "myValue" );
115
- assertThat (CollectionUtils .contains (ht .keys (), "myElement" )).isTrue ();
113
+ @ Test
114
+ void containsWithEnumeration () {
115
+ assertThat (CollectionUtils .contains ((Enumeration <String >) null , "myElement" )).isFalse ();
116
+ assertThat (CollectionUtils .contains (Collections .enumeration (List .of ()), "myElement" )).isFalse ();
117
+
118
+ List <String > list = Arrays .asList ("myElement" , null );
119
+ Enumeration <String > enumeration = Collections .enumeration (list );
120
+ assertThat (CollectionUtils .contains (enumeration , "myElement" )).isTrue ();
121
+ assertThat (CollectionUtils .contains (enumeration , null )).isTrue ();
116
122
}
117
123
118
124
@ Test
@@ -128,39 +134,49 @@ void containsAny() {
128
134
candidates .add ("abc" );
129
135
130
136
assertThat (CollectionUtils .containsAny (source , candidates )).isTrue ();
137
+
131
138
candidates .remove ("def" );
132
139
assertThat (CollectionUtils .containsAny (source , candidates )).isTrue ();
140
+
133
141
candidates .remove ("abc" );
134
142
assertThat (CollectionUtils .containsAny (source , candidates )).isFalse ();
143
+
144
+ source .add (null );
145
+ assertThat (CollectionUtils .containsAny (source , candidates )).isFalse ();
146
+
147
+ candidates .add (null );
148
+ assertThat (CollectionUtils .containsAny (source , candidates )).isTrue ();
135
149
}
136
150
137
151
@ Test
138
152
void containsInstanceWithNullCollection () {
139
- assertThat (CollectionUtils .containsInstance (null , this )).as ( "Must return false if supplied Collection argument is null" ). isFalse ();
153
+ assertThat (CollectionUtils .containsInstance (null , this )).isFalse ();
140
154
}
141
155
142
156
@ Test
143
157
void containsInstanceWithInstancesThatAreEqualButDistinct () {
144
- List <Instance > list = new ArrayList <>();
145
- list .add (new Instance ("fiona" ));
146
- assertThat (CollectionUtils .containsInstance (list , new Instance ("fiona" ))).as ("Must return false if instance is not in the supplied Collection argument" ).isFalse ();
158
+ List <Instance > list = List .of (new Instance ("fiona" ));
159
+ assertThat (CollectionUtils .containsInstance (list , new Instance ("fiona" ))).isFalse ();
147
160
}
148
161
149
162
@ Test
150
163
void containsInstanceWithSameInstance () {
151
- List < Instance > list = new ArrayList <>( );
152
- list . add ( new Instance ("apple" ) );
153
- Instance instance = new Instance ( "fiona" );
154
- list . add ( instance );
155
- assertThat (CollectionUtils .containsInstance (list , instance )). as ( "Must return true if instance is in the supplied Collection argument" ).isTrue ();
164
+ Instance fiona = new Instance ( "fiona" );
165
+ Instance apple = new Instance ("apple" );
166
+
167
+ List < Instance > list = List . of ( fiona , apple );
168
+ assertThat (CollectionUtils .containsInstance (list , fiona ) ).isTrue ();
156
169
}
157
170
158
171
@ Test
159
172
void containsInstanceWithNullInstance () {
160
- List <Instance > list = new ArrayList <>();
161
- list .add (new Instance ("apple" ));
162
- list .add (new Instance ("fiona" ));
163
- assertThat (CollectionUtils .containsInstance (list , null )).as ("Must return false if null instance is supplied" ).isFalse ();
173
+ Instance fiona = new Instance ("fiona" );
174
+
175
+ List <Instance > list = List .of (fiona );
176
+ assertThat (CollectionUtils .containsInstance (list , null )).isFalse ();
177
+
178
+ list = Arrays .asList (fiona , null );
179
+ assertThat (CollectionUtils .containsInstance (list , null )).isTrue ();
164
180
}
165
181
166
182
@ Test
@@ -176,6 +192,13 @@ void findFirstMatch() {
176
192
candidates .add ("abc" );
177
193
178
194
assertThat (CollectionUtils .findFirstMatch (source , candidates )).isEqualTo ("def" );
195
+
196
+ source .clear ();
197
+ source .add (null );
198
+ assertThat (CollectionUtils .findFirstMatch (source , candidates )).isNull ();
199
+
200
+ candidates .add (null );
201
+ assertThat (CollectionUtils .findFirstMatch (source , candidates )).isNull ();
179
202
}
180
203
181
204
@ Test
0 commit comments