/** * Copyright (c) 2016-present, RxJava Contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See * the License for the specific language governing permissions and limitations under the License. */ package io.reactivex; import io.reactivex.exceptions.TestException; import org.junit.Test; import org.reactivestreams.Publisher; import static org.junit.Assert.*; public class TransformerTest { @Test public void flowableTransformerThrows() { try { Flowable.just(1).compose(new FlowableTransformer() { @Override public Publisher apply(Flowable v) { throw new TestException("Forced failure"); } }); fail("Should have thrown!"); } catch (TestException ex) { assertEquals("Forced failure", ex.getMessage()); } } @Test public void observableTransformerThrows() { try { Observable.just(1).compose(new ObservableTransformer() { @Override public Observable apply(Observable v) { throw new TestException("Forced failure"); } }); fail("Should have thrown!"); } catch (TestException ex) { assertEquals("Forced failure", ex.getMessage()); } } @Test public void singleTransformerThrows() { try { Single.just(1).compose(new SingleTransformer() { @Override public Single apply(Single v) { throw new TestException("Forced failure"); } }); fail("Should have thrown!"); } catch (TestException ex) { assertEquals("Forced failure", ex.getMessage()); } } @Test public void maybeTransformerThrows() { try { Maybe.just(1).compose(new MaybeTransformer() { @Override public Maybe apply(Maybe v) { throw new TestException("Forced failure"); } }); fail("Should have thrown!"); } catch (TestException ex) { assertEquals("Forced failure", ex.getMessage()); } } @Test public void completableTransformerThrows() { try { Completable.complete().compose(new CompletableTransformer() { @Override public Completable apply(Completable v) { throw new TestException("Forced failure"); } }); fail("Should have thrown!"); } catch (TestException ex) { assertEquals("Forced failure", ex.getMessage()); } } // Test demos for signature generics in compose() methods. Just needs to compile. @Test public void observableGenericsSignatureTest() { A a = new A() { }; Observable.just(a).compose(TransformerTest.testObservableTransformerCreator()); } @Test public void singleGenericsSignatureTest() { A a = new A() { }; Single.just(a).compose(TransformerTest.testSingleTransformerCreator()); } @Test public void maybeGenericsSignatureTest() { A a = new A() { }; Maybe.just(a).compose(TransformerTest.testMaybeTransformerCreator()); } @Test public void flowableGenericsSignatureTest() { A a = new A() { }; Flowable.just(a).compose(TransformerTest.testFlowableTransformerCreator()); } interface A { } interface B { } private static ObservableTransformer, B> testObservableTransformerCreator() { return new ObservableTransformer, B>() { @Override public ObservableSource> apply(Observable> a) { return Observable.empty(); } }; } private static SingleTransformer, B> testSingleTransformerCreator() { return new SingleTransformer, B>() { @Override public SingleSource> apply(Single> a) { return Single.never(); } }; } private static MaybeTransformer, B> testMaybeTransformerCreator() { return new MaybeTransformer, B>() { @Override public MaybeSource> apply(Maybe> a) { return Maybe.empty(); } }; } private static FlowableTransformer, B> testFlowableTransformerCreator() { return new FlowableTransformer, B>() { @Override public Publisher> apply(Flowable> a) { return Flowable.empty(); } }; } }