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.
This commit is contained in:
parent
3b770c4dee
commit
fa6b78233c
|
@ -0,0 +1,5 @@
|
|||
## Core Java Collections List (Part 5)
|
||||
|
||||
This module contains articles about the Java List collection
|
||||
|
||||
### Relevant Articles:
|
|
@ -0,0 +1,35 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-list-5</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-list-5</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>${commons-lang.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-lang.version>2.2</commons-lang.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> fruits = new ArrayList<>();
|
||||
|
||||
fruits.add("Apple");
|
||||
fruits.add("Orange");
|
||||
fruits.add("Banana");
|
||||
|
||||
List<String> 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<String> 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]);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue