| 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.flowable; |
| 15 |
|
| 16 |
import static org.mockito.ArgumentMatchers.any; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.*; |
| 20 |
|
| 21 |
import org.junit.*; |
| 22 |
import org.reactivestreams.Subscriber; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 27 |
|
| 28 |
public class FlowableToMapTest extends RxJavaTest { |
| 29 |
Subscriber<Object> objectSubscriber; |
| 30 |
SingleObserver<Object> singleObserver; |
| 31 |
|
| 32 |
@Before |
| 33 |
public void before() { |
| 34 |
objectSubscriber = TestHelper.mockSubscriber(); |
| 35 |
singleObserver = TestHelper.mockSingleObserver(); |
| 36 |
} |
| 37 |
|
| 38 |
Function<String, Integer> lengthFunc = new Function<String, Integer>() { |
| 39 |
@Override |
| 40 |
public Integer apply(String t1) { |
| 41 |
return t1.length(); |
| 42 |
} |
| 43 |
}; |
| 44 |
Function<String, String> duplicate = new Function<String, String>() { |
| 45 |
@Override |
| 46 |
public String apply(String t1) { |
| 47 |
return t1 + t1; |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
@Test |
| 52 |
public void toMapFlowable() { |
| 53 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 54 |
|
| 55 |
Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc).toFlowable(); |
| 56 |
|
| 57 |
Map<Integer, String> expected = new HashMap<>(); |
| 58 |
expected.put(1, "a"); |
| 59 |
expected.put(2, "bb"); |
| 60 |
expected.put(3, "ccc"); |
| 61 |
expected.put(4, "dddd"); |
| 62 |
|
| 63 |
mapped.subscribe(objectSubscriber); |
| 64 |
|
| 65 |
verify(objectSubscriber, never()).onError(any(Throwable.class)); |
| 66 |
verify(objectSubscriber, times(1)).onNext(expected); |
| 67 |
verify(objectSubscriber, times(1)).onComplete(); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void toMapWithValueSelectorFlowable() { |
| 72 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 73 |
|
| 74 |
Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc, duplicate).toFlowable(); |
| 75 |
|
| 76 |
Map<Integer, String> expected = new HashMap<>(); |
| 77 |
expected.put(1, "aa"); |
| 78 |
expected.put(2, "bbbb"); |
| 79 |
expected.put(3, "cccccc"); |
| 80 |
expected.put(4, "dddddddd"); |
| 81 |
|
| 82 |
mapped.subscribe(objectSubscriber); |
| 83 |
|
| 84 |
verify(objectSubscriber, never()).onError(any(Throwable.class)); |
| 85 |
verify(objectSubscriber, times(1)).onNext(expected); |
| 86 |
verify(objectSubscriber, times(1)).onComplete(); |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void toMapWithErrorFlowable() { |
| 91 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 92 |
|
| 93 |
Function<String, Integer> lengthFuncErr = new Function<String, Integer>() { |
| 94 |
@Override |
| 95 |
public Integer apply(String t1) { |
| 96 |
if ("bb".equals(t1)) { |
| 97 |
throw new RuntimeException("Forced Failure"); |
| 98 |
} |
| 99 |
return t1.length(); |
| 100 |
} |
| 101 |
}; |
| 102 |
Flowable<Map<Integer, String>> mapped = source.toMap(lengthFuncErr).toFlowable(); |
| 103 |
|
| 104 |
Map<Integer, String> expected = new HashMap<>(); |
| 105 |
expected.put(1, "a"); |
| 106 |
expected.put(2, "bb"); |
| 107 |
expected.put(3, "ccc"); |
| 108 |
expected.put(4, "dddd"); |
| 109 |
|
| 110 |
mapped.subscribe(objectSubscriber); |
| 111 |
|
| 112 |
verify(objectSubscriber, never()).onNext(expected); |
| 113 |
verify(objectSubscriber, never()).onComplete(); |
| 114 |
verify(objectSubscriber, times(1)).onError(any(Throwable.class)); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void toMapWithErrorInValueSelectorFlowable() { |
| 120 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 121 |
|
| 122 |
Function<String, String> duplicateErr = new Function<String, String>() { |
| 123 |
@Override |
| 124 |
public String apply(String t1) { |
| 125 |
if ("bb".equals(t1)) { |
| 126 |
throw new RuntimeException("Forced failure"); |
| 127 |
} |
| 128 |
return t1 + t1; |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc, duplicateErr).toFlowable(); |
| 133 |
|
| 134 |
Map<Integer, String> expected = new HashMap<>(); |
| 135 |
expected.put(1, "aa"); |
| 136 |
expected.put(2, "bbbb"); |
| 137 |
expected.put(3, "cccccc"); |
| 138 |
expected.put(4, "dddddddd"); |
| 139 |
|
| 140 |
mapped.subscribe(objectSubscriber); |
| 141 |
|
| 142 |
verify(objectSubscriber, never()).onNext(expected); |
| 143 |
verify(objectSubscriber, never()).onComplete(); |
| 144 |
verify(objectSubscriber, times(1)).onError(any(Throwable.class)); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void toMapWithFactoryFlowable() { |
| 150 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 151 |
|
| 152 |
Supplier<Map<Integer, String>> mapFactory = new Supplier<Map<Integer, String>>() { |
| 153 |
@Override |
| 154 |
public Map<Integer, String> get() { |
| 155 |
return new LinkedHashMap<Integer, String>() { |
| 156 |
|
| 157 |
private static final long serialVersionUID = -3296811238780863394L; |
| 158 |
|
| 159 |
@Override |
| 160 |
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) { |
| 161 |
return size() > 3; |
| 162 |
} |
| 163 |
}; |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
Function<String, Integer> lengthFunc = new Function<String, Integer>() { |
| 168 |
@Override |
| 169 |
public Integer apply(String t1) { |
| 170 |
return t1.length(); |
| 171 |
} |
| 172 |
}; |
| 173 |
Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc, new Function<String, String>() { |
| 174 |
@Override |
| 175 |
public String apply(String v) { |
| 176 |
return v; |
| 177 |
} |
| 178 |
}, mapFactory).toFlowable(); |
| 179 |
|
| 180 |
Map<Integer, String> expected = new LinkedHashMap<>(); |
| 181 |
expected.put(2, "bb"); |
| 182 |
expected.put(3, "ccc"); |
| 183 |
expected.put(4, "dddd"); |
| 184 |
|
| 185 |
mapped.subscribe(objectSubscriber); |
| 186 |
|
| 187 |
verify(objectSubscriber, never()).onError(any(Throwable.class)); |
| 188 |
verify(objectSubscriber, times(1)).onNext(expected); |
| 189 |
verify(objectSubscriber, times(1)).onComplete(); |
| 190 |
} |
| 191 |
|
| 192 |
@Test |
| 193 |
public void toMapWithErrorThrowingFactoryFlowable() { |
| 194 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 195 |
|
| 196 |
Supplier<Map<Integer, String>> mapFactory = new Supplier<Map<Integer, String>>() { |
| 197 |
@Override |
| 198 |
public Map<Integer, String> get() { |
| 199 |
throw new RuntimeException("Forced failure"); |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
Function<String, Integer> lengthFunc = new Function<String, Integer>() { |
| 204 |
@Override |
| 205 |
public Integer apply(String t1) { |
| 206 |
return t1.length(); |
| 207 |
} |
| 208 |
}; |
| 209 |
Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc, new Function<String, String>() { |
| 210 |
@Override |
| 211 |
public String apply(String v) { |
| 212 |
return v; |
| 213 |
} |
| 214 |
}, mapFactory).toFlowable(); |
| 215 |
|
| 216 |
Map<Integer, String> expected = new LinkedHashMap<>(); |
| 217 |
expected.put(2, "bb"); |
| 218 |
expected.put(3, "ccc"); |
| 219 |
expected.put(4, "dddd"); |
| 220 |
|
| 221 |
mapped.subscribe(objectSubscriber); |
| 222 |
|
| 223 |
verify(objectSubscriber, never()).onNext(expected); |
| 224 |
verify(objectSubscriber, never()).onComplete(); |
| 225 |
verify(objectSubscriber, times(1)).onError(any(Throwable.class)); |
| 226 |
} |
| 227 |
|
| 228 |
@Test |
| 229 |
public void toMap() { |
| 230 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 231 |
|
| 232 |
Single<Map<Integer, String>> mapped = source.toMap(lengthFunc); |
| 233 |
|
| 234 |
Map<Integer, String> expected = new HashMap<>(); |
| 235 |
expected.put(1, "a"); |
| 236 |
expected.put(2, "bb"); |
| 237 |
expected.put(3, "ccc"); |
| 238 |
expected.put(4, "dddd"); |
| 239 |
|
| 240 |
mapped.subscribe(singleObserver); |
| 241 |
|
| 242 |
verify(singleObserver, never()).onError(any(Throwable.class)); |
| 243 |
verify(singleObserver, times(1)).onSuccess(expected); |
| 244 |
} |
| 245 |
|
| 246 |
@Test |
| 247 |
public void toMapWithValueSelector() { |
| 248 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 249 |
|
| 250 |
Single<Map<Integer, String>> mapped = source.toMap(lengthFunc, duplicate); |
| 251 |
|
| 252 |
Map<Integer, String> expected = new HashMap<>(); |
| 253 |
expected.put(1, "aa"); |
| 254 |
expected.put(2, "bbbb"); |
| 255 |
expected.put(3, "cccccc"); |
| 256 |
expected.put(4, "dddddddd"); |
| 257 |
|
| 258 |
mapped.subscribe(singleObserver); |
| 259 |
|
| 260 |
verify(singleObserver, never()).onError(any(Throwable.class)); |
| 261 |
verify(singleObserver, times(1)).onSuccess(expected); |
| 262 |
} |
| 263 |
|
| 264 |
@Test |
| 265 |
public void toMapWithError() { |
| 266 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 267 |
|
| 268 |
Function<String, Integer> lengthFuncErr = new Function<String, Integer>() { |
| 269 |
@Override |
| 270 |
public Integer apply(String t1) { |
| 271 |
if ("bb".equals(t1)) { |
| 272 |
throw new RuntimeException("Forced Failure"); |
| 273 |
} |
| 274 |
return t1.length(); |
| 275 |
} |
| 276 |
}; |
| 277 |
Single<Map<Integer, String>> mapped = source.toMap(lengthFuncErr); |
| 278 |
|
| 279 |
Map<Integer, String> expected = new HashMap<>(); |
| 280 |
expected.put(1, "a"); |
| 281 |
expected.put(2, "bb"); |
| 282 |
expected.put(3, "ccc"); |
| 283 |
expected.put(4, "dddd"); |
| 284 |
|
| 285 |
mapped.subscribe(singleObserver); |
| 286 |
|
| 287 |
verify(singleObserver, never()).onSuccess(expected); |
| 288 |
verify(singleObserver, times(1)).onError(any(Throwable.class)); |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
@Test |
| 293 |
public void toMapWithErrorInValueSelector() { |
| 294 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 295 |
|
| 296 |
Function<String, String> duplicateErr = new Function<String, String>() { |
| 297 |
@Override |
| 298 |
public String apply(String t1) { |
| 299 |
if ("bb".equals(t1)) { |
| 300 |
throw new RuntimeException("Forced failure"); |
| 301 |
} |
| 302 |
return t1 + t1; |
| 303 |
} |
| 304 |
}; |
| 305 |
|
| 306 |
Single<Map<Integer, String>> mapped = source.toMap(lengthFunc, duplicateErr); |
| 307 |
|
| 308 |
Map<Integer, String> expected = new HashMap<>(); |
| 309 |
expected.put(1, "aa"); |
| 310 |
expected.put(2, "bbbb"); |
| 311 |
expected.put(3, "cccccc"); |
| 312 |
expected.put(4, "dddddddd"); |
| 313 |
|
| 314 |
mapped.subscribe(singleObserver); |
| 315 |
|
| 316 |
verify(singleObserver, never()).onSuccess(expected); |
| 317 |
verify(singleObserver, times(1)).onError(any(Throwable.class)); |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
@Test |
| 322 |
public void toMapWithFactory() { |
| 323 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 324 |
|
| 325 |
Supplier<Map<Integer, String>> mapFactory = new Supplier<Map<Integer, String>>() { |
| 326 |
@Override |
| 327 |
public Map<Integer, String> get() { |
| 328 |
return new LinkedHashMap<Integer, String>() { |
| 329 |
|
| 330 |
private static final long serialVersionUID = -3296811238780863394L; |
| 331 |
|
| 332 |
@Override |
| 333 |
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) { |
| 334 |
return size() > 3; |
| 335 |
} |
| 336 |
}; |
| 337 |
} |
| 338 |
}; |
| 339 |
|
| 340 |
Function<String, Integer> lengthFunc = new Function<String, Integer>() { |
| 341 |
@Override |
| 342 |
public Integer apply(String t1) { |
| 343 |
return t1.length(); |
| 344 |
} |
| 345 |
}; |
| 346 |
Single<Map<Integer, String>> mapped = source.toMap(lengthFunc, new Function<String, String>() { |
| 347 |
@Override |
| 348 |
public String apply(String v) { |
| 349 |
return v; |
| 350 |
} |
| 351 |
}, mapFactory); |
| 352 |
|
| 353 |
Map<Integer, String> expected = new LinkedHashMap<>(); |
| 354 |
expected.put(2, "bb"); |
| 355 |
expected.put(3, "ccc"); |
| 356 |
expected.put(4, "dddd"); |
| 357 |
|
| 358 |
mapped.subscribe(singleObserver); |
| 359 |
|
| 360 |
verify(singleObserver, never()).onError(any(Throwable.class)); |
| 361 |
verify(singleObserver, times(1)).onSuccess(expected); |
| 362 |
} |
| 363 |
|
| 364 |
@Test |
| 365 |
public void toMapWithErrorThrowingFactory() { |
| 366 |
Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd"); |
| 367 |
|
| 368 |
Supplier<Map<Integer, String>> mapFactory = new Supplier<Map<Integer, String>>() { |
| 369 |
@Override |
| 370 |
public Map<Integer, String> get() { |
| 371 |
throw new RuntimeException("Forced failure"); |
| 372 |
} |
| 373 |
}; |
| 374 |
|
| 375 |
Function<String, Integer> lengthFunc = new Function<String, Integer>() { |
| 376 |
@Override |
| 377 |
public Integer apply(String t1) { |
| 378 |
return t1.length(); |
| 379 |
} |
| 380 |
}; |
| 381 |
Single<Map<Integer, String>> mapped = source.toMap(lengthFunc, new Function<String, String>() { |
| 382 |
@Override |
| 383 |
public String apply(String v) { |
| 384 |
return v; |
| 385 |
} |
| 386 |
}, mapFactory); |
| 387 |
|
| 388 |
Map<Integer, String> expected = new LinkedHashMap<>(); |
| 389 |
expected.put(2, "bb"); |
| 390 |
expected.put(3, "ccc"); |
| 391 |
expected.put(4, "dddd"); |
| 392 |
|
| 393 |
mapped.subscribe(singleObserver); |
| 394 |
|
| 395 |
verify(singleObserver, never()).onSuccess(expected); |
| 396 |
verify(singleObserver, times(1)).onError(any(Throwable.class)); |
| 397 |
} |
| 398 |
|
| 399 |
} |