|
8 | 8 | import java.util.Arrays; |
9 | 9 | import java.util.Collections; |
10 | 10 | import java.util.function.Function; |
| 11 | +import java.util.stream.Stream; |
11 | 12 |
|
12 | 13 | import static org.assertj.core.api.Assertions.assertThat; |
| 14 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
13 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; |
14 | 16 | import static org.junit.jupiter.api.Assertions.assertTrue; |
15 | 17 |
|
@@ -368,4 +370,112 @@ void shouldHandleEmptyStreamWhenConvertingToStream() { |
368 | 370 | assertTrue(result.isEmpty()); |
369 | 371 | } |
370 | 372 | } |
| 373 | + |
| 374 | + @Nested |
| 375 | + class CollectToMap { |
| 376 | + @Test |
| 377 | + @DisplayName("Should convert Collection to Map using key and value mappers") |
| 378 | + void shouldConvertCollectionToMapUsingMappers() { |
| 379 | + // given |
| 380 | + var items = Arrays.asList("a", "bb", "ccc"); |
| 381 | + |
| 382 | + // when |
| 383 | + var result = CollectUtil.collectToMap(items, String::length, Function.identity()); |
| 384 | + |
| 385 | + // then |
| 386 | + assertThat(result) |
| 387 | + .containsEntry(1, "a") |
| 388 | + .containsEntry(2, "bb") |
| 389 | + .containsEntry(3, "ccc") |
| 390 | + .hasSize(3); |
| 391 | + } |
| 392 | + |
| 393 | + @Test |
| 394 | + @DisplayName("Should throw on duplicate keys according to Collectors.toMap default behavior") |
| 395 | + void shouldThrowOnDuplicateKeys() { |
| 396 | + // given |
| 397 | + var items = Arrays.asList("a", "b"); // both have length of 1 |
| 398 | + |
| 399 | + // when / then |
| 400 | + assertThatIllegalStateException().isThrownBy( |
| 401 | + () -> |
| 402 | + CollectUtil.collectToMap( |
| 403 | + items, |
| 404 | + String::length, |
| 405 | + Function.identity() |
| 406 | + ) |
| 407 | + ); |
| 408 | + } |
| 409 | + |
| 410 | + @Test |
| 411 | + @DisplayName("Should convert Streamable to Map using key and value mappers") |
| 412 | + void shouldConvertStreamableToMapUsingMappers() { |
| 413 | + // given |
| 414 | + var items = Streamable.of("x", "yy"); |
| 415 | + |
| 416 | + // when |
| 417 | + var result = CollectUtil.collectToMap(items, String::length, Function.identity()); |
| 418 | + |
| 419 | + // then |
| 420 | + assertThat(result) |
| 421 | + .containsEntry(1, "x") |
| 422 | + .containsEntry(2, "yy") |
| 423 | + .hasSize(2); |
| 424 | + } |
| 425 | + |
| 426 | + @Test |
| 427 | + @DisplayName("Should convert Stream to Map using key and value mappers") |
| 428 | + void shouldConvertStreamToMapUsingMappers() { |
| 429 | + // given |
| 430 | + var items = Stream.of("m", "nn"); |
| 431 | + |
| 432 | + // when |
| 433 | + var result = CollectUtil.collectToMap(items, String::length, Function.identity()); |
| 434 | + |
| 435 | + // then |
| 436 | + assertThat(result) |
| 437 | + .containsEntry(1, "m") |
| 438 | + .containsEntry(2, "nn") |
| 439 | + .hasSize(2); |
| 440 | + } |
| 441 | + |
| 442 | + @Test |
| 443 | + @DisplayName("Should return empty map for empty collection") |
| 444 | + void shouldReturnEmptyMapForEmptyCollection() { |
| 445 | + // given |
| 446 | + var items = Collections.<String>emptyList(); |
| 447 | + |
| 448 | + // when |
| 449 | + var result = CollectUtil.collectToMap(items, String::length, Function.identity()); |
| 450 | + |
| 451 | + // then |
| 452 | + assertThat(result).isEmpty(); |
| 453 | + } |
| 454 | + |
| 455 | + @Test |
| 456 | + @DisplayName("Should return empty map for empty streamable") |
| 457 | + void shouldReturnEmptyMapForEmptyStreamable() { |
| 458 | + // given |
| 459 | + var items = Streamable.<String>empty(); |
| 460 | + |
| 461 | + // when |
| 462 | + var result = CollectUtil.collectToMap(items, String::length, Function.identity()); |
| 463 | + |
| 464 | + // then |
| 465 | + assertThat(result).isEmpty(); |
| 466 | + } |
| 467 | + |
| 468 | + @Test |
| 469 | + @DisplayName("Should return empty map for empty stream") |
| 470 | + void shouldReturnEmptyMapForEmptyStream() { |
| 471 | + // given |
| 472 | + var items = Stream.<String>empty(); |
| 473 | + |
| 474 | + // when |
| 475 | + var result = CollectUtil.collectToMap(items, String::length, Function.identity()); |
| 476 | + |
| 477 | + // then |
| 478 | + assertThat(result).isEmpty(); |
| 479 | + } |
| 480 | + } |
371 | 481 | } |
0 commit comments