Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapBiSelectorTest.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.assertEquals;
17
18 import org.junit.Test;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.TestException;
22 import io.reactivex.rxjava3.functions.*;
23 import io.reactivex.rxjava3.observers.TestObserver;
24 import io.reactivex.rxjava3.processors.PublishProcessor;
25 import io.reactivex.rxjava3.testsupport.TestHelper;
26
27 public class MaybeFlatMapBiSelectorTest extends RxJavaTest {
28
29 BiFunction<Integer, Integer, String> stringCombine() {
30 return new BiFunction<Integer, Integer, String>() {
31 @Override
32 public String apply(Integer a, Integer b) throws Exception {
33 return a + ":" + b;
34 }
35 };
36 }
37
38 @Test
39 public void normal() {
40 Maybe.just(1)
41 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
42 @Override
43 public MaybeSource<Integer> apply(Integer v) throws Exception {
44 return Maybe.just(2);
45 }
46 }, stringCombine())
47 .test()
48 .assertResult("1:2");
49 }
50
51 @Test
52 public void normalWithEmpty() {
53 Maybe.just(1)
54 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
55 @Override
56 public MaybeSource<Integer> apply(Integer v) throws Exception {
57 return Maybe.empty();
58 }
59 }, stringCombine())
60 .test()
61 .assertResult();
62 }
63
64 @Test
65 public void emptyWithJust() {
66 final int[] call = { 0 };
67
68 Maybe.<Integer>empty()
69 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
70 @Override
71 public MaybeSource<Integer> apply(Integer v) throws Exception {
72 call[0]++;
73 return Maybe.just(1);
74 }
75 }, stringCombine())
76 .test()
77 .assertResult();
78
79 assertEquals(0, call[0]);
80 }
81
82 @Test
83 public void errorWithJust() {
84 final int[] call = { 0 };
85
86 Maybe.<Integer>error(new TestException())
87 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
88 @Override
89 public MaybeSource<Integer> apply(Integer v) throws Exception {
90 call[0]++;
91 return Maybe.just(1);
92 }
93 }, stringCombine())
94 .test()
95 .assertFailure(TestException.class);
96
97 assertEquals(0, call[0]);
98 }
99
100 @Test
101 public void justWithError() {
102 final int[] call = { 0 };
103
104 Maybe.just(1)
105 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
106 @Override
107 public MaybeSource<Integer> apply(Integer v) throws Exception {
108 call[0]++;
109 return Maybe.<Integer>error(new TestException());
110 }
111 }, stringCombine())
112 .test()
113 .assertFailure(TestException.class);
114
115 assertEquals(1, call[0]);
116 }
117
118 @Test
119 public void dispose() {
120 TestHelper.checkDisposed(PublishProcessor.create().singleElement()
121 .flatMap(new Function<Object, MaybeSource<Integer>>() {
122 @Override
123 public MaybeSource<Integer> apply(Object v) throws Exception {
124 return Maybe.just(1);
125 }
126 }, new BiFunction<Object, Integer, Object>() {
127 @Override
128 public Object apply(Object a, Integer b) throws Exception {
129 return b;
130 }
131 }));
132 }
133
134 @Test
135 public void doubleOnSubscribe() {
136 TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() {
137 @Override
138 public MaybeSource<Object> apply(Maybe<Object> v) throws Exception {
139 return v.flatMap(new Function<Object, MaybeSource<Integer>>() {
140 @Override
141 public MaybeSource<Integer> apply(Object v) throws Exception {
142 return Maybe.just(1);
143 }
144 }, new BiFunction<Object, Integer, Object>() {
145 @Override
146 public Object apply(Object a, Integer b) throws Exception {
147 return b;
148 }
149 });
150 }
151 });
152 }
153
154 @Test
155 public void mapperThrows() {
156 Maybe.just(1)
157 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
158 @Override
159 public MaybeSource<Integer> apply(Integer v) throws Exception {
160 throw new TestException();
161 }
162 }, stringCombine())
163 .test()
164 .assertFailure(TestException.class);
165 }
166
167 @Test
168 public void mapperReturnsNull() {
169 Maybe.just(1)
170 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
171 @Override
172 public MaybeSource<Integer> apply(Integer v) throws Exception {
173 return null;
174 }
175 }, stringCombine())
176 .test()
177 .assertFailure(NullPointerException.class);
178 }
179
180 @Test
181 public void resultSelectorThrows() {
182 Maybe.just(1)
183 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
184 @Override
185 public MaybeSource<Integer> apply(Integer v) throws Exception {
186 return Maybe.just(2);
187 }
188 }, new BiFunction<Integer, Integer, Object>() {
189 @Override
190 public Object apply(Integer a, Integer b) throws Exception {
191 throw new TestException();
192 }
193 })
194 .test()
195 .assertFailure(TestException.class);
196 }
197
198 @Test
199 public void resultSelectorReturnsNull() {
200 Maybe.just(1)
201 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
202 @Override
203 public MaybeSource<Integer> apply(Integer v) throws Exception {
204 return Maybe.just(2);
205 }
206 }, new BiFunction<Integer, Integer, Object>() {
207 @Override
208 public Object apply(Integer a, Integer b) throws Exception {
209 return null;
210 }
211 })
212 .test()
213 .assertFailure(NullPointerException.class);
214 }
215
216 @Test
217 public void mapperCancels() {
218 final TestObserver<Integer> to = new TestObserver<>();
219
220 Maybe.just(1)
221 .flatMap(new Function<Integer, MaybeSource<Integer>>() {
222 @Override
223 public MaybeSource<Integer> apply(Integer v) throws Exception {
224 to.dispose();
225 return Maybe.just(2);
226 }
227 }, new BiFunction<Integer, Integer, Integer>() {
228 @Override
229 public Integer apply(Integer a, Integer b) throws Exception {
230 throw new IllegalStateException();
231 }
232 })
233 .subscribeWith(to)
234 .assertEmpty();
235 }
236 }