Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/jdk8/MaybeMapOptionalTest.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.jdk8;
15
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18
19 import java.util.Optional;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.Function;
26 import io.reactivex.rxjava3.testsupport.TestHelper;
27
28 public class MaybeMapOptionalTest extends RxJavaTest {
29
30 @Test
31 public void successSuccess() {
32 Maybe.just(1)
33 .mapOptional(Optional::of)
34 .test()
35 .assertResult(1);
36 }
37
38 @Test
39 public void successEmpty() {
40 Maybe.just(1)
41 .mapOptional(v -> Optional.empty())
42 .test()
43 .assertResult();
44 }
45
46 @Test
47 public void empty() throws Throwable {
48 @SuppressWarnings("unchecked")
49 Function<? super Integer, Optional<? extends Integer>> f = mock(Function.class);
50
51 Maybe.<Integer>empty()
52 .mapOptional(f)
53 .test()
54 .assertResult();
55
56 verify(f, never()).apply(any());
57 }
58
59 @Test
60 public void error() throws Throwable {
61 @SuppressWarnings("unchecked")
62 Function<? super Integer, Optional<? extends Integer>> f = mock(Function.class);
63
64 Maybe.<Integer>error(new TestException())
65 .mapOptional(f)
66 .test()
67 .assertFailure(TestException.class);
68
69 verify(f, never()).apply(any());
70 }
71
72 @Test
73 public void mapperCrash() {
74 Maybe.just(1)
75 .mapOptional(v -> { throw new TestException(); })
76 .test()
77 .assertFailure(TestException.class);
78 }
79
80 @Test
81 public void dispose() {
82 TestHelper.checkDisposed(Maybe.never().mapOptional(Optional::of));
83 }
84
85 @Test
86 public void doubleOnSubscribe() {
87 TestHelper.checkDoubleOnSubscribeMaybe(m -> m.mapOptional(Optional::of));
88 }
89 }