| 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.junit.Assert.assertNull; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
|
| 22 |
import org.junit.*; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.core.Observable; |
| 26 |
import io.reactivex.rxjava3.core.Observer; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 30 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 31 |
import io.reactivex.rxjava3.subjects.UnicastSubject; |
| 32 |
import io.reactivex.rxjava3.testsupport.*; |
| 33 |
|
| 34 |
public class ObservableMapTest extends RxJavaTest { |
| 35 |
|
| 36 |
Observer<String> stringObserver; |
| 37 |
Observer<String> stringObserver2; |
| 38 |
|
| 39 |
static final BiFunction<String, Integer, String> APPEND_INDEX = new BiFunction<String, Integer, String>() { |
| 40 |
@Override |
| 41 |
public String apply(String value, Integer index) { |
| 42 |
return value + index; |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
@Before |
| 47 |
public void before() { |
| 48 |
stringObserver = TestHelper.mockObserver(); |
| 49 |
stringObserver2 = TestHelper.mockObserver(); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void map() { |
| 54 |
Map<String, String> m1 = getMap("One"); |
| 55 |
Map<String, String> m2 = getMap("Two"); |
| 56 |
Observable<Map<String, String>> o = Observable.just(m1, m2); |
| 57 |
|
| 58 |
Observable<String> m = o.map(new Function<Map<String, String>, String>() { |
| 59 |
@Override |
| 60 |
public String apply(Map<String, String> map) { |
| 61 |
return map.get("firstName"); |
| 62 |
} |
| 63 |
}); |
| 64 |
|
| 65 |
m.subscribe(stringObserver); |
| 66 |
|
| 67 |
verify(stringObserver, never()).onError(any(Throwable.class)); |
| 68 |
verify(stringObserver, times(1)).onNext("OneFirst"); |
| 69 |
verify(stringObserver, times(1)).onNext("TwoFirst"); |
| 70 |
verify(stringObserver, times(1)).onComplete(); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void mapMany() { |
| 75 |
/* simulate a top-level async call which returns IDs */ |
| 76 |
Observable<Integer> ids = Observable.just(1, 2); |
| 77 |
|
| 78 |
/* now simulate the behavior to take those IDs and perform nested async calls based on them */ |
| 79 |
Observable<String> m = ids.flatMap(new Function<Integer, Observable<String>>() { |
| 80 |
|
| 81 |
@Override |
| 82 |
public Observable<String> apply(Integer id) { |
| 83 |
/* simulate making a nested async call which creates another Observable */ |
| 84 |
Observable<Map<String, String>> subObservable = null; |
| 85 |
if (id == 1) { |
| 86 |
Map<String, String> m1 = getMap("One"); |
| 87 |
Map<String, String> m2 = getMap("Two"); |
| 88 |
subObservable = Observable.just(m1, m2); |
| 89 |
} else { |
| 90 |
Map<String, String> m3 = getMap("Three"); |
| 91 |
Map<String, String> m4 = getMap("Four"); |
| 92 |
subObservable = Observable.just(m3, m4); |
| 93 |
} |
| 94 |
|
| 95 |
/* simulate kicking off the async call and performing a select on it to transform the data */ |
| 96 |
return subObservable.map(new Function<Map<String, String>, String>() { |
| 97 |
@Override |
| 98 |
public String apply(Map<String, String> map) { |
| 99 |
return map.get("firstName"); |
| 100 |
} |
| 101 |
}); |
| 102 |
} |
| 103 |
|
| 104 |
}); |
| 105 |
m.subscribe(stringObserver); |
| 106 |
|
| 107 |
verify(stringObserver, never()).onError(any(Throwable.class)); |
| 108 |
verify(stringObserver, times(1)).onNext("OneFirst"); |
| 109 |
verify(stringObserver, times(1)).onNext("TwoFirst"); |
| 110 |
verify(stringObserver, times(1)).onNext("ThreeFirst"); |
| 111 |
verify(stringObserver, times(1)).onNext("FourFirst"); |
| 112 |
verify(stringObserver, times(1)).onComplete(); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void mapMany2() { |
| 117 |
Map<String, String> m1 = getMap("One"); |
| 118 |
Map<String, String> m2 = getMap("Two"); |
| 119 |
Observable<Map<String, String>> observable1 = Observable.just(m1, m2); |
| 120 |
|
| 121 |
Map<String, String> m3 = getMap("Three"); |
| 122 |
Map<String, String> m4 = getMap("Four"); |
| 123 |
Observable<Map<String, String>> observable2 = Observable.just(m3, m4); |
| 124 |
|
| 125 |
Observable<Observable<Map<String, String>>> o = Observable.just(observable1, observable2); |
| 126 |
|
| 127 |
Observable<String> m = o.flatMap(new Function<Observable<Map<String, String>>, Observable<String>>() { |
| 128 |
|
| 129 |
@Override |
| 130 |
public Observable<String> apply(Observable<Map<String, String>> o) { |
| 131 |
return o.map(new Function<Map<String, String>, String>() { |
| 132 |
|
| 133 |
@Override |
| 134 |
public String apply(Map<String, String> map) { |
| 135 |
return map.get("firstName"); |
| 136 |
} |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
}); |
| 141 |
m.subscribe(stringObserver); |
| 142 |
|
| 143 |
verify(stringObserver, never()).onError(any(Throwable.class)); |
| 144 |
verify(stringObserver, times(1)).onNext("OneFirst"); |
| 145 |
verify(stringObserver, times(1)).onNext("TwoFirst"); |
| 146 |
verify(stringObserver, times(1)).onNext("ThreeFirst"); |
| 147 |
verify(stringObserver, times(1)).onNext("FourFirst"); |
| 148 |
verify(stringObserver, times(1)).onComplete(); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
@Test |
| 153 |
public void mapWithError() { |
| 154 |
Observable<String> w = Observable.just("one", "fail", "two", "three", "fail"); |
| 155 |
Observable<String> m = w.map(new Function<String, String>() { |
| 156 |
@Override |
| 157 |
public String apply(String s) { |
| 158 |
if ("fail".equals(s)) { |
| 159 |
throw new RuntimeException("Forced Failure"); |
| 160 |
} |
| 161 |
return s; |
| 162 |
} |
| 163 |
}).doOnError(new Consumer<Throwable>() { |
| 164 |
|
| 165 |
@Override |
| 166 |
public void accept(Throwable t1) { |
| 167 |
t1.printStackTrace(); |
| 168 |
} |
| 169 |
|
| 170 |
}); |
| 171 |
|
| 172 |
m.subscribe(stringObserver); |
| 173 |
verify(stringObserver, times(1)).onNext("one"); |
| 174 |
verify(stringObserver, never()).onNext("two"); |
| 175 |
verify(stringObserver, never()).onNext("three"); |
| 176 |
verify(stringObserver, never()).onComplete(); |
| 177 |
verify(stringObserver, times(1)).onError(any(Throwable.class)); |
| 178 |
} |
| 179 |
|
| 180 |
@Test(expected = IllegalArgumentException.class) |
| 181 |
public void mapWithIssue417() { |
| 182 |
Observable.just(1).observeOn(Schedulers.computation()) |
| 183 |
.map(new Function<Integer, Integer>() { |
| 184 |
@Override |
| 185 |
public Integer apply(Integer arg0) { |
| 186 |
throw new IllegalArgumentException("any error"); |
| 187 |
} |
| 188 |
}).blockingSingle(); |
| 189 |
} |
| 190 |
|
| 191 |
@Test(expected = IllegalArgumentException.class) |
| 192 |
public void mapWithErrorInFuncAndThreadPoolScheduler() throws InterruptedException { |
| 193 |
// The error will throw in one of threads in the thread pool. |
| 194 |
// If map does not handle it, the error will disappear. |
| 195 |
// so map needs to handle the error by itself. |
| 196 |
Observable<String> m = Observable.just("one") |
| 197 |
.observeOn(Schedulers.computation()) |
| 198 |
.map(new Function<String, String>() { |
| 199 |
@Override |
| 200 |
public String apply(String arg0) { |
| 201 |
throw new IllegalArgumentException("any error"); |
| 202 |
} |
| 203 |
}); |
| 204 |
|
| 205 |
// block for response, expecting exception thrown |
| 206 |
m.blockingLast(); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* While mapping over range(1,0).last() we expect NoSuchElementException since the sequence is empty. |
| 211 |
*/ |
| 212 |
@Test |
| 213 |
public void errorPassesThruMap() { |
| 214 |
assertNull(Observable.range(1, 0).lastElement().map(new Function<Integer, Integer>() { |
| 215 |
|
| 216 |
@Override |
| 217 |
public Integer apply(Integer i) { |
| 218 |
return i; |
| 219 |
} |
| 220 |
|
| 221 |
}).blockingGet()); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* We expect IllegalStateException to pass thru map. |
| 226 |
*/ |
| 227 |
@Test(expected = IllegalStateException.class) |
| 228 |
public void errorPassesThruMap2() { |
| 229 |
Observable.error(new IllegalStateException()).map(new Function<Object, Object>() { |
| 230 |
|
| 231 |
@Override |
| 232 |
public Object apply(Object i) { |
| 233 |
return i; |
| 234 |
} |
| 235 |
|
| 236 |
}).blockingSingle(); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* We expect an ArithmeticException exception here because last() emits a single value |
| 241 |
* but then we divide by 0. |
| 242 |
*/ |
| 243 |
@Test(expected = ArithmeticException.class) |
| 244 |
public void mapWithErrorInFunc() { |
| 245 |
Observable.range(1, 1).lastElement().map(new Function<Integer, Integer>() { |
| 246 |
|
| 247 |
@Override |
| 248 |
public Integer apply(Integer i) { |
| 249 |
return i / 0; |
| 250 |
} |
| 251 |
|
| 252 |
}).blockingGet(); |
| 253 |
} |
| 254 |
|
| 255 |
// FIXME RS subscribers can't throw |
| 256 |
// @Test(expected = OnErrorNotImplementedException.class) |
| 257 |
// public void verifyExceptionIsThrownIfThereIsNoExceptionHandler() { |
| 258 |
// |
| 259 |
// ObservableSource<Object> creator = new ObservableSource<Object>() { |
| 260 |
// |
| 261 |
// @Override |
| 262 |
// public void subscribeActual(Observer<? super Object> observer) { |
| 263 |
// observer.onSubscribe(EmptyDisposable.INSTANCE); |
| 264 |
// observer.onNext("a"); |
| 265 |
// observer.onNext("b"); |
| 266 |
// observer.onNext("c"); |
| 267 |
// observer.onComplete(); |
| 268 |
// } |
| 269 |
// }; |
| 270 |
// |
| 271 |
// Function<Object, Observable<Object>> manyMapper = new Function<Object, Observable<Object>>() { |
| 272 |
// |
| 273 |
// @Override |
| 274 |
// public Observable<Object> apply(Object object) { |
| 275 |
// return Observable.just(object); |
| 276 |
// } |
| 277 |
// }; |
| 278 |
// |
| 279 |
// Function<Object, Object> mapper = new Function<Object, Object>() { |
| 280 |
// private int count = 0; |
| 281 |
// |
| 282 |
// @Override |
| 283 |
// public Object apply(Object object) { |
| 284 |
// ++count; |
| 285 |
// if (count > 2) { |
| 286 |
// throw new RuntimeException(); |
| 287 |
// } |
| 288 |
// return object; |
| 289 |
// } |
| 290 |
// }; |
| 291 |
// |
| 292 |
// Consumer<Object> onNext = new Consumer<Object>() { |
| 293 |
// |
| 294 |
// @Override |
| 295 |
// public void accept(Object object) { |
| 296 |
// System.out.println(object.toString()); |
| 297 |
// } |
| 298 |
// }; |
| 299 |
// |
| 300 |
// try { |
| 301 |
// Observable.unsafeCreate(creator).flatMap(manyMapper).map(mapper).subscribe(onNext); |
| 302 |
// } catch (RuntimeException e) { |
| 303 |
// e.printStackTrace(); |
| 304 |
// throw e; |
| 305 |
// } |
| 306 |
// } |
| 307 |
|
| 308 |
private static Map<String, String> getMap(String prefix) { |
| 309 |
Map<String, String> m = new HashMap<>(); |
| 310 |
m.put("firstName", prefix + "First"); |
| 311 |
m.put("lastName", prefix + "Last"); |
| 312 |
return m; |
| 313 |
} |
| 314 |
|
| 315 |
// FIXME RS subscribers can't throw |
| 316 |
// @Test(expected = OnErrorNotImplementedException.class) |
| 317 |
// public void testShouldNotSwallowOnErrorNotImplementedException() { |
| 318 |
// Observable.just("a", "b").flatMap(new Function<String, Observable<String>>() { |
| 319 |
// @Override |
| 320 |
// public Observable<String> apply(String s) { |
| 321 |
// return Observable.just(s + "1", s + "2"); |
| 322 |
// } |
| 323 |
// }).flatMap(new Function<String, Observable<String>>() { |
| 324 |
// @Override |
| 325 |
// public Observable<String> apply(String s) { |
| 326 |
// return Observable.error(new Exception("test")); |
| 327 |
// } |
| 328 |
// }).forEach(new Consumer<String>() { |
| 329 |
// @Override |
| 330 |
// public void accept(String s) { |
| 331 |
// System.out.println(s); |
| 332 |
// } |
| 333 |
// }); |
| 334 |
// } |
| 335 |
|
| 336 |
@Test |
| 337 |
public void dispose() { |
| 338 |
TestHelper.checkDisposed(Observable.range(1, 5).map(Functions.identity())); |
| 339 |
} |
| 340 |
|
| 341 |
@Test |
| 342 |
public void doubleOnSubscribe() { |
| 343 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Object>>() { |
| 344 |
@Override |
| 345 |
public ObservableSource<Object> apply(Observable<Object> o) throws Exception { |
| 346 |
return o.map(Functions.identity()); |
| 347 |
} |
| 348 |
}); |
| 349 |
} |
| 350 |
|
| 351 |
@Test |
| 352 |
public void fusedSync() { |
| 353 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY); |
| 354 |
|
| 355 |
Observable.range(1, 5) |
| 356 |
.map(Functions.<Integer>identity()) |
| 357 |
.subscribe(to); |
| 358 |
|
| 359 |
to.assertFusionMode(QueueFuseable.SYNC) |
| 360 |
.assertResult(1, 2, 3, 4, 5); |
| 361 |
} |
| 362 |
|
| 363 |
@Test |
| 364 |
public void fusedAsync() { |
| 365 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY); |
| 366 |
|
| 367 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 368 |
|
| 369 |
us |
| 370 |
.map(Functions.<Integer>identity()) |
| 371 |
.subscribe(to); |
| 372 |
|
| 373 |
TestHelper.emit(us, 1, 2, 3, 4, 5); |
| 374 |
|
| 375 |
to.assertFusionMode(QueueFuseable.ASYNC) |
| 376 |
.assertResult(1, 2, 3, 4, 5); |
| 377 |
} |
| 378 |
|
| 379 |
@Test |
| 380 |
public void fusedReject() { |
| 381 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY | QueueFuseable.BOUNDARY); |
| 382 |
|
| 383 |
Observable.range(1, 5) |
| 384 |
.map(Functions.<Integer>identity()) |
| 385 |
.subscribe(to); |
| 386 |
|
| 387 |
to.assertFusionMode(QueueFuseable.NONE) |
| 388 |
.assertResult(1, 2, 3, 4, 5); |
| 389 |
} |
| 390 |
|
| 391 |
@Test |
| 392 |
public void badSource() { |
| 393 |
TestHelper.checkBadSourceObservable(new Function<Observable<Object>, Object>() { |
| 394 |
@Override |
| 395 |
public Object apply(Observable<Object> o) throws Exception { |
| 396 |
return o.map(Functions.identity()); |
| 397 |
} |
| 398 |
}, false, 1, 1, 1); |
| 399 |
} |
| 400 |
} |