package com.michaelthomas.collection; /** * @author Michael Thomas (www.michael-thomas.com) michael@michael-thomas.com * */ import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.junit.Test; import org.junit.Before; public class MyCollectionTest { private MyCollectionTraverse myCollectionTraverse = new MyCollectionTraverse(); private MyCollectionSort myCollSort = new MyCollectionSort(); private MyCollectionToArrays myCollectionToArrays = new MyCollectionToArrays(); private MyCollectionData myListData = new MyCollectionData(); private List myListPlay = new ArrayList(); private List myListNumbers; private List myListPets; private List myListPizzas; private List myListItems; @Before public void setUp() throws Exception { myListNumbers = myListData.buildMyListNumbers(); myListPets = myListData.buildMyListPets(); myListPizzas = myListData.buildMyListPizzas(); myListItems = myListData.buildMyListItems(); } @Test public void testAllOperationsInOneMethod() { //Note: For those who want to see all in one method. Other look the other way :-)! //This is an example of using all the methods in the Collection interface. //.clear() myListPlay.clear(); //Note: We can't control the order that methods run! //.isEmpty() - basic operation. assertTrue(myListPlay.isEmpty()); //.add(Object) - basic operation myListPlay.add("Hello"); myListPlay.add("Now"); //.size() - basic operation assertEquals(2, myListPlay.size()); //.add(index, Object) myListPlay.add(1, "World"); //inserts between "Hello" and "Now" in slot 1. "Hello" is in slot 0. //.toString() (part of the Object class, not Collection interface). assertEquals("[Hello, World, Now]", myListPlay.toString()); //.remove(Object) myListPlay.remove("World"); assertEquals("[Hello, Now]", myListPlay.toString()); //.remove(index) myListPlay.remove(0); //Note: Index starts with 0! assertEquals("[Now]", myListPlay.toString()); //.addAll(Collection) myListPlay.clear(); //Start Fresh. myListPlay.addAll(myListPets); myListPlay.addAll(myListNumbers); assertEquals(7, myListPlay.size()); //.addAll(index, Collection) myListPlay.clear(); //Start Fresh. myListPlay.addAll(myListPets); myListPlay.addAll(1, myListNumbers); //Notice how the list was inserted starting slot 1 ! assertEquals("[dog, One, Two, Three, Four, Five, cat]", myListPlay.toString()); //.contains(Object) assertTrue(myListPlay.contains("dog")); assertTrue(myListPlay.contains("One")); //.containsAll(Collection) assertTrue(myListPlay.containsAll(myListPets)); assertTrue(myListPlay.containsAll(myListNumbers)); //.removeAll(Collection) myListPlay.clear(); //Start Fresh. myListPlay.addAll(myListNumbers); myListPlay.addAll(myListPets); assertEquals(7, myListPlay.size()); myListPlay.removeAll(myListPets); assertEquals(5, myListPlay.size()); //.retainAll(Collection) myListPlay.clear(); //Start Fresh. myListPlay.addAll(myListNumbers); myListPlay.addAll(myListPets); assertEquals(7, myListPlay.size()); myListPlay.retainAll(myListPets); //Retain (keep) only the pets! assertEquals(2, myListPlay.size()); //For examples of the following look at the end of this JUnit: //toArray() returns Object[] //toArray(T[] a) returns T[] } @Test public void testBasicOperationsSize() { assertEquals(5, myListNumbers.size()); assertEquals(2, myListPets.size()); assertEquals(3, myListPizzas.size()); assertEquals(0, myListPlay.size()); //Empty list. } @Test public void testBasicOperationsIsEmpty() { assertFalse(myListNumbers.isEmpty()); assertTrue(myListPlay.isEmpty()); //Empty list. } @Test public void testBasicOperationsContains() { //Numbers assertTrue(myListNumbers.contains("One")); assertFalse(myListNumbers.contains("Boo")); //Pets assertTrue(myListPets.contains("dog")); //Pizzas assertTrue(myListPizzas.contains("Cheese")); assertFalse(myListPizzas.contains("cheese")); //Lower case doesn't exist! //Play List assertFalse(myListPlay.contains("")); //Empty list. } @Test public void testBasicOperationsAddRemove() { myListPlay.clear(); //Note: We can't control the order that methods run! //.add(Object) - basic operation myListPlay.add("Hello"); myListPlay.add("Now"); assertEquals(2, myListPlay.size()); //.add(index, Object) myListPlay.add(1, "World"); //inserts between "Hello" and "Now" in slot 1. "Hello" is in slot 0. myListPlay.add("Again"); assertEquals(4, myListPlay.size()); assertEquals("[Hello, World, Now, Again]", myListPlay.toString()); myListPlay.remove("World"); assertEquals("[Hello, Now, Again]", myListPlay.toString()); myListPlay.remove(0); //Note: Index starts with 0! assertEquals("[Now, Again]", myListPlay.toString()); myListPlay.remove(1); //Note: Index starts with 0! assertEquals("[Now]", myListPlay.toString()); } @Test public void testBasicOperationsIterator() { //More detailed examples are in the other .java files! // (Forward, Reverse, While, ForLoop, ForEach etc...) //Here is a very basic example. StringBuilder sb = new StringBuilder(); for (Iterator iterator = myListNumbers.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); sb.append(string + (iterator.hasNext() ? ", " : "")); } assertEquals("One, Two, Three, Four, Five", sb.toString()); } @Test public void testBulkOperationsAddAll() { myListPlay.clear(); myListPlay.addAll(myListNumbers); assertEquals(5, myListPlay.size()); myListPlay.addAll(myListPets); assertEquals(7, myListPlay.size()); } @Test public void testBulkOperationsContainsAll() { assertTrue(myListNumbers.containsAll(myListNumbers)); //Same list :-) assertFalse(myListNumbers.containsAll(myListPets)); myListPlay.clear(); myListPlay.addAll(myListNumbers); myListPlay.addAll(myListPets); assertTrue(myListPlay.containsAll(myListNumbers)); assertTrue(myListPlay.containsAll(myListPets)); assertFalse(myListPlay.containsAll(myListPizzas)); } @Test public void testBulkOperationsRemoveAll() { myListPlay.clear(); myListPlay.addAll(myListNumbers); myListPlay.addAll(myListPets); myListPlay.addAll(myListPizzas); assertEquals(10, myListPlay.size()); assertTrue(myListPlay.contains("dog")); assertTrue(myListPlay.contains("One")); myListPlay.removeAll(myListPets); //Removes only this collection. assertEquals(8, myListPlay.size()); assertFalse(myListPlay.contains("dog")); assertTrue(myListPlay.contains("One")); } @Test public void testBulkOperationsRetainAll() { myListPlay.clear(); myListPlay.addAll(myListNumbers); myListPlay.addAll(myListPets); myListPlay.addAll(myListPizzas); assertEquals(10, myListPlay.size()); assertTrue(myListPlay.contains("dog")); assertTrue(myListPlay.contains("One")); myListPlay.retainAll(myListPets); //Only keeps this collection. Removes the others. assertEquals(2, myListPlay.size()); assertTrue(myListPlay.contains("dog")); assertFalse(myListPlay.contains("One")); } @Test public void testCreateArray_StringsMyListPets() { String[] strings = myCollectionToArrays.createArray(myListPets); String string = myCollectionToArrays.arrayToString(strings, ", "); assertEquals("dog, cat", string); } @Test public void testArrayOperationsToArrayWithTypeStringPets() { //Example of: toArray(T[] a) returns T[] String[] items = myListPets.toArray(new String[0]); assertEquals(2, items.length); String string = myCollectionToArrays.arrayToString(items, ", "); assertEquals("dog, cat", string); } @Test public void testArrayOperationsToArrayForStringPets() { //Example of: toArray() returns Object[] Object[] items = myListPets.toArray(); assertEquals(2, items.length); String string = myCollectionToArrays.arrayToString(items, ", "); assertEquals("dog, cat", string); } @Test public void testArrayOperationsToArrayWithTypeObjMyItem() { //Example of: toArray(T[] a) returns T[] MyItem[] items = myListItems.toArray(new MyItem[0]); assertEquals(2, items.length); StringBuilder sb = new StringBuilder(); String string = myCollectionToArrays.arrayToString(items, ", "); assertEquals("pen100, pen101", string); //Extra example using MyItem.getName() instead of MyItem.toString() for (MyItem obj : items) { sb.append(obj.getName() + ", "); } assertEquals("pen100, pen101, ", sb.toString()); } @Test public void testArrayOperationsToArrayForObjMyItem() { //Example of: toArray() returns Object[] Object[] items = myListItems.toArray(); assertEquals(2, items.length); String string = myCollectionToArrays.arrayToString(items, ", "); assertEquals("pen100, pen101", string); } @Test public void testIterateForLoopExample() { assertEquals("dog, cat", myCollectionTraverse.iterateForLoopExample(myListPets)); } @Test public void testIterateForLoopReverseExample() { assertEquals("cat, dog", myCollectionTraverse.iterateForLoopReverseExample(myListPets)); } @Test public void testIteratorWhileExample() { assertEquals("dog, cat", myCollectionTraverse.iteratorWhileExample(myListPets)); } @Test public void testIteratorWhileReverseExample() { assertEquals("cat, dog", myCollectionTraverse.iteratorWhileReverseExample(myListPets)); } @Test public void testForLoopExample() { assertEquals("dog, cat", myCollectionTraverse.forLoopExample(myListPets)); } @Test public void testArrayListSortNewList() { assertEquals("[dog, cat]", myListPets.toString()); ArrayList myPlayList = new ArrayList(myCollSort.sortTheCollection(myListPets)); assertEquals("[cat, dog]",myPlayList.toString()); } @Test public void testArrayListSortSameList() { assertEquals("[dog, cat]", myListPets.toString()); myListPets = new ArrayList(myCollSort.sortTheCollection(myListPets)); assertEquals("[cat, dog]", myListPets.toString()); } }