Debian Codegraph @

src/test/java/io/reactivex/rxjava3/observers/DisposableSingleObserverTest.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 DisposableSingleObserverTest extends RxJavaTest {
29
30 static final class TestSingle<T> extends DisposableSingleObserver<T> {
31
32 int start;
33
34 final List<T> values = new ArrayList<>();
35
36 final List<Throwable> errors = new ArrayList<>();
37
38 @Override
39 protected void onStart() {
40 super.onStart();
41
42 start++;
43 }
44
45 @Override
46 public void onSuccess(T value) {
47 values.add(value);
48 }
49
50 @Override
51 public void onError(Throwable e) {
52 errors.add(e);
53 }
54
55 }
56
57 @Test
58 public void normal() {
59 TestSingle<Integer> tc = new TestSingle<>();
60
61 assertFalse(tc.isDisposed());
62 assertEquals(0, tc.start);
63 assertTrue(tc.values.isEmpty());
64 assertTrue(tc.errors.isEmpty());
65
66 Single.just(1).subscribe(tc);
67
68 assertFalse(tc.isDisposed());
69 assertEquals(1, tc.start);
70 assertEquals(1, tc.values.get(0).intValue());
71 assertTrue(tc.errors.isEmpty());
72 }
73
74 @Test
75 public void startOnce() {
76
77 List<Throwable> error = TestHelper.trackPluginErrors();
78
79 try {
80 TestSingle<Integer> tc = new TestSingle<>();
81
82 tc.onSubscribe(Disposable.empty());
83
84 Disposable d = Disposable.empty();
85
86 tc.onSubscribe(d);
87
88 assertTrue(d.isDisposed());
89
90 assertEquals(1, tc.start);
91
92 TestHelper.assertError(error, 0, IllegalStateException.class, EndConsumerHelper.composeMessage(tc.getClass().getName()));
93 } finally {
94 RxJavaPlugins.reset();
95 }
96 }
97
98 @Test
99 public void dispose() {
100 TestSingle<Integer> tc = new TestSingle<>();
101 tc.dispose();
102
103 assertTrue(tc.isDisposed());
104
105 Disposable d = Disposable.empty();
106
107 tc.onSubscribe(d);
108
109 assertTrue(d.isDisposed());
110
111 assertEquals(0, tc.start);
112 }
113 }