001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.NoSuchElementException;
023:
024: import org.apache.commons.collections.list.AbstractTestList;
025:
026: /**
027: * Tests base {@link java.util.LinkedList} methods and contracts.
028: * <p>
029: * To use, simply extend this class, and implement
030: * the {@link #makeLinkedList} method.
031: * <p>
032: * If your {@link LinkedList} fails one of these tests by design,
033: * you may still use this base set of cases. Simply override the
034: * test case (method) your {@link List} fails.
035: *
036: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
037: *
038: * @author Rich Dougherty
039: */
040: public abstract class TestLinkedList extends AbstractTestList {
041:
042: public TestLinkedList(String testName) {
043: super (testName);
044: }
045:
046: public List makeEmptyList() {
047: return makeEmptyLinkedList();
048: }
049:
050: public List makeFullList() {
051: return makeFullLinkedList();
052: }
053:
054: /**
055: * Return a new, empty {@link LinkedList} to be used for testing.
056: *
057: * @return an empty list for testing.
058: */
059: protected abstract LinkedList makeEmptyLinkedList();
060:
061: /**
062: * Return a new, full {@link List} to be used for testing.
063: *
064: * @return a full list for testing
065: */
066: protected LinkedList makeFullLinkedList() {
067: // only works if list supports optional "addAll(Collection)"
068: LinkedList list = makeEmptyLinkedList();
069: list.addAll(Arrays.asList(getFullElements()));
070: return list;
071: }
072:
073: /**
074: * Returns the {@link #collection} field cast to a {@link LinkedList}.
075: *
076: * @return the collection field as a List
077: */
078: protected LinkedList getLinkedList() {
079: return (LinkedList) collection;
080: }
081:
082: /**
083: * Returns the {@link #confirmed} field cast to a {@link LinkedList}.
084: *
085: * @return the confirmed field as a List
086: */
087: protected LinkedList getConfirmedLinkedList() {
088: return (LinkedList) confirmed;
089: }
090:
091: /**
092: * Tests {@link LinkedList#addFirst(Object)}.
093: */
094: public void testLinkedListAddFirst() {
095: if (!isAddSupported())
096: return;
097: Object o = "hello";
098:
099: resetEmpty();
100: getLinkedList().addFirst(o);
101: getConfirmedLinkedList().addFirst(o);
102: verify();
103:
104: resetFull();
105: getLinkedList().addFirst(o);
106: getConfirmedLinkedList().addFirst(o);
107: verify();
108: }
109:
110: /**
111: * Tests {@link LinkedList#addLast(Object)}.
112: */
113: public void testLinkedListAddLast() {
114: if (!isAddSupported())
115: return;
116: Object o = "hello";
117:
118: resetEmpty();
119: getLinkedList().addLast(o);
120: getConfirmedLinkedList().addLast(o);
121: verify();
122:
123: resetFull();
124: getLinkedList().addLast(o);
125: getConfirmedLinkedList().addLast(o);
126: verify();
127: }
128:
129: /**
130: * Tests {@link LinkedList#getFirst(Object)}.
131: */
132: public void testLinkedListGetFirst() {
133: resetEmpty();
134: try {
135: getLinkedList().getFirst();
136: fail("getFirst() should throw a NoSuchElementException for an "
137: + "empty list.");
138: } catch (NoSuchElementException e) {
139: // This is correct
140: }
141: verify();
142:
143: resetFull();
144: Object first = getLinkedList().getFirst();
145: Object confirmedFirst = getConfirmedLinkedList().getFirst();
146: assertEquals("Result returned by getFirst() was wrong.",
147: confirmedFirst, first);
148: verify();
149: }
150:
151: /**
152: * Tests {@link LinkedList#getLast(Object)}.
153: */
154: public void testLinkedListGetLast() {
155: resetEmpty();
156: try {
157: getLinkedList().getLast();
158: fail("getLast() should throw a NoSuchElementException for an "
159: + "empty list.");
160: } catch (NoSuchElementException e) {
161: // This is correct
162: }
163: verify();
164:
165: resetFull();
166: Object last = getLinkedList().getLast();
167: Object confirmedLast = getConfirmedLinkedList().getLast();
168: assertEquals("Result returned by getLast() was wrong.",
169: confirmedLast, last);
170: verify();
171: }
172:
173: /**
174: * Tests {@link LinkedList#removeFirst(Object)}.
175: */
176: public void testLinkedListRemoveFirst() {
177: if (!isRemoveSupported())
178: return;
179:
180: resetEmpty();
181: try {
182: getLinkedList().removeFirst();
183: fail("removeFirst() should throw a NoSuchElementException for "
184: + "an empty list.");
185: } catch (NoSuchElementException e) {
186: // This is correct
187: }
188: verify();
189:
190: resetFull();
191: Object first = getLinkedList().removeFirst();
192: Object confirmedFirst = getConfirmedLinkedList().removeFirst();
193: assertEquals("Result returned by removeFirst() was wrong.",
194: confirmedFirst, first);
195: verify();
196: }
197:
198: /**
199: * Tests {@link LinkedList#removeLast(Object)}.
200: */
201: public void testLinkedListRemoveLast() {
202: if (!isRemoveSupported())
203: return;
204:
205: resetEmpty();
206: try {
207: getLinkedList().removeLast();
208: fail("removeLast() should throw a NoSuchElementException for "
209: + "an empty list.");
210: } catch (NoSuchElementException e) {
211: // This is correct
212: }
213: verify();
214:
215: resetFull();
216: Object last = getLinkedList().removeLast();
217: Object confirmedLast = getConfirmedLinkedList().removeLast();
218: assertEquals("Result returned by removeLast() was wrong.",
219: confirmedLast, last);
220: verify();
221: }
222:
223: /**
224: * Returns an empty {@link ArrayList}.
225: */
226: public Collection makeConfirmedCollection() {
227: return new LinkedList();
228: }
229:
230: /**
231: * Returns a full {@link ArrayList}.
232: */
233: public Collection makeConfirmedFullCollection() {
234: List list = new LinkedList();
235: list.addAll(Arrays.asList(getFullElements()));
236: return list;
237: }
238: }
|