Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableSwitchIfEmptyTest.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.observable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.Arrays;
19 import java.util.concurrent.atomic.AtomicBoolean;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.disposables.*;
25 import io.reactivex.rxjava3.functions.Consumer;
26 import io.reactivex.rxjava3.observers.DefaultObserver;
27
28 public class ObservableSwitchIfEmptyTest extends RxJavaTest {
29
30 @Test
31 public void switchWhenNotEmpty() throws Exception {
32 final AtomicBoolean subscribed = new AtomicBoolean(false);
33 final Observable<Integer> o = Observable.just(4)
34 .switchIfEmpty(Observable.just(2)
35 .doOnSubscribe(new Consumer<Disposable>() {
36 @Override
37 public void accept(Disposable d) {
38 subscribed.set(true);
39 }
40 }));
41
42 assertEquals(4, o.blockingSingle().intValue());
43 assertFalse(subscribed.get());
44 }
45
46 @Test
47 public void switchWhenEmpty() throws Exception {
48 final Observable<Integer> o = Observable.<Integer>empty()
49 .switchIfEmpty(Observable.fromIterable(Arrays.asList(42)));
50
51 assertEquals(42, o.blockingSingle().intValue());
52 }
53
54 @Test
55 public void switchTriggerUnsubscribe() throws Exception {
56
57 final Disposable d = Disposable.empty();
58
59 Observable<Long> withProducer = Observable.unsafeCreate(new ObservableSource<Long>() {
60 @Override
61 public void subscribe(final Observer<? super Long> observer) {
62 observer.onSubscribe(d);
63 observer.onNext(42L);
64 }
65 });
66
67 Observable.<Long>empty()
68 .switchIfEmpty(withProducer)
69 .lift(new ObservableOperator<Long, Long>() {
70 @Override
71 public Observer<? super Long> apply(final Observer<? super Long> child) {
72 return new DefaultObserver<Long>() {
73 @Override
74 public void onComplete() {
75
76 }
77
78 @Override
79 public void onError(Throwable e) {
80
81 }
82
83 @Override
84 public void onNext(Long aLong) {
85 cancel();
86 }
87
88 };
89 }
90 }).subscribe();
91
92 assertTrue(d.isDisposed());
93 // FIXME no longer assertable
94 // assertTrue(sub.isUnsubscribed());
95 }
96
97 @Test
98 public void switchShouldTriggerUnsubscribe() {
99 final Disposable d = Disposable.empty();
100
101 Observable.unsafeCreate(new ObservableSource<Long>() {
102 @Override
103 public void subscribe(final Observer<? super Long> observer) {
104 observer.onSubscribe(d);
105 observer.onComplete();
106 }
107 }).switchIfEmpty(Observable.<Long>never()).subscribe();
108 assertTrue(d.isDisposed());
109 }
110 }