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.list;
017:
018: import java.util.Arrays;
019:
020: /**
021: * Test case for {@link AbstractLinkedList}.
022: *
023: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
024: *
025: * @author Rich Dougherty
026: * @author David Hay
027: * @author Phil Steitz
028: */
029: public abstract class TestAbstractLinkedList extends AbstractTestList {
030:
031: public TestAbstractLinkedList(String testName) {
032: super (testName);
033: }
034:
035: //-----------------------------------------------------------------------
036: public void testRemoveFirst() {
037: resetEmpty();
038: AbstractLinkedList list = (AbstractLinkedList) collection;
039: if (isRemoveSupported() == false) {
040: try {
041: list.removeFirst();
042: } catch (UnsupportedOperationException ex) {
043: }
044: }
045:
046: list.addAll(Arrays.asList(new String[] { "value1", "value2" }));
047: assertEquals("value1", list.removeFirst());
048: checkNodes();
049: list.addLast("value3");
050: checkNodes();
051: assertEquals("value2", list.removeFirst());
052: assertEquals("value3", list.removeFirst());
053: checkNodes();
054: list.addLast("value4");
055: checkNodes();
056: assertEquals("value4", list.removeFirst());
057: checkNodes();
058: }
059:
060: public void testRemoveLast() {
061: resetEmpty();
062: AbstractLinkedList list = (AbstractLinkedList) collection;
063: if (isRemoveSupported() == false) {
064: try {
065: list.removeLast();
066: } catch (UnsupportedOperationException ex) {
067: }
068: }
069:
070: list.addAll(Arrays.asList(new String[] { "value1", "value2" }));
071: assertEquals("value2", list.removeLast());
072: list.addFirst("value3");
073: checkNodes();
074: assertEquals("value1", list.removeLast());
075: assertEquals("value3", list.removeLast());
076: list.addFirst("value4");
077: checkNodes();
078: assertEquals("value4", list.removeFirst());
079: }
080:
081: public void testAddNodeAfter() {
082: resetEmpty();
083: AbstractLinkedList list = (AbstractLinkedList) collection;
084: if (isAddSupported() == false) {
085: try {
086: list.addFirst(null);
087: } catch (UnsupportedOperationException ex) {
088: }
089: }
090:
091: list.addFirst("value1");
092: list.addNodeAfter(list.getNode(0, false), "value2");
093: assertEquals("value1", list.getFirst());
094: assertEquals("value2", list.getLast());
095: list.removeFirst();
096: checkNodes();
097: list.addNodeAfter(list.getNode(0, false), "value3");
098: checkNodes();
099: assertEquals("value2", list.getFirst());
100: assertEquals("value3", list.getLast());
101: list.addNodeAfter(list.getNode(0, false), "value4");
102: checkNodes();
103: assertEquals("value2", list.getFirst());
104: assertEquals("value3", list.getLast());
105: assertEquals("value4", list.get(1));
106: list.addNodeAfter(list.getNode(2, false), "value5");
107: checkNodes();
108: assertEquals("value2", list.getFirst());
109: assertEquals("value4", list.get(1));
110: assertEquals("value3", list.get(2));
111: assertEquals("value5", list.getLast());
112: }
113:
114: public void testRemoveNode() {
115: resetEmpty();
116: if (isAddSupported() == false || isRemoveSupported() == false)
117: return;
118: AbstractLinkedList list = (AbstractLinkedList) collection;
119:
120: list.addAll(Arrays.asList(new String[] { "value1", "value2" }));
121: list.removeNode(list.getNode(0, false));
122: checkNodes();
123: assertEquals("value2", list.getFirst());
124: assertEquals("value2", list.getLast());
125: list.addFirst("value1");
126: list.addFirst("value0");
127: checkNodes();
128: list.removeNode(list.getNode(1, false));
129: assertEquals("value0", list.getFirst());
130: assertEquals("value2", list.getLast());
131: checkNodes();
132: list.removeNode(list.getNode(1, false));
133: assertEquals("value0", list.getFirst());
134: assertEquals("value0", list.getLast());
135: checkNodes();
136: }
137:
138: public void testGetNode() {
139: resetEmpty();
140: AbstractLinkedList list = (AbstractLinkedList) collection;
141: // get marker
142: assertEquals(list.getNode(0, true).previous, list.getNode(0,
143: true).next);
144: try {
145: Object obj = list.getNode(0, false);
146: fail("Expecting IndexOutOfBoundsException.");
147: } catch (IndexOutOfBoundsException ex) {
148: // expected
149: }
150: list.addAll(Arrays.asList(new String[] { "value1", "value2" }));
151: checkNodes();
152: list.addFirst("value0");
153: checkNodes();
154: list.removeNode(list.getNode(1, false));
155: checkNodes();
156: try {
157: Object obj = list.getNode(2, false);
158: fail("Expecting IndexOutOfBoundsException.");
159: } catch (IndexOutOfBoundsException ex) {
160: // expected
161: }
162: try {
163: Object obj = list.getNode(-1, false);
164: fail("Expecting IndexOutOfBoundsException.");
165: } catch (IndexOutOfBoundsException ex) {
166: // expected
167: }
168: try {
169: Object obj = list.getNode(3, true);
170: fail("Expecting IndexOutOfBoundsException.");
171: } catch (IndexOutOfBoundsException ex) {
172: // expected
173: }
174: }
175:
176: protected void checkNodes() {
177: AbstractLinkedList list = (AbstractLinkedList) collection;
178: for (int i = 0; i < list.size; i++) {
179: assertEquals(list.getNode(i, false).next, list.getNode(
180: i + 1, true));
181: if (i < list.size - 1) {
182: assertEquals(list.getNode(i + 1, false).previous, list
183: .getNode(i, false));
184: }
185: }
186: }
187:
188: }
|