| 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.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
import org.reactivestreams.Publisher; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.testsupport.*; |
| 27 |
|
| 28 |
public class SingleFlatMapTest extends RxJavaTest { |
| 29 |
|
| 30 |
@Test |
| 31 |
public void normal() { |
| 32 |
final boolean[] b = { false }; |
| 33 |
|
| 34 |
Single.just(1) |
| 35 |
.flatMapCompletable(new Function<Integer, Completable>() { |
| 36 |
@Override |
| 37 |
public Completable apply(Integer t) throws Exception { |
| 38 |
return Completable.complete().doOnComplete(new Action() { |
| 39 |
@Override |
| 40 |
public void run() throws Exception { |
| 41 |
b[0] = true; |
| 42 |
} |
| 43 |
}); |
| 44 |
} |
| 45 |
}) |
| 46 |
.test() |
| 47 |
.assertResult(); |
| 48 |
|
| 49 |
assertTrue(b[0]); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void error() { |
| 54 |
final boolean[] b = { false }; |
| 55 |
|
| 56 |
Single.<Integer>error(new TestException()) |
| 57 |
.flatMapCompletable(new Function<Integer, Completable>() { |
| 58 |
@Override |
| 59 |
public Completable apply(Integer t) throws Exception { |
| 60 |
return Completable.complete().doOnComplete(new Action() { |
| 61 |
@Override |
| 62 |
public void run() throws Exception { |
| 63 |
b[0] = true; |
| 64 |
} |
| 65 |
}); |
| 66 |
} |
| 67 |
}) |
| 68 |
.test() |
| 69 |
.assertFailure(TestException.class); |
| 70 |
|
| 71 |
assertFalse(b[0]); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void mapperThrows() { |
| 76 |
final boolean[] b = { false }; |
| 77 |
|
| 78 |
Single.just(1) |
| 79 |
.flatMapCompletable(new Function<Integer, Completable>() { |
| 80 |
@Override |
| 81 |
public Completable apply(Integer t) throws Exception { |
| 82 |
throw new TestException(); |
| 83 |
} |
| 84 |
}) |
| 85 |
.test() |
| 86 |
.assertFailure(TestException.class); |
| 87 |
|
| 88 |
assertFalse(b[0]); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void mapperReturnsNull() { |
| 93 |
final boolean[] b = { false }; |
| 94 |
|
| 95 |
Single.just(1) |
| 96 |
.flatMapCompletable(new Function<Integer, Completable>() { |
| 97 |
@Override |
| 98 |
public Completable apply(Integer t) throws Exception { |
| 99 |
return null; |
| 100 |
} |
| 101 |
}) |
| 102 |
.test() |
| 103 |
.assertFailure(NullPointerException.class); |
| 104 |
|
| 105 |
assertFalse(b[0]); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void flatMapObservable() { |
| 110 |
Single.just(1).flatMapObservable(new Function<Integer, Observable<Integer>>() { |
| 111 |
@Override |
| 112 |
public Observable<Integer> apply(Integer v) throws Exception { |
| 113 |
return Observable.range(v, 5); |
| 114 |
} |
| 115 |
}) |
| 116 |
.test() |
| 117 |
.assertResult(1, 2, 3, 4, 5); |
| 118 |
} |
| 119 |
|
| 120 |
@Test |
| 121 |
public void flatMapPublisher() { |
| 122 |
Single.just(1).flatMapPublisher(new Function<Integer, Publisher<Integer>>() { |
| 123 |
@Override |
| 124 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 125 |
return Flowable.range(v, 5); |
| 126 |
} |
| 127 |
}) |
| 128 |
.test() |
| 129 |
.assertResult(1, 2, 3, 4, 5); |
| 130 |
} |
| 131 |
|
| 132 |
@Test |
| 133 |
public void flatMapPublisherMapperThrows() { |
| 134 |
final TestException ex = new TestException(); |
| 135 |
Single.just(1) |
| 136 |
.flatMapPublisher(new Function<Integer, Publisher<Integer>>() { |
| 137 |
@Override |
| 138 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 139 |
throw ex; |
| 140 |
} |
| 141 |
}) |
| 142 |
.test() |
| 143 |
.assertNoValues() |
| 144 |
.assertError(ex); |
| 145 |
} |
| 146 |
|
| 147 |
@Test |
| 148 |
public void flatMapPublisherSingleError() { |
| 149 |
final TestException ex = new TestException(); |
| 150 |
Single.<Integer>error(ex) |
| 151 |
.flatMapPublisher(new Function<Integer, Publisher<Integer>>() { |
| 152 |
@Override |
| 153 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 154 |
return Flowable.just(1); |
| 155 |
} |
| 156 |
}) |
| 157 |
.test() |
| 158 |
.assertNoValues() |
| 159 |
.assertError(ex); |
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void flatMapPublisherCancelDuringSingle() { |
| 164 |
final AtomicBoolean disposed = new AtomicBoolean(); |
| 165 |
TestSubscriberEx<Integer> ts = Single.<Integer>never() |
| 166 |
.doOnDispose(new Action() { |
| 167 |
@Override |
| 168 |
public void run() throws Exception { |
| 169 |
disposed.set(true); |
| 170 |
} |
| 171 |
}) |
| 172 |
.flatMapPublisher(new Function<Integer, Publisher<Integer>>() { |
| 173 |
@Override |
| 174 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 175 |
return Flowable.range(v, 5); |
| 176 |
} |
| 177 |
}) |
| 178 |
.to(TestHelper.<Integer>testConsumer()) |
| 179 |
.assertNoValues() |
| 180 |
.assertNotTerminated(); |
| 181 |
assertFalse(disposed.get()); |
| 182 |
ts.cancel(); |
| 183 |
assertTrue(disposed.get()); |
| 184 |
ts.assertNotTerminated(); |
| 185 |
} |
| 186 |
|
| 187 |
@Test |
| 188 |
public void flatMapPublisherCancelDuringFlowable() { |
| 189 |
final AtomicBoolean disposed = new AtomicBoolean(); |
| 190 |
TestSubscriberEx<Integer> ts = |
| 191 |
Single.just(1) |
| 192 |
.flatMapPublisher(new Function<Integer, Publisher<Integer>>() { |
| 193 |
@Override |
| 194 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 195 |
return Flowable.<Integer>never() |
| 196 |
.doOnCancel(new Action() { |
| 197 |
@Override |
| 198 |
public void run() throws Exception { |
| 199 |
disposed.set(true); |
| 200 |
} |
| 201 |
}); |
| 202 |
} |
| 203 |
}) |
| 204 |
.to(TestHelper.<Integer>testConsumer()) |
| 205 |
.assertNoValues() |
| 206 |
.assertNotTerminated(); |
| 207 |
assertFalse(disposed.get()); |
| 208 |
ts.cancel(); |
| 209 |
assertTrue(disposed.get()); |
| 210 |
ts.assertNotTerminated(); |
| 211 |
} |
| 212 |
|
| 213 |
@Test |
| 214 |
public void flatMapValue() { |
| 215 |
Single.just(1).flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 216 |
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception { |
| 217 |
if (integer == 1) { |
| 218 |
return Single.just(2); |
| 219 |
} |
| 220 |
|
| 221 |
return Single.just(1); |
| 222 |
} |
| 223 |
}) |
| 224 |
.test() |
| 225 |
.assertResult(2); |
| 226 |
} |
| 227 |
|
| 228 |
@Test |
| 229 |
public void flatMapValueDifferentType() { |
| 230 |
Single.just(1).flatMap(new Function<Integer, SingleSource<String>>() { |
| 231 |
@Override public SingleSource<String> apply(final Integer integer) throws Exception { |
| 232 |
if (integer == 1) { |
| 233 |
return Single.just("2"); |
| 234 |
} |
| 235 |
|
| 236 |
return Single.just("1"); |
| 237 |
} |
| 238 |
}) |
| 239 |
.test() |
| 240 |
.assertResult("2"); |
| 241 |
} |
| 242 |
|
| 243 |
@Test |
| 244 |
public void flatMapValueNull() { |
| 245 |
Single.just(1).flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 246 |
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception { |
| 247 |
return null; |
| 248 |
} |
| 249 |
}) |
| 250 |
.to(TestHelper.<Integer>testConsumer()) |
| 251 |
.assertNoValues() |
| 252 |
.assertError(NullPointerException.class) |
| 253 |
.assertErrorMessage("The single returned by the mapper is null"); |
| 254 |
} |
| 255 |
|
| 256 |
@Test |
| 257 |
public void flatMapValueErrorThrown() { |
| 258 |
Single.just(1).flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 259 |
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception { |
| 260 |
throw new RuntimeException("something went terribly wrong!"); |
| 261 |
} |
| 262 |
}) |
| 263 |
.to(TestHelper.<Integer>testConsumer()) |
| 264 |
.assertNoValues() |
| 265 |
.assertError(RuntimeException.class) |
| 266 |
.assertErrorMessage("something went terribly wrong!"); |
| 267 |
} |
| 268 |
|
| 269 |
@Test |
| 270 |
public void flatMapError() { |
| 271 |
RuntimeException exception = new RuntimeException("test"); |
| 272 |
|
| 273 |
Single.error(exception).flatMap(new Function<Object, SingleSource<Object>>() { |
| 274 |
@Override public SingleSource<Object> apply(final Object integer) throws Exception { |
| 275 |
return Single.just(new Object()); |
| 276 |
} |
| 277 |
}) |
| 278 |
.test() |
| 279 |
.assertError(exception); |
| 280 |
} |
| 281 |
|
| 282 |
@Test |
| 283 |
public void dispose() { |
| 284 |
TestHelper.checkDisposed(Single.just(1).flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 285 |
@Override |
| 286 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 287 |
return Single.just(2); |
| 288 |
} |
| 289 |
})); |
| 290 |
} |
| 291 |
|
| 292 |
@Test |
| 293 |
public void mappedSingleOnError() { |
| 294 |
Single.just(1).flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 295 |
@Override |
| 296 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 297 |
return Single.error(new TestException()); |
| 298 |
} |
| 299 |
}) |
| 300 |
.test() |
| 301 |
.assertFailure(TestException.class); |
| 302 |
} |
| 303 |
|
| 304 |
@Test |
| 305 |
public void doubleOnSubscribe() { |
| 306 |
TestHelper.checkDoubleOnSubscribeSingle(new Function<Single<Object>, SingleSource<Object>>() { |
| 307 |
@Override |
| 308 |
public SingleSource<Object> apply(Single<Object> s) |
| 309 |
throws Exception { |
| 310 |
return s.flatMap(new Function<Object, SingleSource<? extends Object>>() { |
| 311 |
@Override |
| 312 |
public SingleSource<? extends Object> apply(Object v) |
| 313 |
throws Exception { |
| 314 |
return Single.just(v); |
| 315 |
} |
| 316 |
}); |
| 317 |
} |
| 318 |
}); |
| 319 |
} |
| 320 |
} |