From a67d99a09a774a67dc16ec6dfd24c5bff5a56eef Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Wed, 14 Feb 2018 17:10:18 +0700 Subject: [PATCH 1/4] Initial commit for List TDD --- .../com/baeldung/java/list/CustomList.java | 232 ++++++++++++++ .../java/list/CustomListUnitTest.java | 284 ++++++++++++++++++ 2 files changed, 516 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/java/list/CustomList.java create mode 100644 core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/java/list/CustomList.java b/core-java/src/main/java/com/baeldung/java/list/CustomList.java new file mode 100644 index 0000000000..bed9cefeee --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/list/CustomList.java @@ -0,0 +1,232 @@ +package com.baeldung.java.list; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +public class CustomList implements List { + private Object[] internal = {}; + + @Override + public boolean add(E element) { + // the first cycle + // internal = new Object[1]; + // internal[0] = element; + // return true; + + Object[] temp = new Object[internal.length + 1]; + System.arraycopy(internal, 0, temp, 0, internal.length); + temp[internal.length] = element; + internal = temp; + return true; + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + return (E) internal[index]; + } + + @Override + public void add(int index, E element) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(int index, Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public E remove(int index) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object object) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean retainAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public int size() { + return internal.length; + } + + @Override + public boolean isEmpty() { + return internal.length == 0; + } + + @Override + public boolean contains(Object object) { + // the first cycle + // if (object.equals(internal[0])) { + // return true; + // } + + for (Object element : internal) { + if (object.equals(element)) { + return true; + } + } + return false; + } + + @Override + public boolean containsAll(Collection collection) { + // the first cycle + // for (Object element : collection) { + // if (element.equals(internal[0])) { + // return true; + // } + // } + // return false; + + for (Object element : collection) + if (!contains(element)) { + return false; + } + return true; + } + + @SuppressWarnings("unchecked") + @Override + public E set(int index, E element) { + E oldElement = (E) internal[index]; + internal[index] = element; + return oldElement; + } + + @Override + public void clear() { + internal = new Object[0]; + } + + @Override + public int indexOf(Object object) { + // the first cycle + // if (object.equals(internal[0])) { + // return 0; + // } + // return -1; + + for (int i = 0; i < internal.length; i++) { + if (object.equals(internal[i])) { + return i; + } + } + return -1; + } + + @Override + public int lastIndexOf(Object object) { + // the first cycle + // if (object.equals(internal[0])) { + // return 0; + // } + // return -1; + + for (int i = internal.length - 1; i >= 0; i--) { + if (object.equals(internal[i])) { + return i; + } + } + return -1; + } + + @SuppressWarnings("unchecked") + @Override + public List subList(int fromIndex, int toIndex) { + // the first cycle + // return (List) Arrays.asList(internal); + + Object[] temp = new Object[toIndex - fromIndex]; + System.arraycopy(internal, fromIndex, temp, 0, temp.length); + return (List) Arrays.asList(temp); + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(internal, internal.length); + } + + @SuppressWarnings("unchecked") + @Override + public T[] toArray(T[] array) { + // the first cycle + // for (int i = 0; i < array.length; i++) { + // array[i] = (T) internal[i]; + // } + // return array; + + // the second cycle + // if (array.length < internal.length) { + // return (T[]) Arrays.copyOf(internal, internal.length, array.getClass()); + // } + // return (T[]) Arrays.copyOf(internal, internal.length, array.getClass()); + + if (array.length < internal.length) { + return (T[]) Arrays.copyOf(internal, internal.length, array.getClass()); + } + System.arraycopy(internal, 0, array, 0, internal.length); + if (array.length > internal.length) { + array[internal.length] = null; + } + return array; + } + + @Override + public Iterator iterator() { + return new CustomIterator(); + } + + @Override + public ListIterator listIterator() { + return null; + } + + @Override + public ListIterator listIterator(int index) { + return null; + } + + private class CustomIterator implements Iterator { + int index; + + @Override + public boolean hasNext() { + // the first cycle + // return true; + + return index != internal.length; + } + + @SuppressWarnings("unchecked") + @Override + public E next() { + // the first cycle + // return (E) CustomList.this.internal[0]; + + E element = (E) CustomList.this.internal[index]; + index++; + return element; + } + } +} diff --git a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java new file mode 100644 index 0000000000..bc4c95dc79 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java @@ -0,0 +1,284 @@ +package com.baeldung.java.list; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import org.junit.Test; + +public class CustomListUnitTest { + @Test + public void givenAddToTheEndAndGetImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + boolean succeeded = list.add("baeldung"); + Object element = list.get(0); + + assertTrue(succeeded); + assertEquals("baeldung", element); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenAddToSpecifiedIndexImpl_whenCycle1_thenPasses() { + new CustomList<>().add(0, null); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenAddAllToTheEndImpl_whenCycle1_thenPasses() { + Collection collection = new ArrayList<>(); + List list = new CustomList<>(); + list.addAll(collection); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenAddAllToSpecifiedIndexImpl_whenCycle1_thenPasses() { + Collection collection = new ArrayList<>(); + List list = new CustomList<>(); + list.addAll(0, collection); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenRemoveAtSpecifiedIndexImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + list.remove(0); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenRemoveSpecifiedElementImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + list.remove("baeldung"); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenRemoveAllImpl_whenCycle1_thenPasses() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.removeAll(collection); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenRetainAllImpl_whenCycle1_thenPasses() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.add("baeldung"); + list.retainAll(collection); + } + + @Test + public void givenSizeImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + + assertEquals(0, list.size()); + } + + @Test + public void givenIsEmptyImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + + assertTrue(list.isEmpty()); + } + + @Test + public void givenContainsImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertTrue(list.contains("baeldung")); + } + + @Test + public void givenContainsAllImpl_whenCycle1_thenPasses() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.add("baeldung"); + + assertTrue(list.containsAll(collection)); + } + + @Test + public void givenSetImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + Object element = list.set(0, null); + + assertEquals("baeldung", element); + assertNull(list.get(0)); + } + + @Test + public void givenClearImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + list.clear(); + + assertTrue(list.isEmpty()); + } + + @Test + public void givenIndexOfImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.indexOf("baeldung")); + } + + @Test + public void givenLastIndexOfImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.lastIndexOf("baeldung")); + } + + @Test + public void givenSubListImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + List subList = list.subList(0, 1); + + assertEquals("baeldung", subList.get(0)); + } + + @Test + public void givenToNewArrayImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + Object[] array = list.toArray(); + + assertEquals("baeldung", array[0]); + } + + @Test + public void givenToExistingArrayImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[1]; + String[] output = list.toArray(input); + + assertEquals("baeldung", output[0]); + } + + @Test + public void givenIteratorImpl_whenCycle1_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + Iterator iterator = list.iterator(); + + assertTrue(iterator.hasNext()); + assertEquals("baeldung", iterator.next()); + } + + @Test + public void givenAddToTheEndAndGetImpl_whenCycle2_thenPasses() { + List list = new CustomList<>(); + boolean succeeded1 = list.add("baeldung"); + boolean succeeded2 = list.add(".com"); + Object element1 = list.get(0); + Object element2 = list.get(1); + + assertTrue(succeeded1); + assertTrue(succeeded2); + assertEquals("baeldung", element1); + assertEquals(".com", element2); + } + + @Test + public void givenContainsImpl_whenCycle2_thenPasses() { + List list = new CustomList<>(); + assertFalse(list.contains("baeldung")); + + list.add("baeldung"); + assertTrue(list.contains("baeldung")); + } + + @Test + public void givenContainsAllImpl_whenCycle2_thenPasses() { + Collection collection1 = new ArrayList<>(); + collection1.add("baeldung"); + collection1.add(".com"); + Collection collection2 = new ArrayList<>(); + collection2.add("baeldung"); + + List list = new CustomList<>(); + list.add("baeldung"); + + assertFalse(list.containsAll(collection1)); + assertTrue(list.containsAll(collection2)); + } + + @Test + public void givenIndexOfImpl_whenCycle2_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add(".com"); + list.add(".com"); + + assertEquals(1, list.indexOf(".com")); + assertEquals(-1, list.indexOf("com")); + } + + @Test + public void givenLastIndexOfImpl_whenCycle2_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add("baeldung"); + list.add(".com"); + + assertEquals(1, list.lastIndexOf("baeldung")); + assertEquals(-1, list.indexOf("com")); + } + + @Test + public void givenSubListImpl_whenCycle2_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add("."); + list.add("com"); + List subList = list.subList(1, 2); + + assertEquals(1, subList.size()); + assertEquals(".", subList.get(0)); + } + + @Test + public void givenToExistingArrayImpl_whenCycle2_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = {}; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung" }, output); + } + + @Test + public void givenIteratorImpl_whenCycle2_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + Iterator iterator = list.iterator(); + + assertTrue(iterator.hasNext()); + assertEquals("baeldung", iterator.next()); + assertFalse(iterator.hasNext()); + } + + @Test + public void givenToExistingArrayImpl_whenCycle3_thenPasses() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[2]; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung", null }, output); + } +} From 08d53a2cbb9685611a3969dba15786723e222510 Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Sat, 17 Feb 2018 11:45:03 +0700 Subject: [PATCH 2/4] Change the names of test methods --- .../com/baeldung/java/list/CustomList.java | 51 ++-- .../java/list/CustomListUnitTest.java | 244 +++++++++--------- 2 files changed, 145 insertions(+), 150 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/java/list/CustomList.java b/core-java/src/main/java/com/baeldung/java/list/CustomList.java index bed9cefeee..bc1321b1a3 100644 --- a/core-java/src/main/java/com/baeldung/java/list/CustomList.java +++ b/core-java/src/main/java/com/baeldung/java/list/CustomList.java @@ -9,26 +9,6 @@ import java.util.ListIterator; public class CustomList implements List { private Object[] internal = {}; - @Override - public boolean add(E element) { - // the first cycle - // internal = new Object[1]; - // internal[0] = element; - // return true; - - Object[] temp = new Object[internal.length + 1]; - System.arraycopy(internal, 0, temp, 0, internal.length); - temp[internal.length] = element; - internal = temp; - return true; - } - - @SuppressWarnings("unchecked") - @Override - public E get(int index) { - return (E) internal[index]; - } - @Override public void add(int index, E element) { throw new UnsupportedOperationException(); @@ -75,11 +55,28 @@ public class CustomList implements List { } @Override - public boolean contains(Object object) { + public boolean add(E element) { // the first cycle - // if (object.equals(internal[0])) { - // return true; - // } + // internal = new Object[1]; + // internal[0] = element; + // return true; + + Object[] temp = new Object[internal.length + 1]; + System.arraycopy(internal, 0, temp, 0, internal.length); + temp[internal.length] = element; + internal = temp; + return true; + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + return (E) internal[index]; + } + + @Override + public boolean contains(Object object) { + // return false for (Object element : internal) { if (object.equals(element)) { @@ -171,9 +168,7 @@ public class CustomList implements List { @Override public T[] toArray(T[] array) { // the first cycle - // for (int i = 0; i < array.length; i++) { - // array[i] = (T) internal[i]; - // } + // array[0] = (T) internal[0]; // return array; // the second cycle @@ -185,6 +180,7 @@ public class CustomList implements List { if (array.length < internal.length) { return (T[]) Arrays.copyOf(internal, internal.length, array.getClass()); } + System.arraycopy(internal, 0, array, 0, internal.length); if (array.length > internal.length) { array[internal.length] = null; @@ -204,6 +200,7 @@ public class CustomList implements List { @Override public ListIterator listIterator(int index) { + // ignored for brevity return null; } diff --git a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java index bc4c95dc79..471d4f8a63 100644 --- a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java @@ -14,51 +14,41 @@ import java.util.List; import org.junit.Test; public class CustomListUnitTest { - @Test - public void givenAddToTheEndAndGetImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - boolean succeeded = list.add("baeldung"); - Object element = list.get(0); - - assertTrue(succeeded); - assertEquals("baeldung", element); - } - @Test(expected = UnsupportedOperationException.class) - public void givenAddToSpecifiedIndexImpl_whenCycle1_thenPasses() { + public void whenAddToSpecifiedIndex_thenExceptionIsThrown() { new CustomList<>().add(0, null); } @Test(expected = UnsupportedOperationException.class) - public void givenAddAllToTheEndImpl_whenCycle1_thenPasses() { + public void whenAddAllToTheEnd_thenExceptionIsThrown() { Collection collection = new ArrayList<>(); List list = new CustomList<>(); list.addAll(collection); } @Test(expected = UnsupportedOperationException.class) - public void givenAddAllToSpecifiedIndexImpl_whenCycle1_thenPasses() { + public void whenAddAllToSpecifiedIndex_thenExceptionIsThrown() { Collection collection = new ArrayList<>(); List list = new CustomList<>(); list.addAll(0, collection); } @Test(expected = UnsupportedOperationException.class) - public void givenRemoveAtSpecifiedIndexImpl_whenCycle1_thenPasses() { + public void whenRemoveAtSpecifiedIndex_thenExceptionIsThrown() { List list = new CustomList<>(); list.add("baeldung"); list.remove(0); } @Test(expected = UnsupportedOperationException.class) - public void givenRemoveSpecifiedElementImpl_whenCycle1_thenPasses() { + public void whenRemoveSpecifiedElement_thenExceptionIsThrown() { List list = new CustomList<>(); list.add("baeldung"); list.remove("baeldung"); } @Test(expected = UnsupportedOperationException.class) - public void givenRemoveAllImpl_whenCycle1_thenPasses() { + public void whenRemoveAll_thenExceptionIsThrown() { Collection collection = new ArrayList<>(); collection.add("baeldung"); List list = new CustomList<>(); @@ -66,7 +56,7 @@ public class CustomListUnitTest { } @Test(expected = UnsupportedOperationException.class) - public void givenRetainAllImpl_whenCycle1_thenPasses() { + public void whenRetainAll_thenExceptionIsThrown() { Collection collection = new ArrayList<>(); collection.add("baeldung"); List list = new CustomList<>(); @@ -75,112 +65,31 @@ public class CustomListUnitTest { } @Test - public void givenSizeImpl_whenCycle1_thenPasses() { + public void whenSize_thenNumberOfElementsIsReturned() { List list = new CustomList<>(); assertEquals(0, list.size()); } @Test - public void givenIsEmptyImpl_whenCycle1_thenPasses() { + public void whenIsEmpty_thenWhetherListIsEmptyIsReturned() { List list = new CustomList<>(); assertTrue(list.isEmpty()); } @Test - public void givenContainsImpl_whenCycle1_thenPasses() { + public void whenAddToTheEndAndGet_thenAddedElementIsReturned() { List list = new CustomList<>(); - list.add("baeldung"); - - assertTrue(list.contains("baeldung")); - } - - @Test - public void givenContainsAllImpl_whenCycle1_thenPasses() { - Collection collection = new ArrayList<>(); - collection.add("baeldung"); - List list = new CustomList<>(); - list.add("baeldung"); - - assertTrue(list.containsAll(collection)); - } - - @Test - public void givenSetImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - Object element = list.set(0, null); + boolean succeeded = list.add("baeldung"); + Object element = list.get(0); + assertTrue(succeeded); assertEquals("baeldung", element); - assertNull(list.get(0)); } @Test - public void givenClearImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - list.clear(); - - assertTrue(list.isEmpty()); - } - - @Test - public void givenIndexOfImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - - assertEquals(0, list.indexOf("baeldung")); - } - - @Test - public void givenLastIndexOfImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - - assertEquals(0, list.lastIndexOf("baeldung")); - } - - @Test - public void givenSubListImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - List subList = list.subList(0, 1); - - assertEquals("baeldung", subList.get(0)); - } - - @Test - public void givenToNewArrayImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - Object[] array = list.toArray(); - - assertEquals("baeldung", array[0]); - } - - @Test - public void givenToExistingArrayImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - String[] input = new String[1]; - String[] output = list.toArray(input); - - assertEquals("baeldung", output[0]); - } - - @Test - public void givenIteratorImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - Iterator iterator = list.iterator(); - - assertTrue(iterator.hasNext()); - assertEquals("baeldung", iterator.next()); - } - - @Test - public void givenAddToTheEndAndGetImpl_whenCycle2_thenPasses() { + public void whenAddToTheEndAndGet_thenAddedElemenetsAreReturned() { List list = new CustomList<>(); boolean succeeded1 = list.add("baeldung"); boolean succeeded2 = list.add(".com"); @@ -194,16 +103,32 @@ public class CustomListUnitTest { } @Test - public void givenContainsImpl_whenCycle2_thenPasses() { + public void whenContains_thenFalseIsReturned() { List list = new CustomList<>(); - assertFalse(list.contains("baeldung")); + assertFalse(list.contains("baeldung")); + } + + @Test + public void whenContains_thenTrueIsReturned() { + List list = new CustomList<>(); list.add("baeldung"); + assertTrue(list.contains("baeldung")); } @Test - public void givenContainsAllImpl_whenCycle2_thenPasses() { + public void whenContainsAll_thenTrueIsReturned() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.add("baeldung"); + + assertTrue(list.containsAll(collection)); + } + + @Test + public void whenContainsAll_thenEitherTrueOfFalseIsReturned() { Collection collection1 = new ArrayList<>(); collection1.add("baeldung"); collection1.add(".com"); @@ -218,7 +143,34 @@ public class CustomListUnitTest { } @Test - public void givenIndexOfImpl_whenCycle2_thenPasses() { + public void whenSet_thenOldElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + Object element = list.set(0, null); + + assertEquals("baeldung", element); + assertNull(list.get(0)); + } + + @Test + public void whenClear_thenAllElementsAreRemoved() { + List list = new CustomList<>(); + list.add("baeldung"); + list.clear(); + + assertTrue(list.isEmpty()); + } + + @Test + public void whenIndexOf_thenIndexZeroIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.indexOf("baeldung")); + } + + @Test + public void whenIndexOf_thenPositiveOrNegativeIndexIsReturned() { List list = new CustomList<>(); list.add("baeldung"); list.add(".com"); @@ -229,7 +181,15 @@ public class CustomListUnitTest { } @Test - public void givenLastIndexOfImpl_whenCycle2_thenPasses() { + public void whenLastIndexOf_thenIndexZeroIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.lastIndexOf("baeldung")); + } + + @Test + public void whenLastIndexOf_thenPositiveOrNegativeIndexIsReturned() { List list = new CustomList<>(); list.add("baeldung"); list.add("baeldung"); @@ -240,7 +200,16 @@ public class CustomListUnitTest { } @Test - public void givenSubListImpl_whenCycle2_thenPasses() { + public void whenSubList_thenListContainingFirstElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + List subList = list.subList(0, 1); + + assertEquals("baeldung", subList.get(0)); + } + + @Test + public void whenSubList_thenListContainingSecondElementIsReturned() { List list = new CustomList<>(); list.add("baeldung"); list.add("."); @@ -252,7 +221,26 @@ public class CustomListUnitTest { } @Test - public void givenToExistingArrayImpl_whenCycle2_thenPasses() { + public void whenToNewArray_thenArrayIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + Object[] array = list.toArray(); + + assertEquals("baeldung", array[0]); + } + + @Test + public void whenToArray_thenOldArrayIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[1]; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung" }, input); + } + + @Test + public void whenToArray_thenNewArrayIsReturned() { List list = new CustomList<>(); list.add("baeldung"); String[] input = {}; @@ -262,7 +250,27 @@ public class CustomListUnitTest { } @Test - public void givenIteratorImpl_whenCycle2_thenPasses() { + public void whenToArray_thenNewArrayWithTrailingNullIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[2]; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung", null }, output); + } + + @Test + public void whenIterator_thenOneCheckIsCorrect() { + List list = new CustomList<>(); + list.add("baeldung"); + Iterator iterator = list.iterator(); + + assertTrue(iterator.hasNext()); + assertEquals("baeldung", iterator.next()); + } + + @Test + public void whenIterator_thenTwoChecksAreCorrect() { List list = new CustomList<>(); list.add("baeldung"); Iterator iterator = list.iterator(); @@ -271,14 +279,4 @@ public class CustomListUnitTest { assertEquals("baeldung", iterator.next()); assertFalse(iterator.hasNext()); } - - @Test - public void givenToExistingArrayImpl_whenCycle3_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - String[] input = new String[2]; - String[] output = list.toArray(input); - - assertArrayEquals(new String[] { "baeldung", null }, output); - } } From c1787cc07a3943885c3064fb08a7782c251b05d5 Mon Sep 17 00:00:00 2001 From: Nguyen Nam Thai Date: Sun, 18 Feb 2018 11:05:00 +0700 Subject: [PATCH 3/4] Change the test method names --- .../java/list/CustomListUnitTest.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java index 471d4f8a63..898ea02f16 100644 --- a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java @@ -65,21 +65,21 @@ public class CustomListUnitTest { } @Test - public void whenSize_thenNumberOfElementsIsReturned() { + public void givenEmptyList_whenSize_thenZeroIsReturned() { List list = new CustomList<>(); assertEquals(0, list.size()); } @Test - public void whenIsEmpty_thenWhetherListIsEmptyIsReturned() { + public void givenEmptyList_whenIsEmpty_thenTrueIsReturned() { List list = new CustomList<>(); assertTrue(list.isEmpty()); } @Test - public void whenAddToTheEndAndGet_thenAddedElementIsReturned() { + public void givenEmptyList_whenElementIsAdded_thenGetReturnsThatElement() { List list = new CustomList<>(); boolean succeeded = list.add("baeldung"); Object element = list.get(0); @@ -89,7 +89,7 @@ public class CustomListUnitTest { } @Test - public void whenAddToTheEndAndGet_thenAddedElemenetsAreReturned() { + public void givenListWithAnElement_whenAnotherIsAdded_thenGetReturnsBoth() { List list = new CustomList<>(); boolean succeeded1 = list.add("baeldung"); boolean succeeded2 = list.add(".com"); @@ -103,14 +103,14 @@ public class CustomListUnitTest { } @Test - public void whenContains_thenFalseIsReturned() { + public void givenEmptyList_whenContains_thenFalseIsReturned() { List list = new CustomList<>(); - assertFalse(list.contains("baeldung")); + assertFalse(list.contains(null)); } @Test - public void whenContains_thenTrueIsReturned() { + public void givenListWithAnElement_whenContains_thenTrueIsReturned() { List list = new CustomList<>(); list.add("baeldung"); @@ -118,7 +118,7 @@ public class CustomListUnitTest { } @Test - public void whenContainsAll_thenTrueIsReturned() { + public void givenListWithAnElement_whenContainsAll_thenTrueIsReturned() { Collection collection = new ArrayList<>(); collection.add("baeldung"); List list = new CustomList<>(); @@ -128,7 +128,7 @@ public class CustomListUnitTest { } @Test - public void whenContainsAll_thenEitherTrueOfFalseIsReturned() { + public void givenList_whenContainsAll_thenEitherTrueOrFalseIsReturned() { Collection collection1 = new ArrayList<>(); collection1.add("baeldung"); collection1.add(".com"); @@ -143,7 +143,7 @@ public class CustomListUnitTest { } @Test - public void whenSet_thenOldElementIsReturned() { + public void givenList_whenSet_thenOldElementIsReturned() { List list = new CustomList<>(); list.add("baeldung"); Object element = list.set(0, null); @@ -153,7 +153,7 @@ public class CustomListUnitTest { } @Test - public void whenClear_thenAllElementsAreRemoved() { + public void givenList_whenClear_thenAllElementsAreRemoved() { List list = new CustomList<>(); list.add("baeldung"); list.clear(); @@ -162,7 +162,7 @@ public class CustomListUnitTest { } @Test - public void whenIndexOf_thenIndexZeroIsReturned() { + public void givenList_whenIndexOf_thenIndexZeroIsReturned() { List list = new CustomList<>(); list.add("baeldung"); @@ -170,7 +170,7 @@ public class CustomListUnitTest { } @Test - public void whenIndexOf_thenPositiveOrNegativeIndexIsReturned() { + public void givenList_whenIndexOf_thenPositiveIndexOrMinusOneIsReturned() { List list = new CustomList<>(); list.add("baeldung"); list.add(".com"); @@ -189,7 +189,7 @@ public class CustomListUnitTest { } @Test - public void whenLastIndexOf_thenPositiveOrNegativeIndexIsReturned() { + public void whenLastIndexOf_thenPositiveIndexOrMinusOneIsReturned() { List list = new CustomList<>(); list.add("baeldung"); list.add("baeldung"); @@ -200,7 +200,7 @@ public class CustomListUnitTest { } @Test - public void whenSubList_thenListContainingFirstElementIsReturned() { + public void whenSubListZeroToOne_thenListContainingFirstElementIsReturned() { List list = new CustomList<>(); list.add("baeldung"); List subList = list.subList(0, 1); @@ -209,7 +209,7 @@ public class CustomListUnitTest { } @Test - public void whenSubList_thenListContainingSecondElementIsReturned() { + public void whenSubListOneToTwo_thenListContainingSecondElementIsReturned() { List list = new CustomList<>(); list.add("baeldung"); list.add("."); @@ -221,16 +221,17 @@ public class CustomListUnitTest { } @Test - public void whenToNewArray_thenArrayIsReturned() { + public void givenListWithElements_whenToArray_thenArrayContainsThose() { List list = new CustomList<>(); list.add("baeldung"); + list.add(".com"); Object[] array = list.toArray(); - assertEquals("baeldung", array[0]); + assertArrayEquals(new Object[] { "baeldung", ".com" }, array); } @Test - public void whenToArray_thenOldArrayIsReturned() { + public void givenListWithAnElement_whenToArray_thenInputArrayIsReturned() { List list = new CustomList<>(); list.add("baeldung"); String[] input = new String[1]; @@ -240,7 +241,7 @@ public class CustomListUnitTest { } @Test - public void whenToArray_thenNewArrayIsReturned() { + public void whenToArrayIsCalledWithEmptyInputArray_thenNewArrayIsReturned() { List list = new CustomList<>(); list.add("baeldung"); String[] input = {}; @@ -250,7 +251,7 @@ public class CustomListUnitTest { } @Test - public void whenToArray_thenNewArrayWithTrailingNullIsReturned() { + public void whenToArrayIsCalledWithLargerInput_thenOutputHasTrailingNull() { List list = new CustomList<>(); list.add("baeldung"); String[] input = new String[2]; @@ -260,7 +261,7 @@ public class CustomListUnitTest { } @Test - public void whenIterator_thenOneCheckIsCorrect() { + public void givenListWithOneElement_whenIterator_thenThisElementIsNext() { List list = new CustomList<>(); list.add("baeldung"); Iterator iterator = list.iterator(); @@ -270,13 +271,12 @@ public class CustomListUnitTest { } @Test - public void whenIterator_thenTwoChecksAreCorrect() { + public void whenIteratorNextIsCalledTwice_thenTheSecondReturnsFalse() { List list = new CustomList<>(); list.add("baeldung"); Iterator iterator = list.iterator(); assertTrue(iterator.hasNext()); - assertEquals("baeldung", iterator.next()); assertFalse(iterator.hasNext()); } } From 7debccf695d29cbdae221bd8b8795cc9073588cf Mon Sep 17 00:00:00 2001 From: Nguyen Nam Thai Date: Sun, 18 Feb 2018 11:08:00 +0700 Subject: [PATCH 4/4] Fix a test --- .../test/java/com/baeldung/java/list/CustomListUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java index 898ea02f16..3ee3195e80 100644 --- a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java @@ -276,7 +276,7 @@ public class CustomListUnitTest { list.add("baeldung"); Iterator iterator = list.iterator(); - assertTrue(iterator.hasNext()); + iterator.next(); assertFalse(iterator.hasNext()); } }