/** * 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.flowable; import static io.reactivex.internal.util.TestingHelper.addToList; import static io.reactivex.internal.util.TestingHelper.biConsumerThrows; import static io.reactivex.internal.util.TestingHelper.callableListCreator; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicBoolean; import org.junit.Test; import io.reactivex.Flowable; import io.reactivex.functions.BiConsumer; import io.reactivex.plugins.RxJavaPlugins; public final class FlowableCollectTest { @Test public void testCollectToList() { Flowable> o = Flowable.just(1, 2, 3) .collect(new Callable>() { @Override public List call() { return new ArrayList(); } }, new BiConsumer, Integer>() { @Override public void accept(List list, Integer v) { list.add(v); } }); List list = o.blockingLast(); assertEquals(3, list.size()); assertEquals(1, list.get(0).intValue()); assertEquals(2, list.get(1).intValue()); assertEquals(3, list.get(2).intValue()); // test multiple subscribe List list2 = o.blockingLast(); assertEquals(3, list2.size()); assertEquals(1, list2.get(0).intValue()); assertEquals(2, list2.get(1).intValue()); assertEquals(3, list2.get(2).intValue()); } @Test public void testCollectToString() { String value = Flowable.just(1, 2, 3) .collect( new Callable() { @Override public StringBuilder call() { return new StringBuilder(); } }, new BiConsumer() { @Override public void accept(StringBuilder sb, Integer v) { if (sb.length() > 0) { sb.append("-"); } sb.append(v); } }).blockingLast().toString(); assertEquals("1-2-3", value); } @Test public void testFactoryFailureResultsInErrorEmission() { final RuntimeException e = new RuntimeException(); Flowable.just(1).collect(new Callable>() { @Override public List call() throws Exception { throw e; } }, new BiConsumer, Integer>() { @Override public void accept(List list, Integer t) { list.add(t); } }) .test() .assertNoValues() .assertError(e) .assertNotComplete(); } @Test public void testCollectorFailureDoesNotResultInTwoErrorEmissions() { try { final List list = new CopyOnWriteArrayList(); RxJavaPlugins.setErrorHandler(addToList(list)); final RuntimeException e1 = new RuntimeException(); final RuntimeException e2 = new RuntimeException(); Burst.items(1).error(e2) // .collect(callableListCreator(), biConsumerThrows(e1)) // .test() // .assertError(e1) // .assertNotComplete(); assertEquals(Arrays.asList(e2), list); } finally { RxJavaPlugins.reset(); } } @Test public void testCollectorFailureDoesNotResultInErrorAndCompletedEmissions() { final RuntimeException e = new RuntimeException(); Burst.item(1).create() // .collect(callableListCreator(), biConsumerThrows(e)) // .test() // .assertError(e) // .assertNotComplete(); } @Test public void testCollectorFailureDoesNotResultInErrorAndOnNextEmissions() { final RuntimeException e = new RuntimeException(); final AtomicBoolean added = new AtomicBoolean(); BiConsumer throwOnFirstOnly = new BiConsumer() { boolean once = true; @Override public void accept(Object o, Integer t) { if (once) { once = false; throw e; } else { added.set(true); } } }; Burst.items(1, 2).create() // .collect(callableListCreator(), throwOnFirstOnly)// .test() // .assertError(e) // .assertNoValues() // .assertNotComplete(); assertFalse(added.get()); } }