KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestLinkedList


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import org.apache.commons.collections.list.AbstractTestList;
25
26 /**
27  * Tests base {@link java.util.LinkedList} methods and contracts.
28  * <p>
29  * To use, simply extend this class, and implement
30  * the {@link #makeLinkedList} method.
31  * <p>
32  * If your {@link LinkedList} fails one of these tests by design,
33  * you may still use this base set of cases. Simply override the
34  * test case (method) your {@link List} fails.
35  *
36  * @version $Revision: 1.9 $ $Date: 2004/02/18 01:20:35 $
37  *
38  * @author Rich Dougherty
39  */

40 public abstract class TestLinkedList extends AbstractTestList {
41
42     public TestLinkedList(String JavaDoc testName) {
43         super(testName);
44     }
45
46     public List JavaDoc makeEmptyList() {
47         return makeEmptyLinkedList();
48     }
49
50     public List JavaDoc makeFullList() {
51         return makeFullLinkedList();
52     }
53
54     /**
55      * Return a new, empty {@link LinkedList} to be used for testing.
56      *
57      * @return an empty list for testing.
58      */

59     protected abstract LinkedList JavaDoc makeEmptyLinkedList();
60
61     /**
62      * Return a new, full {@link List} to be used for testing.
63      *
64      * @return a full list for testing
65      */

66     protected LinkedList JavaDoc makeFullLinkedList() {
67         // only works if list supports optional "addAll(Collection)"
68
LinkedList JavaDoc list = makeEmptyLinkedList();
69         list.addAll(Arrays.asList(getFullElements()));
70         return list;
71     }
72
73     /**
74      * Returns the {@link #collection} field cast to a {@link LinkedList}.
75      *
76      * @return the collection field as a List
77      */

78     protected LinkedList JavaDoc getLinkedList() {
79         return (LinkedList JavaDoc)collection;
80     }
81
82     /**
83      * Returns the {@link #confirmed} field cast to a {@link LinkedList}.
84      *
85      * @return the confirmed field as a List
86      */

87     protected LinkedList JavaDoc getConfirmedLinkedList() {
88         return (LinkedList JavaDoc)confirmed;
89     }
90
91     /**
92      * Tests {@link LinkedList#addFirst(Object)}.
93      */

94     public void testLinkedListAddFirst() {
95         if (!isAddSupported()) return;
96         Object JavaDoc o = "hello";
97
98         resetEmpty();
99         getLinkedList().addFirst(o);
100         getConfirmedLinkedList().addFirst(o);
101         verify();
102
103         resetFull();
104         getLinkedList().addFirst(o);
105         getConfirmedLinkedList().addFirst(o);
106         verify();
107     }
108
109     /**
110      * Tests {@link LinkedList#addLast(Object)}.
111      */

112     public void testLinkedListAddLast() {
113         if (!isAddSupported()) return;
114         Object JavaDoc o = "hello";
115
116         resetEmpty();
117         getLinkedList().addLast(o);
118         getConfirmedLinkedList().addLast(o);
119         verify();
120
121         resetFull();
122         getLinkedList().addLast(o);
123         getConfirmedLinkedList().addLast(o);
124         verify();
125     }
126
127     /**
128      * Tests {@link LinkedList#getFirst(Object)}.
129      */

130     public void testLinkedListGetFirst() {
131         resetEmpty();
132         try {
133             getLinkedList().getFirst();
134             fail("getFirst() should throw a NoSuchElementException for an " +
135                     "empty list.");
136         } catch (NoSuchElementException JavaDoc e) {
137             // This is correct
138
}
139         verify();
140
141         resetFull();
142         Object JavaDoc first = getLinkedList().getFirst();
143         Object JavaDoc confirmedFirst = getConfirmedLinkedList().getFirst();
144         assertEquals("Result returned by getFirst() was wrong.",
145                 confirmedFirst, first);
146         verify();
147     }
148
149     /**
150      * Tests {@link LinkedList#getLast(Object)}.
151      */

152     public void testLinkedListGetLast() {
153         resetEmpty();
154         try {
155             getLinkedList().getLast();
156             fail("getLast() should throw a NoSuchElementException for an " +
157                     "empty list.");
158         } catch (NoSuchElementException JavaDoc e) {
159             // This is correct
160
}
161         verify();
162         
163         resetFull();
164         Object JavaDoc last = getLinkedList().getLast();
165         Object JavaDoc confirmedLast = getConfirmedLinkedList().getLast();
166         assertEquals("Result returned by getLast() was wrong.",
167                 confirmedLast, last);
168         verify();
169     }
170
171     /**
172      * Tests {@link LinkedList#removeFirst(Object)}.
173      */

174     public void testLinkedListRemoveFirst() {
175         if (!isRemoveSupported()) return;
176
177         resetEmpty();
178         try {
179             getLinkedList().removeFirst();
180             fail("removeFirst() should throw a NoSuchElementException for " +
181                     "an empty list.");
182         } catch (NoSuchElementException JavaDoc e) {
183             // This is correct
184
}
185         verify();
186         
187         resetFull();
188         Object JavaDoc first = getLinkedList().removeFirst();
189         Object JavaDoc confirmedFirst = getConfirmedLinkedList().removeFirst();
190         assertEquals("Result returned by removeFirst() was wrong.",
191                 confirmedFirst, first);
192         verify();
193     }
194
195     /**
196      * Tests {@link LinkedList#removeLast(Object)}.
197      */

198     public void testLinkedListRemoveLast() {
199         if (!isRemoveSupported()) return;
200
201         resetEmpty();
202         try {
203             getLinkedList().removeLast();
204             fail("removeLast() should throw a NoSuchElementException for " +
205                     "an empty list.");
206         } catch (NoSuchElementException JavaDoc e) {
207             // This is correct
208
}
209         verify();
210
211         resetFull();
212         Object JavaDoc last = getLinkedList().removeLast();
213         Object JavaDoc confirmedLast = getConfirmedLinkedList().removeLast();
214         assertEquals("Result returned by removeLast() was wrong.",
215                 confirmedLast, last);
216         verify();
217     }
218
219     /**
220      * Returns an empty {@link ArrayList}.
221      */

222     public Collection JavaDoc makeConfirmedCollection() {
223         return new LinkedList JavaDoc();
224     }
225
226     /**
227      * Returns a full {@link ArrayList}.
228      */

229     public Collection JavaDoc makeConfirmedFullCollection() {
230         List JavaDoc list = new LinkedList JavaDoc();
231         list.addAll(Arrays.asList(getFullElements()));
232         return list;
233     }
234 }
235
Popular Tags