From 5a305a25ae7b222a8954ce980c466d7701732081 Mon Sep 17 00:00:00 2001 From: Alex Theedom Date: Tue, 2 Aug 2016 19:37:55 +0100 Subject: [PATCH] Minor changes following review --- .../arrays/ArraysJoinAndSplitJUnitTest.java | 32 ++++----- .../CollectionsJoinAndSplitJUnitTest.java | 70 ++++++++----------- 2 files changed, 45 insertions(+), 57 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java index ec59d08525..ad1f2dc70c 100644 --- a/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java @@ -6,39 +6,39 @@ import org.junit.Assert; import org.junit.Test; public class ArraysJoinAndSplitJUnitTest { - + private final String[] sauces = {"Marinara", "Olive Oil"}; private final String[] cheeses = {"Mozzarella", "Feta", "Parmesan"}; private final String[] vegetables = {"Olives", "Spinach", "Green Peppers"}; - + + private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"}; @Test - public void givenThreeStringArrays_whenJoiningIntoOneStringArray_ShouldSucceed() throws Exception { + public void givenThreeStringArrays_whenJoiningIntoOneStringArray_shouldSucceed() { String[] toppings = new String[sauces.length + cheeses.length + vegetables.length]; - + System.arraycopy(sauces, 0, toppings, 0, sauces.length); int AddedSoFar = sauces.length; - + System.arraycopy(cheeses, 0, toppings, AddedSoFar, cheeses.length); AddedSoFar += cheeses.length; System.arraycopy(vegetables, 0, toppings, AddedSoFar, vegetables.length); - + Assert.assertArrayEquals(toppings, - new String[] {"Marinara", "Olive Oil", "Mozzarella", "Feta", - "Parmesan", "Olives", "Spinach", "Green Peppers"} ); + new String[]{"Marinara", "Olive Oil", "Mozzarella", "Feta", + "Parmesan", "Olives", "Spinach", "Green Peppers"}); } - - - private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"}; + + @Test - public void givenOneStringArray_whenSplittingInHalfTwoStringArrays_ShouldSucceed() throws Exception { + public void givenOneStringArray_whenSplittingInHalfTwoStringArrays_shouldSucceed() { int ordersHalved = (customers.length / 2) + (customers.length % 2); - + String[] driverOne = Arrays.copyOf(customers, ordersHalved); String[] driverTwo = Arrays.copyOfRange(customers, ordersHalved, customers.length); - - Assert.assertArrayEquals(driverOne, new String[] {"Jay", "Harry", "Ronnie"} ); - Assert.assertArrayEquals(driverTwo, new String[] {"Gary", "Ross"} ); + + Assert.assertArrayEquals(driverOne, new String[]{"Jay", "Harry", "Ronnie"}); + Assert.assertArrayEquals(driverTwo, new String[]{"Gary", "Ross"}); } } diff --git a/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java index 3a7a527656..c288cf499d 100644 --- a/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java @@ -1,53 +1,42 @@ package org.baeldung.java.collections; import java.util.ArrayList; +import java.util.Collections; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; public class CollectionsJoinAndSplitJUnitTest { - public ArrayList sauces = new ArrayList(); - public ArrayList cheeses = new ArrayList(); - public ArrayList vegetables = new ArrayList(); - - public ArrayList> ingredients = new ArrayList>(); + private ArrayList sauces = new ArrayList<>(); + private ArrayList cheeses = new ArrayList<>(); + private ArrayList vegetables = new ArrayList<>(); - public CollectionsJoinAndSplitJUnitTest() throws Exception { - whenGeneratingTestData_ShouldSucceed(); - } + private ArrayList> ingredients = new ArrayList<>(); - @Test - public void whenGeneratingTestData_ShouldSucceed() throws Exception { - sauces.clear(); + @Before + public void init() { sauces.add("Olive Oil"); sauces.add("Marinara"); - - cheeses.clear(); - cheeses.add("Mozarella"); + + cheeses.add("Mozzarella"); cheeses.add("Feta"); cheeses.add("Parmesan"); - - vegetables.clear(); + vegetables.add("Olives"); vegetables.add("Spinach"); vegetables.add("Green Peppers"); - - ingredients.clear(); + ingredients.add(sauces); ingredients.add(cheeses); ingredients.add(vegetables); - - Assert.assertTrue(sauces.size() == 2); - Assert.assertTrue(cheeses.size() == 3); - Assert.assertTrue(vegetables.size() == 3); - Assert.assertTrue(ingredients.size() == 3); } - + @Test - public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() throws Exception { - ArrayList> toppings = new ArrayList>(); - + public void givenThreeArrayLists_whenJoiningIntoOneArrayList_shouldSucceed() { + ArrayList> toppings = new ArrayList<>(); + toppings.add(sauces); toppings.add(cheeses); toppings.add(vegetables); @@ -57,18 +46,17 @@ public class CollectionsJoinAndSplitJUnitTest { Assert.assertTrue(toppings.contains(cheeses)); Assert.assertTrue(toppings.contains(vegetables)); } - - @Test - public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() throws Exception { - ArrayList> toppings = ingredients; - ArrayList> removedToppings = new ArrayList>(); - - removedToppings.add(toppings.remove(toppings.indexOf(vegetables))); - - Assert.assertTrue(removedToppings.contains(vegetables)); - Assert.assertTrue(removedToppings.size() == 1); - Assert.assertTrue(toppings.size() == 2); - Assert.assertTrue(toppings.contains(sauces)); - Assert.assertTrue(toppings.contains(cheeses)); - } + + @Test + public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() { + + ArrayList> removedToppings = new ArrayList<>(); + removedToppings.add(ingredients.remove(ingredients.indexOf(vegetables))); + + Assert.assertTrue(removedToppings.contains(vegetables)); + Assert.assertTrue(removedToppings.size() == 1); + Assert.assertTrue(ingredients.size() == 2); + Assert.assertTrue(ingredients.contains(sauces)); + Assert.assertTrue(ingredients.contains(cheeses)); + } }