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
44
45
46
47
48 @Override
49 public LinkedList<T> getCollection() {
50 return (LinkedList<T>) super.getCollection();
51 }
52
53
54
55
56
57
58 protected LinkedList<T> getConfirmedLinkedList() {
59 return (LinkedList<T>) getConfirmed();
60 }
61
62
63
64
65 @Override
66 public Collection<T> makeConfirmedCollection() {
67 return new LinkedList<>();
68 }
69
70
71
72
73 @Override
74 public Collection<T> makeConfirmedFullCollection() {
75 return new LinkedList<>(Arrays.asList(getFullElements()));
76 }
77
78 @Override
79 public abstract LinkedList<T> makeObject();
80
81
82
83
84 @Test
85 @SuppressWarnings("unchecked")
86 public void testLinkedListAddFirst() {
87 if (!isAddSupported()) {
88 return;
89 }
90 final T o = (T) "hello";
91
92 resetEmpty();
93 getCollection().addFirst(o);
94 getConfirmedLinkedList().addFirst(o);
95 verify();
96
97 resetFull();
98 getCollection().addFirst(o);
99 getConfirmedLinkedList().addFirst(o);
100 verify();
101 }
102
103
104
105
106 @Test
107 @SuppressWarnings("unchecked")
108 public void testLinkedListAddLast() {
109 if (!isAddSupported()) {
110 return;
111 }
112 final T o = (T) "hello";
113
114 resetEmpty();
115 getCollection().addLast(o);
116 getConfirmedLinkedList().addLast(o);
117 verify();
118
119 resetFull();
120 getCollection().addLast(o);
121 getConfirmedLinkedList().addLast(o);
122 verify();
123 }
124
125
126
127
128 @Test
129 public void testLinkedListGetFirst() {
130 resetEmpty();
131 assertThrows(NoSuchElementException.class, () -> getCollection().getFirst(), "getFirst() should throw a NoSuchElementException for an empty list.");
132 verify();
133
134 resetFull();
135 final Object first = getCollection().getFirst();
136 final Object confirmedFirst = getConfirmedLinkedList().getFirst();
137 assertEquals(confirmedFirst, first, "Result returned by getFirst() was wrong.");
138 verify();
139 }
140
141
142
143
144 @Test
145 public void testLinkedListGetLast() {
146 resetEmpty();
147 assertThrows(NoSuchElementException.class, () -> getCollection().getLast(), "getLast() should throw a NoSuchElementException for an empty list.");
148 verify();
149
150 resetFull();
151 final Object last = getCollection().getLast();
152 final Object confirmedLast = getConfirmedLinkedList().getLast();
153 assertEquals(confirmedLast, last, "Result returned by getLast() was wrong.");
154 verify();
155 }
156
157
158
159
160 @Test
161 public void testLinkedListRemoveFirst() {
162 if (!isRemoveSupported()) {
163 return;
164 }
165
166 resetEmpty();
167 assertThrows(NoSuchElementException.class, () -> getCollection().removeFirst(),
168 "removeFirst() should throw a NoSuchElementException for an empty list.");
169 verify();
170
171 resetFull();
172 final Object first = getCollection().removeFirst();
173 final Object confirmedFirst = getConfirmedLinkedList().removeFirst();
174 assertEquals(confirmedFirst, first, "Result returned by removeFirst() was wrong.");
175 verify();
176 }
177
178
179
180
181 @Test
182 public void testLinkedListRemoveLast() {
183 if (!isRemoveSupported()) {
184 return;
185 }
186
187 resetEmpty();
188 assertThrows(NoSuchElementException.class, () -> getCollection().removeLast(), "removeLast() should throw a NoSuchElementException for an empty list.");
189 verify();
190
191 resetFull();
192 final Object last = getCollection().removeLast();
193 final Object confirmedLast = getConfirmedLinkedList().removeLast();
194 assertEquals(confirmedLast, last, "Result returned by removeLast() was wrong.");
195 verify();
196 }
197
198 }