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
35   * the {@link #makeObject()} method.
36   * <p>
37   * If your {@link LinkedList} fails one of these tests by design,
38   * you may still use this base set of cases.  Simply override the
39   * test case (method) your {@link List} fails.
40   */
41  public abstract class AbstractLinkedListTest<T> extends AbstractListTest<T> {
42  
43      public AbstractLinkedListTest(final String testName) {
44          super(testName);
45      }
46  
47      /**
48       *  Returns the {@link #collection} field cast to a {@link LinkedList}.
49       *
50       *  @return the collection field as a List
51       */
52      @Override
53      public LinkedList<T> getCollection() {
54          return (LinkedList<T>) super.getCollection();
55      }
56  
57      /**
58       *  Returns the {@link #confirmed} field cast to a {@link LinkedList}.
59       *
60       *  @return the confirmed field as a List
61       */
62      protected LinkedList<T> getConfirmedLinkedList() {
63          return (LinkedList<T>) getConfirmed();
64      }
65  
66      /**
67       *  Returns an empty {@link LinkedList}.
68       */
69      @Override
70      public Collection<T> makeConfirmedCollection() {
71          return new LinkedList<>();
72      }
73  
74      /**
75       *  Returns a full {@link LinkedList}.
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       *  Tests {@link LinkedList#addFirst(Object)}.
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      *  Tests {@link LinkedList#addLast(Object)}.
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      *  Tests {@link LinkedList#getFirst()}.
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      *  Tests {@link LinkedList#getLast()}.
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      *  Tests {@link LinkedList#removeFirst()}.
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      *  Tests {@link LinkedList#removeLast()}.
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 }