Refactor JoinSplitCollectionsUnitTest

This commit is contained in:
pivovarit 2016-12-03 17:09:34 +01:00
parent 34e5a6475f
commit a6bdad2cb9

View File

@ -1,31 +1,24 @@
package org.baeldung.java.collections; package org.baeldung.java.collections;
import static org.junit.Assert.assertArrayEquals; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.Test; import static org.junit.Assert.*;
public class JoinSplitCollectionsUnitTest { public class JoinSplitCollectionsUnitTest {
@Test @Test
public void whenJoiningTwoArrays_thenJoined() { public void whenJoiningTwoArrays_thenJoined() {
String[] animals1 = new String[] { "Dog", "Cat" }; String[] animals1 = new String[]{"Dog", "Cat"};
String[] animals2 = new String[] { "Bird", "Cow" }; String[] animals2 = new String[]{"Bird", "Cow"};
String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)) String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2))
.toArray(String[]::new); .toArray(String[]::new);
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" }); assertArrayEquals(result, new String[]{"Dog", "Cat", "Bird", "Cow"});
} }
@Test @Test
@ -33,7 +26,7 @@ public class JoinSplitCollectionsUnitTest {
Collection<Integer> collection1 = Arrays.asList(7, 8, 9); Collection<Integer> collection1 = Arrays.asList(7, 8, 9);
Collection<Integer> collection2 = Arrays.asList(10, 11, 12); Collection<Integer> collection2 = Arrays.asList(10, 11, 12);
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()) Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
.collect(Collectors.toList()); .collect(Collectors.toList());
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12))); assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12)));
} }
@ -43,17 +36,17 @@ public class JoinSplitCollectionsUnitTest {
Collection<Integer> collection1 = Arrays.asList(7, 8, 11); Collection<Integer> collection1 = Arrays.asList(7, 8, 11);
Collection<Integer> collection2 = Arrays.asList(9, 12, 10); Collection<Integer> collection2 = Arrays.asList(9, 12, 10);
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()) Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
.filter(next -> next <= 10) .filter(next -> next <= 10)
.collect(Collectors.toList()); .collect(Collectors.toList());
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10))); assertTrue(result.equals(Arrays.asList(7, 8, 9, 10)));
} }
@Test @Test
public void whenConvertArrayToString_thenConverted() { public void whenConvertArrayToString_thenConverted() {
String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" }; String[] colors = new String[]{"Red", "Blue", "Green", "Yellow"};
String result = Arrays.stream(colors) String result = Arrays.stream(colors)
.collect(Collectors.joining(", ")); .collect(Collectors.joining(", "));
assertEquals(result, "Red, Blue, Green, Yellow"); assertEquals(result, "Red, Blue, Green, Yellow");
} }
@ -62,7 +55,7 @@ public class JoinSplitCollectionsUnitTest {
public void whenConvertCollectionToString_thenConverted() { public void whenConvertCollectionToString_thenConverted() {
Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom"); Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom");
String result = directions.stream() String result = directions.stream()
.collect(Collectors.joining(", ")); .collect(Collectors.joining(", "));
assertEquals(result, "Left, Right, Top, Bottom"); assertEquals(result, "Left, Right, Top, Bottom");
} }
@ -75,8 +68,8 @@ public class JoinSplitCollectionsUnitTest {
users.put(3, "Susan Anderson"); users.put(3, "Susan Anderson");
String result = users.entrySet().stream() String result = users.entrySet().stream()
.map(entry -> entry.getKey() + " = " + entry.getValue()) .map(entry -> entry.getKey() + " = " + entry.getValue())
.collect(Collectors.joining(", ")); .collect(Collectors.joining(", "));
assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"); assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson");
} }
@ -88,9 +81,9 @@ public class JoinSplitCollectionsUnitTest {
nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow")); nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow"));
String result = nested.stream() String result = nested.stream()
.map(nextList -> nextList.stream() .map(nextList -> nextList.stream()
.collect(Collectors.joining("-"))) .collect(Collectors.joining("-")))
.collect(Collectors.joining("; ")); .collect(Collectors.joining("; "));
assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow"); assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow");
} }
@ -99,8 +92,8 @@ public class JoinSplitCollectionsUnitTest {
public void whenConvertCollectionToStringAndSkipNull_thenConverted() { public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape"); Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
String result = fruits.stream() String result = fruits.stream()
.filter(next -> next != null) .filter(Objects::nonNull)
.collect(Collectors.joining(", ")); .collect(Collectors.joining(", "));
assertEquals(result, "Apple, Orange, Grape"); assertEquals(result, "Apple, Orange, Grape");
} }
@ -128,9 +121,9 @@ public class JoinSplitCollectionsUnitTest {
@Test @Test
public void whenSplitArrayByWordLength_thenConverted() { public void whenSplitArrayByWordLength_thenConverted() {
String[] words = new String[] { "bye", "cold", "it", "and", "my", "word" }; String[] words = new String[]{"bye", "cold", "it", "and", "my", "word"};
Map<Integer, List<String>> result = Arrays.stream(words) Map<Integer, List<String>> result = Arrays.stream(words)
.collect(Collectors.groupingBy(word -> word.length())); .collect(Collectors.groupingBy(String::length));
assertTrue(result.get(2).equals(Arrays.asList("it", "my"))); assertTrue(result.get(2).equals(Arrays.asList("it", "my")));
assertTrue(result.get(3).equals(Arrays.asList("bye", "and"))); assertTrue(result.get(3).equals(Arrays.asList("bye", "and")));
@ -142,7 +135,7 @@ public class JoinSplitCollectionsUnitTest {
String colors = "Red, Blue, Green, Yellow"; String colors = "Red, Blue, Green, Yellow";
String[] result = colors.split(", "); String[] result = colors.split(", ");
assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" }); assertArrayEquals(result, new String[]{"Red", "Blue", "Green", "Yellow"});
} }
@Test @Test
@ -158,8 +151,8 @@ public class JoinSplitCollectionsUnitTest {
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"; String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson";
Map<Integer, String> result = Arrays.stream(users.split(", ")) Map<Integer, String> result = Arrays.stream(users.split(", "))
.map(next -> next.split(" = ")) .map(next -> next.split(" = "))
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1])); .collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
assertEquals(result.get(1), "John Doe"); assertEquals(result.get(1), "John Doe");
assertEquals(result.get(2), "Paul Smith"); assertEquals(result.get(2), "Paul Smith");
@ -171,9 +164,9 @@ public class JoinSplitCollectionsUnitTest {
String fruits = "Apple. , Orange, Grape. Lemon"; String fruits = "Apple. , Orange, Grape. Lemon";
Collection<String> result = Arrays.stream(fruits.split("[,|.]")) Collection<String> result = Arrays.stream(fruits.split("[,|.]"))
.map(String::trim) .map(String::trim)
.filter(next -> !next.isEmpty()) .filter(next -> !next.isEmpty())
.collect(Collectors.toList()); .collect(Collectors.toList());
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon"))); assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon")));
} }