Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElementTest.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.maybe;
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 MaybeFlatMapSingleElementTest extends RxJavaTest {
24 @Test
25 public void flatMapSingleValue() {
26 Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
27 @Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
28 if (integer == 1) {
29 return Single.just(2);
30 }
31
32 return Single.just(1);
33 }
34 })
35 .test()
36 .assertResult(2);
37 }
38
39 @Test
40 public void flatMapSingleValueDifferentType() {
41 Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<String>>() {
42 @Override public SingleSource<String> apply(final Integer integer) throws Exception {
43 if (integer == 1) {
44 return Single.just("2");
45 }
46
47 return Single.just("1");
48 }
49 })
50 .test()
51 .assertResult("2");
52 }
53
54 @Test
55 public void flatMapSingleValueNull() {
56 Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
57 @Override public SingleSource<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 SingleSource");
65 }
66
67 @Test
68 public void flatMapSingleValueErrorThrown() {
69 Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
70 @Override public SingleSource<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 flatMapSingleError() {
82 RuntimeException exception = new RuntimeException("test");
83
84 Maybe.error(exception).flatMapSingle(new Function<Object, SingleSource<Object>>() {
85 @Override public SingleSource<Object> apply(final Object integer) throws Exception {
86 return Single.just(new Object());
87 }
88 })
89 .test()
90 .assertError(exception);
91 }
92
93 @Test
94 public void flatMapSingleEmpty() {
95 Maybe.<Integer>empty().flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
96 @Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
97 return Single.just(2);
98 }
99 })
100 .test()
101 .assertNoValues()
102 .assertResult();
103 }
104
105 @Test
106 public void dispose() {
107 TestHelper.checkDisposed(Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
108 @Override
109 public SingleSource<Integer> apply(final Integer integer) throws Exception {
110 return Single.just(2);
111 }
112 }));
113 }
114
115 @Test
116 public void doubleOnSubscribe() {
117 TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Integer>, Maybe<Integer>>() {
118 @Override
119 public Maybe<Integer> apply(Maybe<Integer> m) throws Exception {
120 return m.flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
121 @Override
122 public SingleSource<Integer> apply(final Integer integer) throws Exception {
123 return Single.just(2);
124 }
125 });
126 }
127 });
128 }
129
130 @Test
131 public void singleErrors() {
132 Maybe.just(1)
133 .flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
134 @Override
135 public SingleSource<Integer> apply(final Integer integer) throws Exception {
136 return Single.error(new TestException());
137 }
138 })
139 .test()
140 .assertFailure(TestException.class);
141 }
142 }