[JAVA-26433] Formatting (#15179)

This commit is contained in:
panos-kakos 2023-11-13 09:27:36 +00:00 committed by GitHub
parent 5e681a24dd
commit 7d0c744c7c

View File

@ -3,6 +3,7 @@ package com.baeldung.streams.collectors;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.junit.Test; import org.junit.Test;
import java.util.*; import java.util.*;
@ -23,62 +24,72 @@ public class Java8CollectorsUnitTest {
private final List<String> listWithDuplicates = Arrays.asList("a", "bb", "c", "d", "bb"); private final List<String> listWithDuplicates = Arrays.asList("a", "bb", "c", "d", "bb");
@Test @Test
public void whenCollectingToList_shouldCollectToList() throws Exception { public void whenCollectingToList_shouldCollectToList() {
final List<String> result = givenList.stream().collect(toList()); final List<String> result = givenList.stream()
.collect(toList());
assertThat(result).containsAll(givenList); assertThat(result).containsAll(givenList);
} }
@Test @Test
public void whenCollectingToUnmodifiableList_shouldCollectToUnmodifiableList() { public void whenCollectingToUnmodifiableList_shouldCollectToUnmodifiableList() {
final List<String> result = givenList.stream().collect(toUnmodifiableList()); final List<String> result = givenList.stream()
.collect(toUnmodifiableList());
assertThatThrownBy(() -> result.add("foo")) assertThatThrownBy(() -> result.add("foo")).isInstanceOf(UnsupportedOperationException.class);
.isInstanceOf(UnsupportedOperationException.class);
} }
@Test @Test
public void whenCollectingToSet_shouldCollectToSet() throws Exception { public void whenCollectingToSet_shouldCollectToSet() {
final Set<String> result = givenList.stream().collect(toSet()); final Set<String> result = givenList.stream()
.collect(toSet());
assertThat(result).containsAll(givenList); assertThat(result).containsAll(givenList);
} }
@Test @Test
public void whenCollectingToUnmodifiableSet_shouldCollectToUnmodifiableSet() { public void whenCollectingToUnmodifiableSet_shouldCollectToUnmodifiableSet() {
final Set<String> result = givenList.stream().collect(toUnmodifiableSet()); final Set<String> result = givenList.stream()
.collect(toUnmodifiableSet());
assertThatThrownBy(() -> result.add("foo")) assertThatThrownBy(() -> result.add("foo")).isInstanceOf(UnsupportedOperationException.class);
.isInstanceOf(UnsupportedOperationException.class);
} }
@Test @Test
public void givenContainsDuplicateElements_whenCollectingToSet_shouldAddDuplicateElementsOnlyOnce() throws Exception { public void givenContainsDuplicateElements_whenCollectingToSet_shouldAddDuplicateElementsOnlyOnce() throws Exception {
final Set<String> result = listWithDuplicates.stream().collect(toSet()); final Set<String> result = listWithDuplicates.stream()
.collect(toSet());
assertThat(result).hasSize(4); assertThat(result).hasSize(4);
} }
@Test @Test
public void whenCollectingToCollection_shouldCollectToCollection() throws Exception { public void whenCollectingToCollection_shouldCollectToCollection() {
final List<String> result = givenList.stream().collect(toCollection(LinkedList::new)); final List<String> result = givenList.stream()
.collect(toCollection(LinkedList::new));
assertThat(result).containsAll(givenList).isInstanceOf(LinkedList.class); assertThat(result).containsAll(givenList)
.isInstanceOf(LinkedList.class);
} }
@Test @Test
public void whenCollectingToImmutableCollection_shouldThrowException() throws Exception { public void whenCollectingToImmutableCollection_shouldThrowException() {
assertThatThrownBy(() -> { assertThatThrownBy(() -> {
givenList.stream().collect(toCollection(ImmutableList::of)); givenList.stream()
.collect(toCollection(ImmutableList::of));
}).isInstanceOf(UnsupportedOperationException.class); }).isInstanceOf(UnsupportedOperationException.class);
} }
@Test @Test
public void whenCollectingToMap_shouldCollectToMap() throws Exception { public void whenCollectingToMap_shouldCollectToMap() {
final Map<String, Integer> result = givenList.stream().collect(toMap(Function.identity(), String::length)); final Map<String, Integer> result = givenList.stream()
.collect(toMap(Function.identity(), String::length));
assertThat(result).containsEntry("a", 1).containsEntry("bb", 2).containsEntry("ccc", 3).containsEntry("dd", 2); assertThat(result).containsEntry("a", 1)
.containsEntry("bb", 2)
.containsEntry("ccc", 3)
.containsEntry("dd", 2);
} }
@Test @Test
@ -86,63 +97,68 @@ public class Java8CollectorsUnitTest {
final Map<String, Integer> result = givenList.stream() final Map<String, Integer> result = givenList.stream()
.collect(toUnmodifiableMap(Function.identity(), String::length)); .collect(toUnmodifiableMap(Function.identity(), String::length));
assertThatThrownBy(() -> result.put("foo", 3)) assertThatThrownBy(() -> result.put("foo", 3)).isInstanceOf(UnsupportedOperationException.class);
.isInstanceOf(UnsupportedOperationException.class);
} }
@Test @Test
public void whenCollectingToMapwWithDuplicates_shouldCollectToMapMergingTheIdenticalItems() throws Exception { public void whenCollectingToMapwWithDuplicates_shouldCollectToMapMergingTheIdenticalItems() throws Exception {
final Map<String, Integer> result = listWithDuplicates.stream().collect( final Map<String, Integer> result = listWithDuplicates.stream()
toMap( .collect(toMap(Function.identity(), String::length, (item, identicalItem) -> item));
Function.identity(),
String::length,
(item, identicalItem) -> item
)
);
assertThat(result).containsEntry("a", 1).containsEntry("bb", 2).containsEntry("c", 1).containsEntry("d", 1); assertThat(result).containsEntry("a", 1)
.containsEntry("bb", 2)
.containsEntry("c", 1)
.containsEntry("d", 1);
} }
@Test @Test
public void givenContainsDuplicateElements_whenCollectingToMap_shouldThrowException() throws Exception { public void givenContainsDuplicateElements_whenCollectingToMap_shouldThrowException() {
assertThatThrownBy(() -> { assertThatThrownBy(() -> {
listWithDuplicates.stream().collect(toMap(Function.identity(), String::length)); listWithDuplicates.stream()
.collect(toMap(Function.identity(), String::length));
}).isInstanceOf(IllegalStateException.class); }).isInstanceOf(IllegalStateException.class);
} }
@Test @Test
public void whenCollectingAndThen_shouldCollect() throws Exception { public void whenCollectingAndThen_shouldCollect() {
final List<String> result = givenList.stream().collect(collectingAndThen(toList(), ImmutableList::copyOf)); final List<String> result = givenList.stream()
.collect(collectingAndThen(toList(), ImmutableList::copyOf));
assertThat(result).containsAll(givenList).isInstanceOf(ImmutableList.class); assertThat(result).containsAll(givenList)
.isInstanceOf(ImmutableList.class);
} }
@Test @Test
public void whenJoining_shouldJoin() throws Exception { public void whenJoining_shouldJoin() {
final String result = givenList.stream().collect(joining()); final String result = givenList.stream()
.collect(joining());
assertThat(result).isEqualTo("abbcccdd"); assertThat(result).isEqualTo("abbcccdd");
} }
@Test @Test
public void whenJoiningWithSeparator_shouldJoinWithSeparator() throws Exception { public void whenJoiningWithSeparator_shouldJoinWithSeparator() {
final String result = givenList.stream().collect(joining(" ")); final String result = givenList.stream()
.collect(joining(" "));
assertThat(result).isEqualTo("a bb ccc dd"); assertThat(result).isEqualTo("a bb ccc dd");
} }
@Test @Test
public void whenJoiningWithSeparatorAndPrefixAndPostfix_shouldJoinWithSeparatorPrePost() throws Exception { public void whenJoiningWithSeparatorAndPrefixAndPostfix_shouldJoinWithSeparatorPrePost() {
final String result = givenList.stream().collect(joining(" ", "PRE-", "-POST")); final String result = givenList.stream()
.collect(joining(" ", "PRE-", "-POST"));
assertThat(result).isEqualTo("PRE-a bb ccc dd-POST"); assertThat(result).isEqualTo("PRE-a bb ccc dd-POST");
} }
@Test @Test
public void whenPartitioningBy_shouldPartition() throws Exception { public void whenPartitioningBy_shouldPartition() {
final Map<Boolean, List<String>> result = givenList.stream().collect(partitioningBy(s -> s.length() > 2)); final Map<Boolean, List<String>> result = givenList.stream()
.collect(partitioningBy(s -> s.length() > 2));
assertThat(result).containsKeys(true, false).satisfies(booleanListMap -> { assertThat(result).containsKeys(true, false)
.satisfies(booleanListMap -> {
assertThat(booleanListMap.get(true)).contains("ccc"); assertThat(booleanListMap.get(true)).contains("ccc");
assertThat(booleanListMap.get(false)).contains("a", "bb", "dd"); assertThat(booleanListMap.get(false)).contains("a", "bb", "dd");
@ -150,15 +166,17 @@ public class Java8CollectorsUnitTest {
} }
@Test @Test
public void whenCounting_shouldCount() throws Exception { public void whenCounting_shouldCount() {
final Long result = givenList.stream().collect(counting()); final Long result = givenList.stream()
.collect(counting());
assertThat(result).isEqualTo(4); assertThat(result).isEqualTo(4);
} }
@Test @Test
public void whenSummarizing_shouldSummarize() throws Exception { public void whenSummarizing_shouldSummarize() {
final DoubleSummaryStatistics result = givenList.stream().collect(summarizingDouble(String::length)); final DoubleSummaryStatistics result = givenList.stream()
.collect(summarizingDouble(String::length));
assertThat(result.getAverage()).isEqualTo(2); assertThat(result.getAverage()).isEqualTo(2);
assertThat(result.getCount()).isEqualTo(4); assertThat(result.getCount()).isEqualTo(4);
@ -168,38 +186,47 @@ public class Java8CollectorsUnitTest {
} }
@Test @Test
public void whenAveraging_shouldAverage() throws Exception { public void whenAveraging_shouldAverage() {
final Double result = givenList.stream().collect(averagingDouble(String::length)); final Double result = givenList.stream()
.collect(averagingDouble(String::length));
assertThat(result).isEqualTo(2); assertThat(result).isEqualTo(2);
} }
@Test @Test
public void whenSumming_shouldSum() throws Exception { public void whenSumming_shouldSum() {
final Double result = givenList.stream().filter(i -> true).collect(summingDouble(String::length)); final Double result = givenList.stream()
.collect(summingDouble(String::length));
assertThat(result).isEqualTo(8); assertThat(result).isEqualTo(8);
} }
@Test @Test
public void whenMaxingBy_shouldMaxBy() throws Exception { public void whenMaxingBy_shouldMaxBy() {
final Optional<String> result = givenList.stream().collect(maxBy(Comparator.naturalOrder())); final Optional<String> result = givenList.stream()
.collect(maxBy(Comparator.naturalOrder()));
assertThat(result).isPresent().hasValue("dd"); assertThat(result).isPresent()
.hasValue("dd");
} }
@Test @Test
public void whenGroupingBy_shouldGroupBy() throws Exception { public void whenGroupingBy_shouldGroupBy() {
final Map<Integer, Set<String>> result = givenList.stream().collect(groupingBy(String::length, toSet())); final Map<Integer, Set<String>> result = givenList.stream()
.collect(groupingBy(String::length, toSet()));
assertThat(result).containsEntry(1, newHashSet("a")).containsEntry(2, newHashSet("bb", "dd")).containsEntry(3, newHashSet("ccc")); assertThat(result).containsEntry(1, newHashSet("a"))
.containsEntry(2, newHashSet("bb", "dd"))
.containsEntry(3, newHashSet("ccc"));
} }
@Test @Test
public void whenCreatingCustomCollector_shouldCollect() throws Exception { public void whenCreatingCustomCollector_shouldCollect() {
final ImmutableSet<String> result = givenList.stream().collect(toImmutableSet()); final ImmutableSet<String> result = givenList.stream()
.collect(toImmutableSet());
assertThat(result).isInstanceOf(ImmutableSet.class).contains("a", "bb", "ccc", "dd"); assertThat(result).isInstanceOf(ImmutableSet.class)
.contains("a", "bb", "ccc", "dd");
} }