Unify, simply, format

This commit is contained in:
Marek Lewandowski 2016-12-12 09:58:08 +01:00
parent 6038c51423
commit 3db2e80ab6
1 changed files with 74 additions and 54 deletions

View File

@ -15,85 +15,99 @@ public class JoinSplitCollectionsUnitTest {
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)).toArray(String[]::new); String[] result = Stream.concat(
Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" }); assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
} }
@Test @Test
public void whenJoiningTwoCollections_thenJoined() { public void whenJoiningTwoCollections_thenJoined() {
Collection<Integer> collection1 = Arrays.asList(7, 8, 9); Collection<String> collection1 = Arrays.asList("Dog", "Cat");
Collection<Integer> collection2 = Arrays.asList(10, 11, 12); Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()).collect(Collectors.toList()); Collection<String> result = Stream.concat(
collection1.stream(), collection2.stream())
.collect(Collectors.toList());
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12))); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow", "Moose")));
} }
@Test @Test
public void whenJoiningTwoCollectionsWithFilter_thenJoined() { public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
Collection<Integer> collection1 = Arrays.asList(7, 8, 11); Collection<String> collection1 = Arrays.asList("Dog", "Cat");
Collection<Integer> collection2 = Arrays.asList(9, 12, 10); Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()).filter(next -> next <= 10).collect(Collectors.toList()); Collection<String> result = Stream.concat(
collection1.stream(), collection2.stream())
.filter(e -> e.length() == 3)
.collect(Collectors.toList());
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10))); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Cow")));
} }
@Test @Test
public void whenConvertArrayToString_thenConverted() { public void whenConvertArrayToString_thenConverted() {
String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" }; String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow" };
String result = Arrays.stream(colors).collect(Collectors.joining(", ")); String result = Arrays.stream(animals).collect(Collectors.joining(", "));
assertEquals(result, "Red, Blue, Green, Yellow"); assertEquals(result, "Dog, Cat, Bird, Cow");
} }
@Test @Test
public void whenConvertCollectionToString_thenConverted() { public void whenConvertCollectionToString_thenConverted() {
Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom"); Collection<String> animals = Arrays.asList("Dog", "Cat", "Bird", "Cow");
String result = directions.stream().collect(Collectors.joining(", ")); String result = animals.stream().collect(Collectors.joining(", "));
assertEquals(result, "Left, Right, Top, Bottom"); assertEquals(result, "Dog, Cat, Bird, Cow");
} }
@Test @Test
public void whenConvertMapToString_thenConverted() { public void whenConvertMapToString_thenConverted() {
Map<Integer, String> users = new HashMap<>(); Map<Integer, String> animals = new HashMap<>();
users.put(1, "John Doe"); animals.put(1, "Dog");
users.put(2, "Paul Smith"); animals.put(2, "Cat");
users.put(3, "Susan Anderson"); animals.put(3, "Cow");
String result = users.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()).collect(Collectors.joining(", ")); String result = animals.entrySet().stream()
.map(entry -> entry.getKey() + " = " + entry.getValue())
.collect(Collectors.joining(", "));
assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"); assertEquals(result, "1 = Dog, 2 = Cat, 3 = Cow");
} }
@Test @Test
public void whenConvertNestedCollectionToString_thenConverted() { public void whenConvertNestedCollectionToString_thenConverted() {
Collection<List<String>> nested = new ArrayList<>(); Collection<List<String>> nested = new ArrayList<>();
nested.add(Arrays.asList("Left", "Right", "Top", "Bottom")); nested.add(Arrays.asList("Dog", "Cat"));
nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow")); nested.add(Arrays.asList("Cow", "Pig"));
String result = nested.stream().map(nextList -> nextList.stream().collect(Collectors.joining("-"))).collect(Collectors.joining("; ")); String result = nested.stream().map(
nextList -> nextList.stream()
.collect(Collectors.joining("-")))
.collect(Collectors.joining("; "));
assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow"); assertEquals(result, "Dog-Cat; Cow-Pig");
} }
@Test @Test
public void whenConvertCollectionToStringAndSkipNull_thenConverted() { public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape"); Collection<String> animals = Arrays.asList("Dog", "Cat", null, "Moose");
String result = fruits.stream().filter(Objects::nonNull).collect(Collectors.joining(", ")); String result = animals.stream()
.filter(Objects::nonNull)
.collect(Collectors.joining(", "));
assertEquals(result, "Apple, Orange, Grape"); assertEquals(result, "Dog, Cat, Moose");
} }
@Test @Test
public void whenSplitCollectionHalf_thenConverted() { public void whenSplitCollectionHalf_thenConverted() {
Collection<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Collection<String> animals = Arrays.asList(
Collection<Integer> result1 = new ArrayList<>(); "Dog", "Cat", "Cow", "Bird", "Moose", "Pig");
Collection<Integer> result2 = new ArrayList<>(); Collection<String> result1 = new ArrayList<>();
Collection<String> result2 = new ArrayList<>();
AtomicInteger count = new AtomicInteger(); AtomicInteger count = new AtomicInteger();
int midpoint = Math.round(numbers.size() / 2); int midpoint = Math.round(animals.size() / 2);
numbers.forEach(next -> { animals.forEach(next -> {
int index = count.getAndIncrement(); int index = count.getAndIncrement();
if (index < midpoint) { if (index < midpoint) {
result1.add(next); result1.add(next);
@ -102,53 +116,59 @@ public class JoinSplitCollectionsUnitTest {
} }
}); });
assertTrue(result1.equals(Arrays.asList(1, 2, 3, 4, 5))); assertTrue(result1.equals(Arrays.asList("Dog", "Cat", "Cow")));
assertTrue(result2.equals(Arrays.asList(6, 7, 8, 9, 10))); assertTrue(result2.equals(Arrays.asList("Bird", "Moose", "Pig")));
} }
@Test @Test
public void whenSplitArrayByWordLength_thenConverted() { public void whenSplitArrayByWordLength_thenConverted() {
String[] words = new String[] { "bye", "cold", "it", "and", "my", "word" }; String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose"};
Map<Integer, List<String>> result = Arrays.stream(words).collect(Collectors.groupingBy(String::length)); Map<Integer, List<String>> result = Arrays.stream(animals)
.collect(Collectors.groupingBy(String::length));
assertTrue(result.get(2).equals(Arrays.asList("it", "my"))); assertTrue(result.get(3).equals(Arrays.asList("Dog", "Cat", "Cow", "Pig")));
assertTrue(result.get(3).equals(Arrays.asList("bye", "and"))); assertTrue(result.get(4).equals(Arrays.asList("Bird")));
assertTrue(result.get(4).equals(Arrays.asList("cold", "word"))); assertTrue(result.get(5).equals(Arrays.asList("Moose")));
} }
@Test @Test
public void whenConvertStringToArray_thenConverted() { public void whenConvertStringToArray_thenConverted() {
String colors = "Red, Blue, Green, Yellow"; String animals = "Dog, Cat, Bird, Cow";
String[] result = colors.split(", "); String[] result = animals.split(", ");
assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" }); assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
} }
@Test @Test
public void whenConvertStringToCollection_thenConverted() { public void whenConvertStringToCollection_thenConverted() {
String colors = "Left, Right, Top, Bottom"; String animals = "Dog, Cat, Bird, Cow";
Collection<String> result = Arrays.asList(colors.split(", ")); Collection<String> result = Arrays.asList(animals.split(", "));
assertTrue(result.equals(Arrays.asList("Left", "Right", "Top", "Bottom"))); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow")));
} }
@Test @Test
public void whenConvertStringToMap_thenConverted() { public void whenConvertStringToMap_thenConverted() {
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"; String animals = "1 = Dog, 2 = Cat, 3 = Bird";
Map<Integer, String> result = Arrays.stream(users.split(", ")).map(next -> next.split(" = ")).collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1])); Map<Integer, String> result = Arrays.stream(
animals.split(", ")).map(next -> next.split(" = "))
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
assertEquals(result.get(1), "John Doe"); assertEquals(result.get(1), "Dog");
assertEquals(result.get(2), "Paul Smith"); assertEquals(result.get(2), "Cat");
assertEquals(result.get(3), "Susan Anderson"); assertEquals(result.get(3), "Bird");
} }
@Test @Test
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() { public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
String fruits = "Apple. , Orange, Grape. Lemon"; String animals = "Dog. , Cat, Bird. Cow";
Collection<String> result = Arrays.stream(fruits.split("[,|.]")).map(String::trim).filter(next -> !next.isEmpty()).collect(Collectors.toList()); Collection<String> result = Arrays.stream(animals.split("[,|.]"))
.map(String::trim)
.filter(next -> !next.isEmpty())
.collect(Collectors.toList());
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon"))); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow")));
} }
} }