Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableDematerializeTest.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.List;
20
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import org.junit.Test;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.functions.Function;
27 import io.reactivex.rxjava3.internal.functions.Functions;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.testsupport.*;
30
31 public class ObservableDematerializeTest extends RxJavaTest {
32
33 @Test
34 public void simpleSelector() {
35 Observable<Notification<Integer>> notifications = Observable.just(1, 2).materialize();
36 Observable<Integer> dematerialize = notifications.dematerialize(Functions.<Notification<Integer>>identity());
37
38 Observer<Integer> observer = TestHelper.mockObserver();
39
40 dematerialize.subscribe(observer);
41
42 verify(observer, times(1)).onNext(1);
43 verify(observer, times(1)).onNext(2);
44 verify(observer, times(1)).onComplete();
45 verify(observer, never()).onError(any(Throwable.class));
46 }
47
48 @Test
49 public void selectorCrash() {
50 Observable.just(1, 2)
51 .materialize()
52 .dematerialize(new Function<Notification<Integer>, Notification<Object>>() {
53 @Override
54 public Notification<Object> apply(Notification<Integer> v) throws Exception {
55 throw new TestException();
56 }
57 })
58 .test()
59 .assertFailure(TestException.class);
60 }
61
62 @Test
63 public void selectorNull() {
64 Observable.just(1, 2)
65 .materialize()
66 .dematerialize(new Function<Notification<Integer>, Notification<Object>>() {
67 @Override
68 public Notification<Object> apply(Notification<Integer> v) throws Exception {
69 return null;
70 }
71 })
72 .test()
73 .assertFailure(NullPointerException.class);
74 }
75
76 @Test
77 public void dematerialize1() {
78 Observable<Notification<Integer>> notifications = Observable.just(1, 2).materialize();
79 Observable<Integer> dematerialize = notifications.dematerialize(Functions.<Notification<Integer>>identity());
80
81 Observer<Integer> observer = TestHelper.mockObserver();
82
83 dematerialize.subscribe(observer);
84
85 verify(observer, times(1)).onNext(1);
86 verify(observer, times(1)).onNext(2);
87 verify(observer, times(1)).onComplete();
88 verify(observer, never()).onError(any(Throwable.class));
89 }
90
91 @Test
92 public void dematerialize2() {
93 Throwable exception = new Throwable("test");
94 Observable<Integer> o = Observable.error(exception);
95 Observable<Integer> dematerialize = o.materialize().dematerialize(Functions.<Notification<Integer>>identity());
96
97 Observer<Integer> observer = TestHelper.mockObserver();
98
99 dematerialize.subscribe(observer);
100
101 verify(observer, times(1)).onError(exception);
102 verify(observer, times(0)).onComplete();
103 verify(observer, times(0)).onNext(any(Integer.class));
104 }
105
106 @Test
107 public void dematerialize3() {
108 Exception exception = new Exception("test");
109 Observable<Integer> o = Observable.error(exception);
110 Observable<Integer> dematerialize = o.materialize().dematerialize(Functions.<Notification<Integer>>identity());
111
112 Observer<Integer> observer = TestHelper.mockObserver();
113
114 dematerialize.subscribe(observer);
115
116 verify(observer, times(1)).onError(exception);
117 verify(observer, times(0)).onComplete();
118 verify(observer, times(0)).onNext(any(Integer.class));
119 }
120
121 @Test
122 public void errorPassThru() {
123 Exception exception = new Exception("test");
124 Observable<Notification<Integer>> o = Observable.error(exception);
125 Observable<Integer> dematerialize = o.dematerialize(Functions.<Notification<Integer>>identity());
126
127 Observer<Integer> observer = TestHelper.mockObserver();
128
129 dematerialize.subscribe(observer);
130
131 verify(observer, times(1)).onError(exception);
132 verify(observer, times(0)).onComplete();
133 verify(observer, times(0)).onNext(any(Integer.class));
134 }
135
136 @Test
137 public void completePassThru() {
138 Observable<Notification<Integer>> o = Observable.empty();
139 Observable<Integer> dematerialize = o.dematerialize(Functions.<Notification<Integer>>identity());
140
141 Observer<Integer> observer = TestHelper.mockObserver();
142
143 TestObserverEx<Integer> to = new TestObserverEx<>(observer);
144 dematerialize.subscribe(to);
145
146 System.out.println(to.errors());
147
148 verify(observer, never()).onError(any(Throwable.class));
149 verify(observer, times(1)).onComplete();
150 verify(observer, times(0)).onNext(any(Integer.class));
151 }
152
153 @Test
154 public void honorsContractWhenCompleted() {
155 Observable<Integer> source = Observable.just(1);
156
157 Observable<Integer> result = source.materialize().dematerialize(Functions.<Notification<Integer>>identity());
158
159 Observer<Integer> o = TestHelper.mockObserver();
160
161 result.subscribe(o);
162
163 verify(o).onNext(1);
164 verify(o).onComplete();
165 verify(o, never()).onError(any(Throwable.class));
166 }
167
168 @Test
169 public void honorsContractWhenThrows() {
170 Observable<Integer> source = Observable.error(new TestException());
171
172 Observable<Integer> result = source.materialize().dematerialize(Functions.<Notification<Integer>>identity());
173
174 Observer<Integer> o = TestHelper.mockObserver();
175
176 result.subscribe(o);
177
178 verify(o, never()).onNext(any(Integer.class));
179 verify(o, never()).onComplete();
180 verify(o).onError(any(TestException.class));
181 }
182
183 @Test
184 public void dispose() {
185 TestHelper.checkDisposed(Observable.just(Notification.<Integer>createOnComplete()).dematerialize(Functions.<Notification<Integer>>identity()));
186 }
187
188 @Test
189 public void doubleOnSubscribe() {
190 TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Notification<Object>>, ObservableSource<Object>>() {
191 @Override
192 public ObservableSource<Object> apply(Observable<Notification<Object>> o) throws Exception {
193 return o.dematerialize(Functions.<Notification<Object>>identity());
194 }
195 });
196 }
197
198 @Test
199 public void eventsAfterDematerializedTerminal() {
200 List<Throwable> errors = TestHelper.trackPluginErrors();
201 try {
202 new Observable<Notification<Object>>() {
203 @Override
204 protected void subscribeActual(Observer<? super Notification<Object>> observer) {
205 observer.onSubscribe(Disposable.empty());
206 observer.onNext(Notification.createOnComplete());
207 observer.onNext(Notification.<Object>createOnNext(1));
208 observer.onNext(Notification.createOnError(new TestException("First")));
209 observer.onError(new TestException("Second"));
210 }
211 }
212 .dematerialize(Functions.<Notification<Object>>identity())
213 .test()
214 .assertResult();
215
216 TestHelper.assertUndeliverable(errors, 0, TestException.class, "First");
217 TestHelper.assertUndeliverable(errors, 1, TestException.class, "Second");
218 } finally {
219 RxJavaPlugins.reset();
220 }
221 }
222
223 @Test
224 @SuppressWarnings("unchecked")
225 public void nonNotificationInstanceAfterDispose() {
226 new Observable<Object>() {
227 @Override
228 protected void subscribeActual(Observer<? super Object> observer) {
229 observer.onSubscribe(Disposable.empty());
230 observer.onNext(Notification.createOnComplete());
231 observer.onNext(1);
232 }
233 }
234 .dematerialize(v -> (Notification<Object>)v)
235 .test()
236 .assertResult();
237 }
238 }