example code for BAEL-966 (#2152)

* added updated example codes

* updated example code StringToCharStream

* deleted StringToCharStream.java locally

* removed redundant file

* added code for apache commons collection SetUtils

* refactored example code
This commit is contained in:
Seun Matt 2017-06-26 23:18:54 +01:00 committed by Zeger Hendrikse
parent 75e0473822
commit 3ae96995ad
3 changed files with 96 additions and 4 deletions

View File

@ -12,7 +12,6 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class StringToCharStreamUnitTest { public class StringToCharStreamUnitTest {
@ -44,7 +43,6 @@ public class StringToCharStreamUnitTest {
.mapToObj(c -> (char) c); .mapToObj(c -> (char) c);
Stream<Character> characterStream1 = testString.codePoints() Stream<Character> characterStream1 = testString.codePoints()
.mapToObj(c -> (char) c); .mapToObj(c -> (char) c);
assertNotNull("IntStream returned by chars() did not map to Stream<Character>", characterStream); assertNotNull("IntStream returned by chars() did not map to Stream<Character>", characterStream);
assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1); assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1);
} }

View File

@ -90,6 +90,11 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version> <version>${commons-lang.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons.collections.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.jasypt</groupId> <groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId> <artifactId>jasypt</artifactId>
@ -340,13 +345,17 @@
<artifactId>opennlp-tools</artifactId> <artifactId>opennlp-tools</artifactId>
<version>1.8.0</version> <version>1.8.0</version>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>info.debatty</groupId> <groupId>info.debatty</groupId>
<artifactId>java-lsh</artifactId> <artifactId>java-lsh</artifactId>
<version>${java-lsh.version}</version> <version>${java-lsh.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
<properties> <properties>
<multiverse.version>0.7.0</multiverse.version> <multiverse.version>0.7.0</multiverse.version>
@ -373,6 +382,8 @@
<serenity.plugin.version>1.4.0</serenity.plugin.version> <serenity.plugin.version>1.4.0</serenity.plugin.version>
<jUnitParams.version>1.1.0</jUnitParams.version> <jUnitParams.version>1.1.0</jUnitParams.version>
<netty.version>4.1.10.Final</netty.version> <netty.version>4.1.10.Final</netty.version>
<commons.collections.version>4.1</commons.collections.version>
<junit.version>4.12</junit.version>
<java-lsh.version>0.10</java-lsh.version> <java-lsh.version>0.10</java-lsh.version>
</properties> </properties>

View File

@ -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<String> sourceSet = new HashSet<>();
sourceSet.addAll(Arrays.asList("London", "Lagos", "Err Source1"));
Set<String> validatingSet
= SetUtils.predicatedSet(sourceSet, (s) -> s.startsWith("L"));
validatingSet.add("Err Source2");
}
@Test
public void givenTwoSets_whenDifference_thenSetView() {
Set<Integer> a = new HashSet<>(Arrays.asList(1,2,5));
Set<Integer> b = new HashSet<>(Arrays.asList(1,2));
SetUtils.SetView<Integer> result = SetUtils.difference(a, b);
assertTrue(result.size() == 1 && result.contains(5));
}
@Test
public void givenTwoSets_whenUnion_thenUnionResult() {
Set<Integer> a = new HashSet<>(Arrays.asList(1,2,5));
Set<Integer> b = new HashSet<>(Arrays.asList(1,2));
Set<Integer> expected = new HashSet<>(Arrays.asList(1,2,5));
SetUtils.SetView<Integer> union = SetUtils.union(a, b);
assertTrue(SetUtils.isEqualSet(expected, union));
}
@Test
public void givenTwoSets_whenIntersection_thenIntersectionResult() {
Set<Integer> a = new HashSet<>(Arrays.asList(1,2,5));
Set<Integer> b = new HashSet<>(Arrays.asList(1,2));
Set<Integer> expected = new HashSet<>(Arrays.asList(1,2));
SetUtils.SetView<Integer> intersect = SetUtils.intersection(a, b);
assertTrue(SetUtils.isEqualSet(expected, intersect));
}
@Test
public void givenSet_whenTransformedSet_thenTransformedResult() {
Set<Integer> a = SetUtils.transformedSet(new HashSet<>(), (e) -> e * 2 );
a.add(2);
assertEquals(a.toArray()[0], 4);
Set<Integer> source = new HashSet<>(Arrays.asList(1));
Set<Integer> newSet = TransformedSet.transformedSet(source, (e) -> e * 2);
assertEquals(newSet.toArray()[0], 2);
assertEquals(source.toArray()[0], 2);
}
@Test
public void givenTwoSet_whenDisjunction_thenDisjunctionSet() {
Set<Integer> a = new HashSet<>(Arrays.asList(1,2,5));
Set<Integer> b = new HashSet<>(Arrays.asList(1,2,3));
SetUtils.SetView<Integer> result = SetUtils.disjunction(a, b);
assertTrue(result.toSet().contains(5) && result.toSet().contains(3));
}
@Test
public void givenSet_when_OrderedSet_thenMaintainElementOrder() {
Set<Integer> set = new HashSet<>(Arrays.asList(10,1,5));
System.out.println("unordered set: " + set);
Set<Integer> orderedSet = SetUtils.orderedSet(new HashSet<>());
orderedSet.addAll(Arrays.asList(10,1,5));
System.out.println("ordered set = " + orderedSet);
}
}