added more example

This commit is contained in:
Yasser Afifi
2016-12-02 23:31:10 +00:00
parent 6835625602
commit b18e801370
4 changed files with 51 additions and 27 deletions

View File

@@ -1,4 +1,8 @@
import java.util.Iterator;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
@@ -7,40 +11,45 @@ public class GenericsTest {
// testing the generic method with Integer
@Test
public void fromArrayToListIntTest() {
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
Integer[] intArray = { 1, 2, 3, 4, 5 };
List<Integer> list = Generics.fromArrayToList(intArray);
Iterator<Integer> iterator;
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
assertThat(list, hasItems(intArray));
}
// testing the generic method with String
@Test
public void fromArrayToListStringTest() {
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
List<String> list = Generics.fromArrayToList(stringArray);
Iterator<String> iterator;
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
assertThat(list, hasItems(stringArray));
}
// testing the generic method with Number as upper bound with Integer
// if we test fromArrayToListWithUpperBound with any type that doesn't
// extend Number it will fail to compile
@Test
public void fromArrayToListUpperboundIntTest() {
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
Integer[] intArray = { 1, 2, 3, 4, 5 };
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
Iterator<Integer> iterator;
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
assertThat(list, hasItems(intArray));
}
}
// testing paintAllBuildings method with a subtype of Building, the method
// will work with all subtypes of Building
@Test
public void givenSubTypeOfAwildCardBoundedGenericMethod() {
List<SubBuilding> subBuildingsList = new ArrayList<>();
subBuildingsList.add(new SubBuilding());
subBuildingsList.add(new SubBuilding());
boolean result = Generics.paintAllBuildings(subBuildingsList);
assertTrue(result);
}
}