Repository navigation
Expand file tree
/
Copy pathObservableDistinctTest.java
More file actions
123 lines (103 loc) · 4.22 KB
/
Copy pathObservableDistinctTest.java
File metadata and controls
123 lines (103 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.internal.operators.observable;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import org.junit.*;
import org.mockito.InOrder;
import io.reactivex.*;
import io.reactivex.functions.Function;
public class ObservableDistinctTest {
Observer<String> w;
// nulls lead to exceptions
final Function<String, String> TO_UPPER_WITH_EXCEPTION = new Function<String, String>() {
@Override
public String apply(String s) {
if (s.equals("x")) {
return "XX";
}
return s.toUpperCase();
}
};
@Before
public void before() {
w = TestHelper.mockObserver();
}
@Test
public void testDistinctOfNone() {
Observable<String> src = Observable.empty();
src.distinct().subscribe(w);
verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));
verify(w, times(1)).onComplete();
}
@Test
public void testDistinctOfNoneWithKeySelector() {
Observable<String> src = Observable.empty();
src.distinct(TO_UPPER_WITH_EXCEPTION).subscribe(w);
verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));
verify(w, times(1)).onComplete();
}
@Test
public void testDistinctOfNormalSource() {
Observable<String> src = Observable.just("a", "b", "c", "c", "c", "b", "b", "a", "e");
src.distinct().subscribe(w);
InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("a");
inOrder.verify(w, times(1)).onNext("b");
inOrder.verify(w, times(1)).onNext("c");
inOrder.verify(w, times(1)).onNext("e");
inOrder.verify(w, times(1)).onComplete();
inOrder.verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));
}
@Test
public void testDistinctOfNormalSourceWithKeySelector() {
Observable<String> src = Observable.just("a", "B", "c", "C", "c", "B", "b", "a", "E");
src.distinct(TO_UPPER_WITH_EXCEPTION).subscribe(w);
InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("a");
inOrder.verify(w, times(1)).onNext("B");
inOrder.verify(w, times(1)).onNext("c");
inOrder.verify(w, times(1)).onNext("E");
inOrder.verify(w, times(1)).onComplete();
inOrder.verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));
}
@Test
@Ignore("Null values no longer allowed")
public void testDistinctOfSourceWithNulls() {
Observable<String> src = Observable.just(null, "a", "a", null, null, "b", null);
src.distinct().subscribe(w);
InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext(null);
inOrder.verify(w, times(1)).onNext("a");
inOrder.verify(w, times(1)).onNext("b");
inOrder.verify(w, times(1)).onComplete();
inOrder.verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));
}
@Test
@Ignore("Null values no longer allowed")
public void testDistinctOfSourceWithExceptionsFromKeySelector() {
Observable<String> src = Observable.just("a", "b", null, "c");
src.distinct(TO_UPPER_WITH_EXCEPTION).subscribe(w);
InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("a");
inOrder.verify(w, times(1)).onNext("b");
inOrder.verify(w, times(1)).onError(any(NullPointerException.class));
inOrder.verify(w, never()).onNext(anyString());
inOrder.verify(w, never()).onComplete();
}
}