Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeCallbackObserverTest.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.maybe;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.RxJavaTest;
23 import io.reactivex.rxjava3.disposables.*;
24 import io.reactivex.rxjava3.exceptions.*;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class MaybeCallbackObserverTest extends RxJavaTest {
31
32 @Test
33 public void dispose() {
34 MaybeCallbackObserver<Object> mo = new MaybeCallbackObserver<>(Functions.emptyConsumer(), Functions.emptyConsumer(), Functions.EMPTY_ACTION);
35
36 Disposable d = Disposable.empty();
37
38 mo.onSubscribe(d);
39
40 assertFalse(mo.isDisposed());
41
42 mo.dispose();
43
44 assertTrue(mo.isDisposed());
45
46 assertTrue(d.isDisposed());
47 }
48
49 @Test
50 public void onSuccessCrashes() {
51 List<Throwable> errors = TestHelper.trackPluginErrors();
52 try {
53 MaybeCallbackObserver<Object> mo = new MaybeCallbackObserver<>(
54 new Consumer<Object>() {
55 @Override
56 public void accept(Object v) throws Exception {
57 throw new TestException();
58 }
59 },
60 Functions.emptyConsumer(),
61 Functions.EMPTY_ACTION);
62
63 mo.onSubscribe(Disposable.empty());
64
65 mo.onSuccess(1);
66
67 TestHelper.assertUndeliverable(errors, 0, TestException.class);
68 } finally {
69 RxJavaPlugins.reset();
70 }
71 }
72
73 @Test
74 public void onErrorCrashes() {
75 List<Throwable> errors = TestHelper.trackPluginErrors();
76 try {
77 MaybeCallbackObserver<Object> mo = new MaybeCallbackObserver<>(
78 Functions.emptyConsumer(),
79 new Consumer<Object>() {
80 @Override
81 public void accept(Object v) throws Exception {
82 throw new TestException("Inner");
83 }
84 },
85 Functions.EMPTY_ACTION);
86
87 mo.onSubscribe(Disposable.empty());
88
89 mo.onError(new TestException("Outer"));
90
91 TestHelper.assertError(errors, 0, CompositeException.class);
92
93 List<Throwable> ce = TestHelper.compositeList(errors.get(0));
94
95 TestHelper.assertError(ce, 0, TestException.class, "Outer");
96 TestHelper.assertError(ce, 1, TestException.class, "Inner");
97 } finally {
98 RxJavaPlugins.reset();
99 }
100 }
101
102 @Test
103 public void onCompleteCrashes() {
104 List<Throwable> errors = TestHelper.trackPluginErrors();
105 try {
106 MaybeCallbackObserver<Object> mo = new MaybeCallbackObserver<>(
107 Functions.emptyConsumer(),
108 Functions.emptyConsumer(),
109 new Action() {
110 @Override
111 public void run() throws Exception {
112 throw new TestException();
113 }
114 });
115
116 mo.onSubscribe(Disposable.empty());
117
118 mo.onComplete();
119
120 TestHelper.assertUndeliverable(errors, 0, TestException.class);
121 } finally {
122 RxJavaPlugins.reset();
123 }
124 }
125
126 @Test
127 public void onErrorMissingShouldReportNoCustomOnError() {
128 MaybeCallbackObserver<Integer> o = new MaybeCallbackObserver<>(Functions.<Integer>emptyConsumer(),
129 Functions.ON_ERROR_MISSING,
130 Functions.EMPTY_ACTION);
131
132 assertFalse(o.hasCustomOnError());
133 }
134
135 @Test
136 public void customOnErrorShouldReportCustomOnError() {
137 MaybeCallbackObserver<Integer> o = new MaybeCallbackObserver<>(Functions.<Integer>emptyConsumer(),
138 Functions.<Throwable>emptyConsumer(),
139 Functions.EMPTY_ACTION);
140
141 assertTrue(o.hasCustomOnError());
142 }
143 }