| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.flowable; |
| 15 |
|
| 16 |
import static io.reactivex.rxjava3.internal.util.TestingHelper.*; |
| 17 |
import static org.junit.Assert.*; |
| 18 |
|
| 19 |
import java.util.*; |
| 20 |
import java.util.concurrent.CopyOnWriteArrayList; |
| 21 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.functions.*; |
| 27 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public final class FlowableCollectTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void collectToListFlowable() { |
| 35 |
Flowable<List<Integer>> f = Flowable.just(1, 2, 3) |
| 36 |
.collect(new Supplier<List<Integer>>() { |
| 37 |
@Override |
| 38 |
public List<Integer> get() { |
| 39 |
return new ArrayList<>(); |
| 40 |
} |
| 41 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 42 |
@Override |
| 43 |
public void accept(List<Integer> list, Integer v) { |
| 44 |
list.add(v); |
| 45 |
} |
| 46 |
}).toFlowable(); |
| 47 |
|
| 48 |
List<Integer> list = f.blockingLast(); |
| 49 |
|
| 50 |
assertEquals(3, list.size()); |
| 51 |
assertEquals(1, list.get(0).intValue()); |
| 52 |
assertEquals(2, list.get(1).intValue()); |
| 53 |
assertEquals(3, list.get(2).intValue()); |
| 54 |
|
| 55 |
// test multiple subscribe |
| 56 |
List<Integer> list2 = f.blockingLast(); |
| 57 |
|
| 58 |
assertEquals(3, list2.size()); |
| 59 |
assertEquals(1, list2.get(0).intValue()); |
| 60 |
assertEquals(2, list2.get(1).intValue()); |
| 61 |
assertEquals(3, list2.get(2).intValue()); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void collectToStringFlowable() { |
| 66 |
String value = Flowable.just(1, 2, 3) |
| 67 |
.collect( |
| 68 |
new Supplier<StringBuilder>() { |
| 69 |
@Override |
| 70 |
public StringBuilder get() { |
| 71 |
return new StringBuilder(); |
| 72 |
} |
| 73 |
}, |
| 74 |
new BiConsumer<StringBuilder, Integer>() { |
| 75 |
@Override |
| 76 |
public void accept(StringBuilder sb, Integer v) { |
| 77 |
if (sb.length() > 0) { |
| 78 |
sb.append("-"); |
| 79 |
} |
| 80 |
sb.append(v); |
| 81 |
} |
| 82 |
}).toFlowable().blockingLast().toString(); |
| 83 |
|
| 84 |
assertEquals("1-2-3", value); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void factoryFailureResultsInErrorEmissionFlowable() { |
| 89 |
final RuntimeException e = new RuntimeException(); |
| 90 |
Flowable.just(1).collect(new Supplier<List<Integer>>() { |
| 91 |
|
| 92 |
@Override |
| 93 |
public List<Integer> get() throws Exception { |
| 94 |
throw e; |
| 95 |
} |
| 96 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 97 |
|
| 98 |
@Override |
| 99 |
public void accept(List<Integer> list, Integer t) { |
| 100 |
list.add(t); |
| 101 |
} |
| 102 |
}) |
| 103 |
.test() |
| 104 |
.assertNoValues() |
| 105 |
.assertError(e) |
| 106 |
.assertNotComplete(); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void collectorFailureDoesNotResultInTwoErrorEmissionsFlowable() { |
| 111 |
try { |
| 112 |
final List<Throwable> list = new CopyOnWriteArrayList<>(); |
| 113 |
RxJavaPlugins.setErrorHandler(addToList(list)); |
| 114 |
final RuntimeException e1 = new RuntimeException(); |
| 115 |
final RuntimeException e2 = new RuntimeException(); |
| 116 |
|
| 117 |
Burst.items(1).error(e2) // |
| 118 |
.collect(supplierListCreator(), biConsumerThrows(e1)) |
| 119 |
.toFlowable() |
| 120 |
.test() // |
| 121 |
.assertError(e1) // |
| 122 |
.assertNotComplete(); |
| 123 |
|
| 124 |
assertEquals(1, list.size()); |
| 125 |
assertEquals(e2, list.get(0).getCause()); |
| 126 |
} finally { |
| 127 |
RxJavaPlugins.reset(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void collectorFailureDoesNotResultInErrorAndCompletedEmissionsFlowable() { |
| 133 |
final RuntimeException e = new RuntimeException(); |
| 134 |
Burst.item(1).create() // |
| 135 |
.collect(supplierListCreator(), biConsumerThrows(e)) // |
| 136 |
.toFlowable() |
| 137 |
.test() // |
| 138 |
.assertError(e) // |
| 139 |
.assertNotComplete(); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void collectorFailureDoesNotResultInErrorAndOnNextEmissionsFlowable() { |
| 144 |
final RuntimeException e = new RuntimeException(); |
| 145 |
final AtomicBoolean added = new AtomicBoolean(); |
| 146 |
BiConsumer<Object, Integer> throwOnFirstOnly = new BiConsumer<Object, Integer>() { |
| 147 |
|
| 148 |
boolean once = true; |
| 149 |
|
| 150 |
@Override |
| 151 |
public void accept(Object o, Integer t) { |
| 152 |
if (once) { |
| 153 |
once = false; |
| 154 |
throw e; |
| 155 |
} else { |
| 156 |
added.set(true); |
| 157 |
} |
| 158 |
} |
| 159 |
}; |
| 160 |
Burst.items(1, 2).create() // |
| 161 |
.collect(supplierListCreator(), throwOnFirstOnly)// |
| 162 |
.toFlowable() |
| 163 |
.test() // |
| 164 |
.assertError(e) // |
| 165 |
.assertNoValues() // |
| 166 |
.assertNotComplete(); |
| 167 |
assertFalse(added.get()); |
| 168 |
} |
| 169 |
|
| 170 |
@Test |
| 171 |
public void collectIntoFlowable() { |
| 172 |
Flowable.just(1, 1, 1, 1, 2) |
| 173 |
.collectInto(new HashSet<>(), new BiConsumer<HashSet<Integer>, Integer>() { |
| 174 |
@Override |
| 175 |
public void accept(HashSet<Integer> s, Integer v) throws Exception { |
| 176 |
s.add(v); |
| 177 |
} |
| 178 |
}) |
| 179 |
.toFlowable() |
| 180 |
.test() |
| 181 |
.assertResult(new HashSet<>(Arrays.asList(1, 2))); |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void collectToList() { |
| 186 |
Single<List<Integer>> o = Flowable.just(1, 2, 3) |
| 187 |
.collect(new Supplier<List<Integer>>() { |
| 188 |
@Override |
| 189 |
public List<Integer> get() { |
| 190 |
return new ArrayList<>(); |
| 191 |
} |
| 192 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 193 |
@Override |
| 194 |
public void accept(List<Integer> list, Integer v) { |
| 195 |
list.add(v); |
| 196 |
} |
| 197 |
}); |
| 198 |
|
| 199 |
List<Integer> list = o.blockingGet(); |
| 200 |
|
| 201 |
assertEquals(3, list.size()); |
| 202 |
assertEquals(1, list.get(0).intValue()); |
| 203 |
assertEquals(2, list.get(1).intValue()); |
| 204 |
assertEquals(3, list.get(2).intValue()); |
| 205 |
|
| 206 |
// test multiple subscribe |
| 207 |
List<Integer> list2 = o.blockingGet(); |
| 208 |
|
| 209 |
assertEquals(3, list2.size()); |
| 210 |
assertEquals(1, list2.get(0).intValue()); |
| 211 |
assertEquals(2, list2.get(1).intValue()); |
| 212 |
assertEquals(3, list2.get(2).intValue()); |
| 213 |
} |
| 214 |
|
| 215 |
@Test |
| 216 |
public void collectToString() { |
| 217 |
String value = Flowable.just(1, 2, 3) |
| 218 |
.collect( |
| 219 |
new Supplier<StringBuilder>() { |
| 220 |
@Override |
| 221 |
public StringBuilder get() { |
| 222 |
return new StringBuilder(); |
| 223 |
} |
| 224 |
}, |
| 225 |
new BiConsumer<StringBuilder, Integer>() { |
| 226 |
@Override |
| 227 |
public void accept(StringBuilder sb, Integer v) { |
| 228 |
if (sb.length() > 0) { |
| 229 |
sb.append("-"); |
| 230 |
} |
| 231 |
sb.append(v); |
| 232 |
} |
| 233 |
}).blockingGet().toString(); |
| 234 |
|
| 235 |
assertEquals("1-2-3", value); |
| 236 |
} |
| 237 |
|
| 238 |
@Test |
| 239 |
public void factoryFailureResultsInErrorEmission() { |
| 240 |
final RuntimeException e = new RuntimeException(); |
| 241 |
Flowable.just(1).collect(new Supplier<List<Integer>>() { |
| 242 |
|
| 243 |
@Override |
| 244 |
public List<Integer> get() throws Exception { |
| 245 |
throw e; |
| 246 |
} |
| 247 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 248 |
|
| 249 |
@Override |
| 250 |
public void accept(List<Integer> list, Integer t) { |
| 251 |
list.add(t); |
| 252 |
} |
| 253 |
}) |
| 254 |
.test() |
| 255 |
.assertNoValues() |
| 256 |
.assertError(e) |
| 257 |
.assertNotComplete(); |
| 258 |
} |
| 259 |
|
| 260 |
@Test |
| 261 |
public void collectorFailureDoesNotResultInTwoErrorEmissions() { |
| 262 |
try { |
| 263 |
final List<Throwable> list = new CopyOnWriteArrayList<>(); |
| 264 |
RxJavaPlugins.setErrorHandler(addToList(list)); |
| 265 |
final RuntimeException e1 = new RuntimeException(); |
| 266 |
final RuntimeException e2 = new RuntimeException(); |
| 267 |
|
| 268 |
Burst.items(1).error(e2) // |
| 269 |
.collect(supplierListCreator(), biConsumerThrows(e1)) // |
| 270 |
.test() // |
| 271 |
.assertError(e1) // |
| 272 |
.assertNotComplete(); |
| 273 |
|
| 274 |
assertEquals(1, list.size()); |
| 275 |
assertEquals(e2, list.get(0).getCause()); |
| 276 |
} finally { |
| 277 |
RxJavaPlugins.reset(); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
@Test |
| 282 |
public void collectorFailureDoesNotResultInErrorAndCompletedEmissions() { |
| 283 |
final RuntimeException e = new RuntimeException(); |
| 284 |
Burst.item(1).create() // |
| 285 |
.collect(supplierListCreator(), biConsumerThrows(e)) // |
| 286 |
.test() // |
| 287 |
.assertError(e) // |
| 288 |
.assertNotComplete(); |
| 289 |
} |
| 290 |
|
| 291 |
@Test |
| 292 |
public void collectorFailureDoesNotResultInErrorAndOnNextEmissions() { |
| 293 |
final RuntimeException e = new RuntimeException(); |
| 294 |
final AtomicBoolean added = new AtomicBoolean(); |
| 295 |
BiConsumer<Object, Integer> throwOnFirstOnly = new BiConsumer<Object, Integer>() { |
| 296 |
|
| 297 |
boolean once = true; |
| 298 |
|
| 299 |
@Override |
| 300 |
public void accept(Object o, Integer t) { |
| 301 |
if (once) { |
| 302 |
once = false; |
| 303 |
throw e; |
| 304 |
} else { |
| 305 |
added.set(true); |
| 306 |
} |
| 307 |
} |
| 308 |
}; |
| 309 |
Burst.items(1, 2).create() // |
| 310 |
.collect(supplierListCreator(), throwOnFirstOnly)// |
| 311 |
.test() // |
| 312 |
.assertError(e) // |
| 313 |
.assertNoValues() // |
| 314 |
.assertNotComplete(); |
| 315 |
assertFalse(added.get()); |
| 316 |
} |
| 317 |
|
| 318 |
@Test |
| 319 |
public void collectInto() { |
| 320 |
Flowable.just(1, 1, 1, 1, 2) |
| 321 |
.collectInto(new HashSet<>(), new BiConsumer<HashSet<Integer>, Integer>() { |
| 322 |
@Override |
| 323 |
public void accept(HashSet<Integer> s, Integer v) throws Exception { |
| 324 |
s.add(v); |
| 325 |
} |
| 326 |
}) |
| 327 |
.test() |
| 328 |
.assertResult(new HashSet<>(Arrays.asList(1, 2))); |
| 329 |
} |
| 330 |
|
| 331 |
@Test |
| 332 |
public void dispose() { |
| 333 |
TestHelper.checkDisposed(Flowable.just(1, 2) |
| 334 |
.collect(Functions.justSupplier(new ArrayList<>()), new BiConsumer<ArrayList<Integer>, Integer>() { |
| 335 |
@Override |
| 336 |
public void accept(ArrayList<Integer> a, Integer b) throws Exception { |
| 337 |
a.add(b); |
| 338 |
} |
| 339 |
})); |
| 340 |
|
| 341 |
TestHelper.checkDisposed(Flowable.just(1, 2) |
| 342 |
.collect(Functions.justSupplier(new ArrayList<>()), new BiConsumer<ArrayList<Integer>, Integer>() { |
| 343 |
@Override |
| 344 |
public void accept(ArrayList<Integer> a, Integer b) throws Exception { |
| 345 |
a.add(b); |
| 346 |
} |
| 347 |
}).toFlowable()); |
| 348 |
} |
| 349 |
|
| 350 |
@Test |
| 351 |
public void doubleOnSubscribe() { |
| 352 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Integer>, Flowable<ArrayList<Integer>>>() { |
| 353 |
@Override |
| 354 |
public Flowable<ArrayList<Integer>> apply(Flowable<Integer> f) throws Exception { |
| 355 |
return f.collect(Functions.justSupplier(new ArrayList<>()), |
| 356 |
new BiConsumer<ArrayList<Integer>, Integer>() { |
| 357 |
@Override |
| 358 |
public void accept(ArrayList<Integer> a, Integer b) throws Exception { |
| 359 |
a.add(b); |
| 360 |
} |
| 361 |
}).toFlowable(); |
| 362 |
} |
| 363 |
}); |
| 364 |
TestHelper.checkDoubleOnSubscribeFlowableToSingle(new Function<Flowable<Integer>, Single<ArrayList<Integer>>>() { |
| 365 |
@Override |
| 366 |
public Single<ArrayList<Integer>> apply(Flowable<Integer> f) throws Exception { |
| 367 |
return f.collect(Functions.justSupplier(new ArrayList<>()), |
| 368 |
new BiConsumer<ArrayList<Integer>, Integer>() { |
| 369 |
@Override |
| 370 |
public void accept(ArrayList<Integer> a, Integer b) throws Exception { |
| 371 |
a.add(b); |
| 372 |
} |
| 373 |
}); |
| 374 |
} |
| 375 |
}); |
| 376 |
} |
| 377 |
} |