| 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.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.subjects.SingleSubject; |
| 25 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 26 |
|
| 27 |
public class SingleFlatMapBiSelectorTest 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 |
Single.just(1) |
| 41 |
.flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 42 |
@Override |
| 43 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 44 |
return Single.just(2); |
| 45 |
} |
| 46 |
}, stringCombine()) |
| 47 |
.test() |
| 48 |
.assertResult("1:2"); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void errorWithJust() { |
| 53 |
final int[] call = { 0 }; |
| 54 |
|
| 55 |
Single.<Integer>error(new TestException()) |
| 56 |
.flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 57 |
@Override |
| 58 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 59 |
call[0]++; |
| 60 |
return Single.just(1); |
| 61 |
} |
| 62 |
}, stringCombine()) |
| 63 |
.test() |
| 64 |
.assertFailure(TestException.class); |
| 65 |
|
| 66 |
assertEquals(0, call[0]); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void justWithError() { |
| 71 |
final int[] call = { 0 }; |
| 72 |
|
| 73 |
Single.just(1) |
| 74 |
.flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 75 |
@Override |
| 76 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 77 |
call[0]++; |
| 78 |
return Single.<Integer>error(new TestException()); |
| 79 |
} |
| 80 |
}, stringCombine()) |
| 81 |
.test() |
| 82 |
.assertFailure(TestException.class); |
| 83 |
|
| 84 |
assertEquals(1, call[0]); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void dispose() { |
| 89 |
TestHelper.checkDisposed(SingleSubject.create() |
| 90 |
.flatMap(new Function<Object, SingleSource<Integer>>() { |
| 91 |
@Override |
| 92 |
public SingleSource<Integer> apply(Object v) throws Exception { |
| 93 |
return Single.just(1); |
| 94 |
} |
| 95 |
}, new BiFunction<Object, Integer, Object>() { |
| 96 |
@Override |
| 97 |
public Object apply(Object a, Integer b) throws Exception { |
| 98 |
return b; |
| 99 |
} |
| 100 |
})); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void doubleOnSubscribe() { |
| 105 |
TestHelper.checkDoubleOnSubscribeSingle(new Function<Single<Object>, SingleSource<Object>>() { |
| 106 |
@Override |
| 107 |
public SingleSource<Object> apply(Single<Object> v) throws Exception { |
| 108 |
return v.flatMap(new Function<Object, SingleSource<Integer>>() { |
| 109 |
@Override |
| 110 |
public SingleSource<Integer> apply(Object v) throws Exception { |
| 111 |
return Single.just(1); |
| 112 |
} |
| 113 |
}, new BiFunction<Object, Integer, Object>() { |
| 114 |
@Override |
| 115 |
public Object apply(Object a, Integer b) throws Exception { |
| 116 |
return b; |
| 117 |
} |
| 118 |
}); |
| 119 |
} |
| 120 |
}); |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void mapperThrows() { |
| 125 |
Single.just(1) |
| 126 |
.flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 127 |
@Override |
| 128 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 129 |
throw new TestException(); |
| 130 |
} |
| 131 |
}, stringCombine()) |
| 132 |
.test() |
| 133 |
.assertFailure(TestException.class); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void mapperReturnsNull() { |
| 138 |
Single.just(1) |
| 139 |
.flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 140 |
@Override |
| 141 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 142 |
return null; |
| 143 |
} |
| 144 |
}, stringCombine()) |
| 145 |
.test() |
| 146 |
.assertFailure(NullPointerException.class); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void resultSelectorThrows() { |
| 151 |
Single.just(1) |
| 152 |
.flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 153 |
@Override |
| 154 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 155 |
return Single.just(2); |
| 156 |
} |
| 157 |
}, new BiFunction<Integer, Integer, Object>() { |
| 158 |
@Override |
| 159 |
public Object apply(Integer a, Integer b) throws Exception { |
| 160 |
throw new TestException(); |
| 161 |
} |
| 162 |
}) |
| 163 |
.test() |
| 164 |
.assertFailure(TestException.class); |
| 165 |
} |
| 166 |
|
| 167 |
@Test |
| 168 |
public void resultSelectorReturnsNull() { |
| 169 |
Single.just(1) |
| 170 |
.flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 171 |
@Override |
| 172 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 173 |
return Single.just(2); |
| 174 |
} |
| 175 |
}, new BiFunction<Integer, Integer, Object>() { |
| 176 |
@Override |
| 177 |
public Object apply(Integer a, Integer b) throws Exception { |
| 178 |
return null; |
| 179 |
} |
| 180 |
}) |
| 181 |
.test() |
| 182 |
.assertFailure(NullPointerException.class); |
| 183 |
} |
| 184 |
|
| 185 |
@Test |
| 186 |
public void mapperCancels() { |
| 187 |
final TestObserver<Integer> to = new TestObserver<>(); |
| 188 |
|
| 189 |
Single.just(1) |
| 190 |
.flatMap(new Function<Integer, SingleSource<Integer>>() { |
| 191 |
@Override |
| 192 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 193 |
to.dispose(); |
| 194 |
return Single.just(2); |
| 195 |
} |
| 196 |
}, new BiFunction<Integer, Integer, Integer>() { |
| 197 |
@Override |
| 198 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 199 |
throw new IllegalStateException(); |
| 200 |
} |
| 201 |
}) |
| 202 |
.subscribeWith(to) |
| 203 |
.assertEmpty(); |
| 204 |
} |
| 205 |
} |