Skip to content

Commit a6bdad2

Browse files
committed
Refactor JoinSplitCollectionsUnitTest
1 parent 34e5a64 commit a6bdad2

1 file changed

Lines changed: 48 additions & 55 deletions

File tree

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,33 @@
11
package org.baeldung.java.collections;
22

3-
import static org.junit.Assert.assertArrayEquals;
4-
import static org.junit.Assert.assertEquals;
5-
import static org.junit.Assert.assertTrue;
6-
7-
import java.util.ArrayList;
8-
import java.util.Arrays;
9-
import java.util.Collection;
10-
import java.util.HashMap;
11-
import java.util.List;
12-
import java.util.Map;
3+
import org.junit.Test;
4+
5+
import java.util.*;
136
import java.util.concurrent.atomic.AtomicInteger;
147
import java.util.stream.Collectors;
158
import java.util.stream.Stream;
169

17-
import org.junit.Test;
10+
import static org.junit.Assert.*;
1811

1912
public class JoinSplitCollectionsUnitTest {
2013

2114
@Test
2215
public void whenJoiningTwoArrays_thenJoined() {
23-
String[] animals1 = new String[] { "Dog", "Cat" };
24-
String[] animals2 = new String[] { "Bird", "Cow" };
16+
String[] animals1 = new String[]{"Dog", "Cat"};
17+
String[] animals2 = new String[]{"Bird", "Cow"};
2518
String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2))
26-
.toArray(String[]::new);
27-
28-
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
19+
.toArray(String[]::new);
20+
21+
assertArrayEquals(result, new String[]{"Dog", "Cat", "Bird", "Cow"});
2922
}
3023

3124
@Test
3225
public void whenJoiningTwoCollections_thenJoined() {
3326
Collection<Integer> collection1 = Arrays.asList(7, 8, 9);
3427
Collection<Integer> collection2 = Arrays.asList(10, 11, 12);
3528
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
36-
.collect(Collectors.toList());
37-
29+
.collect(Collectors.toList());
30+
3831
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12)));
3932
}
4033

@@ -43,27 +36,27 @@ public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
4336
Collection<Integer> collection1 = Arrays.asList(7, 8, 11);
4437
Collection<Integer> collection2 = Arrays.asList(9, 12, 10);
4538
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
46-
.filter(next -> next <= 10)
47-
.collect(Collectors.toList());
48-
39+
.filter(next -> next <= 10)
40+
.collect(Collectors.toList());
41+
4942
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10)));
5043
}
5144

5245
@Test
5346
public void whenConvertArrayToString_thenConverted() {
54-
String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" };
47+
String[] colors = new String[]{"Red", "Blue", "Green", "Yellow"};
5548
String result = Arrays.stream(colors)
56-
.collect(Collectors.joining(", "));
57-
49+
.collect(Collectors.joining(", "));
50+
5851
assertEquals(result, "Red, Blue, Green, Yellow");
5952
}
6053

6154
@Test
6255
public void whenConvertCollectionToString_thenConverted() {
6356
Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom");
6457
String result = directions.stream()
65-
.collect(Collectors.joining(", "));
66-
58+
.collect(Collectors.joining(", "));
59+
6760
assertEquals(result, "Left, Right, Top, Bottom");
6861
}
6962

@@ -73,11 +66,11 @@ public void whenConvertMapToString_thenConverted() {
7366
users.put(1, "John Doe");
7467
users.put(2, "Paul Smith");
7568
users.put(3, "Susan Anderson");
76-
69+
7770
String result = users.entrySet().stream()
78-
.map(entry -> entry.getKey() + " = " + entry.getValue())
79-
.collect(Collectors.joining(", "));
80-
71+
.map(entry -> entry.getKey() + " = " + entry.getValue())
72+
.collect(Collectors.joining(", "));
73+
8174
assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson");
8275
}
8376

