@@ -22,182 +22,200 @@ public class MultiValuedMapUnitTest {
22
22
public void givenMultiValuesMap_whenPuttingMultipleValuesUsingPutMethod_thenReturningAllValues () {
23
23
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
24
24
25
- map .put ("key" , "value1" );
26
- map .put ("key" , "value2" );
27
- map .put ("key" , "value2" );
25
+ map .put ("fruits" , "apple" );
26
+ map .put ("fruits" , "orange" );
28
27
29
- assertThat ((Collection <String >) map .get ("key" )).containsExactly ("value1" , "value2" , "value2" );
28
+ assertThat ((Collection <String >) map .get ("fruits" )).containsExactly ("apple" , "orange" );
29
+
30
30
}
31
31
32
32
@ Test
33
33
public void givenMultiValuesMap_whenPuttingMultipleValuesUsingPutAllMethod_thenReturningAllValues () {
34
34
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
35
+
36
+ map .putAll ("vehicles" , Arrays .asList ("car" , "bike" ));
35
37
36
- map .putAll ("key" , Arrays .asList ("value1" , "value2" , "value2" ));
37
-
38
- assertThat ((Collection <String >) map .get ("key" )).containsExactly ("value1" , "value2" , "value2" );
38
+ assertThat ((Collection <String >) map .get ("vehicles" )).containsExactly ("car" , "bike" );
39
+
39
40
}
40
41
41
42
@ Test
42
43
public void givenMultiValuesMap_whenGettingValueUsingGetMethod_thenReturningValue () {
43
44
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
44
- map .put ("key" , "value" );
45
+
46
+ map .put ("fruits" , "apple" );
45
47
46
- assertThat ((Collection <String >) map .get ("key " )).containsExactly ("value " );
48
+ assertThat ((Collection <String >) map .get ("fruits " )).containsExactly ("apple " );
47
49
}
48
50
49
51
@ Test
50
52
public void givenMultiValuesMap_whenUsingEntriesMethod_thenReturningMappings () {
51
53
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
52
- map .put ("key " , "value1 " );
53
- map .put ("key " , "value2 " );
54
-
54
+ map .put ("fruits " , "apple " );
55
+ map .put ("fruits " , "orange " );
56
+
55
57
Collection <Entry <String , String >> entries = (Collection <Entry <String , String >>) map .entries ();
56
58
57
59
for (Map .Entry <String ,String > entry : entries ) {
58
- assertThat (entry .getKey ()).contains ("key " );
59
- assertTrue (entry .getValue ().equals ("value1 " ) || entry .getValue ().equals ("value2 " ) );
60
+ assertThat (entry .getKey ()).contains ("fruits " );
61
+ assertTrue (entry .getValue ().equals ("apple " ) || entry .getValue ().equals ("orange " ) );
60
62
}
61
63
}
62
64
63
65
@ Test
64
66
public void givenMultiValuesMap_whenUsingKeysMethod_thenReturningAllKeys () {
65
67
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
66
- map .put ("key" , "value" );
67
- map .put ("key1" , "value1" );
68
- map .put ("key2" , "value2" );
68
+
69
+ map .put ("fruits" , "apple" );
70
+ map .put ("fruits" , "orange" );
71
+ map .put ("vehicles" , "car" );
72
+ map .put ("vehicles" , "bike" );
69
73
70
- assertThat (((Collection <String >) map .keys ())).contains ("key " , "key1" , "key2 " );
74
+ assertThat (((Collection <String >) map .keys ())).contains ("fruits " , "vehicles " );
71
75
}
72
76
73
77
@ Test
74
78
public void givenMultiValuesMap_whenUsingKeySetMethod_thenReturningAllKeys () {
75
79
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
76
- map .put ("key" , "value" );
77
- map .put ("key1" , "value1" );
78
- map .put ("key2" , "value2" );
80
+
81
+ map .put ("fruits" , "apple" );
82
+ map .put ("fruits" , "orange" );
83
+ map .put ("vehicles" , "car" );
84
+ map .put ("vehicles" , "bike" );
79
85
80
- assertThat ((Collection <String >) map .keySet ()).contains ("key" , "key1" , "key2" );
86
+ assertThat ((Collection <String >) map .keySet ()).contains ("fruits" , "vehicles" );
87
+
81
88
}
82
89
83
90
@ Test
84
91
public void givenMultiValuesMap_whenUsingValuesMethod_thenReturningAllValues () {
85
92
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
86
- map .put ("key" , "value" );
87
- map .put ("key1" , "value1" );
88
- map .put ("key2" , "value2" );
93
+
94
+ map .put ("fruits" , "apple" );
95
+ map .put ("fruits" , "orange" );
96
+ map .put ("vehicles" , "car" );
97
+ map .put ("vehicles" , "bike" );
89
98
90
- assertThat (((Collection <String >) map .values ())).contains ("value " , "value1 " , "value2 " );
99
+ assertThat (((Collection <String >) map .values ())).contains ("apple " , "orange " , "car" , "bike " );
91
100
}
92
101
93
102
@ Test
94
103
public void givenMultiValuesMap_whenUsingRemoveMethod_thenReturningUpdatedMap () {
95
104
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
96
- map .put ("key" , "value" );
97
- map .put ("key1" , "value1" );
98
- map .put ("key2" , "value2" );
99
- assertThat (((Collection <String >) map .values ())).contains ("value" , "value1" , "value2" );
105
+
106
+ map .put ("fruits" , "apple" );
107
+ map .put ("fruits" , "orange" );
108
+ map .put ("vehicles" , "car" );
109
+ map .put ("vehicles" , "bike" );
110
+ assertThat (((Collection <String >) map .values ())).contains ("apple" , "orange" , "car" , "bike" );
100
111
101
- map .remove ("key " );
112
+ map .remove ("fruits " );
102
113
103
- assertThat (((Collection <String >) map .values ())).contains ("value1" , "value2" );
114
+ assertThat (((Collection <String >) map .values ())).contains ("car" , "bike" );
115
+
104
116
}
105
117
106
118
@ Test
107
119
public void givenMultiValuesMap_whenUsingRemoveMappingMethod_thenReturningUpdatedMapAfterMappingRemoved () {
108
120
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
109
- map .put ("key" , "value" );
110
- map .put ("key1" , "value1" );
111
- map .put ("key2" , "value2" );
112
- assertThat (((Collection <String >) map .values ())).contains ("value" , "value1" , "value2" );
121
+
122
+ map .put ("fruits" , "apple" );
123
+ map .put ("fruits" , "orange" );
124
+ map .put ("vehicles" , "car" );
125
+ map .put ("vehicles" , "bike" );
126
+ assertThat (((Collection <String >) map .values ())).contains ("apple" , "orange" , "car" , "bike" );
113
127
114
- map .removeMapping ("key " , "value " );
128
+ map .removeMapping ("fruits " , "apple " );
115
129
116
- assertThat (((Collection <String >) map .values ())).contains ("value1 " , "value2 " );
130
+ assertThat (((Collection <String >) map .values ())).contains ("orange " , "car" , "bike " );
117
131
}
118
132
119
133
@ Test
120
134
public void givenMultiValuesMap_whenUsingClearMethod_thenReturningEmptyMap () {
121
135
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
122
- map .put ("key" , "value" );
123
- map .put ("key1" , "value1" );
124
- map .put ("key2" , "value2" );
125
- assertThat (((Collection <String >) map .values ())).contains ("value" , "value1" , "value2" );
136
+ map .put ("fruits" , "apple" );
137
+ map .put ("fruits" , "orange" );
138
+ map .put ("vehicles" , "car" );
139
+ map .put ("vehicles" , "bike" );
140
+ assertThat (((Collection <String >) map .values ())).contains ("apple" , "orange" , "car" , "bike" );
126
141
127
142
map .clear ();
128
-
143
+
129
144
assertTrue (map .isEmpty ());
130
145
}
131
146
132
147
@ Test
133
148
public void givenMultiValuesMap_whenUsingContainsKeyMethod_thenReturningTrue () {
134
149
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
135
- map .put ("key" , "value" );
136
- map .put ("key1" , "value1" );
137
- map .put ("key2" , "value2" );
150
+ map .put ("fruits" , "apple" );
151
+ map .put ("fruits" , "orange" );
152
+ map .put ("vehicles" , "car" );
153
+ map .put ("vehicles" , "bike" );
138
154
139
- assertTrue (map .containsKey ("key " ));
155
+ assertTrue (map .containsKey ("fruits " ));
140
156
}
141
157
142
158
@ Test
143
159
public void givenMultiValuesMap_whenUsingContainsValueMethod_thenReturningTrue () {
144
160
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
145
- map .put ("key" , "value" );
146
- map .put ("key1" , "value1" );
147
- map .put ("key2" , "value2" );
161
+ map .put ("fruits" , "apple" );
162
+ map .put ("fruits" , "orange" );
163
+ map .put ("vehicles" , "car" );
164
+ map .put ("vehicles" , "bike" );
148
165
149
- assertTrue (map .containsValue ("value " ));
166
+ assertTrue (map .containsValue ("orange " ));
150
167
}
151
168
152
169
@ Test
153
170
public void givenMultiValuesMap_whenUsingIsEmptyMethod_thenReturningFalse () {
154
171
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
155
- map .put ("key" , "value" );
156
- map .put ("key1" , "value1" );
157
- map .put ("key2" , "value2" );
172
+ map .put ("fruits" , "apple" );
173
+ map .put ("fruits" , "orange" );
174
+ map .put ("vehicles" , "car" );
175
+ map .put ("vehicles" , "bike" );
158
176
159
177
assertFalse (map .isEmpty ());
160
178
}
161
179
162
180
@ Test
163
181
public void givenMultiValuesMap_whenUsingSizeMethod_thenReturningElementCount () {
164
182
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
165
- map .put ("key" , "value" );
166
- map .put ("key1" , "value1" );
167
- map .put ("key2" , "value2" );
183
+ map .put ("fruits" , "apple" );
184
+ map .put ("fruits" , "orange" );
185
+ map .put ("vehicles" , "car" );
186
+ map .put ("vehicles" , "bike" );
168
187
169
- assertEquals (3 , map .size ());
188
+ assertEquals (4 , map .size ());
170
189
}
171
190
172
191
@ Test
173
192
public void givenArrayListValuedHashMap_whenPuttingDoubleValues_thenReturningAllValues () {
174
193
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
194
+ map .put ("fruits" , "apple" );
195
+ map .put ("fruits" , "orange" );
196
+ map .put ("fruits" , "orange" );
175
197
176
- map .put ("key" , "value1" );
177
- map .put ("key" , "value2" );
178
- map .put ("key" , "value2" );
179
-
180
- assertThat ((Collection <String >) map .get ("key" )).containsExactly ("value1" , "value2" , "value2" );
198
+ assertThat ((Collection <String >) map .get ("fruits" )).containsExactly ("apple" , "orange" , "orange" );
181
199
}
182
200
183
201
@ Test
184
202
public void givenHashSetValuedHashMap_whenPuttingTwiceTheSame_thenReturningOneValue () {
185
203
MultiValuedMap <String , String > map = new HashSetValuedHashMap <>();
186
-
187
- map .put ("key1" , "value1" );
188
- map .put ("key1" , "value1" );
189
-
190
- assertThat ((Collection <String >) map .get ("key1" )).containsExactly ("value1" );
204
+ map .put ("fruits" , "apple" );
205
+ map .put ("fruits" , "apple" );
206
+
207
+ assertThat ((Collection <String >) map .get ("fruits" )).containsExactly ("apple" );
191
208
}
192
209
193
210
@ Test (expected = UnsupportedOperationException .class )
194
211
public void givenUnmodifiableMultiValuedMap_whenInserting_thenThrowingException () {
195
212
MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
196
- map .put ("key " , "value1 " );
197
- map .put ("key " , "value2 " );
213
+ map .put ("fruits " , "apple " );
214
+ map .put ("fruits " , "orange " );
198
215
MultiValuedMap <String , String > immutableMap = MultiMapUtils .unmodifiableMultiValuedMap (map );
199
216
200
- immutableMap .put ("key" , "value3" );
217
+ immutableMap .put ("fruits" , "banana" );
218
+
201
219
}
202
220
203
221
0 commit comments