| 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 io.reactivex.rxjava3.disposables.Disposable; |
| 17 |
import org.junit.Test; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.exceptions.*; |
| 21 |
import io.reactivex.rxjava3.functions.*; |
| 22 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 23 |
import io.reactivex.rxjava3.internal.operators.observable.ObservableMapNotification.MapNotificationObserver; |
| 24 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 25 |
import io.reactivex.rxjava3.testsupport.*; |
| 26 |
|
| 27 |
public class ObservableMapNotificationTest extends RxJavaTest { |
| 28 |
@Test |
| 29 |
public void just() { |
| 30 |
TestObserver<Object> to = new TestObserver<>(); |
| 31 |
Observable.just(1) |
| 32 |
.flatMap( |
| 33 |
new Function<Integer, Observable<Object>>() { |
| 34 |
@Override |
| 35 |
public Observable<Object> apply(Integer item) { |
| 36 |
return Observable.just((Object)(item + 1)); |
| 37 |
} |
| 38 |
}, |
| 39 |
new Function<Throwable, Observable<Object>>() { |
| 40 |
@Override |
| 41 |
public Observable<Object> apply(Throwable e) { |
| 42 |
return Observable.error(e); |
| 43 |
} |
| 44 |
}, |
| 45 |
new Supplier<Observable<Object>>() { |
| 46 |
@Override |
| 47 |
public Observable<Object> get() { |
| 48 |
return Observable.never(); |
| 49 |
} |
| 50 |
} |
| 51 |
).subscribe(to); |
| 52 |
|
| 53 |
to.assertNoErrors(); |
| 54 |
to.assertNotComplete(); |
| 55 |
to.assertValue(2); |
| 56 |
} |
| 57 |
|
| 58 |
@Test |
| 59 |
public void dispose() { |
| 60 |
TestHelper.checkDisposed(new Observable<Integer>() { |
| 61 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 62 |
@Override |
| 63 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 64 |
MapNotificationObserver mn = new MapNotificationObserver( |
| 65 |
observer, |
| 66 |
Functions.justFunction(Observable.just(1)), |
| 67 |
Functions.justFunction(Observable.just(2)), |
| 68 |
Functions.justSupplier(Observable.just(3)) |
| 69 |
); |
| 70 |
mn.onSubscribe(Disposable.empty()); |
| 71 |
} |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void doubleOnSubscribe() { |
| 77 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Integer>>() { |
| 78 |
@Override |
| 79 |
public ObservableSource<Integer> apply(Observable<Object> o) throws Exception { |
| 80 |
return o.flatMap( |
| 81 |
Functions.justFunction(Observable.just(1)), |
| 82 |
Functions.justFunction(Observable.just(2)), |
| 83 |
Functions.justSupplier(Observable.just(3)) |
| 84 |
); |
| 85 |
} |
| 86 |
}); |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void onErrorCrash() { |
| 91 |
TestObserverEx<Integer> to = Observable.<Integer>error(new TestException("Outer")) |
| 92 |
.flatMap(Functions.justFunction(Observable.just(1)), |
| 93 |
new Function<Throwable, Observable<Integer>>() { |
| 94 |
@Override |
| 95 |
public Observable<Integer> apply(Throwable t) throws Exception { |
| 96 |
throw new TestException("Inner"); |
| 97 |
} |
| 98 |
}, |
| 99 |
Functions.justSupplier(Observable.just(3))) |
| 100 |
.to(TestHelper.<Integer>testConsumer()) |
| 101 |
.assertFailure(CompositeException.class); |
| 102 |
|
| 103 |
TestHelper.assertError(to, 0, TestException.class, "Outer"); |
| 104 |
TestHelper.assertError(to, 1, TestException.class, "Inner"); |
| 105 |
} |
| 106 |
} |