@@ -86,22 +79,22 @@ public void whenConvertNestedCollectionToString_thenConverted() {
8679
Collection<List<String>> nested = new ArrayList<>();
8780
nested.add(Arrays.asList("Left", "Right", "Top", "Bottom"));
8881
nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow"));
89-
82+
9083
String result = nested.stream()
91-
.map(nextList -> nextList.stream()
92-
.collect(Collectors.joining("-")))
93-
.collect(Collectors.joining("; "));
94-
84+
.map(nextList -> nextList.stream()
85+
.collect(Collectors.joining("-")))
86+
.collect(Collectors.joining("; "));
87+
9588
assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow");
9689
}
9790

9891
@Test
9992
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
10093
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
10194
String result = fruits.stream()
102-
.filter(next -> next != null)
103-
.collect(Collectors.joining(", "));
104-
95+
.filter(Objects::nonNull)
96+
.collect(Collectors.joining(", "));
97+
10598
assertEquals(result, "Apple, Orange, Grape");
10699
}
107100

@@ -112,7 +105,7 @@ public void whenSplitCollectionHalf_thenConverted() {
112105
Collection<Integer> result2 = new ArrayList<>();
113106
AtomicInteger count = new AtomicInteger();
114107
int midpoint = Math.round(numbers.size() / 2);
115-
108+
116109
numbers.forEach(next -> {
117110
int index = count.getAndIncrement();
118111
if (index < midpoint) {
@@ -121,17 +114,17 @@ public void whenSplitCollectionHalf_thenConverted() {
121114
result2.add(next);
122115
}
123116
});
124-
117+
125118
assertTrue(result1.equals(Arrays.asList(1, 2, 3, 4, 5)));
126119
assertTrue(result2.equals(Arrays.asList(6, 7, 8, 9, 10)));
127120
}
128121

129122
@Test
130123
public void whenSplitArrayByWordLength_thenConverted() {
131-
String[] words = new String[] { "bye", "cold", "it", "and", "my", "word" };
124+
String[] words = new String[]{"bye", "cold", "it", "and", "my", "word"};
132125
Map<Integer, List<String>> result = Arrays.stream(words)
133-
.collect(Collectors.groupingBy(word -> word.length()));
134-
126+
.collect(Collectors.groupingBy(String::length));
127+
135128
assertTrue(result.get(2).equals(Arrays.asList("it", "my")));
136129
assertTrue(result.get(3).equals(Arrays.asList("bye", "and")));
137130
assertTrue(result.get(4).equals(Arrays.asList("cold", "word")));
@@ -141,26 +134,26 @@ public void whenSplitArrayByWordLength_thenConverted() {
141134
public void whenConvertStringToArray_thenConverted() {
142135
String colors = "Red, Blue, Green, Yellow";
143136
String[] result = colors.split(", ");
144-
145-
assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" });
137+
138+
assertArrayEquals(result, new String[]{"Red", "Blue", "Green", "Yellow"});
146139
}
147140

148141
@Test
149142
public void whenConvertStringToCollection_thenConverted() {
150143
String colors = "Left, Right, Top, Bottom";
151144
Collection<String> result = Arrays.asList(colors.split(", "));
152-
145+
153146
assertTrue(result.equals(Arrays.asList("Left", "Right", "Top", "Bottom")));
154147
}
155148

156149
@Test
157150
public void whenConvertStringToMap_thenConverted() {
158151
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson";
159-
152+
160153
Map<Integer, String> result = Arrays.stream(users.split(", "))
161-
.map(next -> next.split(" = "))
162-
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
163-
154+
.map(next -> next.split(" = "))
155+
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
156+
164157
assertEquals(result.get(1), "John Doe");
165158
assertEquals(result.get(2), "Paul Smith");
166159
assertEquals(result.get(3), "Susan Anderson");
@@ -169,12 +162,12 @@ public void whenConvertStringToMap_thenConverted() {
169162
@Test
170163
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
171164
String fruits = "Apple. , Orange, Grape. Lemon";
172-
165+
173166
Collection<String> result = Arrays.stream(fruits.split("[,|.]"))
174-
.map(String::trim)
175-
.filter(next -> !next.isEmpty())
176-
.collect(Collectors.toList());
177-
167+
.map(String::trim)
168+
.filter(next -> !next.isEmpty())
169+
.collect(Collectors.toList());
170+
178171
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon")));
179172
}
180173
}

0 commit comments

Comments
 (0)