rx.observers.TestObserver Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2014 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 rx.observers;
import java.util.*;
import rx.Notification;
import rx.Observer;
import rx.exceptions.CompositeException;
/**
* Observer usable for unit testing to perform assertions, inspect received events or wrap a mocked Observer.
* @param the observed value type
* @deprecated use the {@link TestSubscriber} instead.
*/
@Deprecated
public class TestObserver implements Observer {
private final Observer delegate;
private final List onNextEvents = new ArrayList();
private final List onErrorEvents = new ArrayList();
private final List> onCompletedEvents = new ArrayList>();
public TestObserver(Observer delegate) {
this.delegate = delegate;
}
@SuppressWarnings("unchecked")
public TestObserver() {
this.delegate = (Observer) INERT;
}
@Override
public void onCompleted() {
onCompletedEvents.add(Notification. createOnCompleted());
delegate.onCompleted();
}
/**
* 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> 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 getOnErrorEvents() {
return Collections.unmodifiableList(onErrorEvents);
}
@Override
public void onNext(T t) {
onNextEvents.add(t);
delegate.onNext(t);
}
/**
* 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 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