| 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.*; |
| 17 |
|
| 18 |
import org.junit.Test; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.disposables.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 23 |
import io.reactivex.rxjava3.functions.Action; |
| 24 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 25 |
import io.reactivex.rxjava3.subjects.MaybeSubject; |
| 26 |
|
| 27 |
public class ObservableConcatWithMaybeTest extends RxJavaTest { |
| 28 |
|
| 29 |
@Test |
| 30 |
public void normalEmpty() { |
| 31 |
final TestObserver<Integer> to = new TestObserver<>(); |
| 32 |
|
| 33 |
Observable.range(1, 5) |
| 34 |
.concatWith(Maybe.<Integer>fromAction(new Action() { |
| 35 |
@Override |
| 36 |
public void run() throws Exception { |
| 37 |
to.onNext(100); |
| 38 |
} |
| 39 |
})) |
| 40 |
.subscribe(to); |
| 41 |
|
| 42 |
to.assertResult(1, 2, 3, 4, 5, 100); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void normalNonEmpty() { |
| 47 |
final TestObserver<Integer> to = new TestObserver<>(); |
| 48 |
|
| 49 |
Observable.range(1, 5) |
| 50 |
.concatWith(Maybe.just(100)) |
| 51 |
.subscribe(to); |
| 52 |
|
| 53 |
to.assertResult(1, 2, 3, 4, 5, 100); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void mainError() { |
| 58 |
final TestObserver<Integer> to = new TestObserver<>(); |
| 59 |
|
| 60 |
Observable.<Integer>error(new TestException()) |
| 61 |
.concatWith(Maybe.<Integer>fromAction(new Action() { |
| 62 |
@Override |
| 63 |
public void run() throws Exception { |
| 64 |
to.onNext(100); |
| 65 |
} |
| 66 |
})) |
| 67 |
.subscribe(to); |
| 68 |
|
| 69 |
to.assertFailure(TestException.class); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void otherError() { |
| 74 |
final TestObserver<Integer> to = new TestObserver<>(); |
| 75 |
|
| 76 |
Observable.range(1, 5) |
| 77 |
.concatWith(Maybe.<Integer>error(new TestException())) |
| 78 |
.subscribe(to); |
| 79 |
|
| 80 |
to.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void takeMain() { |
| 85 |
final TestObserver<Integer> to = new TestObserver<>(); |
| 86 |
|
| 87 |
Observable.range(1, 5) |
| 88 |
.concatWith(Maybe.<Integer>fromAction(new Action() { |
| 89 |
@Override |
| 90 |
public void run() throws Exception { |
| 91 |
to.onNext(100); |
| 92 |
} |
| 93 |
})) |
| 94 |
.take(3) |
| 95 |
.subscribe(to); |
| 96 |
|
| 97 |
to.assertResult(1, 2, 3); |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void cancelOther() { |
| 102 |
MaybeSubject<Object> other = MaybeSubject.create(); |
| 103 |
|
| 104 |
TestObserver<Object> to = Observable.empty() |
| 105 |
.concatWith(other) |
| 106 |
.test(); |
| 107 |
|
| 108 |
assertTrue(other.hasObservers()); |
| 109 |
|
| 110 |
to.dispose(); |
| 111 |
|
| 112 |
assertFalse(other.hasObservers()); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void consumerDisposed() { |
| 117 |
new Observable<Integer>() { |
| 118 |
@Override |
| 119 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 120 |
Disposable bs1 = Disposable.empty(); |
| 121 |
observer.onSubscribe(bs1); |
| 122 |
|
| 123 |
assertFalse(((Disposable)observer).isDisposed()); |
| 124 |
|
| 125 |
observer.onNext(1); |
| 126 |
|
| 127 |
assertTrue(((Disposable)observer).isDisposed()); |
| 128 |
assertTrue(bs1.isDisposed()); |
| 129 |
} |
| 130 |
}.concatWith(Maybe.just(100)) |
| 131 |
.take(1) |
| 132 |
.test() |
| 133 |
.assertResult(1); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void badSource() { |
| 138 |
new Observable<Integer>() { |
| 139 |
@Override |
| 140 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 141 |
Disposable bs1 = Disposable.empty(); |
| 142 |
observer.onSubscribe(bs1); |
| 143 |
|
| 144 |
Disposable bs2 = Disposable.empty(); |
| 145 |
observer.onSubscribe(bs2); |
| 146 |
|
| 147 |
assertFalse(bs1.isDisposed()); |
| 148 |
assertTrue(bs2.isDisposed()); |
| 149 |
|
| 150 |
observer.onComplete(); |
| 151 |
} |
| 152 |
}.concatWith(Maybe.<Integer>empty()) |
| 153 |
.test() |
| 154 |
.assertResult(); |
| 155 |
} |
| 156 |
|
| 157 |
@Test |
| 158 |
public void badSource2() { |
| 159 |
Flowable.empty().concatWith(new Maybe<Integer>() { |
| 160 |
@Override |
| 161 |
protected void subscribeActual(MaybeObserver<? super Integer> observer) { |
| 162 |
Disposable bs1 = Disposable.empty(); |
| 163 |
observer.onSubscribe(bs1); |
| 164 |
|
| 165 |
Disposable bs2 = Disposable.empty(); |
| 166 |
observer.onSubscribe(bs2); |
| 167 |
|
| 168 |
assertFalse(bs1.isDisposed()); |
| 169 |
assertTrue(bs2.isDisposed()); |
| 170 |
|
| 171 |
observer.onComplete(); |
| 172 |
} |
| 173 |
}) |
| 174 |
.test() |
| 175 |
.assertResult(); |
| 176 |
} |
| 177 |
|
| 178 |
} |