Skip to content

Commit 5a305a2

Browse files
committed
Minor changes following review
1 parent 8e203dd commit 5a305a2

2 files changed

Lines changed: 45 additions & 57 deletions

File tree

‎core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@
66
import org.junit.Test;
77

88
public class ArraysJoinAndSplitJUnitTest {
9-
9+
1010
private final String[] sauces = {"Marinara", "Olive Oil"};
1111
private final String[] cheeses = {"Mozzarella", "Feta", "Parmesan"};
1212
private final String[] vegetables = {"Olives", "Spinach", "Green Peppers"};
13-
13+
14+
private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"};
1415

1516
@Test
16-
public void givenThreeStringArrays_whenJoiningIntoOneStringArray_ShouldSucceed() throws Exception {
17+
public void givenThreeStringArrays_whenJoiningIntoOneStringArray_shouldSucceed() {
1718
String[] toppings = new String[sauces.length + cheeses.length + vegetables.length];
18-
19+
1920
System.arraycopy(sauces, 0, toppings, 0, sauces.length);
2021
int AddedSoFar = sauces.length;
21-
22+
2223
System.arraycopy(cheeses, 0, toppings, AddedSoFar, cheeses.length);
2324
AddedSoFar += cheeses.length;
2425

2526
System.arraycopy(vegetables, 0, toppings, AddedSoFar, vegetables.length);
26-
27+
2728
Assert.assertArrayEquals(toppings,
28-
new String[] {"Marinara", "Olive Oil", "Mozzarella", "Feta",
29-
"Parmesan", "Olives", "Spinach", "Green Peppers"} );
29+
new String[]{"Marinara", "Olive Oil", "Mozzarella", "Feta",
30+
"Parmesan", "Olives", "Spinach", "Green Peppers"});
3031
}
31-
32-
33-
private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"};
32+
33+
3434
@Test
35-
public void givenOneStringArray_whenSplittingInHalfTwoStringArrays_ShouldSucceed() throws Exception {
35+
public void givenOneStringArray_whenSplittingInHalfTwoStringArrays_shouldSucceed() {
3636
int ordersHalved = (customers.length / 2) + (customers.length % 2);
37-
37+
3838
String[] driverOne = Arrays.copyOf(customers, ordersHalved);
3939
String[] driverTwo = Arrays.copyOfRange(customers, ordersHalved, customers.length);
40-
41-
Assert.assertArrayEquals(driverOne, new String[] {"Jay", "Harry", "Ronnie"} );
42-
Assert.assertArrayEquals(driverTwo, new String[] {"Gary", "Ross"} );
40+
41+
Assert.assertArrayEquals(driverOne, new String[]{"Jay", "Harry", "Ronnie"});
42+
Assert.assertArrayEquals(driverTwo, new String[]{"Gary", "Ross"});
4343
}
4444
}
Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,42 @@
11
package org.baeldung.java.collections;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
45

56
import org.junit.Assert;
7+
import org.junit.Before;
68
import org.junit.Test;
79

810
public class CollectionsJoinAndSplitJUnitTest {
911

10-
public ArrayList<String> sauces = new ArrayList<String>();
11-
public ArrayList<String> cheeses = new ArrayList<String>();
12-
public ArrayList<String> vegetables = new ArrayList<String>();
13-
14-
public ArrayList<ArrayList<String>> ingredients = new ArrayList<ArrayList<String>>();
12+
private ArrayList<String> sauces = new ArrayList<>();
13+
private ArrayList<String> cheeses = new ArrayList<>();
14+
private ArrayList<String> vegetables = new ArrayList<>();
1515

16-
public CollectionsJoinAndSplitJUnitTest() throws Exception {
17-
whenGeneratingTestData_ShouldSucceed();
18-
}
16+
private ArrayList<ArrayList<String>> ingredients = new ArrayList<>();
1917

20-
@Test
21-
public void whenGeneratingTestData_ShouldSucceed() throws Exception {
22-
sauces.clear();
18+
@Before
19+
public void init() {
2320
sauces.add("Olive Oil");
2421
sauces.add("Marinara");
25-
26-
cheeses.clear();
27-
cheeses.add("Mozarella");
22+
23+
cheeses.add("Mozzarella");
2824
cheeses.add("Feta");
2925
cheeses.add("Parmesan");
30-
31-
vegetables.clear();
26+
3227
vegetables.add("Olives");
3328
vegetables.add("Spinach");
3429
vegetables.add("Green Peppers");
35-
36-
ingredients.clear();
30+
3731
ingredients.add(sauces);
3832
ingredients.add(cheeses);
3933
ingredients.add(vegetables);
40-
41-
Assert.assertTrue(sauces.size() == 2);
42-
Assert.assertTrue(cheeses.size() == 3);
43-
Assert.assertTrue(vegetables.size() == 3);
44-
Assert.assertTrue(ingredients.size() == 3);
4534
}
46-
35+
4736
@Test
48-
public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() throws Exception {
49-
ArrayList<ArrayList<String>> toppings = new ArrayList<ArrayList<String>>();
50-
37+
public void givenThreeArrayLists_whenJoiningIntoOneArrayList_shouldSucceed() {
38+
ArrayList<ArrayList<String>> toppings = new ArrayList<>();
39+
5140
toppings.add(sauces);
5241
toppings.add(cheeses);
5342
toppings.add(vegetables);
@@ -57,18 +46,17 @@ public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() thr
5746
Assert.assertTrue(toppings.contains(cheeses));
5847
Assert.assertTrue(toppings.contains(vegetables));
5948
}
60-
61-
@Test
62-
public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() throws Exception {
63-
ArrayList<ArrayList<String>> toppings = ingredients;
64-
ArrayList<ArrayList<String>> removedToppings = new ArrayList<ArrayList<String>>();
65-
66-
removedToppings.add(toppings.remove(toppings.indexOf(vegetables)));
67-
68-
Assert.assertTrue(removedToppings.contains(vegetables));
69-
Assert.assertTrue(removedToppings.size() == 1);
70-
Assert.assertTrue(toppings.size() == 2);
71-
Assert.assertTrue(toppings.contains(sauces));
72-
Assert.assertTrue(toppings.contains(cheeses));
73-
}
49+
50+
@Test
51+
public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() {
52+
53+
ArrayList<ArrayList<String>> removedToppings = new ArrayList<>();
54+
removedToppings.add(ingredients.remove(ingredients.indexOf(vegetables)));
55+
56+
Assert.assertTrue(removedToppings.contains(vegetables));
57+
Assert.assertTrue(removedToppings.size() == 1);
58+
Assert.assertTrue(ingredients.size() == 2);
59+
Assert.assertTrue(ingredients.contains(sauces));
60+
Assert.assertTrue(ingredients.contains(cheeses));
61+
}
7462
}

0 commit comments

Comments
 (0)