|
| 1 | +package org.baeldung.java.collections; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +import org.junit.Assert; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +public class CollectionsJoinAndSplitJUnitTest { |
| 9 | + |
| 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>>(); |
| 15 | + |
| 16 | + public CollectionsJoinAndSplitJUnitTest() throws Exception { |
| 17 | + whenGeneratingTestData_ShouldSucceed(); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void whenGeneratingTestData_ShouldSucceed() throws Exception { |
| 22 | + sauces.clear(); |
| 23 | + sauces.add("Olive Oil"); |
| 24 | + sauces.add("Marinara"); |
| 25 | + |
| 26 | + cheeses.clear(); |
| 27 | + cheeses.add("Mozarella"); |
| 28 | + cheeses.add("Feta"); |
| 29 | + cheeses.add("Parmesan"); |
| 30 | + |
| 31 | + vegetables.clear(); |
| 32 | + vegetables.add("Olives"); |
| 33 | + vegetables.add("Spinach"); |
| 34 | + vegetables.add("Green Peppers"); |
| 35 | + |
| 36 | + ingredients.clear(); |
| 37 | + ingredients.add(sauces); |
| 38 | + ingredients.add(cheeses); |
| 39 | + 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); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() throws Exception { |
| 49 | + ArrayList<ArrayList<String>> toppings = new ArrayList<ArrayList<String>>(); |
| 50 | + |
| 51 | + toppings.add(sauces); |
| 52 | + toppings.add(cheeses); |
| 53 | + toppings.add(vegetables); |
| 54 | + |
| 55 | + Assert.assertTrue(toppings.size() == 3); |
| 56 | + Assert.assertTrue(toppings.contains(sauces)); |
| 57 | + Assert.assertTrue(toppings.contains(cheeses)); |
| 58 | + Assert.assertTrue(toppings.contains(vegetables)); |
| 59 | + } |
| 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 | + } |
| 74 | +} |
0 commit comments