View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests base {@link java.util.LinkedList} methods and contracts.
33   * <p>
34   * To use, simply extend this class, and implement the {@link #makeObject()} method.
35   * </p>
36   * <p>
37   * If your {@link LinkedList} fails one of these tests by design, you may still use this base set of cases. Simply override the test case (method) your
38   * {@link List} fails.
39   * </p>
40   */
41  public abstract class AbstractLinkedListTest<T> extends AbstractListTest<T> {
42  
43      /**
44       * Returns the {@link #collection} field cast to a {@link LinkedList}.
45       *
46       * @return the collection field as a List
47       */
48      @Override
49      public LinkedList<T> getCollection() {
50          return (LinkedList<T>) super.getCollection();
51      }
52  
53      /**
54       * Returns the {@link #confirmed} field cast to a {@link LinkedList}.
55       *
56       * @return the confirmed field as a List
57       */
58      protected LinkedList<T> getConfirmedLinkedList() {
59          return (LinkedList<T>) getConfirmed();
60      }
61  
62      /**
63       * Returns an empty {@link LinkedList}.
64       */
65      @Override
66      public Collection<T> makeConfirmedCollection() {
67          return new LinkedList<>();
68      }
69  
70      /**
71       * Returns a full {@link LinkedList}.
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       * Tests {@link LinkedList#addFirst(Object)}.
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      * Tests {@link LinkedList#addLast(Object)}.
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      * Tests {@link LinkedList#getFirst()}.
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      * Tests {@link LinkedList#getLast()}.
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      * Tests {@link LinkedList#removeFirst()}.
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      * Tests {@link LinkedList#removeLast()}.
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 }