From 497f35f64c89b85bbd17a5f4d50a4eb56e545424 Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Tue, 11 Oct 2016 09:29:01 +0200 Subject: [PATCH] 2.x: Test static from methods and add Maybe.fromSingle & fromCompletable (#4685) --- src/main/java/io/reactivex/Maybe.java | 36 ++++++ .../CompletableFromObservable.java | 42 ++++--- .../completable/CompletableFromSingle.java | 35 +++--- .../CompletableFromActionTest.java | 100 +++++++++++++++++ .../CompletableFromCallableTest.java | 103 ++++++++++++++++++ .../CompletableFromObservableTest.java | 46 ++++++++ .../CompletableFromPublisherTest.java | 46 ++++++++ .../CompletableFromRunnableTest.java | 99 +++++++++++++++++ .../CompletableFromSingleTest.java | 39 +++++++ .../operators/maybe/MaybeFromActionTest.java | 100 +++++++++++++++++ .../maybe/MaybeFromCallableTest.java | 103 ++++++++++++++++++ .../maybe/MaybeFromCompletableTest.java | 39 +++++++ .../maybe/MaybeFromRunnableTest.java | 98 +++++++++++++++++ .../operators/maybe/MaybeFromSingleTest.java | 39 +++++++ .../single/SingleFromCallableTest.java | 53 +++++++++ 15 files changed, 945 insertions(+), 33 deletions(-) create mode 100644 src/test/java/io/reactivex/internal/operators/completable/CompletableFromActionTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/completable/CompletableFromCallableTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/completable/CompletableFromObservableTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/completable/CompletableFromPublisherTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/completable/CompletableFromRunnableTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/completable/CompletableFromSingleTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/maybe/MaybeFromActionTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/maybe/MaybeFromCallableTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/maybe/MaybeFromCompletableTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/maybe/MaybeFromRunnableTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/maybe/MaybeFromSingleTest.java create mode 100644 src/test/java/io/reactivex/internal/operators/single/SingleFromCallableTest.java diff --git a/src/main/java/io/reactivex/Maybe.java b/src/main/java/io/reactivex/Maybe.java index 3c3386a6fd..209a49c71a 100644 --- a/src/main/java/io/reactivex/Maybe.java +++ b/src/main/java/io/reactivex/Maybe.java @@ -563,6 +563,42 @@ public static Maybe fromAction(final Action run) { return RxJavaPlugins.onAssembly(new MaybeFromAction(run)); } + /** + * Wraps a CompletableSource into a Maybe. + * + *
+ *
Scheduler:
+ *
{@code fromCompletable} does not operate by default on a particular {@link Scheduler}.
+ *
+ * @param the target type + * @param completableSource the CompletableSource to convert from + * @return the new Maybe instance + * @throws NullPointerException if completable is null + */ + @SchedulerSupport(SchedulerSupport.NONE) + public static Maybe fromCompletable(CompletableSource completableSource) { + ObjectHelper.requireNonNull(completableSource, "completableSource is null"); + return RxJavaPlugins.onAssembly(new MaybeFromCompletable(completableSource)); + } + + /** + * Wraps a SingleSource into a Maybe. + * + *
+ *
Scheduler:
+ *
{@code fromSingle} does not operate by default on a particular {@link Scheduler}.
+ *
+ * @param the target type + * @param singleSource the SingleSource to convert from + * @return the new Maybe instance + * @throws NullPointerException if single is null + */ + @SchedulerSupport(SchedulerSupport.NONE) + public static Maybe fromSingle(SingleSource singleSource) { + ObjectHelper.requireNonNull(singleSource, "singleSource is null"); + return RxJavaPlugins.onAssembly(new MaybeFromSingle(singleSource)); + } + /** * Returns a {@link Maybe} that invokes passed function and emits its result for each new MaybeObserver that subscribes * while considering {@code null} value from the callable as indication for valueless completion. diff --git a/src/main/java/io/reactivex/internal/operators/completable/CompletableFromObservable.java b/src/main/java/io/reactivex/internal/operators/completable/CompletableFromObservable.java index d26755456f..c7f7d9a9bf 100644 --- a/src/main/java/io/reactivex/internal/operators/completable/CompletableFromObservable.java +++ b/src/main/java/io/reactivex/internal/operators/completable/CompletableFromObservable.java @@ -26,28 +26,34 @@ public CompletableFromObservable(ObservableSource observable) { @Override protected void subscribeActual(final CompletableObserver s) { - observable.subscribe(new Observer() { + observable.subscribe(new CompletableFromObservableObserver(s)); + } + + static final class CompletableFromObservableObserver implements Observer { + final CompletableObserver co; - @Override - public void onComplete() { - s.onComplete(); - } + CompletableFromObservableObserver(CompletableObserver co) { + this.co = co; + } - @Override - public void onError(Throwable e) { - s.onError(e); - } + @Override + public void onSubscribe(Disposable d) { + co.onSubscribe(d); + } - @Override - public void onNext(T value) { - // ignored - } + @Override + public void onNext(T value) { + // Deliberately ignored. + } - @Override - public void onSubscribe(Disposable d) { - s.onSubscribe(d); - } + @Override + public void onError(Throwable e) { + co.onError(e); + } - }); + @Override + public void onComplete() { + co.onComplete(); + } } } diff --git a/src/main/java/io/reactivex/internal/operators/completable/CompletableFromSingle.java b/src/main/java/io/reactivex/internal/operators/completable/CompletableFromSingle.java index d4d43d54c9..3410c0954c 100644 --- a/src/main/java/io/reactivex/internal/operators/completable/CompletableFromSingle.java +++ b/src/main/java/io/reactivex/internal/operators/completable/CompletableFromSingle.java @@ -26,24 +26,29 @@ public CompletableFromSingle(SingleSource single) { @Override protected void subscribeActual(final CompletableObserver s) { - single.subscribe(new SingleObserver() { + single.subscribe(new CompletableFromSingleObserver(s)); + } - @Override - public void onError(Throwable e) { - s.onError(e); - } + static final class CompletableFromSingleObserver implements SingleObserver { + final CompletableObserver co; - @Override - public void onSubscribe(Disposable d) { - s.onSubscribe(d); - } + CompletableFromSingleObserver(CompletableObserver co) { + this.co = co; + } - @Override - public void onSuccess(T value) { - s.onComplete(); - } + @Override + public void onError(Throwable e) { + co.onError(e); + } - }); - } + @Override + public void onSubscribe(Disposable d) { + co.onSubscribe(d); + } + @Override + public void onSuccess(T value) { + co.onComplete(); + } + } } diff --git a/src/test/java/io/reactivex/internal/operators/completable/CompletableFromActionTest.java b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromActionTest.java new file mode 100644 index 0000000000..c3e7b850dc --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromActionTest.java @@ -0,0 +1,100 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.completable; + +import io.reactivex.Completable; +import io.reactivex.functions.Action; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CompletableFromActionTest { + @Test(expected = NullPointerException.class) + public void fromActionNull() { + Completable.fromAction(null); + } + + @Test + public void fromAction() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Completable.fromAction(new Action() { + @Override + public void run() throws Exception { + atomicInteger.incrementAndGet(); + } + }) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromActionTwice() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Action run = new Action() { + @Override + public void run() throws Exception { + atomicInteger.incrementAndGet(); + } + }; + + Completable.fromAction(run) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + + Completable.fromAction(run) + .test() + .assertResult(); + + assertEquals(2, atomicInteger.get()); + } + + @Test + public void fromActionInvokesLazy() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Completable completable = Completable.fromAction(new Action() { + @Override + public void run() throws Exception { + atomicInteger.incrementAndGet(); + } + }); + + assertEquals(0, atomicInteger.get()); + + completable + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromActionThrows() { + Completable.fromAction(new Action() { + @Override + public void run() throws Exception { + throw new UnsupportedOperationException(); + } + }) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/completable/CompletableFromCallableTest.java b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromCallableTest.java new file mode 100644 index 0000000000..3e6ab7f7b4 --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromCallableTest.java @@ -0,0 +1,103 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.completable; + +import io.reactivex.Completable; +import java.util.concurrent.Callable; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CompletableFromCallableTest { + @Test(expected = NullPointerException.class) + public void fromCallableNull() { + Completable.fromCallable(null); + } + + @Test + public void fromCallable() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Completable.fromCallable(new Callable() { + @Override + public Object call() throws Exception { + atomicInteger.incrementAndGet(); + return null; + } + }) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromCallableTwice() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Callable callable = new Callable() { + @Override + public Object call() throws Exception { + atomicInteger.incrementAndGet(); + return null; + } + }; + + Completable.fromCallable(callable) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + + Completable.fromCallable(callable) + .test() + .assertResult(); + + assertEquals(2, atomicInteger.get()); + } + + @Test + public void fromCallableInvokesLazy() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Completable completable = Completable.fromCallable(new Callable() { + @Override + public Object call() throws Exception { + atomicInteger.incrementAndGet(); + return null; + } + }); + + assertEquals(0, atomicInteger.get()); + + completable + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromCallableThrows() { + Completable.fromCallable(new Callable() { + @Override + public Object call() throws Exception { + throw new UnsupportedOperationException(); + } + }) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/completable/CompletableFromObservableTest.java b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromObservableTest.java new file mode 100644 index 0000000000..0b464ecff7 --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromObservableTest.java @@ -0,0 +1,46 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.completable; + +import io.reactivex.Completable; +import io.reactivex.Observable; +import org.junit.Test; + +public class CompletableFromObservableTest { + @Test(expected = NullPointerException.class) + public void fromObservableNull() { + Completable.fromObservable(null); + } + + @Test + public void fromObservable() { + Completable.fromObservable(Observable.just(1)) + .test() + .assertResult(); + } + + @Test + public void fromObservableEmpty() { + Completable.fromObservable(Observable.empty()) + .test() + .assertResult(); + } + + @Test + public void fromObservableError() { + Completable.fromObservable(Observable.error(new UnsupportedOperationException())) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/completable/CompletableFromPublisherTest.java b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromPublisherTest.java new file mode 100644 index 0000000000..58d8460b1b --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromPublisherTest.java @@ -0,0 +1,46 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.completable; + +import io.reactivex.Completable; +import io.reactivex.Flowable; +import org.junit.Test; + +public class CompletableFromPublisherTest { + @Test(expected = NullPointerException.class) + public void fromPublisherNull() { + Completable.fromPublisher(null); + } + + @Test + public void fromPublisher() { + Completable.fromPublisher(Flowable.just(1)) + .test() + .assertResult(); + } + + @Test + public void fromPublisherEmpty() { + Completable.fromPublisher(Flowable.empty()) + .test() + .assertResult(); + } + + @Test + public void fromPublisherThrows() { + Completable.fromPublisher(Flowable.error(new UnsupportedOperationException())) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/completable/CompletableFromRunnableTest.java b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromRunnableTest.java new file mode 100644 index 0000000000..08140dc807 --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromRunnableTest.java @@ -0,0 +1,99 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.completable; + +import io.reactivex.Completable; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CompletableFromRunnableTest { + @Test(expected = NullPointerException.class) + public void fromRunnableNull() { + Completable.fromRunnable(null); + } + + @Test + public void fromRunnable() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Completable.fromRunnable(new Runnable() { + @Override + public void run() { + atomicInteger.incrementAndGet(); + } + }) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromRunnableTwice() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Runnable run = new Runnable() { + @Override + public void run() { + atomicInteger.incrementAndGet(); + } + }; + + Completable.fromRunnable(run) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + + Completable.fromRunnable(run) + .test() + .assertResult(); + + assertEquals(2, atomicInteger.get()); + } + + @Test + public void fromRunnableInvokesLazy() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Completable completable = Completable.fromRunnable(new Runnable() { + @Override + public void run() { + atomicInteger.incrementAndGet(); + } + }); + + assertEquals(0, atomicInteger.get()); + + completable + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromRunnableThrows() { + Completable.fromRunnable(new Runnable() { + @Override + public void run() { + throw new UnsupportedOperationException(); + } + }) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/completable/CompletableFromSingleTest.java b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromSingleTest.java new file mode 100644 index 0000000000..af24daa41b --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/completable/CompletableFromSingleTest.java @@ -0,0 +1,39 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.completable; + +import io.reactivex.Completable; +import io.reactivex.Single; +import org.junit.Test; + +public class CompletableFromSingleTest { + @Test(expected = NullPointerException.class) + public void fromSingleNull() { + Completable.fromSingle(null); + } + + @Test + public void fromSingle() { + Completable.fromSingle(Single.just(1)) + .test() + .assertResult(); + } + + @Test + public void fromSingleError() { + Completable.fromSingle(Single.error(new UnsupportedOperationException())) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromActionTest.java b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromActionTest.java new file mode 100644 index 0000000000..bea10bb0d2 --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromActionTest.java @@ -0,0 +1,100 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.maybe; + +import io.reactivex.Maybe; +import io.reactivex.functions.Action; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class MaybeFromActionTest { + @Test(expected = NullPointerException.class) + public void fromActionNull() { + Maybe.fromAction(null); + } + + @Test + public void fromAction() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Maybe.fromAction(new Action() { + @Override + public void run() throws Exception { + atomicInteger.incrementAndGet(); + } + }) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromActionTwice() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Action run = new Action() { + @Override + public void run() throws Exception { + atomicInteger.incrementAndGet(); + } + }; + + Maybe.fromAction(run) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + + Maybe.fromAction(run) + .test() + .assertResult(); + + assertEquals(2, atomicInteger.get()); + } + + @Test + public void fromActionInvokesLazy() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Maybe maybe = Maybe.fromAction(new Action() { + @Override + public void run() throws Exception { + atomicInteger.incrementAndGet(); + } + }); + + assertEquals(0, atomicInteger.get()); + + maybe + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromActionThrows() { + Maybe.fromAction(new Action() { + @Override + public void run() throws Exception { + throw new UnsupportedOperationException(); + } + }) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromCallableTest.java b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromCallableTest.java new file mode 100644 index 0000000000..0e4ea23524 --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromCallableTest.java @@ -0,0 +1,103 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.maybe; + +import io.reactivex.Maybe; +import java.util.concurrent.Callable; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class MaybeFromCallableTest { + @Test(expected = NullPointerException.class) + public void fromCallableNull() { + Maybe.fromCallable(null); + } + + @Test + public void fromCallable() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Maybe.fromCallable(new Callable() { + @Override + public Object call() throws Exception { + atomicInteger.incrementAndGet(); + return null; + } + }) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromCallableTwice() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Callable callable = new Callable() { + @Override + public Object call() throws Exception { + atomicInteger.incrementAndGet(); + return null; + } + }; + + Maybe.fromCallable(callable) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + + Maybe.fromCallable(callable) + .test() + .assertResult(); + + assertEquals(2, atomicInteger.get()); + } + + @Test + public void fromCallableInvokesLazy() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Maybe completable = Maybe.fromCallable(new Callable() { + @Override + public Object call() throws Exception { + atomicInteger.incrementAndGet(); + return null; + } + }); + + assertEquals(0, atomicInteger.get()); + + completable + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromCallableThrows() { + Maybe.fromCallable(new Callable() { + @Override + public Object call() throws Exception { + throw new UnsupportedOperationException(); + } + }) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromCompletableTest.java b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromCompletableTest.java new file mode 100644 index 0000000000..612e3f5ef0 --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromCompletableTest.java @@ -0,0 +1,39 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.maybe; + +import io.reactivex.Completable; +import io.reactivex.Maybe; +import org.junit.Test; + +public class MaybeFromCompletableTest { + @Test(expected = NullPointerException.class) + public void fromCompletableNull() { + Maybe.fromCompletable(null); + } + + @Test + public void fromCompletable() { + Maybe.fromCompletable(Completable.complete()) + .test() + .assertResult(); + } + + @Test + public void fromCompletableError() { + Maybe.fromCompletable(Completable.error(new UnsupportedOperationException())) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromRunnableTest.java b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromRunnableTest.java new file mode 100644 index 0000000000..e5c5d1460a --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromRunnableTest.java @@ -0,0 +1,98 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.maybe; + +import io.reactivex.Maybe; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class MaybeFromRunnableTest { + @Test(expected = NullPointerException.class) + public void fromRunnableNull() { + Maybe.fromRunnable(null); + } + + @Test + public void fromRunnable() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Maybe.fromRunnable(new Runnable() { + @Override + public void run() { + atomicInteger.incrementAndGet(); + } + }) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromRunnableTwice() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + Runnable run = new Runnable() { + @Override + public void run() { + atomicInteger.incrementAndGet(); + } + }; + + Maybe.fromRunnable(run) + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + + Maybe.fromRunnable(run) + .test() + .assertResult(); + + assertEquals(2, atomicInteger.get()); + } + + @Test + public void fromRunnableInvokesLazy() { + final AtomicInteger atomicInteger = new AtomicInteger(); + + final Maybe maybe = Maybe.fromRunnable(new Runnable() { + @Override public void run() { + atomicInteger.incrementAndGet(); + } + }); + + assertEquals(0, atomicInteger.get()); + + maybe + .test() + .assertResult(); + + assertEquals(1, atomicInteger.get()); + } + + @Test + public void fromRunnableThrows() { + Maybe.fromRunnable(new Runnable() { + @Override + public void run() { + throw new UnsupportedOperationException(); + } + }) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromSingleTest.java b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromSingleTest.java new file mode 100644 index 0000000000..c2ff77ee81 --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/maybe/MaybeFromSingleTest.java @@ -0,0 +1,39 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.maybe; + +import io.reactivex.Maybe; +import io.reactivex.Single; +import org.junit.Test; + +public class MaybeFromSingleTest { + @Test(expected = NullPointerException.class) + public void fromSingleNull() { + Maybe.fromSingle(null); + } + + @Test + public void fromSingle() { + Maybe.fromSingle(Single.just(1)) + .test() + .assertResult(1); + } + + @Test + public void fromSingleThrows() { + Maybe.fromSingle(Single.error(new UnsupportedOperationException())) + .test() + .assertFailure(UnsupportedOperationException.class); + } +} diff --git a/src/test/java/io/reactivex/internal/operators/single/SingleFromCallableTest.java b/src/test/java/io/reactivex/internal/operators/single/SingleFromCallableTest.java new file mode 100644 index 0000000000..46607c7f90 --- /dev/null +++ b/src/test/java/io/reactivex/internal/operators/single/SingleFromCallableTest.java @@ -0,0 +1,53 @@ +/** + * Copyright 2016 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 io.reactivex.internal.operators.single; + +import io.reactivex.Single; +import java.util.concurrent.Callable; +import org.junit.Test; + +public class SingleFromCallableTest { + @Test + public void fromCallableValue() { + Single.fromCallable(new Callable() { + @Override public Integer call() throws Exception { + return 5; + } + }) + .test() + .assertResult(5); + } + + @Test + public void fromCallableError() { + Single.fromCallable(new Callable() { + @Override public Integer call() throws Exception { + throw new UnsupportedOperationException(); + } + }) + .test() + .assertFailure(UnsupportedOperationException.class); + } + + @Test + public void fromCallableNull() { + Single.fromCallable(new Callable() { + @Override public Integer call() throws Exception { + return null; + } + }) + .test() + .assertFailureAndMessage(NullPointerException.class, "The callable returned a null value"); + } +}