| 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.internal.operators.observable; |
| 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.core.Observable; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public final class ObservableCollectTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void collectToListObservable() { |
| 35 |
Observable<List<Integer>> o = Observable.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 |
}).toObservable(); |
| 47 |
|
| 48 |
List<Integer> list = o.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 = o.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 collectToStringObservable() { |
| 66 |
String value = Observable.just(1, 2, 3).collect(new Supplier<StringBuilder>() { |
| 67 |
@Override |
| 68 |
public StringBuilder get() { |
| 69 |
return new StringBuilder(); |
| 70 |
} |
| 71 |
}, |
| 72 |
new BiConsumer<StringBuilder, Integer>() { |
| 73 |
@Override |
| 74 |
public void accept(StringBuilder sb, Integer v) { |
| 75 |
if (sb.length() > 0) { |
| 76 |
sb.append("-"); |
| 77 |
} |
| 78 |
sb.append(v); |
| 79 |
} |
| 80 |
}).toObservable().blockingLast().toString(); |
| 81 |
|
| 82 |
assertEquals("1-2-3", value); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void collectorFailureDoesNotResultInTwoErrorEmissionsObservable() { |
| 87 |
try { |
| 88 |
final List<Throwable> list = new CopyOnWriteArrayList<>(); |
| 89 |
RxJavaPlugins.setErrorHandler(addToList(list)); |
| 90 |
final RuntimeException e1 = new RuntimeException(); |
| 91 |
final RuntimeException e2 = new RuntimeException(); |
| 92 |
|
| 93 |
Burst.items(1).error(e2) // |
| 94 |
.collect(supplierListCreator(), biConsumerThrows(e1)) // |
| 95 |
.toObservable() |
| 96 |
.test() // |
| 97 |
.assertError(e1) // |
| 98 |
.assertNotComplete(); |
| 99 |
|
| 100 |
assertEquals(1, list.size()); |
| 101 |
assertEquals(e2, list.get(0).getCause()); |
| 102 |
} finally { |
| 103 |
RxJavaPlugins.reset(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void collectorFailureDoesNotResultInErrorAndCompletedEmissionsObservable() { |
| 109 |
final RuntimeException e = new RuntimeException(); |
| 110 |
Burst.item(1).create() // |
| 111 |
.collect(supplierListCreator(), biConsumerThrows(e)) // |
| 112 |
.toObservable() |
| 113 |
.test() // |
| 114 |
.assertError(e) // |
| 115 |
.assertNotComplete(); |
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void collectorFailureDoesNotResultInErrorAndOnNextEmissionsObservable() { |
| 120 |
final RuntimeException e = new RuntimeException(); |
| 121 |
final AtomicBoolean added = new AtomicBoolean(); |
| 122 |
BiConsumer<Object, Integer> throwOnFirstOnly = new BiConsumer<Object, Integer>() { |
| 123 |
|
| 124 |
boolean once = true; |
| 125 |
|
| 126 |
@Override |
| 127 |
public void accept(Object o, Integer t) { |
| 128 |
if (once) { |
| 129 |
once = false; |
| 130 |
throw e; |
| 131 |
} else { |
| 132 |
added.set(true); |
| 133 |
} |
| 134 |
} |
| 135 |
}; |
| 136 |
Burst.items(1, 2).create() // |
| 137 |
.collect(supplierListCreator(), throwOnFirstOnly)// |
| 138 |
.test() // |
| 139 |
.assertError(e) // |
| 140 |
.assertNoValues() // |
| 141 |
.assertNotComplete(); |
| 142 |
assertFalse(added.get()); |
| 143 |
} |
| 144 |
|
| 145 |
@Test |
| 146 |
public void collectIntoObservable() { |
| 147 |
Observable.just(1, 1, 1, 1, 2) |
| 148 |
.collectInto(new HashSet<>(), new BiConsumer<HashSet<Integer>, Integer>() { |
| 149 |
@Override |
| 150 |
public void accept(HashSet<Integer> s, Integer v) throws Exception { |
| 151 |
s.add(v); |
| 152 |
} |
| 153 |
}).toObservable() |
| 154 |
.test() |
| 155 |
.assertResult(new HashSet<>(Arrays.asList(1, 2))); |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void collectToList() { |
| 160 |
Single<List<Integer>> o = Observable.just(1, 2, 3) |
| 161 |
.collect(new Supplier<List<Integer>>() { |
| 162 |
@Override |
| 163 |
public List<Integer> get() { |
| 164 |
return new ArrayList<>(); |
| 165 |
} |
| 166 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 167 |
@Override |
| 168 |
public void accept(List<Integer> list, Integer v) { |
| 169 |
list.add(v); |
| 170 |
} |
| 171 |
}); |
| 172 |
|
| 173 |
List<Integer> list = o.blockingGet(); |
| 174 |
|
| 175 |
assertEquals(3, list.size()); |
| 176 |
assertEquals(1, list.get(0).intValue()); |
| 177 |
assertEquals(2, list.get(1).intValue()); |
| 178 |
assertEquals(3, list.get(2).intValue()); |
| 179 |
|
| 180 |
// test multiple subscribe |
| 181 |
List<Integer> list2 = o.blockingGet(); |
| 182 |
|
| 183 |
assertEquals(3, list2.size()); |
| 184 |
assertEquals(1, list2.get(0).intValue()); |
| 185 |
assertEquals(2, list2.get(1).intValue()); |
| 186 |
assertEquals(3, list2.get(2).intValue()); |
| 187 |
} |
| 188 |
|
| 189 |
@Test |
| 190 |
public void collectToString() { |
| 191 |
String value = Observable.just(1, 2, 3).collect(new Supplier<StringBuilder>() { |
| 192 |
@Override |
| 193 |
public StringBuilder get() { |
| 194 |
return new StringBuilder(); |
| 195 |
} |
| 196 |
}, |
| 197 |
new BiConsumer<StringBuilder, Integer>() { |
| 198 |
@Override |
| 199 |
public void accept(StringBuilder sb, Integer v) { |
| 200 |
if (sb.length() > 0) { |
| 201 |
sb.append("-"); |
| 202 |
} |
| 203 |
sb.append(v); |
| 204 |
} |
| 205 |
}).blockingGet().toString(); |
| 206 |
|
| 207 |
assertEquals("1-2-3", value); |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void collectorFailureDoesNotResultInTwoErrorEmissions() { |
| 212 |
try { |
| 213 |
final List<Throwable> list = new CopyOnWriteArrayList<>(); |
| 214 |
RxJavaPlugins.setErrorHandler(addToList(list)); |
| 215 |
final RuntimeException e1 = new RuntimeException(); |
| 216 |
final RuntimeException e2 = new RuntimeException(); |
| 217 |
|
| 218 |
Burst.items(1).error(e2) // |
| 219 |
.collect(supplierListCreator(), biConsumerThrows(e1)) // |
| 220 |
.test() // |
| 221 |
.assertError(e1) // |
| 222 |
.assertNotComplete(); |
| 223 |
|
| 224 |
assertEquals(1, list.size()); |
| 225 |
assertEquals(e2, list.get(0).getCause()); |
| 226 |
} finally { |
| 227 |
RxJavaPlugins.reset(); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void collectorFailureDoesNotResultInErrorAndCompletedEmissions() { |
| 233 |
final RuntimeException e = new RuntimeException(); |
| 234 |
Burst.item(1).create() // |
| 235 |
.collect(supplierListCreator(), biConsumerThrows(e)) // |
| 236 |
.test() // |
| 237 |
.assertError(e) // |
| 238 |
.assertNotComplete(); |
| 239 |
} |
| 240 |
|
| 241 |
@Test |
| 242 |
public void collectorFailureDoesNotResultInErrorAndOnNextEmissions() { |
| 243 |
final RuntimeException e = new RuntimeException(); |
| 244 |
final AtomicBoolean added = new AtomicBoolean(); |
| 245 |
BiConsumer<Object, Integer> throwOnFirstOnly = new BiConsumer<Object, Integer>() { |
| 246 |
|
| 247 |
boolean once = true; |
| 248 |
|
| 249 |
@Override |
| 250 |
public void accept(Object o, Integer t) { |
| 251 |
if (once) { |
| 252 |
once = false; |
| 253 |
throw e; |
| 254 |
} else { |
| 255 |
added.set(true); |
| 256 |
} |
| 257 |
} |
| 258 |
}; |
| 259 |
Burst.items(1, 2).create() // |
| 260 |
.collect(supplierListCreator(), throwOnFirstOnly)// |
| 261 |
.test() // |
| 262 |
.assertError(e) // |
| 263 |
.assertNoValues() // |
| 264 |
.assertNotComplete(); |
| 265 |
assertFalse(added.get()); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void collectInto() { |
| 270 |
Observable.just(1, 1, 1, 1, 2) |
| 271 |
.collectInto(new HashSet<>(), new BiConsumer<HashSet<Integer>, Integer>() { |
| 272 |
@Override |
| 273 |
public void accept(HashSet<Integer> s, Integer v) throws Exception { |
| 274 |
s.add(v); |
| 275 |
} |
| 276 |
}) |
| 277 |
.test() |
| 278 |
.assertResult(new HashSet<>(Arrays.asList(1, 2))); |
| 279 |
} |
| 280 |
|
| 281 |
@Test |
| 282 |
public void dispose() { |
| 283 |
TestHelper.checkDisposed(Observable.range(1, 3).collect(new Supplier<List<Integer>>() { |
| 284 |
@Override |
| 285 |
public List<Integer> get() throws Exception { |
| 286 |
return new ArrayList<>(); |
| 287 |
} |
| 288 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 289 |
@Override |
| 290 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 291 |
a.add(b); |
| 292 |
} |
| 293 |
})); |
| 294 |
|
| 295 |
TestHelper.checkDisposed(Observable.range(1, 3).collect(new Supplier<List<Integer>>() { |
| 296 |
@Override |
| 297 |
public List<Integer> get() throws Exception { |
| 298 |
return new ArrayList<>(); |
| 299 |
} |
| 300 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 301 |
@Override |
| 302 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 303 |
a.add(b); |
| 304 |
} |
| 305 |
}).toObservable()); |
| 306 |
} |
| 307 |
|
| 308 |
@Test |
| 309 |
public void doubleOnSubscribe() { |
| 310 |
TestHelper.checkDoubleOnSubscribeObservableToSingle(new Function<Observable<Integer>, SingleSource<List<Integer>>>() { |
| 311 |
@Override |
| 312 |
public SingleSource<List<Integer>> apply(Observable<Integer> o) throws Exception { |
| 313 |
return o.collect(new Supplier<List<Integer>>() { |
| 314 |
@Override |
| 315 |
public List<Integer> get() throws Exception { |
| 316 |
return new ArrayList<>(); |
| 317 |
} |
| 318 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 319 |
@Override |
| 320 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 321 |
a.add(b); |
| 322 |
} |
| 323 |
}); |
| 324 |
} |
| 325 |
}); |
| 326 |
|
| 327 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Integer>, ObservableSource<List<Integer>>>() { |
| 328 |
@Override |
| 329 |
public ObservableSource<List<Integer>> apply(Observable<Integer> o) throws Exception { |
| 330 |
return o.collect(new Supplier<List<Integer>>() { |
| 331 |
@Override |
| 332 |
public List<Integer> get() throws Exception { |
| 333 |
return new ArrayList<>(); |
| 334 |
} |
| 335 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 336 |
@Override |
| 337 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 338 |
a.add(b); |
| 339 |
} |
| 340 |
}).toObservable(); |
| 341 |
} |
| 342 |
}); |
| 343 |
} |
| 344 |
|
| 345 |
@Test |
| 346 |
public void badSource() { |
| 347 |
TestHelper.checkBadSourceObservable(new Function<Observable<Integer>, Object>() { |
| 348 |
@Override |
| 349 |
public Object apply(Observable<Integer> o) throws Exception { |
| 350 |
return o.collect(new Supplier<List<Integer>>() { |
| 351 |
@Override |
| 352 |
public List<Integer> get() throws Exception { |
| 353 |
return new ArrayList<>(); |
| 354 |
} |
| 355 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 356 |
@Override |
| 357 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 358 |
a.add(b); |
| 359 |
} |
| 360 |
}).toObservable(); |
| 361 |
} |
| 362 |
}, false, 1, 2, Arrays.asList(1)); |
| 363 |
} |
| 364 |
} |