Debian Codegraph @

src/test/java/io/reactivex/rxjava3/testsupport/TestObserverExTest.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.testsupport;
15
16 import static org.junit.Assert.*;
17 import static org.mockito.Mockito.*;
18
19 import java.io.IOException;
20 import java.util.*;
21 import java.util.concurrent.TimeUnit;
22
23 import org.junit.Test;
24 import org.mockito.InOrder;
25
26 import io.reactivex.rxjava3.core.Observable;
27 import io.reactivex.rxjava3.core.Observer;
28 import io.reactivex.rxjava3.core.RxJavaTest;
29 import io.reactivex.rxjava3.disposables.Disposable;
30 import io.reactivex.rxjava3.exceptions.TestException;
31 import io.reactivex.rxjava3.functions.*;
32 import io.reactivex.rxjava3.internal.functions.Functions;
33 import io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable;
34 import io.reactivex.rxjava3.observers.TestObserver;
35 import io.reactivex.rxjava3.operators.QueueFuseable;
36 import io.reactivex.rxjava3.schedulers.Schedulers;
37 import io.reactivex.rxjava3.subjects.*;
38
39 public class TestObserverExTest extends RxJavaTest {
40
41 @Test
42 public void assertTestObserverEx() {
43 Observable<Integer> oi = Observable.fromIterable(Arrays.asList(1, 2));
44 TestObserverEx<Integer> subscriber = new TestObserverEx<>();
45 oi.subscribe(subscriber);
46
47 subscriber.assertValues(1, 2);
48 subscriber.assertValueCount(2);
49 subscriber.assertTerminated();
50 }
51
52 @Test
53 public void assertNotMatchCount() {
54 assertThrows(AssertionError.class, () -> {
55 Observable<Integer> oi = Observable.fromIterable(Arrays.asList(1, 2));
56 TestObserverEx<Integer> subscriber = new TestObserverEx<>();
57 oi.subscribe(subscriber);
58
59 subscriber.assertValue(1);
60 subscriber.assertValueCount(2);
61 subscriber.assertTerminated();
62 });
63 }
64
65 @Test
66 public void assertNotMatchValue() {
67 assertThrows(AssertionError.class, () -> {
68 Observable<Integer> oi = Observable.fromIterable(Arrays.asList(1, 2));
69 TestObserverEx<Integer> subscriber = new TestObserverEx<>();
70 oi.subscribe(subscriber);
71
72 subscriber.assertValues(1, 3);
73 subscriber.assertValueCount(2);
74 subscriber.assertTerminated();
75 });
76 }
77
78 @Test
79 public void assertNeverAtNotMatchingValue() {
80 Observable<Integer> oi = Observable.fromIterable(Arrays.asList(1, 2));
81 TestObserverEx<Integer> subscriber = new TestObserverEx<>();
82 oi.subscribe(subscriber);
83
84 subscriber.assertNever(3);
85 subscriber.assertValueCount(2);
86 subscriber.assertTerminated();
87 }
88
89 @Test
90 public void assertNeverAtMatchingValue() {
91 assertThrows(AssertionError.class, () -> {
92 Observable<Integer> oi = Observable.fromIterable(Arrays.asList(1, 2));
93 TestObserverEx<Integer> subscriber = new TestObserverEx<>();
94 oi.subscribe(subscriber);
95
96 subscriber.assertValues(1, 2);
97
98 subscriber.assertNever(2);
99 subscriber.assertValueCount(2);
100 subscriber.assertTerminated();
101 });
102 }
103
104 @Test
105 public void assertNeverAtMatchingPredicate() {
106 assertThrows(AssertionError.class, () -> {
107 TestObserverEx<Integer> to = new TestObserverEx<>();
108
109 Observable.just(1, 2).subscribe(to);
110
111 to.assertValues(1, 2);
112
113 to.assertNever(new Predicate<Integer>() {
114 @Override
115 public boolean test(final Integer o) throws Exception {
116 return o == 1;
117 }
118 });
119 });
120 }
121
122 @Test
123 public void assertNeverAtNotMatchingPredicate() {
124 TestObserverEx<Integer> to = new TestObserverEx<>();
125
126 Observable.just(2, 3).subscribe(to);
127
128 to.assertNever(new Predicate<Integer>() {
129 @Override
130 public boolean test(final Integer o) throws Exception {
131 return o == 1;
132 }
133 });
134 }
135
136 @Test
137 public void assertTerminalEventNotReceived() {
138 assertThrows(AssertionError.class, () -> {
139 PublishSubject<Integer> p = PublishSubject.create();
140 TestObserverEx<Integer> subscriber = new TestObserverEx<>();
141 p.subscribe(subscriber);
142
143 p.onNext(1);
144 p.onNext(2);
145
146 subscriber.assertValues(1, 2);
147 subscriber.assertValueCount(2);
148 subscriber.assertTerminated();
149 });
150 }
151
152 @Test
153 public void wrappingMock() {
154 Observable<Integer> oi = Observable.fromIterable(Arrays.asList(1, 2));
155
156 Observer<Integer> mockSubscriber = TestHelper.mockObserver();
157
158 oi.subscribe(new TestObserverEx<>(mockSubscriber));
159
160 InOrder inOrder = inOrder(mockSubscriber);
161 inOrder.verify(mockSubscriber, times(1)).onNext(1);
162 inOrder.verify(mockSubscriber, times(1)).onNext(2);
163 inOrder.verify(mockSubscriber, times(1)).onComplete();
164 inOrder.verifyNoMoreInteractions();
165 }
166
167 @Test
168 public void wrappingMockWhenUnsubscribeInvolved() {
169 Observable<Integer> oi = Observable.fromIterable(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)).take(2);
170 Observer<Integer> mockSubscriber = TestHelper.mockObserver();
171 oi.subscribe(new TestObserverEx<>(mockSubscriber));
172
173 InOrder inOrder = inOrder(mockSubscriber);
174 inOrder.verify(mockSubscriber, times(1)).onNext(1);
175 inOrder.verify(mockSubscriber, times(1)).onNext(2);
176 inOrder.verify(mockSubscriber, times(1)).onComplete();
177 inOrder.verifyNoMoreInteractions();
178 }
179
180 @Test
181 public void errorSwallowed() {
182 Observable.error(new RuntimeException()).subscribe(new TestObserverEx<>());
183 }
184
185 @Test
186 public void nullExpected() {
187 TestObserverEx<Integer> to = new TestObserverEx<>();
188 to.onNext(1);
189
190 try {
191 to.assertValue((Integer) null);
192 } catch (AssertionError ex) {
193 // this is expected
194 return;
195 }
196 fail("Null element check assertion didn't happen!");
197 }
198
199 @Test
200 public void nullActual() {
201 TestObserverEx<Integer> to = new TestObserverEx<>();
202 to.onNext(null);
203
204 try {
205 to.assertValue(1);
206 } catch (AssertionError ex) {
207 // this is expected
208 return;
209 }
210 fail("Null element check assertion didn't happen!");
211 }
212
213 @Test
214 public void terminalErrorOnce() {
215 TestObserverEx<Integer> to = new TestObserverEx<>();
216 to.onError(new TestException());
217 to.onError(new TestException());
218
219 try {
220 to.assertTerminated();
221 } catch (AssertionError ex) {
222 // this is expected
223 return;
224 }
225 fail("Failed to report multiple onError terminal events!");
226 }
227
228 @Test
229 public void terminalCompletedOnce() {
230 TestObserverEx<Integer> to = new TestObserverEx<>();
231 to.onComplete();
232 to.onComplete();
233
234 try {
235 to.assertTerminated();
236 } catch (AssertionError ex) {
237 // this is expected
238 return;
239 }
240 fail("Failed to report multiple onComplete terminal events!");
241 }
242
243 @Test
244 public void terminalOneKind() {
245 TestObserverEx<Integer> to = new TestObserverEx<>();
246 to.onError(new TestException());
247 to.onComplete();
248
249 try {
250 to.assertTerminated();
251 } catch (AssertionError ex) {
252 // this is expected
253 return;
254 }
255 fail("Failed to report multiple kinds of events!");
256 }
257
258 @Test
259 public void createDelegate() {
260 TestObserverEx<Integer> to1 = new TestObserverEx<>();
261
262 TestObserverEx<Integer> to = new TestObserverEx<>(to1);
263
264 to.assertNotSubscribed();
265
266 assertFalse(to.hasSubscription());
267
268 to.onSubscribe(Disposable.empty());
269
270 try {
271 to.assertNotSubscribed();
272 throw new RuntimeException("Should have thrown");
273 } catch (AssertionError ex) {
274 // expected
275 }
276
277 assertTrue(to.hasSubscription());
278
279 assertFalse(to.isDisposed());
280
281 to.onNext(1);
282 to.onError(new TestException());
283 to.onComplete();
284
285 to1.assertValue(1).assertError(TestException.class).assertComplete();
286
287 to.dispose();
288
289 assertTrue(to.isDisposed());
290
291 assertSame(Thread.currentThread(), to.lastThread());
292
293 try {
294 to.assertNoValues();
295 throw new RuntimeException("Should have thrown");
296 } catch (AssertionError exc) {
297 // expected
298 }
299
300 try {
301 to.assertValueCount(0);
302 throw new RuntimeException("Should have thrown");
303 } catch (AssertionError exc) {
304 // expected
305 }
306
307 to.assertValueSequence(Collections.singletonList(1));
308
309 try {
310 to.assertValueSequence(Collections.singletonList(2));
311 throw new RuntimeException("Should have thrown");
312 } catch (AssertionError exc) {
313 // expected
314 }
315 }
316
317 @Test
318 public void assertError() {
319 TestObserverEx<Integer> to = new TestObserverEx<>();
320
321 try {
322 to.assertError(TestException.class);
323 throw new RuntimeException("Should have thrown");
324 } catch (AssertionError ex) {
325 // expected
326 }
327
328 try {
329 to.assertError(new TestException());
330 throw new RuntimeException("Should have thrown");
331 } catch (AssertionError ex) {
332 // expected
333 }
334
335 try {
336 to.assertError(Functions.<Throwable>alwaysTrue());
337 throw new RuntimeException("Should have thrown");
338 } catch (AssertionError ex) {
339 // expected
340 }
341
342 try {
343 to.assertErrorMessage("");
344 throw new RuntimeException("Should have thrown");
345 } catch (AssertionError exc) {
346 // expected
347 }
348
349 try {
350 to.assertSubscribed();
351 throw new RuntimeException("Should have thrown");
352 } catch (AssertionError exc) {
353 // expected
354 }
355
356 try {
357 to.assertTerminated();
358 throw new RuntimeException("Should have thrown");
359 } catch (AssertionError exc) {
360 // expected
361 }
362
363 to.onSubscribe(Disposable.empty());
364
365 to.assertSubscribed();
366
367 to.assertNoErrors();
368
369 TestException ex = new TestException("Forced failure");
370
371 to.onError(ex);
372
373 to.assertError(ex);
374
375 to.assertError(TestException.class);
376
377 to.assertError(Functions.<Throwable>alwaysTrue());
378
379 to.assertError(new Predicate<Throwable>() {
380 @Override
381 public boolean test(Throwable t) throws Exception {
382 return t.getMessage() != null && t.getMessage().contains("Forced");
383 }
384 });
385
386 to.assertErrorMessage("Forced failure");
387
388 try {
389 to.assertErrorMessage("");
390 throw new RuntimeException("Should have thrown");
391 } catch (AssertionError exc) {
392 // expected
393 }
394
395 try {
396 to.assertError(new RuntimeException());
397 throw new RuntimeException("Should have thrown");
398 } catch (AssertionError exc) {
399 // expected
400 }
401
402 try {
403 to.assertError(IOException.class);
404 throw new RuntimeException("Should have thrown");
405 } catch (AssertionError exc) {
406 // expected
407 }
408
409 try {
410 to.assertError(Functions.<Throwable>alwaysFalse());
411 throw new RuntimeException("Should have thrown");
412 } catch (AssertionError exc) {
413 // expected
414 }
415
416 try {
417 to.assertNoErrors();
418 throw new RuntimeException("Should have thrown");
419 } catch (AssertionError exc) {
420 // expected
421 }
422
423 to.assertTerminated();
424
425 to.assertValueCount(0);
426
427 to.assertNoValues();
428 }
429
430 @Test
431 public void emptyObserverEnum() {
432 assertEquals(1, TestObserverEx.EmptyObserver.values().length);
433 assertNotNull(TestObserverEx.EmptyObserver.valueOf("INSTANCE"));
434 }
435
436 @Test
437 public void valueAndClass() {
438 assertEquals("null", TestObserver.valueAndClass(null));
439 assertEquals("1 (class: Integer)", TestObserver.valueAndClass(1));
440 }
441
442 @Test
443 public void assertFailure() {
444 TestObserverEx<Integer> to = new TestObserverEx<>();
445
446 to.onSubscribe(Disposable.empty());
447
448 to.onError(new TestException("Forced failure"));
449
450 to.assertFailure(TestException.class);
451
452 to.assertFailure(Functions.<Throwable>alwaysTrue());
453
454 to.assertFailureAndMessage(TestException.class, "Forced failure");
455
456 to.onNext(1);
457
458 to.assertFailure(TestException.class, 1);
459
460 to.assertFailure(Functions.<Throwable>alwaysTrue(), 1);
461
462 to.assertFailureAndMessage(TestException.class, "Forced failure", 1);
463 }
464
465 @Test
466 public void assertFuseable() {
467 TestObserverEx<Integer> to = new TestObserverEx<>();
468
469 to.onSubscribe(Disposable.empty());
470
471 to.assertNotFuseable();
472
473 try {
474 to.assertFuseable();
475 throw new RuntimeException("Should have thrown");
476 } catch (AssertionError ex) {
477 // expected
478 }
479
480 try {
481 to.assertFusionMode(QueueFuseable.SYNC);
482 throw new RuntimeException("Should have thrown");
483 } catch (AssertionError ex) {
484 // expected
485 }
486
487 to = new TestObserverEx<>();
488 to.setInitialFusionMode(QueueFuseable.ANY);
489
490 to.onSubscribe(new ScalarDisposable<>(to, 1));
491
492 to.assertFuseable();
493
494 to.assertFusionMode(QueueFuseable.SYNC);
495
496 try {
497 to.assertFusionMode(QueueFuseable.NONE);
498 throw new RuntimeException("Should have thrown");
499 } catch (AssertionError ex) {
500 // expected
501 }
502
503 try {
504 to.assertNotFuseable();
505 throw new RuntimeException("Should have thrown");
506 } catch (AssertionError ex) {
507 // expected
508 }
509
510 }
511
512 @Test
513 public void assertTerminated() {
514 TestObserverEx<Integer> to = new TestObserverEx<>();
515
516 to.assertNotTerminated();
517
518 to.onError(null);
519
520 try {
521 to.assertNotTerminated();
522 throw new RuntimeException("Should have thrown!");
523 } catch (AssertionError ex) {
524 // expected
525 }
526 }
527
528 @Test
529 public void assertResult() {
530 TestObserverEx<Integer> to = new TestObserverEx<>();
531
532 to.onSubscribe(Disposable.empty());
533
534 to.onComplete();
535
536 to.assertResult();
537
538 try {
539 to.assertResult(1);
540 throw new RuntimeException("Should have thrown");
541 } catch (AssertionError ex) {
542 // expected
543 }
544
545 to.onNext(1);
546
547 to.assertResult(1);
548
549 try {
550 to.assertResult(2);
551 throw new RuntimeException("Should have thrown");
552 } catch (AssertionError ex) {
553 // expected
554 }
555
556 try {
557 to.assertResult();
558 throw new RuntimeException("Should have thrown");
559 } catch (AssertionError ex) {
560 // expected
561 }
562
563 }
564
565 @Test
566 public void await() throws Exception {
567 TestObserverEx<Integer> to = new TestObserverEx<>();
568
569 to.onSubscribe(Disposable.empty());
570
571 assertFalse(to.await(100, TimeUnit.MILLISECONDS));
572
573 to.awaitDone(100, TimeUnit.MILLISECONDS);
574
575 assertTrue(to.isDisposed());
576
577 to.assertNotTerminated();
578
579 to.onComplete();
580
581 assertTrue(to.await(100, TimeUnit.MILLISECONDS));
582
583 to.await();
584
585 to.awaitDone(5, TimeUnit.SECONDS);
586
587 to.assertNoErrors().assertComplete();
588
589 final TestObserverEx<Integer> to1 = new TestObserverEx<>();
590
591 to1.onSubscribe(Disposable.empty());
592
593 Schedulers.single().scheduleDirect(new Runnable() {
594 @Override
595 public void run() {
596 to1.onComplete();
597 }
598 }, 200, TimeUnit.MILLISECONDS);
599
600 to1.await();
601 }
602
603 @Test
604 public void errors() {
605 TestObserverEx<Integer> to = new TestObserverEx<>();
606
607 to.onSubscribe(Disposable.empty());
608
609 assertEquals(0, to.errors().size());
610
611 to.onError(new TestException());
612
613 assertEquals(1, to.errors().size());
614
615 TestHelper.assertError(to.errors(), 0, TestException.class);
616 }
617
618 @Test
619 public void onNext() {
620 TestObserverEx<Integer> to = new TestObserverEx<>();
621
622 to.onSubscribe(Disposable.empty());
623
624 to.assertValueCount(0);
625
626 assertEquals(Collections.emptyList(), to.values());
627
628 to.onNext(1);
629
630 assertEquals(Collections.singletonList(1), to.values());
631
632 to.dispose();
633
634 assertTrue(to.isDisposed());
635
636 to.assertValue(1);
637
638 to.onComplete();
639 }
640
641 @Test
642 public void fusionModeToString() {
643 assertEquals("NONE", TestObserverEx.fusionModeToString(QueueFuseable.NONE));
644 assertEquals("SYNC", TestObserverEx.fusionModeToString(QueueFuseable.SYNC));
645 assertEquals("ASYNC", TestObserverEx.fusionModeToString(QueueFuseable.ASYNC));
646 assertEquals("Unknown(100)", TestObserverEx.fusionModeToString(100));
647 }
648
649 @Test
650 public void multipleTerminals() {
651 TestObserverEx<Integer> to = new TestObserverEx<>();
652
653 to.onSubscribe(Disposable.empty());
654
655 to.assertNotComplete();
656
657 to.onComplete();
658
659 try {
660 to.assertNotComplete();
661 throw new RuntimeException("Should have thrown");
662 } catch (Throwable ex) {
663 // expected
664 }
665
666 to.assertTerminated();
667
668 to.onComplete();
669
670 try {
671 to.assertComplete();
672 throw new RuntimeException("Should have thrown");
673 } catch (Throwable ex) {
674 // expected
675 }
676
677 try {
678 to.assertTerminated();
679 throw new RuntimeException("Should have thrown");
680 } catch (Throwable ex) {
681 // expected
682 }
683
684 try {
685 to.assertNotComplete();
686 throw new RuntimeException("Should have thrown");
687 } catch (Throwable ex) {
688 // expected
689 }
690 }
691
692 @Test
693 public void assertValue() {
694 TestObserverEx<Integer> to = new TestObserverEx<>();
695
696 to.onSubscribe(Disposable.empty());
697
698 try {
699 to.assertValue(1);
700 throw new RuntimeException("Should have thrown");
701 } catch (Throwable ex) {
702 // expected
703 }
704
705 to.onNext(1);
706
707 to.assertValue(1);
708
709 try {
710 to.assertValue(2);
711 throw new RuntimeException("Should have thrown");
712 } catch (Throwable ex) {
713 // expected
714 }
715
716 to.onNext(2);
717
718 try {
719 to.assertValue(1);
720 throw new RuntimeException("Should have thrown");
721 } catch (Throwable ex) {
722 // expected
723 }
724 }
725
726 @Test
727 public void onNextMisbehave() {
728 TestObserverEx<Integer> to = new TestObserverEx<>();
729
730 to.onNext(1);
731
732 to.assertError(IllegalStateException.class);
733
734 to = new TestObserverEx<>();
735
736 to.onSubscribe(Disposable.empty());
737
738 to.onNext(null);
739
740 to.assertFailure(NullPointerException.class, (Integer)null);
741 }
742
743 @Test
744 public void assertTerminated2() {
745 TestObserverEx<Integer> to = new TestObserverEx<>();
746
747 to.onSubscribe(Disposable.empty());
748
749 to.assertNotTerminated();
750
751 to.onError(new TestException());
752
753 to.assertTerminated();
754
755 to.onError(new IOException());
756
757 try {
758 to.assertTerminated();
759 throw new RuntimeException("Should have thrown");
760 } catch (AssertionError ex) {
761 // expected
762 }
763
764 try {
765 to.assertError(TestException.class);
766 throw new RuntimeException("Should have thrown");
767 } catch (AssertionError ex) {
768 // expected
769 }
770
771 to = new TestObserverEx<>();
772
773 to.onSubscribe(Disposable.empty());
774
775 to.onError(new TestException());
776 to.onComplete();
777
778 try {
779 to.assertTerminated();
780 throw new RuntimeException("Should have thrown");
781 } catch (AssertionError ex) {
782 // expected
783 }
784 }
785
786 @Test
787 public void onSubscribe() {
788 TestObserverEx<Integer> to = new TestObserverEx<>();
789
790 to.onSubscribe(null);
791
792 to.assertError(NullPointerException.class);
793
794 to = new TestObserverEx<>();
795
796 to.onSubscribe(Disposable.empty());
797
798 Disposable d1 = Disposable.empty();
799
800 to.onSubscribe(d1);
801
802 assertTrue(d1.isDisposed());
803
804 to.assertError(IllegalStateException.class);
805
806 to = new TestObserverEx<>();
807 to.dispose();
808
809 d1 = Disposable.empty();
810
811 to.onSubscribe(d1);
812
813 assertTrue(d1.isDisposed());
814
815 }
816
817 @Test
818 public void assertValueSequence() {
819 TestObserverEx<Integer> to = new TestObserverEx<>();
820
821 to.onSubscribe(Disposable.empty());
822
823 to.onNext(1);
824 to.onNext(2);
825
826 try {
827 to.assertValueSequence(Collections.<Integer>emptyList());
828 throw new RuntimeException("Should have thrown");
829 } catch (AssertionError expected) {
830 assertTrue(expected.getMessage(), expected.getMessage().startsWith("More values received than expected (0)"));
831 }
832
833 try {
834 to.assertValueSequence(Collections.singletonList(1));
835 throw new RuntimeException("Should have thrown");
836 } catch (AssertionError expected) {
837 assertTrue(expected.getMessage(), expected.getMessage().startsWith("More values received than expected (1)"));
838 }
839
840 to.assertValueSequence(Arrays.asList(1, 2));
841
842 try {
843 to.assertValueSequence(Arrays.asList(1, 2, 3));
844 throw new RuntimeException("Should have thrown");
845 } catch (AssertionError expected) {
846 assertTrue(expected.getMessage(), expected.getMessage().startsWith("Fewer values received than expected (2)"));
847 }
848 }
849
850 @Test
851 public void assertEmpty() {
852 TestObserverEx<Integer> to = new TestObserverEx<>();
853
854 try {
855 to.assertEmpty();
856 throw new RuntimeException("Should have thrown!");
857 } catch (AssertionError ex) {
858 // expected
859 }
860
861 to.onSubscribe(Disposable.empty());
862
863 to.assertEmpty();
864
865 to.onNext(1);
866
867 try {
868 to.assertEmpty();
869 throw new RuntimeException("Should have thrown!");
870 } catch (AssertionError ex) {
871 // expected
872 }
873 }
874
875 @Test
876 public void awaitDoneTimed() {
877 TestObserverEx<Integer> to = new TestObserverEx<>();
878
879 Thread.currentThread().interrupt();
880
881 try {
882 to.awaitDone(5, TimeUnit.SECONDS);
883 } catch (RuntimeException ex) {
884 assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
885 }
886 }
887
888 @Test
889 public void assertNotSubscribed() {
890 TestObserverEx<Integer> to = new TestObserverEx<>();
891
892 to.assertNotSubscribed();
893
894 to.errors().add(new TestException());
895
896 try {
897 to.assertNotSubscribed();
898 throw new RuntimeException("Should have thrown!");
899 } catch (AssertionError ex) {
900 // expected
901 }
902 }
903
904 @Test
905 public void assertErrorMultiple() {
906 TestObserverEx<Integer> to = new TestObserverEx<>();
907
908 TestException e = new TestException();
909 to.errors().add(e);
910 to.errors().add(new TestException());
911
912 try {
913 to.assertError(TestException.class);
914 throw new RuntimeException("Should have thrown!");
915 } catch (AssertionError ex) {
916 // expected
917 }
918 try {
919 to.assertError(e);
920 throw new RuntimeException("Should have thrown!");
921 } catch (AssertionError ex) {
922 // expected
923 }
924 try {
925 to.assertError(Functions.<Throwable>alwaysTrue());
926 throw new RuntimeException("Should have thrown!");
927 } catch (AssertionError ex) {
928 // expected
929 }
930 try {
931 to.assertErrorMessage("");
932 throw new RuntimeException("Should have thrown!");
933 } catch (AssertionError ex) {
934 // expected
935 }
936 }
937
938 @Test
939 public void errorInPredicate() {
940 TestObserverEx<Object> to = new TestObserverEx<>();
941 to.onError(new RuntimeException());
942 try {
943 to.assertError(new Predicate<Throwable>() {
944 @Override
945 public boolean test(Throwable throwable) throws Exception {
946 throw new TestException();
947 }
948 });
949 } catch (TestException ex) {
950 // expected
951 return;
952 }
953 fail("Error in predicate but not thrown!");
954 }
955
956 @Test
957 public void assertComplete() {
958 TestObserverEx<Integer> to = new TestObserverEx<>();
959
960 to.onSubscribe(Disposable.empty());
961
962 try {
963 to.assertComplete();
964 throw new RuntimeException("Should have thrown!");
965 } catch (AssertionError ex) {
966 // expected
967 }
968
969 to.onComplete();
970
971 to.assertComplete();
972
973 to.onComplete();
974
975 try {
976 to.assertComplete();
977 throw new RuntimeException("Should have thrown!");
978 } catch (AssertionError ex) {
979 // expected
980 }
981 }
982
983 @Test
984 public void completeWithoutOnSubscribe() {
985 TestObserverEx<Integer> to = new TestObserverEx<>();
986
987 to.onComplete();
988
989 to.assertError(IllegalStateException.class);
990 }
991
992 @Test
993 public void completeDelegateThrows() {
994 TestObserverEx<Integer> to = new TestObserverEx<>(new Observer<Integer>() {
995
996 @Override
997 public void onSubscribe(Disposable d) {
998
999 }
1000
1001 @Override
1002 public void onNext(Integer value) {
1003
1004 }
1005
1006 @Override
1007 public void onError(Throwable e) {
1008 throw new TestException();
1009 }
1010
1011 @Override
1012 public void onComplete() {
1013 throw new TestException();
1014 }
1015
1016 });
1017
1018 to.onSubscribe(Disposable.empty());
1019
1020 try {
1021 to.onComplete();
1022 throw new RuntimeException("Should have thrown!");
1023 } catch (TestException ex) {
1024 to.assertTerminated();
1025 }
1026 }
1027
1028 @Test
1029 public void errorDelegateThrows() {
1030 TestObserverEx<Integer> to = new TestObserverEx<>(new Observer<Integer>() {
1031
1032 @Override
1033 public void onSubscribe(Disposable d) {
1034
1035 }
1036
1037 @Override
1038 public void onNext(Integer value) {
1039
1040 }
1041
1042 @Override
1043 public void onError(Throwable e) {
1044 throw new TestException();
1045 }
1046
1047 @Override
1048 public void onComplete() {
1049 throw new TestException();
1050 }
1051
1052 });
1053
1054 to.onSubscribe(Disposable.empty());
1055
1056 try {
1057 to.onError(new IOException());
1058 throw new RuntimeException("Should have thrown!");
1059 } catch (TestException ex) {
1060 to.assertTerminated();
1061 }
1062 }
1063
1064 @Test
1065 public void syncQueueThrows() {
1066 TestObserverEx<Object> to = new TestObserverEx<>();
1067 to.setInitialFusionMode(QueueFuseable.SYNC);
1068
1069 Observable.range(1, 5)
1070 .map(new Function<Integer, Object>() {
1071 @Override
1072 public Object apply(Integer v) throws Exception { throw new TestException(); }
1073 })
1074 .subscribe(to);
1075
1076 to.assertSubscribed()
1077 .assertFuseable()
1078 .assertFusionMode(QueueFuseable.SYNC)
1079 .assertFailure(TestException.class);
1080 }
1081
1082 @Test
1083 public void asyncQueueThrows() {
1084 TestObserverEx<Object> to = new TestObserverEx<>();
1085 to.setInitialFusionMode(QueueFuseable.ANY);
1086
1087 UnicastSubject<Integer> us = UnicastSubject.create();
1088
1089 us
1090 .map(new Function<Integer, Object>() {
1091 @Override
1092 public Object apply(Integer v) throws Exception { throw new TestException(); }
1093 })
1094 .subscribe(to);
1095
1096 us.onNext(1);
1097
1098 to.assertSubscribed()
1099 .assertFuseable()
1100 .assertFusionMode(QueueFuseable.ASYNC)
1101 .assertFailure(TestException.class);
1102 }
1103
1104 @Test
1105 public void completedMeansDisposed() {
1106 // 2.0.2 - a terminated TestObserver no longer reports isDisposed
1107 assertFalse(Observable.just(1)
1108 .test()
1109 .assertResult(1).isDisposed());
1110 }
1111
1112 @Test
1113 public void errorMeansDisposed() {
1114 // 2.0.2 - a terminated TestObserver no longer reports isDisposed
1115 assertFalse(Observable.error(new TestException())
1116 .test()
1117 .assertFailure(TestException.class).isDisposed());
1118 }
1119
1120 @Test
1121 public void asyncFusion() {
1122 TestObserverEx<Object> to = new TestObserverEx<>();
1123 to.setInitialFusionMode(QueueFuseable.ANY);
1124
1125 UnicastSubject<Integer> us = UnicastSubject.create();
1126
1127 us
1128 .subscribe(to);
1129
1130 us.onNext(1);
1131 us.onComplete();
1132
1133 to.assertSubscribed()
1134 .assertFuseable()
1135 .assertFusionMode(QueueFuseable.ASYNC)
1136 .assertResult(1);
1137 }
1138
1139 @Test
1140 public void assertValuePredicateEmpty() {
1141 assertThrows("No values", AssertionError.class, () -> {
1142 TestObserverEx<Object> to = new TestObserverEx<>();
1143
1144 Observable.empty().subscribe(to);
1145
1146 to.assertValue(new Predicate<Object>() {
1147 @Override public boolean test(final Object o) throws Exception {
1148 return false;
1149 }
1150 });
1151 });
1152 }
1153
1154 @Test
1155 public void assertValuePredicateMatch() {
1156 TestObserverEx<Integer> to = new TestObserverEx<>();
1157
1158 Observable.just(1).subscribe(to);
1159
1160 to.assertValue(new Predicate<Integer>() {
1161 @Override public boolean test(final Integer o) throws Exception {
1162 return o == 1;
1163 }
1164 });
1165 }
1166
1167 @Test
1168 public void assertValuePredicateNoMatch() {
1169 assertThrows("Value not present", AssertionError.class, () -> {
1170 TestObserverEx<Integer> to = new TestObserverEx<>();
1171
1172 Observable.just(1).subscribe(to);
1173
1174 to.assertValue(new Predicate<Integer>() {
1175 @Override public boolean test(final Integer o) throws Exception {
1176 return o != 1;
1177 }
1178 });
1179 });
1180 }
1181
1182 @Test
1183 public void assertValuePredicateMatchButMore() {
1184 assertThrows("Value present but other values as well", AssertionError.class, () -> {
1185 TestObserverEx<Integer> to = new TestObserverEx<>();
1186
1187 Observable.just(1, 2).subscribe(to);
1188
1189 to.assertValue(new Predicate<Integer>() {
1190 @Override public boolean test(final Integer o) throws Exception {
1191 return o == 1;
1192 }
1193 });
1194 });
1195 }
1196
1197 @Test
1198 public void assertValueAtPredicateEmpty() {
1199 assertThrows("No values", AssertionError.class, () -> {
1200 TestObserverEx<Object> to = new TestObserverEx<>();
1201
1202 Observable.empty().subscribe(to);
1203
1204 to.assertValueAt(0, new Predicate<Object>() {
1205 @Override public boolean test(final Object o) throws Exception {
1206 return false;
1207 }
1208 });
1209 });
1210 }
1211
1212 @Test
1213 public void assertValueAtPredicateMatch() {
1214 TestObserverEx<Integer> to = new TestObserverEx<>();
1215
1216 Observable.just(1, 2).subscribe(to);
1217
1218 to.assertValueAt(1, new Predicate<Integer>() {
1219 @Override public boolean test(final Integer o) throws Exception {
1220 return o == 2;
1221 }
1222 });
1223 }
1224
1225 @Test
1226 public void assertValueAtPredicateNoMatch() {
1227 assertThrows("Value not present", AssertionError.class, () -> {
1228 TestObserverEx<Integer> to = new TestObserverEx<>();
1229
1230 Observable.just(1, 2, 3).subscribe(to);
1231
1232 to.assertValueAt(2, new Predicate<Integer>() {
1233 @Override public boolean test(final Integer o) throws Exception {
1234 return o != 3;
1235 }
1236 });
1237 });
1238 }
1239
1240 @Test
1241 public void assertValueAtInvalidIndex() {
1242 assertThrows("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> {
1243 TestObserverEx<Integer> to = new TestObserverEx<>();
1244
1245 Observable.just(1, 2).subscribe(to);
1246
1247 to.assertValueAt(2, new Predicate<Integer>() {
1248 @Override public boolean test(final Integer o) throws Exception {
1249 return o == 1;
1250 }
1251 });
1252 });
1253 }
1254
1255 @Test
1256 public void assertValueAtIndexEmpty() {
1257 assertThrows("No values", AssertionError.class, () -> {
1258 TestObserverEx<Object> to = new TestObserverEx<>();
1259
1260 Observable.empty().subscribe(to);
1261
1262 to.assertValueAt(0, "a");
1263 });
1264 }
1265
1266 @Test
1267 public void assertValueAtIndexMatch() {
1268 TestObserverEx<String> to = new TestObserverEx<>();
1269
1270 Observable.just("a", "b").subscribe(to);
1271
1272 to.assertValueAt(1, "b");
1273 }
1274
1275 @Test
1276 public void assertValueAtIndexNoMatch() {
1277 assertThrows("\nexpected: b (class: String)\ngot: c (class: String) (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> {
1278 TestObserverEx<String> to = new TestObserverEx<>();
1279
1280 Observable.just("a", "b", "c").subscribe(to);
1281
1282 to.assertValueAt(2, "b");
1283 });
1284 }
1285
1286 @Test
1287 public void assertValueAtIndexInvalidIndex() {
1288 assertThrows("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> {
1289 TestObserverEx<String> to = new TestObserverEx<>();
1290
1291 Observable.just("a", "b").subscribe(to);
1292
1293 to.assertValueAt(2, "c");
1294 });
1295 }
1296
1297 @Test
1298 public void withTag() {
1299 try {
1300 for (int i = 1; i < 3; i++) {
1301 Observable.just(i)
1302 .test()
1303 .withTag("testing with item=" + i)
1304 .assertResult(1)
1305 ;
1306 }
1307 throw new RuntimeException("Should have thrown!");
1308 } catch (AssertionError ex) {
1309 assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
1310 }
1311 }
1312
1313 @Test
1314 public void assertValuesOnly() {
1315 TestObserverEx<Integer> to = new TestObserverEx<>();
1316 to.onSubscribe(Disposable.empty());
1317 to.assertValuesOnly();
1318
1319 to.onNext(5);
1320 to.assertValuesOnly(5);
1321
1322 to.onNext(-1);
1323 to.assertValuesOnly(5, -1);
1324 }
1325
1326 @Test
1327 public void assertValuesOnlyThrowsOnUnexpectedValue() {
1328 TestObserverEx<Integer> to = new TestObserverEx<>();
1329 to.onSubscribe(Disposable.empty());
1330 to.assertValuesOnly();
1331
1332 to.onNext(5);
1333 to.assertValuesOnly(5);
1334
1335 to.onNext(-1);
1336
1337 try {
1338 to.assertValuesOnly(5);
1339 throw new RuntimeException();
1340 } catch (AssertionError ex) {
1341 // expected
1342 }
1343 }
1344
1345 @Test
1346 public void assertValuesOnlyThrowsWhenCompleted() {
1347 TestObserverEx<Integer> to = new TestObserverEx<>();
1348 to.onSubscribe(Disposable.empty());
1349
1350 to.onComplete();
1351
1352 try {
1353 to.assertValuesOnly();
1354 throw new RuntimeException();
1355 } catch (AssertionError ex) {
1356 // expected
1357 }
1358 }
1359
1360 @Test
1361 public void assertValuesOnlyThrowsWhenErrored() {
1362 TestObserverEx<Integer> to = new TestObserverEx<>();
1363 to.onSubscribe(Disposable.empty());
1364
1365 to.onError(new TestException());
1366
1367 try {
1368 to.assertValuesOnly();
1369 throw new RuntimeException();
1370 } catch (AssertionError ex) {
1371 // expected
1372 }
1373 }
1374 }