Skip to content

Commit fa6b782

Browse files
authored
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.
1 parent 3b770c4 commit fa6b782

3 files changed

Lines changed: 166 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Core Java Collections List (Part 5)
2+
3+
This module contains articles about the Java List collection
4+
5+
### Relevant Articles:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-collections-list-5</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>core-java-collections-list-5</name>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>com.baeldung.core-java-modules</groupId>
13+
<artifactId>core-java-modules</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>commons-lang</groupId>
20+
<artifactId>commons-lang</artifactId>
21+
<version>${commons-lang.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-lang3</artifactId>
26+
<version>${commons-lang3.version}</version>
27+
</dependency>
28+
</dependencies>
29+
30+
<properties>
31+
<commons-lang.version>2.2</commons-lang.version>
32+
<commons-lang3.version>3.12.0</commons-lang3.version>
33+
</properties>
34+
35+
</project>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.baeldung.java.list;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.util.ArrayList;
8+
import java.util.Comparator;
9+
import java.util.Iterator;
10+
import java.util.List;
11+
12+
import org.junit.Test;
13+
14+
public class ListUnitTest {
15+
16+
@Test
17+
public void givenAFruitList_whenAddNewFruit_thenFruitIsAdded(){
18+
List<String> fruits = new ArrayList<>();
19+
assertEquals("Unexpected number of fruits in the list, should have been 0", 0, fruits.size());
20+
21+
fruits.add("Apple");
22+
assertEquals("Unexpected number of fruits in the list, should have been 1", 1, fruits.size());
23+
}
24+
25+
@Test
26+
public void givenAFruitList_whenContainsFruit_thenFruitIsInTheList(){
27+
List<String> fruits = new ArrayList<>();
28+
29+
fruits.add("Apple");
30+
assertTrue("Apple should be in the fruit list", fruits.contains("Apple"));
31+
assertFalse("Banana should not be in the fruit list", fruits.contains("Banana"));
32+
}
33+
34+
@Test
35+
public void givenAnEmptyFruitList_whenEmptyCheck_thenListIsEmpty(){
36+
List<String> fruits = new ArrayList<>();
37+
assertTrue("Fruit list should be empty", fruits.isEmpty());
38+
39+
fruits.add("Apple");
40+
assertFalse("Fruit list should not be empty", fruits.isEmpty());
41+
}
42+
43+
@Test
44+
public void givenAFruitList_whenIterateOverIt_thenFruitsAreInOrder(){
45+
List<String> fruits = new ArrayList<>();
46+
47+
fruits.add("Apple"); // fruit at index 0
48+
fruits.add("Orange");// fruit at index 1
49+
fruits.add("Banana");// fruit at index 2
50+
int index = 0;
51+
for (Iterator<String> it = fruits.listIterator(); it.hasNext(); ) {
52+
String fruit = it.next();
53+
assertEquals("Fruits should be in order", fruits.get(index++), fruit);
54+
}
55+
}
56+
57+
@Test
58+
public void givenAFruitList_whenRemoveFruit_thenFruitIsRemoved(){
59+
List<String> fruits = new ArrayList<>();
60+
61+
fruits.add("Apple");
62+
fruits.add("Orange");
63+
assertEquals("Unexpected number of fruits in the list, should have been 2", 2, fruits.size());
64+
65+
fruits.remove("Apple");
66+
assertEquals("Unexpected number of fruits in the list, should have been 1", 1, fruits.size());
67+
}
68+
69+
@Test
70+
public void givenAFruitList_whenSetFruit_thenFruitIsUpdated(){
71+
List<String> fruits = new ArrayList<>();
72+
73+
fruits.add("Apple");
74+
fruits.add("Orange");
75+
76+
fruits.set(0, "Banana");
77+
assertEquals("Fruit at index 0 should be Banana", "Banana", fruits.get(0));
78+
}
79+
80+
@Test
81+
public void givenAFruitList_whenSort_thenFruitsAreSorted(){
82+
List<String> fruits = new ArrayList<>();
83+
84+
fruits.add("Apple");
85+
fruits.add("Orange");
86+
fruits.add("Banana");
87+
88+
fruits.sort(Comparator.naturalOrder());
89+
90+
assertEquals("Fruit at index 0 should be Apple", "Apple", fruits.get(0));
91+
assertEquals("Fruit at index 1 should be Banana", "Banana", fruits.get(1));
92+
assertEquals("Fruit at index 2 should be Orange", "Orange", fruits.get(2));
93+
}
94+
95+
@Test
96+
public void givenAFruitList_whenSublist_thenWeGetASublist(){
97+
List<String> fruits = new ArrayList<>();
98+
99+
fruits.add("Apple");
100+
fruits.add("Orange");
101+
fruits.add("Banana");
102+
103+
List<String> fruitsSublist = fruits.subList(0, 2);
104+
assertEquals("Unexpected number of fruits in the sublist, should have been 2", 2, fruitsSublist.size());
105+
106+
assertEquals("Fruit at index 0 should be Apple", "Apple", fruitsSublist.get(0));
107+
assertEquals("Fruit at index 1 should be Orange", "Orange", fruitsSublist.get(1));
108+
}
109+
110+
@Test
111+
public void givenAFruitList_whenToArray_thenWeGetAnArray(){
112+
List<String> fruits = new ArrayList<>();
113+
114+
fruits.add("Apple");
115+
fruits.add("Orange");
116+
fruits.add("Banana");
117+
118+
String[] fruitsArray = fruits.toArray(new String[0]);
119+
assertEquals("Unexpected number of fruits in the array, should have been 3", 3, fruitsArray.length);
120+
121+
assertEquals("Fruit at index 0 should be Apple", "Apple", fruitsArray[0]);
122+
assertEquals("Fruit at index 1 should be Orange", "Orange", fruitsArray[1]);
123+
assertEquals("Fruit at index 2 should be Banana", "Banana", fruitsArray[2]);
124+
}
125+
126+
}

0 commit comments

Comments
 (0)