diff --git a/language-adaptors/rxjava-clojure/src/test/clojure/rx/lang/clojure/blocking_test.clj b/language-adaptors/rxjava-clojure/src/test/clojure/rx/lang/clojure/blocking_test.clj index df8e9fae1e..260e6a3dc5 100644 --- a/language-adaptors/rxjava-clojure/src/test/clojure/rx/lang/clojure/blocking_test.clj +++ b/language-adaptors/rxjava-clojure/src/test/clojure/rx/lang/clojure/blocking_test.clj @@ -36,7 +36,7 @@ (testing "returns one element" (is (= 1 (b/single (rx/return 1))))) (testing "throw if empty" - (is (thrown? java.lang.IllegalArgumentException (b/single (rx/empty))))) + (is (thrown? java.util.NoSuchElementException (b/single (rx/empty))))) (testing "throw if many" (is (thrown? java.lang.IllegalArgumentException (b/single (rx/seq->o [1 2]))))) (testing "rethrows errors" diff --git a/language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala b/language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala index e09e6a7ec9..4ef20263a3 100644 --- a/language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala +++ b/language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala @@ -388,6 +388,18 @@ class RxScalaDemo extends JUnitSuite { assertEquals(10, List(-1, 0, 1).toObservable.filter(condition).firstOrElse(10).toBlockingObservable.single) } + @Test def firstLastSingleExample() { + assertEquals(1, List(1, 2, 3, 4).toObservable.head.toBlockingObservable.single) + assertEquals(1, List(1, 2, 3, 4).toObservable.first.toBlockingObservable.single) + assertEquals(4, List(1, 2, 3, 4).toObservable.last.toBlockingObservable.single) + assertEquals(1, List(1).toObservable.single.toBlockingObservable.single) + + assertEquals(1, List(1, 2, 3, 4).toObservable.toBlockingObservable.head) + assertEquals(1, List(1, 2, 3, 4).toObservable.toBlockingObservable.first) + assertEquals(4, List(1, 2, 3, 4).toObservable.toBlockingObservable.last) + assertEquals(1, List(1).toObservable.toBlockingObservable.single) + } + def square(x: Int): Int = { println(s"$x*$x is being calculated on thread ${Thread.currentThread().getId}") Thread.sleep(100) // calculating a square is heavy work :) diff --git a/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala b/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala index 8dafa30e2b..1d17b3ebee 100644 --- a/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala +++ b/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala @@ -1949,36 +1949,64 @@ trait Observable[+T] def headOrElse[U >: T](default: => U): Observable[U] = firstOrElse(default) /** - * Returns an Observable that emits only the very first item emitted by the source Observable. - * This is just a shorthand for `take(1)`. - * + * Returns an Observable that emits only the very first item emitted by the source Observable, or raises an + * `NoSuchElementException` if the source Observable is empty. + *

* - * - * @return an Observable that emits only the very first item from the source, or none if the - * source Observable completes without emitting a single item. + * + * @return an Observable that emits only the very first item emitted by the source Observable, or raises an + * `NoSuchElementException` if the source Observable is empty + * @see RxJava Wiki: first() + * @see "MSDN: Observable.firstAsync()" */ - def first: Observable[T] = take(1) + def first: Observable[T] = { + toScalaObservable[T](asJavaObservable.first) + } - /* - - TODO once https://github.com/Netflix/RxJava/issues/417 is fixed, we can add head and tail methods + /** + * Returns an Observable that emits only the very first item emitted by the source Observable, or raises an + * `NoSuchElementException` if the source Observable is empty. + *

+ * + * + * @return an Observable that emits only the very first item emitted by the source Observable, or raises an + * `NoSuchElementException` if the source Observable is empty + * @see RxJava Wiki: first() + * @see "MSDN: Observable.firstAsync()" + * @see [[Observable.first]] + */ + def head: Observable[T] = first /** - * emits NoSuchElementException("head of empty Observable") if empty + * Returns an Observable that emits the last item emitted by the source Observable or notifies observers of + * an `NoSuchElementException` if the source Observable is empty. + * + * + * + * @return an Observable that emits the last item from the source Observable or notifies observers of an + * error + * @see RxJava Wiki: last() + * @see "MSDN: Observable.lastAsync()" */ - def head: Observable[T] = { - this.take(1).fold[Option[T]](None)((v: Option[T], e: T) => Some(e)).map({ - case Some(element) => element - case None => throw new NoSuchElementException("head of empty Observable") - }) + def last: Observable[T] = { + toScalaObservable[T](asJavaObservable.last) } - + /** - * emits an UnsupportedOperationException("tail of empty list") if empty + * If the source Observable completes after emitting a single item, return an Observable that emits that + * item. If the source Observable emits more than one item or no items, throw an `NoSuchElementException`. + * + * + * + * @return an Observable that emits the single item emitted by the source Observable + * @throws NoSuchElementException + * if the source emits more than one item or no items + * @see RxJava Wiki: single() + * @see "MSDN: Observable.singleAsync()" */ - def tail: Observable[T] = ??? - - */ + def single: Observable[T] = { + toScalaObservable[T](asJavaObservable.single) + } /** * Returns an Observable that forwards all sequentially distinct items emitted from the source Observable. diff --git a/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/observables/BlockingObservable.scala b/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/observables/BlockingObservable.scala index c1ce913095..4ae6a54ce7 100644 --- a/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/observables/BlockingObservable.scala +++ b/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/observables/BlockingObservable.scala @@ -53,6 +53,49 @@ class BlockingObservable[+T] private[scala] (val asJava: rx.observables.Blocking new WithFilter[T](p, asJava) } + /** + * Returns the last item emitted by a specified [[Observable]], or + * throws `NoSuchElementException` if it emits no items. + * + * + * + * @return the last item emitted by the source [[Observable]] + * @throws NoSuchElementException + * if source contains no elements + * @see RxJava Wiki: last() + * @see MSDN: Observable.Last + */ + def last : T = { + asJava.last : T + } + + /** + * Returns the first item emitted by a specified [[Observable]], or + * `NoSuchElementException` if source contains no elements. + * + * @return the first item emitted by the source [[Observable]] + * @throws NoSuchElementException + * if source contains no elements + * @see RxJava Wiki: first() + * @see MSDN: Observable.First + */ + def first : T = { + asJava.first : T + } + + /** + * Returns the first item emitted by a specified [[Observable]], or + * `NoSuchElementException` if source contains no elements. + * + * @return the first item emitted by the source [[Observable]] + * @throws NoSuchElementException + * if source contains no elements + * @see RxJava Wiki: first() + * @see MSDN: Observable.First + * @see [[BlockingObservable.first]] + */ + def head : T = first + // last -> use toIterable.last // lastOrDefault -> use toIterable.lastOption // first -> use toIterable.head diff --git a/rxjava-contrib/rxjava-math/src/test/java/rx/math/operators/OperationMinMaxTest.java b/rxjava-contrib/rxjava-math/src/test/java/rx/math/operators/OperationMinMaxTest.java index 63eee7b28f..64d0a38f4d 100644 --- a/rxjava-contrib/rxjava-math/src/test/java/rx/math/operators/OperationMinMaxTest.java +++ b/rxjava-contrib/rxjava-math/src/test/java/rx/math/operators/OperationMinMaxTest.java @@ -23,6 +23,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; +import java.util.NoSuchElementException; import org.junit.Test; import org.mockito.InOrder; @@ -56,7 +57,7 @@ public void testMinWithEmpty() { observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } @@ -96,7 +97,7 @@ public int compare(Integer o1, Integer o2) { observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } @@ -216,7 +217,7 @@ public void testMaxWithEmpty() { observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } @@ -256,7 +257,7 @@ public int compare(Integer o1, Integer o2) { observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index 235e1e62e4..17eb5af99d 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -86,7 +86,7 @@ import rx.operators.OperationReplay; import rx.operators.OperationSample; import rx.operators.OperationSequenceEqual; -import rx.operators.OperationSingle; +import rx.operators.OperatorSingle; import rx.operators.OperationSkip; import rx.operators.OperationSkipLast; import rx.operators.OperationSkipUntil; @@ -4545,12 +4545,12 @@ public final Observable finallyDo(Action0 action) { /** * Returns an Observable that emits only the very first item emitted by the source Observable, or raises an - * {@code IllegalArgumentException} if the source Observable is empty. + * {@code NoSuchElementException} if the source Observable is empty. *

* * * @return an Observable that emits only the very first item emitted by the source Observable, or raises an - * {@code IllegalArgumentException} if the source Observable is empty + * {@code NoSuchElementException} if the source Observable is empty * @see RxJava Wiki: first() * @see "MSDN: Observable.firstAsync()" */ @@ -4560,14 +4560,14 @@ public final Observable first() { /** * Returns an Observable that emits only the very first item emitted by the source Observable that satisfies - * a specified condition, or raises an {@code IllegalArgumentException} if no such items are emitted. + * a specified condition, or raises an {@code NoSuchElementException} if no such items are emitted. *

* * * @param predicate * the condition that an item emitted by the source Observable has to satisfy * @return an Observable that emits only the very first item emitted by the source Observable that satisfies - * the {@code predicate}, or raises an {@code IllegalArgumentException} if no such items are emitted + * the {@code predicate}, or raises an {@code NoSuchElementException} if no such items are emitted * @see RxJava Wiki: first() * @see "MSDN: Observable.firstAsync()" */ @@ -4778,7 +4778,7 @@ public final Observable join(Obser /** * Returns an Observable that emits the last item emitted by the source Observable or notifies observers of - * an {@code IllegalArgumentException} if the source Observable is empty. + * an {@code NoSuchElementException} if the source Observable is empty. *

* * @@ -4793,14 +4793,14 @@ public final Observable last() { /** * Returns an Observable that emits only the last item emitted by the source Observable that satisfies a - * given condition, or an {@code IllegalArgumentException} if no such items are emitted. + * given condition, or an {@code NoSuchElementException} if no such items are emitted. *

* * * @param predicate * the condition any source emitted item has to satisfy * @return an Observable that emits only the last item satisfying the given condition from the source, or an - * {@code IllegalArgumentException} if no such items are emitted + * {@code NoSuchElementException} if no such items are emitted * @throws IllegalArgumentException * if no items that match the predicate are emitted by the source Observable * @see RxJava Wiki: last() @@ -6205,24 +6205,26 @@ public final Observable serialize() { /** * If the source Observable completes after emitting a single item, return an Observable that emits that * item. If the source Observable emits more than one item or no items, throw an - * {@code IllegalArgumentException}. + * {@code NoSuchElementException}. *

* * * @return an Observable that emits the single item emitted by the source Observable * @throws IllegalArgumentException - * if the source emits more than one item or no items + * if the source emits more than one item + * @throws NoSuchElementException + * if the source emits no items * @see RxJava Wiki: single() * @see "MSDN: Observable.singleAsync()" */ public final Observable single() { - return create(OperationSingle. single(this)); + return lift(new OperatorSingle()); } /** * If the Observable completes after emitting a single item that matches a specified predicate, return an * Observable that emits that item. If the source Observable emits more than one such item or no such items, - * throw an {@code IllegalArgumentException}. + * throw an {@code NoSuchElementException}. *

* * @@ -6231,8 +6233,9 @@ public final Observable single() { * @return an Observable that emits the single item emitted by the source Observable that matches the * predicate * @throws IllegalArgumentException - * if the source Observable emits either more than one item that matches the predicate or no - * items that match the predicate + * if the source Observable emits more than one item that matches the predicate + * @throws NoSuchElementException + * if the source Observable emits no item that matches the predicate * @see RxJava Wiki: single() * @see "MSDN: Observable.singleAsync()" */ @@ -6257,7 +6260,7 @@ public final Observable single(Func1 predicate) { * @see "MSDN: Observable.singleOrDefaultAsync()" */ public final Observable singleOrDefault(T defaultValue) { - return create(OperationSingle. singleOrDefault(this, defaultValue)); + return lift(new OperatorSingle(defaultValue)); } /** diff --git a/rxjava-core/src/main/java/rx/observables/BlockingObservable.java b/rxjava-core/src/main/java/rx/observables/BlockingObservable.java index fdd8db73ed..15957dea4e 100644 --- a/rxjava-core/src/main/java/rx/observables/BlockingObservable.java +++ b/rxjava-core/src/main/java/rx/observables/BlockingObservable.java @@ -163,10 +163,10 @@ public Iterator getIterator() { /** * Returns the first item emitted by a specified {@link Observable}, or - * IllegalArgumentException if source contains no elements. + * NoSuchElementException if source contains no elements. * * @return the first item emitted by the source {@link Observable} - * @throws IllegalArgumentException + * @throws NoSuchElementException * if source contains no elements * @see RxJava Wiki: first() * @see MSDN: Observable.First @@ -177,14 +177,14 @@ public T first() { /** * Returns the first item emitted by a specified {@link Observable} that - * matches a predicate, or IllegalArgumentException if no such + * matches a predicate, or NoSuchElementException if no such * item is emitted. * * @param predicate * a predicate function to evaluate items emitted by the {@link Observable} * @return the first item emitted by the {@link Observable} that matches the * predicate - * @throws IllegalArgumentException + * @throws NoSuchElementException * if no such items are emitted * @see RxJava Wiki: first() * @see MSDN: Observable.First @@ -227,12 +227,12 @@ public T firstOrDefault(T defaultValue, Func1 predicate) { /** * Returns the last item emitted by a specified {@link Observable}, or - * throws IllegalArgumentException if it emits no items. + * throws NoSuchElementException if it emits no items. *

* * * @return the last item emitted by the source {@link Observable} - * @throws IllegalArgumentException + * @throws NoSuchElementException * if source contains no elements * @see RxJava Wiki: last() * @see MSDN: Observable.Last @@ -243,7 +243,7 @@ public T last() { /** * Returns the last item emitted by a specified {@link Observable} that - * matches a predicate, or throws IllegalArgumentException if + * matches a predicate, or throws NoSuchElementException if * it emits no such items. *

* @@ -252,7 +252,7 @@ public T last() { * a predicate function to evaluate items emitted by the {@link Observable} * @return the last item emitted by the {@link Observable} that matches the * predicate - * @throws IllegalArgumentException + * @throws NoSuchElementException * if no such items are emitted * @see RxJava Wiki: last() * @see MSDN: Observable.Last @@ -349,7 +349,7 @@ public Iterable latest() { /** * If the {@link Observable} completes after emitting a single item, return - * that item, otherwise throw an IllegalArgumentException. + * that item, otherwise throw an NoSuchElementException. *

* * @@ -364,7 +364,7 @@ public T single() { /** * If the {@link Observable} completes after emitting a single item that * matches a given predicate, return that item, otherwise throw an - * IllegalArgumentException. + * NoSuchElementException. *

* * diff --git a/rxjava-core/src/main/java/rx/operators/OperationSingle.java b/rxjava-core/src/main/java/rx/operators/OperationSingle.java deleted file mode 100644 index 871d565092..0000000000 --- a/rxjava-core/src/main/java/rx/operators/OperationSingle.java +++ /dev/null @@ -1,96 +0,0 @@ -/** - * 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.operators; - -import rx.Observable; -import rx.Observable.OnSubscribeFunc; -import rx.Observer; -import rx.Subscription; - -/** - * If the Observable completes after emitting a single item that matches a - * predicate, return an Observable containing that item. If it emits more than - * one such item or no item, throw an IllegalArgumentException. - */ -public class OperationSingle { - - public static OnSubscribeFunc single( - final Observable source) { - return single(source, false, null); - } - - public static OnSubscribeFunc singleOrDefault( - final Observable source, final T defaultValue) { - return single(source, true, defaultValue); - } - - private static OnSubscribeFunc single( - final Observable source, - final boolean hasDefaultValue, final T defaultValue) { - return new OnSubscribeFunc() { - - @Override - public Subscription onSubscribe(final Observer observer) { - final SafeObservableSubscription subscription = new SafeObservableSubscription(); - subscription.wrap(source.subscribe(new Observer() { - - private T value; - private boolean isEmpty = true; - private boolean hasTooManyElemenets; - - @Override - public void onCompleted() { - if (hasTooManyElemenets) { - // We has already sent an onError message - } else { - if (isEmpty) { - if (hasDefaultValue) { - observer.onNext(defaultValue); - observer.onCompleted(); - } else { - observer.onError(new IllegalArgumentException( - "Sequence contains no elements")); - } - } else { - observer.onNext(value); - observer.onCompleted(); - } - } - } - - @Override - public void onError(Throwable e) { - observer.onError(e); - } - - @Override - public void onNext(T value) { - if (isEmpty) { - this.value = value; - isEmpty = false; - } else { - hasTooManyElemenets = true; - observer.onError(new IllegalArgumentException( - "Sequence contains too many elements")); - subscription.unsubscribe(); - } - } - })); - return subscription; - } - }; - } -} diff --git a/rxjava-core/src/main/java/rx/operators/OperatorSingle.java b/rxjava-core/src/main/java/rx/operators/OperatorSingle.java new file mode 100644 index 0000000000..f633354817 --- /dev/null +++ b/rxjava-core/src/main/java/rx/operators/OperatorSingle.java @@ -0,0 +1,92 @@ +/** + * 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.operators; + +import java.util.NoSuchElementException; + +import rx.Observable.Operator; +import rx.Subscriber; + +/** + * If the Observable completes after emitting a single item that matches a + * predicate, return an Observable containing that item. If it emits more than + * one such item or no item, throw an IllegalArgumentException. + */ +public final class OperatorSingle implements Operator { + + private final boolean hasDefaultValue; + private final T defaultValue; + + public OperatorSingle() { + this(false, null); + } + + public OperatorSingle(T defaultValue) { + this(true, defaultValue); + } + + private OperatorSingle(boolean hasDefaultValue, final T defaultValue) { + this.hasDefaultValue = hasDefaultValue; + this.defaultValue = defaultValue; + } + + @Override + public Subscriber call(final Subscriber subscriber) { + return new Subscriber(subscriber) { + + private T value; + private boolean isNonEmpty = false; + private boolean hasTooManyElements = false; + + @Override + public void onNext(T value) { + if (isNonEmpty) { + hasTooManyElements = true; + subscriber.onError(new IllegalArgumentException("Sequence contains too many elements")); + } else { + this.value = value; + isNonEmpty = true; + } + } + + @Override + public void onCompleted() { + if (hasTooManyElements) { + // We have already sent an onError message + } else { + if (isNonEmpty) { + subscriber.onNext(value); + subscriber.onCompleted(); + } else { + if (hasDefaultValue) { + subscriber.onNext(defaultValue); + subscriber.onCompleted(); + } else { + subscriber.onError(new NoSuchElementException("Sequence contains no elements")); + } + } + } + } + + @Override + public void onError(Throwable e) { + subscriber.onError(e); + } + + }; + } + +} diff --git a/rxjava-core/src/test/java/rx/ObservableTests.java b/rxjava-core/src/test/java/rx/ObservableTests.java index e4a84c15ac..d75af529dc 100644 --- a/rxjava-core/src/test/java/rx/ObservableTests.java +++ b/rxjava-core/src/test/java/rx/ObservableTests.java @@ -23,6 +23,7 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import java.util.NoSuchElementException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -204,7 +205,7 @@ public void testFirstOfNone() { observable.first().subscribe(w); verify(w, never()).onNext(anyInt()); verify(w, never()).onCompleted(); - verify(w, times(1)).onError(isA(IllegalArgumentException.class)); + verify(w, times(1)).onError(isA(NoSuchElementException.class)); } @Test @@ -213,7 +214,7 @@ public void testFirstWithPredicateOfNoneMatchingThePredicate() { observable.first(IS_EVEN).subscribe(w); verify(w, never()).onNext(anyInt()); verify(w, never()).onCompleted(); - verify(w, times(1)).onError(isA(IllegalArgumentException.class)); + verify(w, times(1)).onError(isA(NoSuchElementException.class)); } @Test @@ -233,9 +234,9 @@ public Integer call(Integer t1, Integer t2) { } /** - * A reduce should fail with an IllegalArgumentException if done on an empty Observable. + * A reduce should fail with an NoSuchElementException if done on an empty Observable. */ - @Test(expected = IllegalArgumentException.class) + @Test(expected = NoSuchElementException.class) public void testReduceWithEmptyObservable() { Observable observable = Observable.range(1, 0); observable.reduce(new Func2() { diff --git a/rxjava-core/src/test/java/rx/ZipTests.java b/rxjava-core/src/test/java/rx/ZipTests.java index 8735cb2e66..e8cd9a90dd 100644 --- a/rxjava-core/src/test/java/rx/ZipTests.java +++ b/rxjava-core/src/test/java/rx/ZipTests.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.NoSuchElementException; import org.junit.Test; @@ -100,9 +101,9 @@ public void testCovarianceOfZip() { * Occasionally zip may be invoked with 0 observables. Test that we don't block indefinitely instead * of immediately invoking zip with 0 argument. * - * We now expect an IllegalArgumentException since last() requires at least one value and nothing will be emitted. + * We now expect an NoSuchElementException since last() requires at least one value and nothing will be emitted. */ - @Test(expected = IllegalArgumentException.class) + @Test(expected = NoSuchElementException.class) public void nonBlockingObservable() { final Object invoked = new Object(); diff --git a/rxjava-core/src/test/java/rx/observables/BlockingObservableTest.java b/rxjava-core/src/test/java/rx/observables/BlockingObservableTest.java index 50b3d8f161..663f5d95f6 100644 --- a/rxjava-core/src/test/java/rx/observables/BlockingObservableTest.java +++ b/rxjava-core/src/test/java/rx/observables/BlockingObservableTest.java @@ -52,7 +52,7 @@ public void testLast() { assertEquals("three", obs.last()); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = NoSuchElementException.class) public void testLastEmptyObservable() { BlockingObservable obs = BlockingObservable.from(Observable.empty()); obs.last(); @@ -175,7 +175,7 @@ public void testSingleWrong() { observable.single(); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = NoSuchElementException.class) public void testSingleWrongPredicate() { BlockingObservable observable = BlockingObservable.from(Observable.from(-1)); observable.single(new Func1() { @@ -319,7 +319,7 @@ public void testFirst() { assertEquals("one", observable.first()); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = NoSuchElementException.class) public void testFirstWithEmpty() { BlockingObservable.from(Observable. empty()).first(); } @@ -336,7 +336,7 @@ public Boolean call(String args) { assertEquals("three", first); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = NoSuchElementException.class) public void testFirstWithPredicateAndEmpty() { BlockingObservable observable = BlockingObservable.from(Observable.from("one", "two", "three")); observable.first(new Func1() { diff --git a/rxjava-core/src/test/java/rx/operators/OperationMinMaxTest.java b/rxjava-core/src/test/java/rx/operators/OperationMinMaxTest.java index cad4805c7c..bf0ebe59a0 100644 --- a/rxjava-core/src/test/java/rx/operators/OperationMinMaxTest.java +++ b/rxjava-core/src/test/java/rx/operators/OperationMinMaxTest.java @@ -23,6 +23,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; +import java.util.NoSuchElementException; import org.junit.Test; import org.mockito.InOrder; @@ -56,7 +57,7 @@ public void testMinWithEmpty() { observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } @@ -96,7 +97,7 @@ public int compare(Integer o1, Integer o2) { observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } @@ -216,7 +217,7 @@ public void testMaxWithEmpty() { observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } @@ -256,7 +257,7 @@ public int compare(Integer o1, Integer o2) { observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } diff --git a/rxjava-core/src/test/java/rx/operators/OperationFirstOrDefaultTest.java b/rxjava-core/src/test/java/rx/operators/OperatorFirstTest.java similarity index 98% rename from rxjava-core/src/test/java/rx/operators/OperationFirstOrDefaultTest.java rename to rxjava-core/src/test/java/rx/operators/OperatorFirstTest.java index 84f1b6aee0..2408a6004d 100644 --- a/rxjava-core/src/test/java/rx/operators/OperationFirstOrDefaultTest.java +++ b/rxjava-core/src/test/java/rx/operators/OperatorFirstTest.java @@ -19,6 +19,8 @@ import static org.mockito.Mockito.*; import static org.mockito.MockitoAnnotations.*; +import java.util.NoSuchElementException; + import org.junit.Before; import org.junit.Test; import org.mockito.InOrder; @@ -28,7 +30,7 @@ import rx.Observer; import rx.functions.Func1; -public class OperationFirstOrDefaultTest { +public class OperatorFirstTest { @Mock Observer w; @@ -127,7 +129,7 @@ public void testFirstWithEmpty() { InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } @@ -189,7 +191,7 @@ public Boolean call(Integer t1) { InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } diff --git a/rxjava-core/src/test/java/rx/operators/OperationLastTest.java b/rxjava-core/src/test/java/rx/operators/OperatorLastTest.java similarity index 95% rename from rxjava-core/src/test/java/rx/operators/OperationLastTest.java rename to rxjava-core/src/test/java/rx/operators/OperatorLastTest.java index 6cb6d44ff5..5eee929fbd 100644 --- a/rxjava-core/src/test/java/rx/operators/OperationLastTest.java +++ b/rxjava-core/src/test/java/rx/operators/OperatorLastTest.java @@ -15,9 +15,13 @@ */ package rx.operators; -import static org.junit.Assert.*; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; + +import java.util.NoSuchElementException; import org.junit.Test; import org.mockito.InOrder; @@ -26,7 +30,7 @@ import rx.Observer; import rx.functions.Func1; -public class OperationLastTest { +public class OperatorLastTest { @Test public void testLastWithElements() { @@ -34,7 +38,7 @@ public void testLastWithElements() { assertEquals(3, last.toBlockingObservable().single().intValue()); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = NoSuchElementException.class) public void testLastWithNoElements() { Observable last = Observable.empty().last(); last.toBlockingObservable().single(); @@ -90,7 +94,7 @@ public void testLastWithEmpty() { InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } @@ -152,7 +156,7 @@ public Boolean call(Integer t1) { InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } diff --git a/rxjava-core/src/test/java/rx/operators/OperatorMapTest.java b/rxjava-core/src/test/java/rx/operators/OperatorMapTest.java index fd091bdaef..111f694637 100644 --- a/rxjava-core/src/test/java/rx/operators/OperatorMapTest.java +++ b/rxjava-core/src/test/java/rx/operators/OperatorMapTest.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.NoSuchElementException; import org.junit.Before; import org.junit.Test; @@ -211,9 +212,9 @@ public String call(String arg0) { } /** - * While mapping over range(1,1).last() we expect IllegalArgumentException since the sequence is empty. + * While mapping over range(1,0).last() we expect NoSuchElementException since the sequence is empty. */ - @Test(expected = IllegalArgumentException.class) + @Test(expected = NoSuchElementException.class) public void testErrorPassesThruMap() { Observable.range(1, 0).last().map(new Func1() { diff --git a/rxjava-core/src/test/java/rx/operators/OperationSingleTest.java b/rxjava-core/src/test/java/rx/operators/OperatorSingleTest.java similarity index 97% rename from rxjava-core/src/test/java/rx/operators/OperationSingleTest.java rename to rxjava-core/src/test/java/rx/operators/OperatorSingleTest.java index dee0f1eb88..54673db194 100644 --- a/rxjava-core/src/test/java/rx/operators/OperationSingleTest.java +++ b/rxjava-core/src/test/java/rx/operators/OperatorSingleTest.java @@ -18,6 +18,8 @@ import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; +import java.util.NoSuchElementException; + import org.junit.Test; import org.mockito.InOrder; @@ -25,7 +27,7 @@ import rx.Observer; import rx.functions.Func1; -public class OperationSingleTest { +public class OperatorSingleTest { @Test public void testSingle() { @@ -65,7 +67,7 @@ public void testSingleWithEmpty() { InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } @@ -127,7 +129,7 @@ public Boolean call(Integer t1) { InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( - isA(IllegalArgumentException.class)); + isA(NoSuchElementException.class)); inOrder.verifyNoMoreInteractions(); } diff --git a/rxjava-core/src/test/java/rx/operators/OperatorZipTest.java b/rxjava-core/src/test/java/rx/operators/OperatorZipTest.java index 7862c9f49a..51e1395e3d 100644 --- a/rxjava-core/src/test/java/rx/operators/OperatorZipTest.java +++ b/rxjava-core/src/test/java/rx/operators/OperatorZipTest.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.NoSuchElementException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -1034,10 +1035,10 @@ public Object call(final Object... args) { } /** - * Expect IllegalArgumentException instead of blocking forever as zip should emit onCompleted and no onNext + * Expect NoSuchElementException instead of blocking forever as zip should emit onCompleted and no onNext * and last() expects at least a single response. */ - @Test(expected = IllegalArgumentException.class) + @Test(expected = NoSuchElementException.class) public void testZipEmptyListBlocking() { final Object invoked = new Object();