Skip to content

Commit

Permalink
2.x: Delegate null Collections down to onError in toList (#4731)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanniktech authored and akarnokd committed Oct 19, 2016
1 parent 9e18df6 commit 3e7d63c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package io.reactivex.internal.operators.flowable;

import io.reactivex.internal.functions.ObjectHelper;
import java.util.Collection;
import java.util.concurrent.Callable;

Expand All @@ -33,7 +34,7 @@ public FlowableToList(Publisher<T> source, Callable<U> collectionSupplier) {
protected void subscribeActual(Subscriber<? super U> s) {
U coll;
try {
coll = collectionSupplier.call();
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
EmptySubscription.error(e, s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package io.reactivex.internal.operators.flowable;

import io.reactivex.internal.functions.ObjectHelper;
import java.util.Collection;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -47,7 +48,7 @@ public FlowableToListSingle(Publisher<T> source, Callable<U> collectionSupplier)
protected void subscribeActual(SingleObserver<? super U> s) {
U coll;
try {
coll = collectionSupplier.call();
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
EmptyDisposable.error(e, s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ObservableToList(ObservableSource<T> source, Callable<U> collectionSuppli
public void subscribeActual(Observer<? super U> t) {
U coll;
try {
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null Collection");
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
EmptyDisposable.error(e, t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package io.reactivex.internal.operators.observable;

import io.reactivex.internal.functions.ObjectHelper;
import java.util.*;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -48,7 +49,7 @@ public ObservableToListSingle(ObservableSource<T> source, Callable<U> collection
public void subscribeActual(SingleObserver<? super U> t) {
U coll;
try {
coll = collectionSupplier.call();
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
EmptyDisposable.error(e, t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,22 @@ public Collection<Integer> call() throws Exception {
.assertFailure(TestException.class);
}

@SuppressWarnings("unchecked")
@Test
public void collectionSupplierReturnsNull() {
Flowable.just(1)
.toList(new Callable<Collection<Integer>>() {
@Override
public Collection<Integer> call() throws Exception {
return null;
}
})
.toFlowable()
.test()
.assertFailure(NullPointerException.class)
.assertErrorMessage("The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
}

@SuppressWarnings("unchecked")
@Test
public void singleCollectionSupplierThrows() {
Expand All @@ -358,4 +374,19 @@ public Collection<Integer> call() throws Exception {
.test()
.assertFailure(TestException.class);
}
}

@SuppressWarnings("unchecked")
@Test
public void singleCollectionSupplierReturnsNull() {
Flowable.just(1)
.toList(new Callable<Collection<Integer>>() {
@Override
public Collection<Integer> call() throws Exception {
return null;
}
})
.test()
.assertFailure(NullPointerException.class)
.assertErrorMessage("The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,22 @@ public Collection<Integer> call() throws Exception {
.assertFailure(TestException.class);
}

@SuppressWarnings("unchecked")
@Test
public void collectionSupplierReturnsNull() {
Observable.just(1)
.toList(new Callable<Collection<Integer>>() {
@Override
public Collection<Integer> call() throws Exception {
return null;
}
})
.toObservable()
.test()
.assertFailure(NullPointerException.class)
.assertErrorMessage("The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
}

@SuppressWarnings("unchecked")
@Test
public void singleCollectionSupplierThrows() {
Expand All @@ -239,4 +255,19 @@ public Collection<Integer> call() throws Exception {
.test()
.assertFailure(TestException.class);
}
}

@SuppressWarnings("unchecked")
@Test
public void singleCollectionSupplierReturnsNull() {
Observable.just(1)
.toList(new Callable<Collection<Integer>>() {
@Override
public Collection<Integer> call() throws Exception {
return null;
}
})
.test()
.assertFailure(NullPointerException.class)
.assertErrorMessage("The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
}
}

0 comments on commit 3e7d63c

Please sign in to comment.