|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 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; |
| 15 | + |
| 16 | +import static org.junit.Assert.*; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +import org.junit.Test; |
| 21 | +import org.reactivestreams.Publisher; |
| 22 | + |
| 23 | +import io.reactivex.exceptions.TestException; |
| 24 | + |
| 25 | +public class TransformerTest { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void flowableTransformerThrows() { |
| 29 | + try { |
| 30 | + Flowable.just(1).compose(new FlowableTransformer<Integer, Integer>() { |
| 31 | + @Override |
| 32 | + public Publisher<Integer> apply(Flowable<Integer> v) throws Exception { |
| 33 | + throw new TestException("Forced failure"); |
| 34 | + } |
| 35 | + }); |
| 36 | + fail("Should have thrown!"); |
| 37 | + } catch (TestException ex) { |
| 38 | + assertEquals("Forced failure", ex.getMessage()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void flowableTransformerThrowsChecked() { |
| 44 | + try { |
| 45 | + Flowable.just(1).compose(new FlowableTransformer<Integer, Integer>() { |
| 46 | + @Override |
| 47 | + public Publisher<Integer> apply(Flowable<Integer> v) throws Exception { |
| 48 | + throw new IOException("Forced failure"); |
| 49 | + } |
| 50 | + }); |
| 51 | + fail("Should have thrown!"); |
| 52 | + } catch (RuntimeException ex) { |
| 53 | + assertTrue(ex.toString(), ex.getCause() instanceof IOException); |
| 54 | + assertEquals("Forced failure", ex.getCause().getMessage()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void observableTransformerThrows() { |
| 60 | + try { |
| 61 | + Observable.just(1).compose(new ObservableTransformer<Integer, Integer>() { |
| 62 | + @Override |
| 63 | + public Observable<Integer> apply(Observable<Integer> v) throws Exception { |
| 64 | + throw new TestException("Forced failure"); |
| 65 | + } |
| 66 | + }); |
| 67 | + fail("Should have thrown!"); |
| 68 | + } catch (TestException ex) { |
| 69 | + assertEquals("Forced failure", ex.getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void observableTransformerThrowsChecked() { |
| 75 | + try { |
| 76 | + Observable.just(1).compose(new ObservableTransformer<Integer, Integer>() { |
| 77 | + @Override |
| 78 | + public Observable<Integer> apply(Observable<Integer> v) throws Exception { |
| 79 | + throw new IOException("Forced failure"); |
| 80 | + } |
| 81 | + }); |
| 82 | + fail("Should have thrown!"); |
| 83 | + } catch (RuntimeException ex) { |
| 84 | + assertTrue(ex.toString(), ex.getCause() instanceof IOException); |
| 85 | + assertEquals("Forced failure", ex.getCause().getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void singleTransformerThrows() { |
| 91 | + try { |
| 92 | + Single.just(1).compose(new SingleTransformer<Integer, Integer>() { |
| 93 | + @Override |
| 94 | + public Single<Integer> apply(Single<Integer> v) throws Exception { |
| 95 | + throw new TestException("Forced failure"); |
| 96 | + } |
| 97 | + }); |
| 98 | + fail("Should have thrown!"); |
| 99 | + } catch (TestException ex) { |
| 100 | + assertEquals("Forced failure", ex.getMessage()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void singleTransformerThrowsChecked() { |
| 106 | + try { |
| 107 | + Single.just(1).compose(new SingleTransformer<Integer, Integer>() { |
| 108 | + @Override |
| 109 | + public Single<Integer> apply(Single<Integer> v) throws Exception { |
| 110 | + throw new IOException("Forced failure"); |
| 111 | + } |
| 112 | + }); |
| 113 | + fail("Should have thrown!"); |
| 114 | + } catch (RuntimeException ex) { |
| 115 | + assertTrue(ex.toString(), ex.getCause() instanceof IOException); |
| 116 | + assertEquals("Forced failure", ex.getCause().getMessage()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void maybeTransformerThrows() { |
| 122 | + try { |
| 123 | + Maybe.just(1).compose(new MaybeTransformer<Integer, Integer>() { |
| 124 | + @Override |
| 125 | + public Maybe<Integer> apply(Maybe<Integer> v) throws Exception { |
| 126 | + throw new TestException("Forced failure"); |
| 127 | + } |
| 128 | + }); |
| 129 | + fail("Should have thrown!"); |
| 130 | + } catch (TestException ex) { |
| 131 | + assertEquals("Forced failure", ex.getMessage()); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void maybeTransformerThrowsChecked() { |
| 137 | + try { |
| 138 | + Maybe.just(1).compose(new MaybeTransformer<Integer, Integer>() { |
| 139 | + @Override |
| 140 | + public Maybe<Integer> apply(Maybe<Integer> v) throws Exception { |
| 141 | + throw new IOException("Forced failure"); |
| 142 | + } |
| 143 | + }); |
| 144 | + fail("Should have thrown!"); |
| 145 | + } catch (RuntimeException ex) { |
| 146 | + assertTrue(ex.toString(), ex.getCause() instanceof IOException); |
| 147 | + assertEquals("Forced failure", ex.getCause().getMessage()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void completabeTransformerThrows() { |
| 153 | + try { |
| 154 | + Completable.complete().compose(new CompletableTransformer() { |
| 155 | + @Override |
| 156 | + public Completable apply(Completable v) throws Exception { |
| 157 | + throw new TestException("Forced failure"); |
| 158 | + } |
| 159 | + }); |
| 160 | + fail("Should have thrown!"); |
| 161 | + } catch (TestException ex) { |
| 162 | + assertEquals("Forced failure", ex.getMessage()); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void completabeTransformerThrowsChecked() { |
| 168 | + try { |
| 169 | + Completable.complete().compose(new CompletableTransformer() { |
| 170 | + @Override |
| 171 | + public Completable apply(Completable v) throws Exception { |
| 172 | + throw new IOException("Forced failure"); |
| 173 | + } |
| 174 | + }); |
| 175 | + fail("Should have thrown!"); |
| 176 | + } catch (RuntimeException ex) { |
| 177 | + assertTrue(ex.toString(), ex.getCause() instanceof IOException); |
| 178 | + assertEquals("Forced failure", ex.getCause().getMessage()); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments