From 9a4a3c790485dec48000db87ce0001d7578018a1 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Tue, 12 Dec 2023 23:00:23 +0100 Subject: [PATCH] add tests; fix ClassCastException --- .../com/dua3/utility/lang/BatchCollector.java | 4 +- .../dua3/utility/lang/BatchCollectorTest.java | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 utility/src/test/java/com/dua3/utility/lang/BatchCollectorTest.java diff --git a/utility/src/main/java/com/dua3/utility/lang/BatchCollector.java b/utility/src/main/java/com/dua3/utility/lang/BatchCollector.java index 8dad834e..e7c7016f 100644 --- a/utility/src/main/java/com/dua3/utility/lang/BatchCollector.java +++ b/utility/src/main/java/com/dua3/utility/lang/BatchCollector.java @@ -61,7 +61,7 @@ public BiConsumer>>, T> accumulator() { K key = keyMapper.apply(item); List bucket; - if (accu.isEmpty() || (key != null && !Objects.equals(key, accu.peekLast().first()))) { + if (accu.isEmpty() || (!Objects.equals(key, accu.peekLast().first()))) { bucket = new ArrayList<>(); accu.addLast(Pair.of(key == null ? defaultKey : key, bucket)); } else { @@ -87,6 +87,6 @@ public Function>>, List>>> finisher() { @Override public Set characteristics() { - return EnumSet.of(Characteristics.IDENTITY_FINISH); + return EnumSet.noneOf(Characteristics.class); } } diff --git a/utility/src/test/java/com/dua3/utility/lang/BatchCollectorTest.java b/utility/src/test/java/com/dua3/utility/lang/BatchCollectorTest.java new file mode 100644 index 00000000..1f31d2f1 --- /dev/null +++ b/utility/src/test/java/com/dua3/utility/lang/BatchCollectorTest.java @@ -0,0 +1,60 @@ +package com.dua3.utility.lang; + +import com.dua3.utility.data.Pair; +import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collector; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * This is a test class for the BatchCollector class. + * It aims to tests the functionality of the methods within the BatchCollector class. + */ +public class BatchCollectorTest { + + /** + * This test is for testing the functionality of BatchCollector when generating grouping. + */ + @Test + public void batchCollector_groupingGenerationTest() { + BatchCollector bc = new BatchCollector<>(s -> s != null ? s.length() : null); + var result = Stream.of("one", "two", "three", "four", "five", "six").collect(bc); + + assertEquals(4, result.size()); + assertEquals(3, result.get(0).first().intValue()); + assertEquals(List.of("one","two"), result.get(0).second()); + assertEquals(5, result.get(1).first().intValue()); + assertEquals(List.of("three"), result.get(1).second()); + assertEquals(4, result.get(2).first().intValue()); + assertEquals(List.of("four","five"), result.get(2).second()); + assertEquals(3, result.get(3).first().intValue()); + assertEquals(List.of("six"), result.get(3).second()); + } + + /** + * This test is for testing the functionality of BatchCollector when generating grouping with default key. + */ + @Test + public void batchCollector_defaultKeyGroupingGenerationTest() { + BatchCollector bc = new BatchCollector<>(s -> s != null ? s.length() : null, 0); + var result = Stream.of("one", "two", "three", "four", "five", "six", null).collect(bc); + + assertEquals(5, result.size()); + assertEquals(3, result.get(0).first().intValue()); + assertEquals(List.of("one","two"), result.get(0).second()); + assertEquals(5, result.get(1).first().intValue()); + assertEquals(List.of("three"), result.get(1).second()); + assertEquals(4, result.get(2).first().intValue()); + assertEquals(List.of("four","five"), result.get(2).second()); + assertEquals(3, result.get(3).first().intValue()); + assertEquals(List.of("six"), result.get(3).second()); + assertEquals(0, result.get(4).first().intValue()); + assertEquals(Collections.singletonList(null), result.get(4).second()); + } + +} \ No newline at end of file