Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableToMultimapTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.observable;
15
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18
19 import java.util.*;
20
21 import org.junit.*;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.core.Observable;
25 import io.reactivex.rxjava3.core.Observer;
26 import io.reactivex.rxjava3.functions.*;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class ObservableToMultimapTest extends RxJavaTest {
30 Observer<Object> objectObserver;
31 SingleObserver<Object> singleObserver;
32
33 @Before
34 public void before() {
35 objectObserver = TestHelper.mockObserver();
36 singleObserver = TestHelper.mockSingleObserver();
37 }
38
39 Function<String, Integer> lengthFunc = new Function<String, Integer>() {
40 @Override
41 public Integer apply(String t1) {
42 return t1.length();
43 }
44 };
45 Function<String, String> duplicate = new Function<String, String>() {
46 @Override
47 public String apply(String t1) {
48 return t1 + t1;
49 }
50 };
51
52 @Test
53 public void toMultimapObservable() {
54 Observable<String> source = Observable.just("a", "b", "cc", "dd");
55
56 Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc).toObservable();
57
58 Map<Integer, Collection<String>> expected = new HashMap<>();
59 expected.put(1, Arrays.asList("a", "b"));
60 expected.put(2, Arrays.asList("cc", "dd"));
61
62 mapped.subscribe(objectObserver);
63
64 verify(objectObserver, never()).onError(any(Throwable.class));
65 verify(objectObserver, times(1)).onNext(expected);
66 verify(objectObserver, times(1)).onComplete();
67 }
68
69 @Test
70 public void toMultimapWithValueSelectorObservable() {
71 Observable<String> source = Observable.just("a", "b", "cc", "dd");
72
73 Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicate).toObservable();
74
75 Map<Integer, Collection<String>> expected = new HashMap<>();
76 expected.put(1, Arrays.asList("aa", "bb"));
77 expected.put(2, Arrays.asList("cccc", "dddd"));
78
79 mapped.subscribe(objectObserver);
80
81 verify(objectObserver, never()).onError(any(Throwable.class));
82 verify(objectObserver, times(1)).onNext(expected);
83 verify(objectObserver, times(1)).onComplete();
84 }
85
86 @Test
87 public void toMultimapWithMapFactoryObservable() {
88 Observable<String> source = Observable.just("a", "b", "cc", "dd", "eee", "fff");
89
90 Supplier<Map<Integer, Collection<String>>> mapFactory = new Supplier<Map<Integer, Collection<String>>>() {
91 @Override
92 public Map<Integer, Collection<String>> get() {
93 return new LinkedHashMap<Integer, Collection<String>>() {
94
95 private static final long serialVersionUID = -2084477070717362859L;
96
97 @Override
98 protected boolean removeEldestEntry(Map.Entry<Integer, Collection<String>> eldest) {
99 return size() > 2;
100 }
101 };
102 }
103 };
104
105 Function<String, String> identity = new Function<String, String>() {
106 @Override
107 public String apply(String v) {
108 return v;
109 }
110 };
111
112 Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(
113 lengthFunc, identity,
114 mapFactory, new Function<Integer, Collection<String>>() {
115 @Override
116 public Collection<String> apply(Integer v) {
117 return new ArrayList<>();
118 }
119 }).toObservable();
120
121 Map<Integer, Collection<String>> expected = new HashMap<>();
122 expected.put(2, Arrays.asList("cc", "dd"));
123 expected.put(3, Arrays.asList("eee", "fff"));
124
125 mapped.subscribe(objectObserver);
126
127 verify(objectObserver, never()).onError(any(Throwable.class));
128 verify(objectObserver, times(1)).onNext(expected);
129 verify(objectObserver, times(1)).onComplete();
130 }
131
132 @Test
133 public void toMultimapWithCollectionFactoryObservable() {
134 Observable<String> source = Observable.just("cc", "dd", "eee", "eee");
135
136 Function<Integer, Collection<String>> collectionFactory = new Function<Integer, Collection<String>>() {
137 @Override
138 public Collection<String> apply(Integer t1) {
139 if (t1 == 2) {
140 return new ArrayList<>();
141 } else {
142 return new HashSet<>();
143 }
144 }
145 };
146
147 Function<String, String> identity = new Function<String, String>() {
148 @Override
149 public String apply(String v) {
150 return v;
151 }
152 };
153 Supplier<Map<Integer, Collection<String>>> mapSupplier = new Supplier<Map<Integer, Collection<String>>>() {
154 @Override
155 public Map<Integer, Collection<String>> get() {
156 return new HashMap<>();
157 }
158 };
159
160 Observable<Map<Integer, Collection<String>>> mapped = source
161 .toMultimap(lengthFunc, identity, mapSupplier, collectionFactory).toObservable();
162
163 Map<Integer, Collection<String>> expected = new HashMap<>();
164 expected.put(2, Arrays.asList("cc", "dd"));
165 expected.put(3, new HashSet<>(Arrays.asList("eee")));
166
167 mapped.subscribe(objectObserver);
168
169 verify(objectObserver, never()).onError(any(Throwable.class));
170 verify(objectObserver, times(1)).onNext(expected);
171 verify(objectObserver, times(1)).onComplete();
172 }
173
174 @Test
175 public void toMultimapWithErrorObservable() {
176 Observable<String> source = Observable.just("a", "b", "cc", "dd");
177
178 Function<String, Integer> lengthFuncErr = new Function<String, Integer>() {
179 @Override
180 public Integer apply(String t1) {
181 if ("b".equals(t1)) {
182 throw new RuntimeException("Forced Failure");
183 }
184 return t1.length();
185 }
186 };
187
188 Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFuncErr).toObservable();
189
190 Map<Integer, Collection<String>> expected = new HashMap<>();
191 expected.put(1, Arrays.asList("a", "b"));
192 expected.put(2, Arrays.asList("cc", "dd"));
193
194 mapped.subscribe(objectObserver);
195
196 verify(objectObserver, times(1)).onError(any(Throwable.class));
197 verify(objectObserver, never()).onNext(expected);
198 verify(objectObserver, never()).onComplete();
199 }
200
201 @Test
202 public void toMultimapWithErrorInValueSelectorObservable() {
203 Observable<String> source = Observable.just("a", "b", "cc", "dd");
204
205 Function<String, String> duplicateErr = new Function<String, String>() {
206 @Override
207 public String apply(String t1) {
208 if ("b".equals(t1)) {
209 throw new RuntimeException("Forced failure");
210 }
211 return t1 + t1;
212 }
213 };
214
215 Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicateErr).toObservable();
216
217 Map<Integer, Collection<String>> expected = new HashMap<>();
218 expected.put(1, Arrays.asList("aa", "bb"));
219 expected.put(2, Arrays.asList("cccc", "dddd"));
220
221 mapped.subscribe(objectObserver);
222
223 verify(objectObserver, times(1)).onError(any(Throwable.class));
224 verify(objectObserver, never()).onNext(expected);
225 verify(objectObserver, never()).onComplete();
226 }
227
228 @Test
229 public void toMultimapWithMapThrowingFactoryObservable() {
230 Observable<String> source = Observable.just("a", "b", "cc", "dd", "eee", "fff");
231
232 Supplier<Map<Integer, Collection<String>>> mapFactory = new Supplier<Map<Integer, Collection<String>>>() {
233 @Override
234 public Map<Integer, Collection<String>> get() {
235 throw new RuntimeException("Forced failure");
236 }
237 };
238
239 Observable<Map<Integer, Collection<String>>> mapped = source
240 .toMultimap(lengthFunc, new Function<String, String>() {
241 @Override
242 public String apply(String v) {
243 return v;
244 }
245 }, mapFactory).toObservable();
246
247 Map<Integer, Collection<String>> expected = new HashMap<>();
248 expected.put(2, Arrays.asList("cc", "dd"));
249 expected.put(3, Arrays.asList("eee", "fff"));
250
251 mapped.subscribe(objectObserver);
252
253 verify(objectObserver, times(1)).onError(any(Throwable.class));
254 verify(objectObserver, never()).onNext(expected);
255 verify(objectObserver, never()).onComplete();
256 }
257
258 @Test
259 public void toMultimapWithThrowingCollectionFactoryObservable() {
260 Observable<String> source = Observable.just("cc", "cc", "eee", "eee");
261
262 Function<Integer, Collection<String>> collectionFactory = new Function<Integer, Collection<String>>() {
263 @Override
264 public Collection<String> apply(Integer t1) {
265 if (t1 == 2) {
266 throw new RuntimeException("Forced failure");
267 } else {
268 return new HashSet<>();
269 }
270 }
271 };
272
273 Function<String, String> identity = new Function<String, String>() {
274 @Override
275 public String apply(String v) {
276 return v;
277 }
278 };
279 Supplier<Map<Integer, Collection<String>>> mapSupplier = new Supplier<Map<Integer, Collection<String>>>() {
280 @Override
281 public Map<Integer, Collection<String>> get() {
282 return new HashMap<>();
283 }
284 };
285
286 Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc,
287 identity, mapSupplier, collectionFactory).toObservable();
288
289 Map<Integer, Collection<String>> expected = new HashMap<>();
290 expected.put(2, Arrays.asList("cc", "dd"));
291 expected.put(3, Collections.singleton("eee"));
292
293 mapped.subscribe(objectObserver);
294
295 verify(objectObserver, times(1)).onError(any(Throwable.class));
296 verify(objectObserver, never()).onNext(expected);
297 verify(objectObserver, never()).onComplete();
298 }
299
300 @Test
301 public void toMultimap() {
302 Observable<String> source = Observable.just("a", "b", "cc", "dd");
303
304 Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc);
305
306 Map<Integer, Collection<String>> expected = new HashMap<>();
307 expected.put(1, Arrays.asList("a", "b"));
308 expected.put(2, Arrays.asList("cc", "dd"));
309
310 mapped.subscribe(singleObserver);
311
312 verify(singleObserver, never()).onError(any(Throwable.class));
313 verify(singleObserver, times(1)).onSuccess(expected);
314 }
315
316 @Test
317 public void toMultimapWithValueSelector() {
318 Observable<String> source = Observable.just("a", "b", "cc", "dd");
319
320 Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicate);
321
322 Map<Integer, Collection<String>> expected = new HashMap<>();
323 expected.put(1, Arrays.asList("aa", "bb"));
324 expected.put(2, Arrays.asList("cccc", "dddd"));
325
326 mapped.subscribe(singleObserver);
327
328 verify(singleObserver, never()).onError(any(Throwable.class));
329 verify(singleObserver, times(1)).onSuccess(expected);
330 }
331
332 @Test
333 public void toMultimapWithMapFactory() {
334 Observable<String> source = Observable.just("a", "b", "cc", "dd", "eee", "fff");
335
336 Supplier<Map<Integer, Collection<String>>> mapFactory = new Supplier<Map<Integer, Collection<String>>>() {
337 @Override
338 public Map<Integer, Collection<String>> get() {
339 return new LinkedHashMap<Integer, Collection<String>>() {
340
341 private static final long serialVersionUID = -2084477070717362859L;
342
343 @Override
344 protected boolean removeEldestEntry(Map.Entry<Integer, Collection<String>> eldest) {
345 return size() > 2;
346 }
347 };
348 }
349 };
350
351 Function<String, String> identity = new Function<String, String>() {
352 @Override
353 public String apply(String v) {
354 return v;
355 }
356 };
357
358 Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(
359 lengthFunc, identity,
360 mapFactory, new Function<Integer, Collection<String>>() {
361 @Override
362 public Collection<String> apply(Integer v) {
363 return new ArrayList<>();
364 }
365 });
366
367 Map<Integer, Collection<String>> expected = new HashMap<>();
368 expected.put(2, Arrays.asList("cc", "dd"));
369 expected.put(3, Arrays.asList("eee", "fff"));
370
371 mapped.subscribe(singleObserver);
372
373 verify(singleObserver, never()).onError(any(Throwable.class));
374 verify(singleObserver, times(1)).onSuccess(expected);
375 }
376
377 @Test
378 public void toMultimapWithCollectionFactory() {
379 Observable<String> source = Observable.just("cc", "dd", "eee", "eee");
380
381 Function<Integer, Collection<String>> collectionFactory = new Function<Integer, Collection<String>>() {
382 @Override
383 public Collection<String> apply(Integer t1) {
384 if (t1 == 2) {
385 return new ArrayList<>();
386 } else {
387 return new HashSet<>();
388 }
389 }
390 };
391
392 Function<String, String> identity = new Function<String, String>() {
393 @Override
394 public String apply(String v) {
395 return v;
396 }
397 };
398 Supplier<Map<Integer, Collection<String>>> mapSupplier = new Supplier<Map<Integer, Collection<String>>>() {
399 @Override
400 public Map<Integer, Collection<String>> get() {
401 return new HashMap<>();
402 }
403 };
404
405 Single<Map<Integer, Collection<String>>> mapped = source
406 .toMultimap(lengthFunc, identity, mapSupplier, collectionFactory);
407
408 Map<Integer, Collection<String>> expected = new HashMap<>();
409 expected.put(2, Arrays.asList("cc", "dd"));
410 expected.put(3, new HashSet<>(Arrays.asList("eee")));
411
412 mapped.subscribe(singleObserver);
413
414 verify(singleObserver, never()).onError(any(Throwable.class));
415 verify(singleObserver, times(1)).onSuccess(expected);
416 }
417
418 @Test
419 public void toMultimapWithError() {
420 Observable<String> source = Observable.just("a", "b", "cc", "dd");
421
422 Function<String, Integer> lengthFuncErr = new Function<String, Integer>() {
423 @Override
424 public Integer apply(String t1) {
425 if ("b".equals(t1)) {
426 throw new RuntimeException("Forced Failure");
427 }
428 return t1.length();
429 }
430 };
431
432 Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFuncErr);
433
434 Map<Integer, Collection<String>> expected = new HashMap<>();
435 expected.put(1, Arrays.asList("a", "b"));
436 expected.put(2, Arrays.asList("cc", "dd"));
437
438 mapped.subscribe(singleObserver);
439
440 verify(singleObserver, times(1)).onError(any(Throwable.class));
441 verify(singleObserver, never()).onSuccess(expected);
442 }
443
444 @Test
445 public void toMultimapWithErrorInValueSelector() {
446 Observable<String> source = Observable.just("a", "b", "cc", "dd");
447
448 Function<String, String> duplicateErr = new Function<String, String>() {
449 @Override
450 public String apply(String t1) {
451 if ("b".equals(t1)) {
452 throw new RuntimeException("Forced failure");
453 }
454 return t1 + t1;
455 }
456 };
457
458 Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicateErr);
459
460 Map<Integer, Collection<String>> expected = new HashMap<>();
461 expected.put(1, Arrays.asList("aa", "bb"));
462 expected.put(2, Arrays.asList("cccc", "dddd"));
463
464 mapped.subscribe(singleObserver);
465
466 verify(singleObserver, times(1)).onError(any(Throwable.class));
467 verify(singleObserver, never()).onSuccess(expected);
468 }
469
470 @Test
471 public void toMultimapWithMapThrowingFactory() {
472 Observable<String> source = Observable.just("a", "b", "cc", "dd", "eee", "fff");
473
474 Supplier<Map<Integer, Collection<String>>> mapFactory = new Supplier<Map<Integer, Collection<String>>>() {
475 @Override
476 public Map<Integer, Collection<String>> get() {
477 throw new RuntimeException("Forced failure");
478 }
479 };
480
481 Single<Map<Integer, Collection<String>>> mapped = source
482 .toMultimap(lengthFunc, new Function<String, String>() {
483 @Override
484 public String apply(String v) {
485 return v;
486 }
487 }, mapFactory);
488
489 Map<Integer, Collection<String>> expected = new HashMap<>();
490 expected.put(2, Arrays.asList("cc", "dd"));
491 expected.put(3, Arrays.asList("eee", "fff"));
492
493 mapped.subscribe(singleObserver);
494
495 verify(singleObserver, times(1)).onError(any(Throwable.class));
496 verify(singleObserver, never()).onSuccess(expected);
497 }
498
499 @Test
500 public void toMultimapWithThrowingCollectionFactory() {
501 Observable<String> source = Observable.just("cc", "cc", "eee", "eee");
502
503 Function<Integer, Collection<String>> collectionFactory = new Function<Integer, Collection<String>>() {
504 @Override
505 public Collection<String> apply(Integer t1) {
506 if (t1 == 2) {
507 throw new RuntimeException("Forced failure");
508 } else {
509 return new HashSet<>();
510 }
511 }
512 };
513
514 Function<String, String> identity = new Function<String, String>() {
515 @Override
516 public String apply(String v) {
517 return v;
518 }
519 };
520 Supplier<Map<Integer, Collection<String>>> mapSupplier = new Supplier<Map<Integer, Collection<String>>>() {
521 @Override
522 public Map<Integer, Collection<String>> get() {
523 return new HashMap<>();
524 }
525 };
526
527 Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc,
528 identity, mapSupplier, collectionFactory);
529
530 Map<Integer, Collection<String>> expected = new HashMap<>();
531 expected.put(2, Arrays.asList("cc", "dd"));
532 expected.put(3, Collections.singleton("eee"));
533
534 mapped.subscribe(singleObserver);
535
536 verify(singleObserver, times(1)).onError(any(Throwable.class));
537 verify(singleObserver, never()).onSuccess(expected);
538 }
539 }