diff --git a/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java b/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java index 2ad670fe5d..1b02e5d291 100644 --- a/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java +++ b/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java @@ -12,7 +12,6 @@ import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; public class StringToCharStreamUnitTest { @@ -44,7 +43,6 @@ public class StringToCharStreamUnitTest { .mapToObj(c -> (char) c); Stream characterStream1 = testString.codePoints() .mapToObj(c -> (char) c); - assertNotNull("IntStream returned by chars() did not map to Stream", characterStream); assertNotNull("IntStream returned by codePoints() did not map to Stream", characterStream1); } diff --git a/libraries/pom.xml b/libraries/pom.xml index b010ce5636..6872e4ad7c 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -90,6 +90,11 @@ commons-lang3 ${commons-lang.version} + + org.apache.commons + commons-collections4 + ${commons.collections.version} + org.jasypt jasypt @@ -340,13 +345,17 @@ opennlp-tools 1.8.0 + + junit + junit + ${junit.version} + test + info.debatty java-lsh ${java-lsh.version} - - 0.7.0 @@ -373,6 +382,8 @@ 1.4.0 1.1.0 4.1.10.Final + 4.1 + 4.12 0.10 diff --git a/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java new file mode 100644 index 0000000000..4d264e3aea --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java @@ -0,0 +1,83 @@ +package com.baeldung.commons.collections; + +import org.apache.commons.collections4.SetUtils; +import org.apache.commons.collections4.set.TransformedSet; +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by smatt on 21/06/2017. + */ +public class SetUtilsUnitTest { + + @Test(expected = IllegalArgumentException.class) + public void givenSetAndPredicate_whenPredicatedSet_thenValidateSet_and_throw_IllegalArgumentException() { + Set sourceSet = new HashSet<>(); + sourceSet.addAll(Arrays.asList("London", "Lagos", "Err Source1")); + Set validatingSet + = SetUtils.predicatedSet(sourceSet, (s) -> s.startsWith("L")); + validatingSet.add("Err Source2"); + } + + @Test + public void givenTwoSets_whenDifference_thenSetView() { + Set a = new HashSet<>(Arrays.asList(1,2,5)); + Set b = new HashSet<>(Arrays.asList(1,2)); + SetUtils.SetView result = SetUtils.difference(a, b); + assertTrue(result.size() == 1 && result.contains(5)); + } + + @Test + public void givenTwoSets_whenUnion_thenUnionResult() { + Set a = new HashSet<>(Arrays.asList(1,2,5)); + Set b = new HashSet<>(Arrays.asList(1,2)); + Set expected = new HashSet<>(Arrays.asList(1,2,5)); + SetUtils.SetView union = SetUtils.union(a, b); + assertTrue(SetUtils.isEqualSet(expected, union)); + } + + @Test + public void givenTwoSets_whenIntersection_thenIntersectionResult() { + Set a = new HashSet<>(Arrays.asList(1,2,5)); + Set b = new HashSet<>(Arrays.asList(1,2)); + Set expected = new HashSet<>(Arrays.asList(1,2)); + SetUtils.SetView intersect = SetUtils.intersection(a, b); + assertTrue(SetUtils.isEqualSet(expected, intersect)); + } + + @Test + public void givenSet_whenTransformedSet_thenTransformedResult() { + Set a = SetUtils.transformedSet(new HashSet<>(), (e) -> e * 2 ); + a.add(2); + assertEquals(a.toArray()[0], 4); + + Set source = new HashSet<>(Arrays.asList(1)); + Set newSet = TransformedSet.transformedSet(source, (e) -> e * 2); + assertEquals(newSet.toArray()[0], 2); + assertEquals(source.toArray()[0], 2); + } + + @Test + public void givenTwoSet_whenDisjunction_thenDisjunctionSet() { + Set a = new HashSet<>(Arrays.asList(1,2,5)); + Set b = new HashSet<>(Arrays.asList(1,2,3)); + SetUtils.SetView result = SetUtils.disjunction(a, b); + assertTrue(result.toSet().contains(5) && result.toSet().contains(3)); + } + + @Test + public void givenSet_when_OrderedSet_thenMaintainElementOrder() { + Set set = new HashSet<>(Arrays.asList(10,1,5)); + System.out.println("unordered set: " + set); + + Set orderedSet = SetUtils.orderedSet(new HashSet<>()); + orderedSet.addAll(Arrays.asList(10,1,5)); + System.out.println("ordered set = " + orderedSet); + } +} \ No newline at end of file