Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleFlatMapMaybeTest.java

raw
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 SingleFlatMapMaybeTest extends RxJavaTest {
24 @Test
25 public void flatMapMaybeValue() {
26 Single.just(1).flatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
27 @Override public MaybeSource<Integer> apply(final Integer integer) throws Exception {
28 if (integer == 1) {
29 return Maybe.just(2);
30 }
31
32 return Maybe.just(1);
33 }
34 })
35 .test()
36 .assertResult(2);
37 }
38
39 @Test
40 public void flatMapMaybeValueDifferentType() {
41 Single.just(1).flatMapMaybe(new Function<Integer, MaybeSource<String>>() {
42 @Override public MaybeSource<String> apply(final Integer integer) throws Exception {
43 if (integer == 1) {
44 return Maybe.just("2");
45 }
46
47 return Maybe.just("1");
48 }
49 })
50 .test()
51 .assertResult("2");
52 }
53
54 @Test
55 public void flatMapMaybeValueNull() {
56 Single.just(1).flatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
57 @Override public MaybeSource<Integer> apply(final Integer integer) throws Exception {
58 return null;
59 }
60 })
61 .to(TestHelper.<Integer>testConsumer())
62 .assertNoValues()
63 .assertError(NullPointerException.class)
64 .assertErrorMessage("The mapper returned a null MaybeSource");
65 }
66
67 @Test
68 public void flatMapMaybeValueErrorThrown() {
69 Single.just(1).flatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
70 @Override public MaybeSource<Integer> apply(final Integer integer) throws Exception {
71 throw new RuntimeException("something went terribly wrong!");
72 }
73 })
74 .to(TestHelper.<Integer>testConsumer())
75 .assertNoValues()
76 .assertError(RuntimeException.class)
77 .assertErrorMessage("something went terribly wrong!");
78 }
79
80 @Test
81 public void flatMapMaybeError() {
82 RuntimeException exception = new RuntimeException("test");
83
84 Single.error(exception).flatMapMaybe(new Function<Object, MaybeSource<Object>>() {
85 @Override public MaybeSource<Object> apply(final Object integer) throws Exception {
86 return Maybe.just(new Object());
87 }
88 })
89 .test()
90 .assertError(exception);
91 }
92
93 @Test
94 public void dispose() {
95 TestHelper.checkDisposed(Single.just(1).flatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
96 @Override
97 public MaybeSource<Integer> apply(Integer v) throws Exception {
98 return Maybe.just(1);
99 }
100 }));
101 }
102
103 @Test
104 public void doubleOnSubscribe() {
105 TestHelper.checkDoubleOnSubscribeSingleToMaybe(new Function<Single<Integer>, MaybeSource<Integer>>() {
106 @Override
107 public MaybeSource<Integer> apply(Single<Integer> v) throws Exception {
108 return v.flatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
109 @Override
110 public MaybeSource<Integer> apply(Integer v) throws Exception {
111 return Maybe.just(1);
112 }
113 });
114 }
115 });
116 }
117
118 @Test
119 public void mapsToError() {
120 Single.just(1).flatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
121 @Override
122 public MaybeSource<Integer> apply(Integer v) throws Exception {
123 return Maybe.error(new TestException());
124 }
125 })
126 .test()
127 .assertFailure(TestException.class);
128 }
129
130 @Test
131 public void mapsToEmpty() {
132 Single.just(1).flatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
133 @Override
134 public MaybeSource<Integer> apply(Integer v) throws Exception {
135 return Maybe.empty();
136 }
137 })
138 .test()
139 .assertResult();
140 }
141 }