|
| 1 | +package com.baeldung.java.filtering; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collection; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.platform.runner.JUnitPlatform; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | + |
| 13 | +@RunWith(JUnitPlatform.class) |
| 14 | +public class CollectionFiltersUnitTest { |
| 15 | + |
| 16 | + private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 14, 2, 7, 1, 5, 8); |
| 17 | + private static final Collection<Integer> EXPECTED_EVEN_FILTERED_COLLECTION = Arrays.asList(14, 2, 8); |
| 18 | + |
| 19 | + @Test |
| 20 | + public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() { |
| 21 | + final Collection<String> baseStrings = Arrays.asList("java", "baeldung", "type", "example", "other"); |
| 22 | + |
| 23 | + Collection<String> filtered = StreamsCollectionFilter.filterCollectionHelperMethod(baseStrings, item -> item.length() == 4); |
| 24 | + |
| 25 | + assertThat(filtered).containsExactlyInAnyOrder("java", "type"); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void givenAnIntegerCollection_whenFilteringEvenValues_thenObtainTheFilteredCollectionForAllCases() { |
| 30 | + Collection<Integer> filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbersUsingLambda(BASE_INTEGER_COLLECTION); |
| 31 | + Collection<Integer> filteredWithStreams2 = StreamsCollectionFilter.findEvenNumbersUsingPredicate(BASE_INTEGER_COLLECTION); |
| 32 | + Collection<Integer> filteredWithCollectionUtils = CollectionUtilsCollectionFilter.findEvenNumbers(new ArrayList<>(BASE_INTEGER_COLLECTION)); |
| 33 | + Collection<Integer> filteredWithEclipseCollections = EclipseCollectionsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION); |
| 34 | + Collection<Integer> filteredWithEclipseCollectionsUsingIterate = EclipseCollectionsCollectionFilter.findEvenNumbersUsingIterate(BASE_INTEGER_COLLECTION); |
| 35 | + Collection<Integer> filteredWithGuava = GuavaCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION); |
| 36 | + |
| 37 | + assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithStreams2) |
| 38 | + .hasSameElementsAs(filteredWithCollectionUtils) |
| 39 | + .hasSameElementsAs(filteredWithEclipseCollections) |
| 40 | + .hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate) |
| 41 | + .hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate) |
| 42 | + .hasSameElementsAs(filteredWithGuava) |
| 43 | + .hasSameElementsAs(EXPECTED_EVEN_FILTERED_COLLECTION); |
| 44 | + } |
| 45 | + |
| 46 | +} |
0 commit comments