| 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.core; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import org.junit.Test; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 21 |
import io.reactivex.rxjava3.parallel.*; |
| 22 |
|
| 23 |
public final class ConverterTest extends RxJavaTest { |
| 24 |
|
| 25 |
@Test |
| 26 |
public void flowableConverterThrows() { |
| 27 |
try { |
| 28 |
Flowable.just(1).to(new FlowableConverter<Integer, Integer>() { |
| 29 |
@Override |
| 30 |
public Integer apply(Flowable<Integer> v) { |
| 31 |
throw new TestException("Forced failure"); |
| 32 |
} |
| 33 |
}); |
| 34 |
fail("Should have thrown!"); |
| 35 |
} catch (TestException ex) { |
| 36 |
assertEquals("Forced failure", ex.getMessage()); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void observableConverterThrows() { |
| 42 |
try { |
| 43 |
Observable.just(1).to(new ObservableConverter<Integer, Integer>() { |
| 44 |
@Override |
| 45 |
public Integer apply(Observable<Integer> v) { |
| 46 |
throw new TestException("Forced failure"); |
| 47 |
} |
| 48 |
}); |
| 49 |
fail("Should have thrown!"); |
| 50 |
} catch (TestException ex) { |
| 51 |
assertEquals("Forced failure", ex.getMessage()); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void singleConverterThrows() { |
| 57 |
try { |
| 58 |
Single.just(1).to(new SingleConverter<Integer, Integer>() { |
| 59 |
@Override |
| 60 |
public Integer apply(Single<Integer> v) { |
| 61 |
throw new TestException("Forced failure"); |
| 62 |
} |
| 63 |
}); |
| 64 |
fail("Should have thrown!"); |
| 65 |
} catch (TestException ex) { |
| 66 |
assertEquals("Forced failure", ex.getMessage()); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void maybeConverterThrows() { |
| 72 |
try { |
| 73 |
Maybe.just(1).to(new MaybeConverter<Integer, Integer>() { |
| 74 |
@Override |
| 75 |
public Integer apply(Maybe<Integer> v) { |
| 76 |
throw new TestException("Forced failure"); |
| 77 |
} |
| 78 |
}); |
| 79 |
fail("Should have thrown!"); |
| 80 |
} catch (TestException ex) { |
| 81 |
assertEquals("Forced failure", ex.getMessage()); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void completableConverterThrows() { |
| 87 |
try { |
| 88 |
Completable.complete().to(new CompletableConverter<Completable>() { |
| 89 |
@Override |
| 90 |
public Completable apply(Completable v) { |
| 91 |
throw new TestException("Forced failure"); |
| 92 |
} |
| 93 |
}); |
| 94 |
fail("Should have thrown!"); |
| 95 |
} catch (TestException ex) { |
| 96 |
assertEquals("Forced failure", ex.getMessage()); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Test demos for signature generics in compose() methods. Just needs to compile. |
| 101 |
|
| 102 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 103 |
@Test |
| 104 |
public void observableGenericsSignatureTest() { |
| 105 |
A<String, Integer> a = new A<String, Integer>() { }; |
| 106 |
|
| 107 |
Observable.just(a).to((ObservableConverter)ConverterTest.testObservableConverterCreator()); |
| 108 |
} |
| 109 |
|
| 110 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 111 |
@Test |
| 112 |
public void singleGenericsSignatureTest() { |
| 113 |
A<String, Integer> a = new A<String, Integer>() { }; |
| 114 |
|
| 115 |
Single.just(a).to((SingleConverter)ConverterTest.<String>testSingleConverterCreator()); |
| 116 |
} |
| 117 |
|
| 118 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 119 |
@Test |
| 120 |
public void maybeGenericsSignatureTest() { |
| 121 |
A<String, Integer> a = new A<String, Integer>() { }; |
| 122 |
|
| 123 |
Maybe.just(a).to((MaybeConverter)ConverterTest.<String>testMaybeConverterCreator()); |
| 124 |
} |
| 125 |
|
| 126 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 127 |
@Test |
| 128 |
public void flowableGenericsSignatureTest() { |
| 129 |
A<String, Integer> a = new A<String, Integer>() { }; |
| 130 |
|
| 131 |
Flowable.just(a).to((FlowableConverter)ConverterTest.<String>testFlowableConverterCreator()); |
| 132 |
} |
| 133 |
|
| 134 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 135 |
@Test |
| 136 |
public void parallelFlowableGenericsSignatureTest() { |
| 137 |
A<String, Integer> a = new A<String, Integer>() { }; |
| 138 |
|
| 139 |
Flowable.just(a).parallel().to((ParallelFlowableConverter)ConverterTest.<String>testParallelFlowableConverterCreator()); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void compositeTest() { |
| 144 |
CompositeConverter converter = new CompositeConverter(); |
| 145 |
|
| 146 |
Flowable.just(1) |
| 147 |
.to(converter) |
| 148 |
.test() |
| 149 |
.assertValue(1); |
| 150 |
|
| 151 |
Observable.just(1) |
| 152 |
.to(converter) |
| 153 |
.test() |
| 154 |
.assertValue(1); |
| 155 |
|
| 156 |
Maybe.just(1) |
| 157 |
.to(converter) |
| 158 |
.test() |
| 159 |
.assertValue(1); |
| 160 |
|
| 161 |
Single.just(1) |
| 162 |
.to(converter) |
| 163 |
.test() |
| 164 |
.assertValue(1); |
| 165 |
|
| 166 |
Completable.complete() |
| 167 |
.to(converter) |
| 168 |
.test() |
| 169 |
.assertComplete(); |
| 170 |
|
| 171 |
Flowable.just(1) |
| 172 |
.parallel() |
| 173 |
.to(converter) |
| 174 |
.test() |
| 175 |
.assertValue(1); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Two argument type. |
| 180 |
* @param <T> the input type |
| 181 |
* @param <R> the output type |
| 182 |
*/ |
| 183 |
interface A<T, R> { } |
| 184 |
|
| 185 |
/** |
| 186 |
* One argument type. |
| 187 |
* @param <T> the type |
| 188 |
*/ |
| 189 |
interface B<T> { } |
| 190 |
|
| 191 |
private static <T> ObservableConverter<A<T, ?>, B<T>> testObservableConverterCreator() { |
| 192 |
return new ObservableConverter<A<T, ?>, B<T>>() { |
| 193 |
@Override |
| 194 |
public B<T> apply(Observable<A<T, ?>> a) { |
| 195 |
return new B<T>() { |
| 196 |
}; |
| 197 |
} |
| 198 |
}; |
| 199 |
} |
| 200 |
|
| 201 |
private static <T> SingleConverter<A<T, ?>, B<T>> testSingleConverterCreator() { |
| 202 |
return new SingleConverter<A<T, ?>, B<T>>() { |
| 203 |
@Override |
| 204 |
public B<T> apply(Single<A<T, ?>> a) { |
| 205 |
return new B<T>() { |
| 206 |
}; |
| 207 |
} |
| 208 |
}; |
| 209 |
} |
| 210 |
|
| 211 |
private static <T> MaybeConverter<A<T, ?>, B<T>> testMaybeConverterCreator() { |
| 212 |
return new MaybeConverter<A<T, ?>, B<T>>() { |
| 213 |
@Override |
| 214 |
public B<T> apply(Maybe<A<T, ?>> a) { |
| 215 |
return new B<T>() { |
| 216 |
}; |
| 217 |
} |
| 218 |
}; |
| 219 |
} |
| 220 |
|
| 221 |
private static <T> FlowableConverter<A<T, ?>, B<T>> testFlowableConverterCreator() { |
| 222 |
return new FlowableConverter<A<T, ?>, B<T>>() { |
| 223 |
@Override |
| 224 |
public B<T> apply(Flowable<A<T, ?>> a) { |
| 225 |
return new B<T>() { |
| 226 |
}; |
| 227 |
} |
| 228 |
}; |
| 229 |
} |
| 230 |
|
| 231 |
private static <T> ParallelFlowableConverter<A<T, ?>, B<T>> testParallelFlowableConverterCreator() { |
| 232 |
return new ParallelFlowableConverter<A<T, ?>, B<T>>() { |
| 233 |
@Override |
| 234 |
public B<T> apply(ParallelFlowable<A<T, ?>> a) { |
| 235 |
return new B<T>() { |
| 236 |
}; |
| 237 |
} |
| 238 |
}; |
| 239 |
} |
| 240 |
|
| 241 |
static class CompositeConverter |
| 242 |
implements ObservableConverter<Integer, Flowable<Integer>>, |
| 243 |
ParallelFlowableConverter<Integer, Flowable<Integer>>, |
| 244 |
FlowableConverter<Integer, Observable<Integer>>, |
| 245 |
MaybeConverter<Integer, Flowable<Integer>>, |
| 246 |
SingleConverter<Integer, Flowable<Integer>>, |
| 247 |
CompletableConverter<Flowable<Integer>> { |
| 248 |
@Override |
| 249 |
public Flowable<Integer> apply(ParallelFlowable<Integer> upstream) { |
| 250 |
return upstream.sequential(); |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
public Flowable<Integer> apply(Completable upstream) { |
| 255 |
return upstream.toFlowable(); |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
public Observable<Integer> apply(Flowable<Integer> upstream) { |
| 260 |
return upstream.toObservable(); |
| 261 |
} |
| 262 |
|
| 263 |
@Override |
| 264 |
public Flowable<Integer> apply(Maybe<Integer> upstream) { |
| 265 |
return upstream.toFlowable(); |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public Flowable<Integer> apply(Observable<Integer> upstream) { |
| 270 |
return upstream.toFlowable(BackpressureStrategy.MISSING); |
| 271 |
} |
| 272 |
|
| 273 |
@Override |
| 274 |
public Flowable<Integer> apply(Single<Integer> upstream) { |
| 275 |
return upstream.toFlowable(); |
| 276 |
} |
| 277 |
} |
| 278 |
} |