1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.NoSuchElementException;
27
28 import org.apache.commons.collections4.list.AbstractListTest;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34
35
36
37
38
39
40
41 public abstract class AbstractLinkedListTest<T> extends AbstractListTest<T> {
42
43 public AbstractLinkedListTest(final String testName) {
44 super(testName);
45 }
46
47
48
49
50
51
52 @Override
53 public LinkedList<T> getCollection() {
54 return (LinkedList<T>) super.getCollection();
55 }
56
57
58
59
60
61
62 protected LinkedList<T> getConfirmedLinkedList() {
63 return (LinkedList<T>) getConfirmed();
64 }
65
66
67
68
69 @Override
70 public Collection<T> makeConfirmedCollection() {
71 return new LinkedList<>();
72 }
73
74
75
76
77 @Override
78 public Collection<T> makeConfirmedFullCollection() {
79 return new LinkedList<>(Arrays.asList(getFullElements()));
80 }
81
82 @Override
83 public abstract LinkedList<T> makeObject();
84
85
86
87
88 @Test
89 @SuppressWarnings("unchecked")
90 public void testLinkedListAddFirst() {
91 if (!isAddSupported()) {
92 return;
93 }
94 final T o = (T) "hello";
95
96 resetEmpty();
97 getCollection().addFirst(o);
98 getConfirmedLinkedList().addFirst(o);
99 verify();
100
101 resetFull();
102 getCollection().addFirst(o);
103 getConfirmedLinkedList().addFirst(o);
104 verify();
105 }
106
107
108
109
110 @Test
111 @SuppressWarnings("unchecked")
112 public void testLinkedListAddLast() {
113 if (!isAddSupported()) {
114 return;
115 }
116 final T o = (T) "hello";
117
118 resetEmpty();
119 getCollection().addLast(o);
120 getConfirmedLinkedList().addLast(o);
121 verify();
122
123 resetFull();
124 getCollection().addLast(o);
125 getConfirmedLinkedList().addLast(o);
126 verify();
127 }
128
129
130
131
132 @Test
133 public void testLinkedListGetFirst() {
134 resetEmpty();
135 assertThrows(NoSuchElementException.class, () -> getCollection().getFirst(),
136 "getFirst() should throw a NoSuchElementException for an empty list.");
137 verify();
138
139 resetFull();
140 final Object first = getCollection().getFirst();
141 final Object confirmedFirst = getConfirmedLinkedList().getFirst();
142 assertEquals(confirmedFirst, first,
143 "Result returned by getFirst() was wrong.");
144 verify();
145 }
146
147
148
149
150 @Test
151 public void testLinkedListGetLast() {
152 resetEmpty();
153 assertThrows(NoSuchElementException.class, () -> getCollection().getLast(),
154 "getLast() should throw a NoSuchElementException for an empty list.");
155 verify();
156
157 resetFull();
158 final Object last = getCollection().getLast();
159 final Object confirmedLast = getConfirmedLinkedList().getLast();
160 assertEquals(confirmedLast, last,
161 "Result returned by getLast() was wrong.");
162 verify();
163 }
164
165
166
167
168 @Test
169 public void testLinkedListRemoveFirst() {
170 if (!isRemoveSupported()) {
171 return;
172 }
173
174 resetEmpty();
175 assertThrows(NoSuchElementException.class, () -> getCollection().removeFirst(),
176 "removeFirst() should throw a NoSuchElementException for an empty list.");
177 verify();
178
179 resetFull();
180 final Object first = getCollection().removeFirst();
181 final Object confirmedFirst = getConfirmedLinkedList().removeFirst();
182 assertEquals(confirmedFirst, first,
183 "Result returned by removeFirst() was wrong.");
184 verify();
185 }
186
187
188
189
190 @Test
191 public void testLinkedListRemoveLast() {
192 if (!isRemoveSupported()) {
193 return;
194 }
195
196 resetEmpty();
197 assertThrows(NoSuchElementException.class, () -> getCollection().removeLast(),
198 "removeLast() should throw a NoSuchElementException for an empty list.");
199 verify();
200
201 resetFull();
202 final Object last = getCollection().removeLast();
203 final Object confirmedLast = getConfirmedLinkedList().removeLast();
204 assertEquals(confirmedLast, last,
205 "Result returned by removeLast() was wrong.");
206 verify();
207 }
208
209 }