diff --git a/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java b/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java index fd764d6203..7183f246da 100644 --- a/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java +++ b/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java @@ -1,11 +1,19 @@ package org.baeldung.guava.collections; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Collection; import java.util.List; +import java.util.Set; import org.junit.Test; import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; public class GuavaCollectionsExamplesTest { @@ -35,8 +43,69 @@ public class GuavaCollectionsExamplesTest { } @Test - public final void when_then() { - // + public final void whenAddingAnIterableToACollection_thenAddedOK() { + final Iterable iter = Lists.newArrayList(); + final Collection collector = Lists.newArrayList(); + Iterables.addAll(collector, iter); + } + + // + + @Test + public final void whenCheckingIfCollectionContainsElementsByCustomMatch1_thenContains() { + final Iterable theCollection = Lists.newArrayList("a", "bc", "def"); + final boolean contains = Iterables.any(theCollection, new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }); + + assertTrue(contains); + } + + @Test + public final void whenCheckingIfCollectionContainsElementsByCustomMatch2_thenContains() { + final Set theCollection = Sets.newHashSet("a", "bc", "def"); + + final boolean contains = !Sets.filter(theCollection, new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }).isEmpty(); + + assertTrue(contains); + } + + @Test + public final void whenCheckingIfCollectionContainsElementsByCustomMatch3_thenContains() { + final Iterable theCollection = Sets.newHashSet("a", "bc", "def"); + + final boolean contains = Iterables.find(theCollection, new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }) != null; + + assertTrue(contains); + } + + // + + @Test + public final void givenNoSearchResult_whenFindingElementInIterable_thenNoException() { + final Iterable theCollection = Sets.newHashSet("abcd", "efgh", "ijkl"); + + final String found = Iterables.find(theCollection, new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }); + + assertNull(found); } }