Debian Codegraph @

src/test/java/io/reactivex/rxjava3/observers/DisposableMaybeObserverTest.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.observers;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.disposables.*;
24 import io.reactivex.rxjava3.internal.util.EndConsumerHelper;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26 import io.reactivex.rxjava3.testsupport.TestHelper;
27
28 public class DisposableMaybeObserverTest extends RxJavaTest {
29
30 static final class TestMaybe<T> extends DisposableMaybeObserver<T> {
31
32 int start;
33
34 final List<T> values = new ArrayList<>();
35
36 final List<Throwable> errors = new ArrayList<>();
37
38 int complete;
39
40 @Override
41 protected void onStart() {
42 super.onStart();
43
44 start++;
45 }
46
47 @Override
48 public void onSuccess(T value) {
49 values.add(value);
50 }
51
52 @Override
53 public void onError(Throwable e) {
54 errors.add(e);
55 }
56
57 @Override
58 public void onComplete() {
59 complete++;
60 }
61 }
62
63 @Test
64 public void normal() {
65 TestMaybe<Integer> tc = new TestMaybe<>();
66
67 assertFalse(tc.isDisposed());
68 assertEquals(0, tc.start);
69 assertTrue(tc.values.isEmpty());
70 assertTrue(tc.errors.isEmpty());
71 assertEquals(0, tc.complete);
72
73 Maybe.just(1).subscribe(tc);
74
75 assertFalse(tc.isDisposed());
76 assertEquals(1, tc.start);
77 assertEquals(1, tc.values.get(0).intValue());
78 assertTrue(tc.errors.isEmpty());
79 assertEquals(0, tc.complete);
80 }
81
82 @Test
83 public void startOnce() {
84
85 List<Throwable> error = TestHelper.trackPluginErrors();
86
87 try {
88 TestMaybe<Integer> tc = new TestMaybe<>();
89
90 tc.onSubscribe(Disposable.empty());
91
92 Disposable d = Disposable.empty();
93
94 tc.onSubscribe(d);
95
96 assertTrue(d.isDisposed());
97
98 assertEquals(1, tc.start);
99
100 TestHelper.assertError(error, 0, IllegalStateException.class, EndConsumerHelper.composeMessage(tc.getClass().getName()));
101 } finally {
102 RxJavaPlugins.reset();
103 }
104 }
105
106 @Test
107 public void dispose() {
108 TestMaybe<Integer> tc = new TestMaybe<>();
109 tc.dispose();
110
111 assertTrue(tc.isDisposed());
112
113 Disposable d = Disposable.empty();
114
115 tc.onSubscribe(d);
116
117 assertTrue(d.isDisposed());
118
119 assertEquals(0, tc.start);
120 }
121 }