Minor changes following review
This commit is contained in:
parent
8e203dd204
commit
5a305a25ae
@ -11,9 +11,10 @@ public class ArraysJoinAndSplitJUnitTest {
|
|||||||
private final String[] cheeses = {"Mozzarella", "Feta", "Parmesan"};
|
private final String[] cheeses = {"Mozzarella", "Feta", "Parmesan"};
|
||||||
private final String[] vegetables = {"Olives", "Spinach", "Green Peppers"};
|
private final String[] vegetables = {"Olives", "Spinach", "Green Peppers"};
|
||||||
|
|
||||||
|
private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"};
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenThreeStringArrays_whenJoiningIntoOneStringArray_ShouldSucceed() throws Exception {
|
public void givenThreeStringArrays_whenJoiningIntoOneStringArray_shouldSucceed() {
|
||||||
String[] toppings = new String[sauces.length + cheeses.length + vegetables.length];
|
String[] toppings = new String[sauces.length + cheeses.length + vegetables.length];
|
||||||
|
|
||||||
System.arraycopy(sauces, 0, toppings, 0, sauces.length);
|
System.arraycopy(sauces, 0, toppings, 0, sauces.length);
|
||||||
@ -25,20 +26,19 @@ public class ArraysJoinAndSplitJUnitTest {
|
|||||||
System.arraycopy(vegetables, 0, toppings, AddedSoFar, vegetables.length);
|
System.arraycopy(vegetables, 0, toppings, AddedSoFar, vegetables.length);
|
||||||
|
|
||||||
Assert.assertArrayEquals(toppings,
|
Assert.assertArrayEquals(toppings,
|
||||||
new String[] {"Marinara", "Olive Oil", "Mozzarella", "Feta",
|
new String[]{"Marinara", "Olive Oil", "Mozzarella", "Feta",
|
||||||
"Parmesan", "Olives", "Spinach", "Green Peppers"} );
|
"Parmesan", "Olives", "Spinach", "Green Peppers"});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"};
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOneStringArray_whenSplittingInHalfTwoStringArrays_ShouldSucceed() throws Exception {
|
public void givenOneStringArray_whenSplittingInHalfTwoStringArrays_shouldSucceed() {
|
||||||
int ordersHalved = (customers.length / 2) + (customers.length % 2);
|
int ordersHalved = (customers.length / 2) + (customers.length % 2);
|
||||||
|
|
||||||
String[] driverOne = Arrays.copyOf(customers, ordersHalved);
|
String[] driverOne = Arrays.copyOf(customers, ordersHalved);
|
||||||
String[] driverTwo = Arrays.copyOfRange(customers, ordersHalved, customers.length);
|
String[] driverTwo = Arrays.copyOfRange(customers, ordersHalved, customers.length);
|
||||||
|
|
||||||
Assert.assertArrayEquals(driverOne, new String[] {"Jay", "Harry", "Ronnie"} );
|
Assert.assertArrayEquals(driverOne, new String[]{"Jay", "Harry", "Ronnie"});
|
||||||
Assert.assertArrayEquals(driverTwo, new String[] {"Gary", "Ross"} );
|
Assert.assertArrayEquals(driverTwo, new String[]{"Gary", "Ross"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,52 +1,41 @@
|
|||||||
package org.baeldung.java.collections;
|
package org.baeldung.java.collections;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class CollectionsJoinAndSplitJUnitTest {
|
public class CollectionsJoinAndSplitJUnitTest {
|
||||||
|
|
||||||
public ArrayList<String> sauces = new ArrayList<String>();
|
private ArrayList<String> sauces = new ArrayList<>();
|
||||||
public ArrayList<String> cheeses = new ArrayList<String>();
|
private ArrayList<String> cheeses = new ArrayList<>();
|
||||||
public ArrayList<String> vegetables = new ArrayList<String>();
|
private ArrayList<String> vegetables = new ArrayList<>();
|
||||||
|
|
||||||
public ArrayList<ArrayList<String>> ingredients = new ArrayList<ArrayList<String>>();
|
private ArrayList<ArrayList<String>> ingredients = new ArrayList<>();
|
||||||
|
|
||||||
public CollectionsJoinAndSplitJUnitTest() throws Exception {
|
@Before
|
||||||
whenGeneratingTestData_ShouldSucceed();
|
public void init() {
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenGeneratingTestData_ShouldSucceed() throws Exception {
|
|
||||||
sauces.clear();
|
|
||||||
sauces.add("Olive Oil");
|
sauces.add("Olive Oil");
|
||||||
sauces.add("Marinara");
|
sauces.add("Marinara");
|
||||||
|
|
||||||
cheeses.clear();
|
cheeses.add("Mozzarella");
|
||||||
cheeses.add("Mozarella");
|
|
||||||
cheeses.add("Feta");
|
cheeses.add("Feta");
|
||||||
cheeses.add("Parmesan");
|
cheeses.add("Parmesan");
|
||||||
|
|
||||||
vegetables.clear();
|
|
||||||
vegetables.add("Olives");
|
vegetables.add("Olives");
|
||||||
vegetables.add("Spinach");
|
vegetables.add("Spinach");
|
||||||
vegetables.add("Green Peppers");
|
vegetables.add("Green Peppers");
|
||||||
|
|
||||||
ingredients.clear();
|
|
||||||
ingredients.add(sauces);
|
ingredients.add(sauces);
|
||||||
ingredients.add(cheeses);
|
ingredients.add(cheeses);
|
||||||
ingredients.add(vegetables);
|
ingredients.add(vegetables);
|
||||||
|
|
||||||
Assert.assertTrue(sauces.size() == 2);
|
|
||||||
Assert.assertTrue(cheeses.size() == 3);
|
|
||||||
Assert.assertTrue(vegetables.size() == 3);
|
|
||||||
Assert.assertTrue(ingredients.size() == 3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() throws Exception {
|
public void givenThreeArrayLists_whenJoiningIntoOneArrayList_shouldSucceed() {
|
||||||
ArrayList<ArrayList<String>> toppings = new ArrayList<ArrayList<String>>();
|
ArrayList<ArrayList<String>> toppings = new ArrayList<>();
|
||||||
|
|
||||||
toppings.add(sauces);
|
toppings.add(sauces);
|
||||||
toppings.add(cheeses);
|
toppings.add(cheeses);
|
||||||
@ -59,16 +48,15 @@ public class CollectionsJoinAndSplitJUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() throws Exception {
|
public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() {
|
||||||
ArrayList<ArrayList<String>> toppings = ingredients;
|
|
||||||
ArrayList<ArrayList<String>> removedToppings = new ArrayList<ArrayList<String>>();
|
|
||||||
|
|
||||||
removedToppings.add(toppings.remove(toppings.indexOf(vegetables)));
|
ArrayList<ArrayList<String>> removedToppings = new ArrayList<>();
|
||||||
|
removedToppings.add(ingredients.remove(ingredients.indexOf(vegetables)));
|
||||||
|
|
||||||
Assert.assertTrue(removedToppings.contains(vegetables));
|
Assert.assertTrue(removedToppings.contains(vegetables));
|
||||||
Assert.assertTrue(removedToppings.size() == 1);
|
Assert.assertTrue(removedToppings.size() == 1);
|
||||||
Assert.assertTrue(toppings.size() == 2);
|
Assert.assertTrue(ingredients.size() == 2);
|
||||||
Assert.assertTrue(toppings.contains(sauces));
|
Assert.assertTrue(ingredients.contains(sauces));
|
||||||
Assert.assertTrue(toppings.contains(cheeses));
|
Assert.assertTrue(ingredients.contains(cheeses));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user