|
| 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.internal.operators.single; |
| 15 | + |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import io.reactivex.Single; |
| 19 | +import io.reactivex.SingleSource; |
| 20 | +import io.reactivex.functions.Function; |
| 21 | + |
| 22 | +public class SingleMapTest { |
| 23 | + |
| 24 | + @Test(expected = NullPointerException.class) |
| 25 | + public void mapNull() { |
| 26 | + Single.just(1).map(null); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void mapValue() { |
| 31 | + Single.just(1).map(new Function<Integer, Integer>() { |
| 32 | + @Override |
| 33 | + public Integer apply(final Integer integer) throws Exception { |
| 34 | + if (integer == 1) { |
| 35 | + return 2; |
| 36 | + } |
| 37 | + |
| 38 | + return 1; |
| 39 | + } |
| 40 | + }) |
| 41 | + .test() |
| 42 | + .assertResult(2); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void mapValueNull() { |
| 47 | + Single.just(1).map(new Function<Integer, SingleSource<Integer>>() { |
| 48 | + @Override |
| 49 | + public SingleSource<Integer> apply(final Integer integer) throws Exception { |
| 50 | + return null; |
| 51 | + } |
| 52 | + }) |
| 53 | + .test() |
| 54 | + .assertNoValues() |
| 55 | + .assertError(NullPointerException.class) |
| 56 | + .assertErrorMessage("The mapper function returned a null value."); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void mapValueErrorThrown() { |
| 61 | + Single.just(1).map(new Function<Integer, SingleSource<Integer>>() { |
| 62 | + @Override |
| 63 | + public SingleSource<Integer> apply(final Integer integer) throws Exception { |
| 64 | + throw new RuntimeException("something went terribly wrong!"); |
| 65 | + } |
| 66 | + }) |
| 67 | + .test() |
| 68 | + .assertNoValues() |
| 69 | + .assertError(RuntimeException.class) |
| 70 | + .assertErrorMessage("something went terribly wrong!"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void mapError() { |
| 75 | + RuntimeException exception = new RuntimeException("test"); |
| 76 | + |
| 77 | + Single.error(exception).map(new Function<Object, Object>() { |
| 78 | + @Override |
| 79 | + public Object apply(final Object integer) throws Exception { |
| 80 | + return new Object(); |
| 81 | + } |
| 82 | + }) |
| 83 | + .test() |
| 84 | + .assertError(exception); |
| 85 | + } |
| 86 | +} |
0 commit comments