From fa6b78233c56d6f6f1d1d33024f5bf0ed1cea958 Mon Sep 17 00:00:00 2001 From: Vali Tuguran Date: Sun, 22 Jan 2023 05:39:34 +0100 Subject: [PATCH] BAEL-6040 Add Java List tests. (#13255) * BAEL-6040 Add list interface example. * BAEL-6040 Updated list iterator. * BAEL-6040 Removed logger import. * BAEL-6040 Added List interface tests. * BAEL-6040 Update tests success names. * BAEL-6040 Small names refactoring. * BAEL-6040 Add new core-java-collections-5 module. * BAEL-6040 Create module core-java-collections-list-5. * BAEL-6040 Create module core-java-collections-list-5. --- .../core-java-collections-list-5/README.md | 5 + .../core-java-collections-list-5/pom.xml | 35 +++++ .../com/baeldung/java/list/ListUnitTest.java | 126 ++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 core-java-modules/core-java-collections-list-5/README.md create mode 100644 core-java-modules/core-java-collections-list-5/pom.xml create mode 100644 core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/java/list/ListUnitTest.java diff --git a/core-java-modules/core-java-collections-list-5/README.md b/core-java-modules/core-java-collections-list-5/README.md new file mode 100644 index 0000000000..4b7fbf8669 --- /dev/null +++ b/core-java-modules/core-java-collections-list-5/README.md @@ -0,0 +1,5 @@ +## Core Java Collections List (Part 5) + +This module contains articles about the Java List collection + +### Relevant Articles: \ No newline at end of file diff --git a/core-java-modules/core-java-collections-list-5/pom.xml b/core-java-modules/core-java-collections-list-5/pom.xml new file mode 100644 index 0000000000..0807f7612c --- /dev/null +++ b/core-java-modules/core-java-collections-list-5/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + core-java-collections-list-5 + 0.1.0-SNAPSHOT + core-java-collections-list-5 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + commons-lang + commons-lang + ${commons-lang.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + 2.2 + 3.12.0 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/java/list/ListUnitTest.java b/core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/java/list/ListUnitTest.java new file mode 100644 index 0000000000..2222bc5a6f --- /dev/null +++ b/core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/java/list/ListUnitTest.java @@ -0,0 +1,126 @@ +package com.baeldung.java.list; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; + +import org.junit.Test; + +public class ListUnitTest { + + @Test + public void givenAFruitList_whenAddNewFruit_thenFruitIsAdded(){ + List fruits = new ArrayList<>(); + assertEquals("Unexpected number of fruits in the list, should have been 0", 0, fruits.size()); + + fruits.add("Apple"); + assertEquals("Unexpected number of fruits in the list, should have been 1", 1, fruits.size()); + } + + @Test + public void givenAFruitList_whenContainsFruit_thenFruitIsInTheList(){ + List fruits = new ArrayList<>(); + + fruits.add("Apple"); + assertTrue("Apple should be in the fruit list", fruits.contains("Apple")); + assertFalse("Banana should not be in the fruit list", fruits.contains("Banana")); + } + + @Test + public void givenAnEmptyFruitList_whenEmptyCheck_thenListIsEmpty(){ + List fruits = new ArrayList<>(); + assertTrue("Fruit list should be empty", fruits.isEmpty()); + + fruits.add("Apple"); + assertFalse("Fruit list should not be empty", fruits.isEmpty()); + } + + @Test + public void givenAFruitList_whenIterateOverIt_thenFruitsAreInOrder(){ + List fruits = new ArrayList<>(); + + fruits.add("Apple"); // fruit at index 0 + fruits.add("Orange");// fruit at index 1 + fruits.add("Banana");// fruit at index 2 + int index = 0; + for (Iterator it = fruits.listIterator(); it.hasNext(); ) { + String fruit = it.next(); + assertEquals("Fruits should be in order", fruits.get(index++), fruit); + } + } + + @Test + public void givenAFruitList_whenRemoveFruit_thenFruitIsRemoved(){ + List fruits = new ArrayList<>(); + + fruits.add("Apple"); + fruits.add("Orange"); + assertEquals("Unexpected number of fruits in the list, should have been 2", 2, fruits.size()); + + fruits.remove("Apple"); + assertEquals("Unexpected number of fruits in the list, should have been 1", 1, fruits.size()); + } + + @Test + public void givenAFruitList_whenSetFruit_thenFruitIsUpdated(){ + List fruits = new ArrayList<>(); + + fruits.add("Apple"); + fruits.add("Orange"); + + fruits.set(0, "Banana"); + assertEquals("Fruit at index 0 should be Banana", "Banana", fruits.get(0)); + } + + @Test + public void givenAFruitList_whenSort_thenFruitsAreSorted(){ + List fruits = new ArrayList<>(); + + fruits.add("Apple"); + fruits.add("Orange"); + fruits.add("Banana"); + + fruits.sort(Comparator.naturalOrder()); + + assertEquals("Fruit at index 0 should be Apple", "Apple", fruits.get(0)); + assertEquals("Fruit at index 1 should be Banana", "Banana", fruits.get(1)); + assertEquals("Fruit at index 2 should be Orange", "Orange", fruits.get(2)); + } + + @Test + public void givenAFruitList_whenSublist_thenWeGetASublist(){ + List fruits = new ArrayList<>(); + + fruits.add("Apple"); + fruits.add("Orange"); + fruits.add("Banana"); + + List fruitsSublist = fruits.subList(0, 2); + assertEquals("Unexpected number of fruits in the sublist, should have been 2", 2, fruitsSublist.size()); + + assertEquals("Fruit at index 0 should be Apple", "Apple", fruitsSublist.get(0)); + assertEquals("Fruit at index 1 should be Orange", "Orange", fruitsSublist.get(1)); + } + + @Test + public void givenAFruitList_whenToArray_thenWeGetAnArray(){ + List fruits = new ArrayList<>(); + + fruits.add("Apple"); + fruits.add("Orange"); + fruits.add("Banana"); + + String[] fruitsArray = fruits.toArray(new String[0]); + assertEquals("Unexpected number of fruits in the array, should have been 3", 3, fruitsArray.length); + + assertEquals("Fruit at index 0 should be Apple", "Apple", fruitsArray[0]); + assertEquals("Fruit at index 1 should be Orange", "Orange", fruitsArray[1]); + assertEquals("Fruit at index 2 should be Banana", "Banana", fruitsArray[2]); + } + +}