Debian Codegraph @

src/test/java/io/reactivex/rxjava3/observers/ResourceSingleObserverTest.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.exceptions.TestException;
25 import io.reactivex.rxjava3.internal.util.EndConsumerHelper;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class ResourceSingleObserverTest extends RxJavaTest {
30 static final class TestResourceSingleObserver<T> extends ResourceSingleObserver<T> {
31 T value;
32
33 final List<Throwable> errors = new ArrayList<>();
34
35 int start;
36
37 @Override
38 protected void onStart() {
39 super.onStart();
40
41 start++;
42 }
43
44 @Override
45 public void onSuccess(final T value) {
46 this.value = value;
47
48 dispose();
49 }
50
51 @Override
52 public void onError(Throwable e) {
53 errors.add(e);
54
55 dispose();
56 }
57 }
58
59 @Test(expected = NullPointerException.class)
60 public void nullResource() {
61 TestResourceSingleObserver<Integer> rso = new TestResourceSingleObserver<>();
62 rso.add(null);
63 }
64
65 @Test
66 public void addResources() {
67 TestResourceSingleObserver<Integer> rso = new TestResourceSingleObserver<>();
68
69 assertFalse(rso.isDisposed());
70
71 Disposable d = Disposable.empty();
72
73 rso.add(d);
74
75 assertFalse(d.isDisposed());
76
77 rso.dispose();
78
79 assertTrue(rso.isDisposed());
80
81 assertTrue(d.isDisposed());
82
83 rso.dispose();
84
85 assertTrue(rso.isDisposed());
86
87 assertTrue(d.isDisposed());
88 }
89
90 @Test
91 public void onSuccessCleansUp() {
92 TestResourceSingleObserver<Integer> rso = new TestResourceSingleObserver<>();
93
94 assertFalse(rso.isDisposed());
95
96 Disposable d = Disposable.empty();
97
98 rso.add(d);
99
100 assertFalse(d.isDisposed());
101
102 rso.onSuccess(1);
103
104 assertTrue(rso.isDisposed());
105
106 assertTrue(d.isDisposed());
107 }
108
109 @Test
110 public void onErrorCleansUp() {
111 TestResourceSingleObserver<Integer> rso = new TestResourceSingleObserver<>();
112
113 assertFalse(rso.isDisposed());
114
115 Disposable d = Disposable.empty();
116
117 rso.add(d);
118
119 assertFalse(d.isDisposed());
120
121 rso.onError(new TestException());
122
123 assertTrue(rso.isDisposed());
124
125 assertTrue(d.isDisposed());
126 }
127
128 @Test
129 public void normal() {
130 TestResourceSingleObserver<Integer> rso = new TestResourceSingleObserver<>();
131
132 assertFalse(rso.isDisposed());
133 assertEquals(0, rso.start);
134 assertNull(rso.value);
135 assertTrue(rso.errors.isEmpty());
136
137 Single.just(1).subscribe(rso);
138
139 assertTrue(rso.isDisposed());
140 assertEquals(1, rso.start);
141 assertEquals(Integer.valueOf(1), rso.value);
142 assertTrue(rso.errors.isEmpty());
143 }
144
145 @Test
146 public void error() {
147 TestResourceSingleObserver<Integer> rso = new TestResourceSingleObserver<>();
148
149 assertFalse(rso.isDisposed());
150 assertEquals(0, rso.start);
151 assertNull(rso.value);
152 assertTrue(rso.errors.isEmpty());
153
154 final RuntimeException error = new RuntimeException("error");
155 Single.<Integer>error(error).subscribe(rso);
156
157 assertTrue(rso.isDisposed());
158 assertEquals(1, rso.start);
159 assertNull(rso.value);
160 assertEquals(1, rso.errors.size());
161 assertTrue(rso.errors.contains(error));
162 }
163
164 @Test
165 public void startOnce() {
166
167 List<Throwable> error = TestHelper.trackPluginErrors();
168
169 try {
170 TestResourceSingleObserver<Integer> rso = new TestResourceSingleObserver<>();
171
172 rso.onSubscribe(Disposable.empty());
173
174 Disposable d = Disposable.empty();
175
176 rso.onSubscribe(d);
177
178 assertTrue(d.isDisposed());
179
180 assertEquals(1, rso.start);
181
182 TestHelper.assertError(error, 0, IllegalStateException.class, EndConsumerHelper.composeMessage(rso.getClass().getName()));
183 } finally {
184 RxJavaPlugins.reset();
185 }
186 }
187
188 @Test
189 public void dispose() {
190 TestResourceSingleObserver<Integer> rso = new TestResourceSingleObserver<>();
191 rso.dispose();
192
193 Disposable d = Disposable.empty();
194
195 rso.onSubscribe(d);
196
197 assertTrue(d.isDisposed());
198
199 assertEquals(0, rso.start);
200 }
201 }