| 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.internal.operators.single; |
| 15 |
|
| 16 |
import org.junit.Test; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 20 |
import io.reactivex.rxjava3.functions.Function; |
| 21 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 22 |
|
| 23 |
public class SingleConcatMapTest extends RxJavaTest { |
| 24 |
|
| 25 |
@Test |
| 26 |
public void concatMapValue() { |
| 27 |
Single.just(1).concatMap(new Function<Integer, SingleSource<Integer>>() { |
| 28 |
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception { |
| 29 |
if (integer == 1) { |
| 30 |
return Single.just(2); |
| 31 |
} |
| 32 |
|
| 33 |
return Single.just(1); |
| 34 |
} |
| 35 |
}) |
| 36 |
.test() |
| 37 |
.assertResult(2); |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void concatMapValueDifferentType() { |
| 42 |
Single.just(1).concatMap(new Function<Integer, SingleSource<String>>() { |
| 43 |
@Override public SingleSource<String> apply(final Integer integer) throws Exception { |
| 44 |
if (integer == 1) { |
| 45 |
return Single.just("2"); |
| 46 |
} |
| 47 |
|
| 48 |
return Single.just("1"); |
| 49 |
} |
| 50 |
}) |
| 51 |
.test() |
| 52 |
.assertResult("2"); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void concatMapValueNull() { |
| 57 |
Single.just(1).concatMap(new Function<Integer, SingleSource<Integer>>() { |
| 58 |
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception { |
| 59 |
return null; |
| 60 |
} |
| 61 |
}) |
| 62 |
.to(TestHelper.<Integer>testConsumer()) |
| 63 |
.assertNoValues() |
| 64 |
.assertError(NullPointerException.class) |
| 65 |
.assertErrorMessage("The single returned by the mapper is null"); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void concatMapValueErrorThrown() { |
| 70 |
Single.just(1).concatMap(new Function<Integer, SingleSource<Integer>>() { |
| 71 |
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception { |
| 72 |
throw new RuntimeException("something went terribly wrong!"); |
| 73 |
} |
| 74 |
}) |
| 75 |
.to(TestHelper.<Integer>testConsumer()) |
| 76 |
.assertNoValues() |
| 77 |
.assertError(RuntimeException.class) |
| 78 |
.assertErrorMessage("something went terribly wrong!"); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void concatMapError() { |
| 83 |
RuntimeException exception = new RuntimeException("test"); |
| 84 |
|
| 85 |
Single.error(exception).concatMap(new Function<Object, SingleSource<Object>>() { |
| 86 |
@Override public SingleSource<Object> apply(final Object integer) throws Exception { |
| 87 |
return Single.just(new Object()); |
| 88 |
} |
| 89 |
}) |
| 90 |
.test() |
| 91 |
.assertError(exception); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void dispose() { |
| 96 |
TestHelper.checkDisposed(Single.just(1).concatMap(new Function<Integer, SingleSource<Integer>>() { |
| 97 |
@Override |
| 98 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 99 |
return Single.just(2); |
| 100 |
} |
| 101 |
})); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void mappedSingleOnError() { |
| 106 |
Single.just(1).concatMap(new Function<Integer, SingleSource<Integer>>() { |
| 107 |
@Override |
| 108 |
public SingleSource<Integer> apply(Integer v) throws Exception { |
| 109 |
return Single.error(new TestException()); |
| 110 |
} |
| 111 |
}) |
| 112 |
.test() |
| 113 |
.assertFailure(TestException.class); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void doubleOnSubscribe() { |
| 118 |
TestHelper.checkDoubleOnSubscribeSingle(new Function<Single<Object>, SingleSource<Object>>() { |
| 119 |
@Override |
| 120 |
public SingleSource<Object> apply(Single<Object> s) |
| 121 |
throws Exception { |
| 122 |
return s.concatMap(new Function<Object, SingleSource<? extends Object>>() { |
| 123 |
@Override |
| 124 |
public SingleSource<? extends Object> apply(Object v) |
| 125 |
throws Exception { |
| 126 |
return Single.just(v); |
| 127 |
} |
| 128 |
}); |
| 129 |
} |
| 130 |
}); |
| 131 |
} |
| 132 |
} |