|
| 1 | +/** |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.operators; |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | +import org.mockito.Mockito; |
| 20 | + |
| 21 | +import rx.Observable; |
| 22 | +import rx.Observer; |
| 23 | +import rx.Subscription; |
| 24 | +import rx.util.functions.Func1; |
| 25 | + |
| 26 | +import static org.mockito.Matchers.any; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.times; |
| 29 | +import static org.mockito.Mockito.verify; |
| 30 | + |
| 31 | +public final class OperationWhere { |
| 32 | + |
| 33 | + public static <T> Func1<Observer<T>, Subscription> where(Observable<T> that, Func1<T, Boolean> predicate) { |
| 34 | + return OperationFilter.filter(that, predicate); |
| 35 | + } |
| 36 | + |
| 37 | + public static class UnitTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testWhere() { |
| 41 | + Observable<String> w = Observable.toObservable("one", "two", "three"); |
| 42 | + Observable<String> observable = Observable.create(where(w, new Func1<String, Boolean>() { |
| 43 | + |
| 44 | + @Override |
| 45 | + public Boolean call(String t1) { |
| 46 | + return t1.equals("two"); |
| 47 | + } |
| 48 | + })); |
| 49 | + |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + Observer<String> aObserver = mock(Observer.class); |
| 52 | + observable.subscribe(aObserver); |
| 53 | + verify(aObserver, Mockito.never()).onNext("one"); |
| 54 | + verify(aObserver, times(1)).onNext("two"); |
| 55 | + verify(aObserver, Mockito.never()).onNext("three"); |
| 56 | + verify(aObserver, Mockito.never()).onError(any(Exception.class)); |
| 57 | + verify(aObserver, times(1)).onCompleted(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments