OperationDistinct { public static <T, U> OnSubscribeFunc<T> distinct(Observable<? extends T> source, Func1<? super T, ? extends U> keySelector) { return new Distinct<T, U>(source, keySelector); }  static OnSubscribeFunc<T> distinct(Observable<? extends T> source, Func1<? super T, ? extends U> keySelector); static OnSubscribeFunc<T> distinct(Observable<? extends T> source, Comparator<T> equalityComparator); static OnSubscribeFunc<T> distinct(Observable<? extends T> source, Func1<? super T, ? extends U> keySelector, Comparator<U> equalityComparator); static OnSubscribeFunc<T> distinct(Observable<? extends T> source);  }
@Test public void testDistinctOfNone() { Observable<String> src = Observable.empty(); Observable.create(distinct(src)).subscribe(w); verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); verify(w, times(1)).onCompleted(); }
@Test public void testDistinctOfNoneWithKeySelector() { Observable<String> src = Observable.empty(); Observable.create(distinct(src, TO_UPPER_WITH_EXCEPTION)).subscribe(w); verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); verify(w, times(1)).onCompleted(); }
@Test public void testDistinctOfNormalSource() { Observable<String> src = Observable.from("a", "b", "c", "c", "c", "b", "b", "a", "e"); Observable.create(distinct(src)).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)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctOfNormalSourceWithKeySelector() { Observable<String> src = Observable.from("a", "B", "c", "C", "c", "B", "b", "a", "E"); Observable.create(distinct(src, 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)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctOfNormalSourceWithComparator() { Observable<String> src = Observable.from("1", "12", "123", "aaa", "321", "12", "21", "1", "12345"); Observable.create(distinct(src, COMPARE_LENGTH)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("1"); inOrder.verify(w, times(1)).onNext("12"); inOrder.verify(w, times(1)).onNext("123"); inOrder.verify(w, times(1)).onNext("12345"); inOrder.verify(w, times(1)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctOfNormalSourceWithKeySelectorAndComparator() { Observable<String> src = Observable.from("a", "x", "ab", "abc", "cba", "de", "x", "a", "abcd"); Observable.create(distinct(src, TO_UPPER_WITH_EXCEPTION, COMPARE_LENGTH)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("x"); inOrder.verify(w, times(1)).onNext("abc"); inOrder.verify(w, times(1)).onNext("abcd"); inOrder.verify(w, times(1)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctOfNormalSourceWithKeySelectorAndComparatorAndTwoSubscriptions() { Observable<String> src = Observable.from("a", "x", "ab", "abc", "cba", "de", "x", "a", "abcd"); Observable.create(distinct(src, TO_UPPER_WITH_EXCEPTION, COMPARE_LENGTH)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("x"); Observable.create(distinct(src, TO_UPPER_WITH_EXCEPTION, COMPARE_LENGTH)).subscribe(w2); inOrder.verify(w, times(1)).onNext("abc"); inOrder.verify(w, times(1)).onNext("abcd"); inOrder.verify(w, times(1)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); InOrder inOrder2 = inOrder(w2); inOrder2.verify(w2, times(1)).onNext("a"); inOrder2.verify(w2, times(1)).onNext("x"); inOrder2.verify(w2, times(1)).onNext("abc"); inOrder2.verify(w2, times(1)).onNext("abcd"); inOrder2.verify(w2, times(1)).onCompleted(); inOrder2.verify(w2, never()).onNext(anyString()); verify(w2, never()).onError(any(Throwable.class)); }
@Test public void testDistinctOfSourceWithNulls() { Observable<String> src = Observable.from(null, "a", "a", null, null, "b", null); Observable.create(distinct(src)).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)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctOfSourceWithExceptionsFromKeySelector() { Observable<String> src = Observable.from("a", "b", null, "c"); Observable.create(distinct(src, 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()).onCompleted(); }