-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBiObserver.java
More file actions
164 lines (135 loc) · 5.71 KB
/
TestBiObserver.java
File metadata and controls
164 lines (135 loc) · 5.71 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package rx.observers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import rx.BiObserver;
import rx.Notification;
public class TestBiObserver<T0, T1> implements BiObserver<T0, T1> {
private final BiObserver<T0, T1> delegate;
private final ArrayList<TestEvent<T0, T1>> onNextEvents = new ArrayList<TestEvent<T0, T1>>();
private final ArrayList<Throwable> onErrorEvents = new ArrayList<Throwable>();
private final ArrayList<BiNotification<T0, T1>> onCompletedEvents = new ArrayList<BiNotification<T0, T1>>();
public TestBiObserver(BiObserver<T0, T1> delegate) {
this.delegate = delegate;
}
@SuppressWarnings("unchecked")
public TestBiObserver() {
this.delegate = (BiObserver<T0, T1>) INERT;
}
@Override
public void onComplete() {
onCompletedEvents.add(BiNotification.<T0, T1>createOnCompleted());
delegate.onComplete();
}
/**
* Get the {@link Notification}s representing each time this observer was notified of sequence
* completion via {@link #onCompleted}, as a {@link List}.
*
* @return a list of Notifications representing calls to this observer's {@link #onCompleted}
* method
*/
public List<BiNotification<T0, T1>> getOnCompletedEvents() {
return Collections.unmodifiableList(onCompletedEvents);
}
@Override
public void onError(Throwable e) {
onErrorEvents.add(e);
delegate.onError(e);
}
/**
* Get the {@link Throwable}s this observer was notified of via {@link #onError} as a
* {@link List}.
*
* @return a list of Throwables passed to this observer's {@link #onError} method
*/
public List<Throwable> getOnErrorEvents() {
return Collections.unmodifiableList(onErrorEvents);
}
@Override
public void onNext(T0 t0, T1 t1) {
onNextEvents.add(new TestEvent<T0, T1>(t0, t1));
delegate.onNext(t0, t1);
}
/**
* Get the sequence of items observed by this observer, as an ordered {@link List}.
*
* @return a list of items observed by this observer, in the order in which they were observed
*/
public List<TestEvent<T0, T1>> getOnNextEvents() {
return Collections.unmodifiableList(onNextEvents);
}
/**
* Get a list containing all of the items and notifications received by this observer, where the
* items will be given as-is, any error notifications will be represented by their
* {@code Throwable}s, and any sequence-complete notifications will be represented by their
* {@code Notification} objects.
*
* @return a {@link List} containing one item for each item or notification received by this
* observer, in the order in which they were observed or received
*/
public List<Object> getEvents() {
ArrayList<Object> events = new ArrayList<Object>();
events.add(onNextEvents);
events.add(onErrorEvents);
events.add(onCompletedEvents);
return Collections.unmodifiableList(events);
}
/**
* Assert that a particular sequence of items was received in order.
*
* @param items
* the sequence of items expected to have been observed
* @throws AssertionError
* if the sequence of items observed does not exactly match {@code items}
*/
public void assertReceivedOnNext(List<TestEvent<T0, T1>> items) {
if (onNextEvents.size() != items.size()) {
throw new AssertionError("Number of items does not match. Provided: " + items.size() + " Actual: " + onNextEvents.size());
}
for (int i = 0; i < items.size(); i++) {
final TestEvent<T0, T1> assertedTestEvent = items.get(i);
if (assertedTestEvent == null) {
// check for null equality
if (onNextEvents.get(i) != null) {
throw new AssertionError("Value at index: " + i + " expected to be [null] but was: [" + onNextEvents.get(i) + "]");
}
} else if (!assertedTestEvent.t0.equals(onNextEvents.get(i).t0) || !(assertedTestEvent.t1.equals(onNextEvents.get(i).t1))) {
throw new AssertionError("Value at index: " + i + " expected to be [" + assertedTestEvent + "] (" + assertedTestEvent.getClass().getSimpleName() + ") but was: [" + onNextEvents.get(i) + "] (" + onNextEvents
.get(i).getClass().getSimpleName() + ")");
}
}
}
/**
* Assert that a single terminal event occurred, either {@link #onCompleted} or {@link #onError}
* .
*
* @throws AssertionError
* if not exactly one terminal event notification was received
*/
public void assertTerminalEvent() {
if (onErrorEvents.size() > 1) {
throw new AssertionError("Too many onError events: " + onErrorEvents.size());
}
if (onCompletedEvents.size() > 1) {
throw new AssertionError("Too many onCompleted events: " + onCompletedEvents.size());
}
if (onCompletedEvents.size() == 1 && onErrorEvents.size() == 1) {
throw new AssertionError("Received both an onError and onCompleted. Should be one or the other.");
}
if (onCompletedEvents.size() == 0 && onErrorEvents.size() == 0) {
throw new AssertionError("No terminal events received.");
}
}
// do nothing ... including swallowing errors
private static BiObserver<Object, Object> INERT = new BiObserver<Object, Object>() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Object t0, Object t1) {
}
};
}