|
| 1 | +package io.reactivex; |
| 2 | + |
| 3 | +import io.reactivex.exceptions.TestException; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import static org.junit.Assert.*; |
| 7 | + |
| 8 | +public final class ConverterTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + public void flowableConverterThrows() { |
| 12 | + try { |
| 13 | + Flowable.just(1).as(new FlowableConverter<Integer, Integer>() { |
| 14 | + @Override |
| 15 | + public Integer apply(Flowable<Integer> v) { |
| 16 | + throw new TestException("Forced failure"); |
| 17 | + } |
| 18 | + }); |
| 19 | + fail("Should have thrown!"); |
| 20 | + } catch (TestException ex) { |
| 21 | + assertEquals("Forced failure", ex.getMessage()); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void observableConverterThrows() { |
| 27 | + try { |
| 28 | + Observable.just(1).as(new ObservableConverter<Integer, Integer>() { |
| 29 | + @Override |
| 30 | + public Integer apply(Observable<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 singleConverterThrows() { |
| 42 | + try { |
| 43 | + Single.just(1).as(new SingleConverter<Integer, Integer>() { |
| 44 | + @Override |
| 45 | + public Integer apply(Single<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 maybeConverterThrows() { |
| 57 | + try { |
| 58 | + Maybe.just(1).as(new MaybeConverter<Integer, Integer>() { |
| 59 | + @Override |
| 60 | + public Integer apply(Maybe<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 completableConverterThrows() { |
| 72 | + try { |
| 73 | + Completable.complete().as(new CompletableConverter() { |
| 74 | + @Override |
| 75 | + public Completable apply(Completable 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 demos for signature generics in compose() methods. Just needs to compile. |
| 86 | + |
| 87 | + @Test |
| 88 | + public void observableGenericsSignatureTest() { |
| 89 | + A<String, Integer> a = new A<String, Integer>() { }; |
| 90 | + |
| 91 | + Observable.just(a).as(ConverterTest.<Integer>testObservableConverterCreator()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void singleGenericsSignatureTest() { |
| 96 | + A<String, Integer> a = new A<String, Integer>() { }; |
| 97 | + |
| 98 | + Single.just(a).as(ConverterTest.<Integer>testSingleConverterCreator()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void maybeGenericsSignatureTest() { |
| 103 | + A<String, Integer> a = new A<String, Integer>() { }; |
| 104 | + |
| 105 | + Maybe.just(a).as(ConverterTest.<Integer>testMaybeConverterCreator()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void flowableGenericsSignatureTest() { |
| 110 | + A<String, Integer> a = new A<String, Integer>() { }; |
| 111 | + |
| 112 | + Flowable.just(a).as(ConverterTest.<Integer>testFlowableConverterCreator()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void compositeTest() { |
| 117 | + CompositeConverter converter = new CompositeConverter(); |
| 118 | + |
| 119 | + Flowable.just(1) |
| 120 | + .as(converter) |
| 121 | + .test() |
| 122 | + .assertValue(1); |
| 123 | + |
| 124 | + Observable.just(1) |
| 125 | + .as(converter) |
| 126 | + .test() |
| 127 | + .assertValue(1); |
| 128 | + |
| 129 | + Maybe.just(1) |
| 130 | + .as(converter) |
| 131 | + .test() |
| 132 | + .assertValue(1); |
| 133 | + |
| 134 | + Single.just(1) |
| 135 | + .as(converter) |
| 136 | + .test() |
| 137 | + .assertValue(1); |
| 138 | + |
| 139 | + Completable.complete() |
| 140 | + .as(converter) |
| 141 | + .test() |
| 142 | + .assertComplete(); |
| 143 | + } |
| 144 | + |
| 145 | + interface A<T, R> { } |
| 146 | + interface B<T> { } |
| 147 | + |
| 148 | + private static <T> ObservableConverter<A<T, ?>, B<T>> testObservableConverterCreator() { |
| 149 | + return new ObservableConverter<A<T, ?>, B<T>>() { |
| 150 | + @Override |
| 151 | + public B<T> apply(Observable<A<T, ?>> a) { |
| 152 | + return new B<T>() { |
| 153 | + }; |
| 154 | + } |
| 155 | + }; |
| 156 | + } |
| 157 | + |
| 158 | + private static <T> SingleConverter<A<T, ?>, B<T>> testSingleConverterCreator() { |
| 159 | + return new SingleConverter<A<T, ?>, B<T>>() { |
| 160 | + @Override |
| 161 | + public B<T> apply(Single<A<T, ?>> a) { |
| 162 | + return new B<T>() { |
| 163 | + }; |
| 164 | + } |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + private static <T> MaybeConverter<A<T, ?>, B<T>> testMaybeConverterCreator() { |
| 169 | + return new MaybeConverter<A<T, ?>, B<T>>() { |
| 170 | + @Override |
| 171 | + public B<T> apply(Maybe<A<T, ?>> a) { |
| 172 | + return new B<T>() { |
| 173 | + }; |
| 174 | + } |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + private static <T> FlowableConverter<A<T, ?>, B<T>> testFlowableConverterCreator() { |
| 179 | + return new FlowableConverter<A<T, ?>, B<T>>() { |
| 180 | + @Override |
| 181 | + public B<T> apply(Flowable<A<T, ?>> a) { |
| 182 | + return new B<T>() { |
| 183 | + }; |
| 184 | + } |
| 185 | + }; |
| 186 | + } |
| 187 | + |
| 188 | + private static class CompositeConverter implements ObservableConverter<Integer, Flowable<Integer>>, |
| 189 | + FlowableConverter<Integer, Observable<Integer>>, |
| 190 | + MaybeConverter<Integer, Flowable<Integer>>, |
| 191 | + SingleConverter<Integer, Flowable<Integer>>, |
| 192 | + CompletableConverter<Flowable<Integer>> { |
| 193 | + @Override |
| 194 | + public Flowable<Integer> apply(Completable upstream) throws Exception { |
| 195 | + return upstream.toFlowable(); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public Observable<Integer> apply(Flowable<Integer> upstream) throws Exception { |
| 200 | + return upstream.toObservable(); |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public Flowable<Integer> apply(Maybe<Integer> upstream) throws Exception { |
| 205 | + return upstream.toFlowable(); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public Flowable<Integer> apply(Observable<Integer> upstream) throws Exception { |
| 210 | + return upstream.toFlowable(BackpressureStrategy.MISSING); |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public Flowable<Integer> apply(Single<Integer> upstream) throws Exception { |
| 215 | + return upstream.toFlowable(); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments