BAEL-7: Fix formatting (#874)
This commit is contained in:
parent
20acf42fca
commit
34e5a6475f
|
@ -18,162 +18,163 @@ import org.junit.Test;
|
||||||
|
|
||||||
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
|
||||||
}
|
public void whenJoiningTwoCollections_thenJoined() {
|
||||||
|
Collection<Integer> collection1 = Arrays.asList(7, 8, 9);
|
||||||
@Test
|
Collection<Integer> collection2 = Arrays.asList(10, 11, 12);
|
||||||
public void whenJoiningTwoCollections_thenJoined() {
|
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
|
||||||
Collection<Integer> collection1 = Arrays.asList(7, 8, 9);
|
.collect(Collectors.toList());
|
||||||
Collection<Integer> collection2 = Arrays.asList(10, 11, 12);
|
|
||||||
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
|
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12)));
|
||||||
.collect(Collectors.toList());
|
}
|
||||||
|
|
||||||
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
|
public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
|
||||||
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");
|
@Test
|
||||||
}
|
public void whenConvertCollectionToString_thenConverted() {
|
||||||
|
Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom");
|
||||||
|
String result = directions.stream()
|
||||||
|
.collect(Collectors.joining(", "));
|
||||||
|
|
||||||
|
assertEquals(result, "Left, Right, Top, Bottom");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertCollectionToString_thenConverted() {
|
public void whenConvertMapToString_thenConverted() {
|
||||||
Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom");
|
Map<Integer, String> users = new HashMap<>();
|
||||||
String result = directions.stream()
|
users.put(1, "John Doe");
|
||||||
.collect(Collectors.joining(", "));
|
users.put(2, "Paul Smith");
|
||||||
|
users.put(3, "Susan Anderson");
|
||||||
assertEquals(result, "Left, Right, Top, Bottom");
|
|
||||||
}
|
String result = users.entrySet().stream()
|
||||||
|
.map(entry -> entry.getKey() + " = " + entry.getValue())
|
||||||
|
.collect(Collectors.joining(", "));
|
||||||
|
|
||||||
|
assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertMapToString_thenConverted() {
|
public void whenConvertNestedCollectionToString_thenConverted() {
|
||||||
Map<Integer, String> users = new HashMap<>();
|
Collection<List<String>> nested = new ArrayList<>();
|
||||||
users.put(1, "John Doe");
|
nested.add(Arrays.asList("Left", "Right", "Top", "Bottom"));
|
||||||
users.put(2, "Paul Smith");
|
nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow"));
|
||||||
users.put(3, "Susan Anderson");
|
|
||||||
|
String result = nested.stream()
|
||||||
|
.map(nextList -> nextList.stream()
|
||||||
|
.collect(Collectors.joining("-")))
|
||||||
|
.collect(Collectors.joining("; "));
|
||||||
|
|
||||||
|
assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow");
|
||||||
|
}
|
||||||
|
|
||||||
String result = users.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue())
|
@Test
|
||||||
.collect(Collectors.joining(", "));
|
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
|
||||||
|
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
|
||||||
|
String result = fruits.stream()
|
||||||
|
.filter(next -> next != null)
|
||||||
|
.collect(Collectors.joining(", "));
|
||||||
|
|
||||||
|
assertEquals(result, "Apple, Orange, Grape");
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson");
|
@Test
|
||||||
}
|
public void whenSplitCollectionHalf_thenConverted() {
|
||||||
|
Collection<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||||
|
Collection<Integer> result1 = new ArrayList<>();
|
||||||
|
Collection<Integer> result2 = new ArrayList<>();
|
||||||
|
AtomicInteger count = new AtomicInteger();
|
||||||
|
int midpoint = Math.round(numbers.size() / 2);
|
||||||
|
|
||||||
|
numbers.forEach(next -> {
|
||||||
|
int index = count.getAndIncrement();
|
||||||
|
if (index < midpoint) {
|
||||||
|
result1.add(next);
|
||||||
|
} else {
|
||||||
|
result2.add(next);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assertTrue(result1.equals(Arrays.asList(1, 2, 3, 4, 5)));
|
||||||
|
assertTrue(result2.equals(Arrays.asList(6, 7, 8, 9, 10)));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertNestedCollectionToString_thenConverted() {
|
public void whenSplitArrayByWordLength_thenConverted() {
|
||||||
Collection<List<String>> nested = new ArrayList<>();
|
String[] words = new String[] { "bye", "cold", "it", "and", "my", "word" };
|
||||||
nested.add(Arrays.asList("Left", "Right", "Top", "Bottom"));
|
Map<Integer, List<String>> result = Arrays.stream(words)
|
||||||
nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow"));
|
.collect(Collectors.groupingBy(word -> word.length()));
|
||||||
|
|
||||||
String result = nested.stream()
|
assertTrue(result.get(2).equals(Arrays.asList("it", "my")));
|
||||||
.map(nextList -> nextList.stream()
|
assertTrue(result.get(3).equals(Arrays.asList("bye", "and")));
|
||||||
.collect(Collectors.joining("-")))
|
assertTrue(result.get(4).equals(Arrays.asList("cold", "word")));
|
||||||
.collect(Collectors.joining("; "));
|
}
|
||||||
|
|
||||||
assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
|
public void whenConvertStringToArray_thenConverted() {
|
||||||
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
|
String colors = "Red, Blue, Green, Yellow";
|
||||||
String result = fruits.stream()
|
String[] result = colors.split(", ");
|
||||||
.filter(next -> next != null)
|
|
||||||
.collect(Collectors.joining(", "));
|
assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" });
|
||||||
|
}
|
||||||
assertEquals(result, "Apple, Orange, Grape");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSplitCollectionHalf_thenConverted() {
|
public void whenConvertStringToCollection_thenConverted() {
|
||||||
Collection<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
String colors = "Left, Right, Top, Bottom";
|
||||||
Collection<Integer> result1 = new ArrayList<>();
|
Collection<String> result = Arrays.asList(colors.split(", "));
|
||||||
Collection<Integer> result2 = new ArrayList<>();
|
|
||||||
AtomicInteger count = new AtomicInteger();
|
assertTrue(result.equals(Arrays.asList("Left", "Right", "Top", "Bottom")));
|
||||||
int midpoint = Math.round(numbers.size() / 2);
|
}
|
||||||
|
|
||||||
numbers.forEach(next -> {
|
|
||||||
int index = count.getAndIncrement();
|
|
||||||
if(index < midpoint){
|
|
||||||
result1.add(next);
|
|
||||||
}else{
|
|
||||||
result2.add(next);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
assertTrue(result1.equals(Arrays.asList(1, 2, 3, 4, 5)));
|
|
||||||
assertTrue(result2.equals(Arrays.asList(6, 7, 8, 9, 10)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSplitArrayByWordLength_thenConverted() {
|
|
||||||
String[] words = new String[]{"bye", "cold", "it", "and", "my", "word"};
|
|
||||||
Map<Integer, List<String>> result = Arrays.stream(words)
|
|
||||||
.collect(Collectors.groupingBy(word -> word.length()));
|
|
||||||
|
|
||||||
assertTrue(result.get(2).equals(Arrays.asList("it", "my")));
|
|
||||||
assertTrue(result.get(3).equals(Arrays.asList("bye", "and")));
|
|
||||||
assertTrue(result.get(4).equals(Arrays.asList("cold", "word")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertStringToArray_thenConverted() {
|
public void whenConvertStringToMap_thenConverted() {
|
||||||
String colors = "Red, Blue, Green, Yellow";
|
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson";
|
||||||
String[] result = colors.split(", ");
|
|
||||||
|
Map<Integer, String> result = Arrays.stream(users.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(2), "Paul Smith");
|
||||||
|
assertEquals(result.get(3), "Susan Anderson");
|
||||||
|
}
|
||||||
|
|
||||||
assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" });
|
@Test
|
||||||
}
|
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
|
||||||
|
String fruits = "Apple. , Orange, Grape. Lemon";
|
||||||
@Test
|
|
||||||
public void whenConvertStringToCollection_thenConverted() {
|
Collection<String> result = Arrays.stream(fruits.split("[,|.]"))
|
||||||
String colors = "Left, Right, Top, Bottom";
|
.map(String::trim)
|
||||||
Collection<String> result = Arrays.asList(colors.split(", "));
|
.filter(next -> !next.isEmpty())
|
||||||
|
.collect(Collectors.toList());
|
||||||
assertTrue(result.equals(Arrays.asList("Left", "Right", "Top", "Bottom")));
|
|
||||||
}
|
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon")));
|
||||||
|
}
|
||||||
@Test
|
|
||||||
public void whenConvertStringToMap_thenConverted() {
|
|
||||||
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson";
|
|
||||||
|
|
||||||
Map<Integer, String> result = Arrays.stream(users.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(2), "Paul Smith");
|
|
||||||
assertEquals(result.get(3), "Susan Anderson");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
|
|
||||||
String fruits = "Apple. , Orange, Grape. Lemon";
|
|
||||||
|
|
||||||
Collection<String> result = Arrays.stream(fruits.split("[,|.]"))
|
|
||||||
.map(String::trim)
|
|
||||||
.filter(next -> !next.isEmpty())
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon")));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue