KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > TestAbstractLinkedList


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.list;
17
18 import java.util.Arrays JavaDoc;
19
20 /**
21  * Test case for {@link AbstractLinkedList}.
22  *
23  * @version $Revision: 1.3 $ $Date: 2004/02/18 01:20:34 $
24  *
25  * @author Rich Dougherty
26  * @author David Hay
27  * @author Phil Steitz
28  */

29 public abstract class TestAbstractLinkedList extends AbstractTestList {
30     
31     public TestAbstractLinkedList(String JavaDoc testName) {
32         super(testName);
33     }
34
35     //-----------------------------------------------------------------------
36
public void testRemoveFirst() {
37         resetEmpty();
38         AbstractLinkedList list = (AbstractLinkedList) collection;
39         if (isRemoveSupported() == false) {
40             try {
41                 list.removeFirst();
42             } catch (UnsupportedOperationException JavaDoc ex) {}
43         }
44         
45         list.addAll( Arrays.asList( new String JavaDoc[]{"value1", "value2"}));
46         assertEquals( "value1", list.removeFirst() );
47         checkNodes();
48         list.addLast( "value3");
49         checkNodes();
50         assertEquals( "value2", list.removeFirst() );
51         assertEquals( "value3", list.removeFirst() );
52         checkNodes();
53         list.addLast( "value4" );
54         checkNodes();
55         assertEquals( "value4", list.removeFirst() );
56         checkNodes();
57     }
58     
59     public void testRemoveLast() {
60         resetEmpty();
61         AbstractLinkedList list = (AbstractLinkedList) collection;
62         if (isRemoveSupported() == false) {
63             try {
64                 list.removeLast();
65             } catch (UnsupportedOperationException JavaDoc ex) {}
66         }
67         
68         list.addAll( Arrays.asList( new String JavaDoc[]{"value1", "value2"}));
69         assertEquals( "value2", list.removeLast() );
70         list.addFirst( "value3");
71         checkNodes();
72         assertEquals( "value1", list.removeLast() );
73         assertEquals( "value3", list.removeLast() );
74         list.addFirst( "value4" );
75         checkNodes();
76         assertEquals( "value4", list.removeFirst() );
77     }
78     
79     public void testAddNodeAfter() {
80         resetEmpty();
81         AbstractLinkedList list = (AbstractLinkedList) collection;
82         if (isAddSupported() == false) {
83             try {
84                 list.addFirst(null);
85             } catch (UnsupportedOperationException JavaDoc ex) {}
86         }
87         
88         list.addFirst("value1");
89         list.addNodeAfter(list.getNode(0,false),"value2");
90         assertEquals("value1", list.getFirst());
91         assertEquals("value2", list.getLast());
92         list.removeFirst();
93         checkNodes();
94         list.addNodeAfter(list.getNode(0,false),"value3");
95         checkNodes();
96         assertEquals("value2", list.getFirst());
97         assertEquals("value3", list.getLast());
98         list.addNodeAfter(list.getNode(0, false),"value4");
99         checkNodes();
100         assertEquals("value2", list.getFirst());
101         assertEquals("value3", list.getLast());
102         assertEquals("value4", list.get(1));
103         list.addNodeAfter(list.getNode(2, false), "value5");
104         checkNodes();
105         assertEquals("value2", list.getFirst());
106         assertEquals("value4", list.get(1));
107         assertEquals("value3", list.get(2));
108         assertEquals("value5", list.getLast());
109     }
110     
111     public void testRemoveNode() {
112         resetEmpty();
113         if (isAddSupported() == false || isRemoveSupported() == false) return;
114         AbstractLinkedList list = (AbstractLinkedList) collection;
115         
116         list.addAll( Arrays.asList( new String JavaDoc[]{"value1", "value2"}));
117         list.removeNode(list.getNode(0, false));
118         checkNodes();
119         assertEquals("value2", list.getFirst());
120         assertEquals("value2", list.getLast());
121         list.addFirst("value1");
122         list.addFirst("value0");
123         checkNodes();
124         list.removeNode(list.getNode(1, false));
125         assertEquals("value0", list.getFirst());
126         assertEquals("value2", list.getLast());
127         checkNodes();
128         list.removeNode(list.getNode(1, false));
129         assertEquals("value0", list.getFirst());
130         assertEquals("value0", list.getLast());
131         checkNodes();
132     }
133     
134     public void testGetNode() {
135         resetEmpty();
136         AbstractLinkedList list = (AbstractLinkedList) collection;
137         // get marker
138
assertEquals(list.getNode(0, true).previous, list.getNode(0, true).next);
139         try {
140             Object JavaDoc obj = list.getNode(0, false);
141             fail("Expecting IndexOutOfBoundsException.");
142         } catch (IndexOutOfBoundsException JavaDoc ex) {
143             // expected
144
}
145         list.addAll( Arrays.asList( new String JavaDoc[]{"value1", "value2"}));
146         checkNodes();
147         list.addFirst("value0");
148         checkNodes();
149         list.removeNode(list.getNode(1, false));
150         checkNodes();
151         try {
152             Object JavaDoc obj = list.getNode(2, false);
153             fail("Expecting IndexOutOfBoundsException.");
154         } catch (IndexOutOfBoundsException JavaDoc ex) {
155             // expected
156
}
157         try {
158             Object JavaDoc obj = list.getNode(-1, false);
159             fail("Expecting IndexOutOfBoundsException.");
160         } catch (IndexOutOfBoundsException JavaDoc ex) {
161             // expected
162
}
163          try {
164             Object JavaDoc obj = list.getNode(3, true);
165             fail("Expecting IndexOutOfBoundsException.");
166         } catch (IndexOutOfBoundsException JavaDoc ex) {
167             // expected
168
}
169     }
170     
171     protected void checkNodes() {
172         AbstractLinkedList list = (AbstractLinkedList) collection;
173         for (int i = 0; i < list.size; i++) {
174             assertEquals(list.getNode(i, false).next, list.getNode(i + 1, true));
175             if (i < list.size - 1) {
176                 assertEquals(list.getNode(i + 1, false).previous,
177                     list.getNode(i, false));
178             }
179         }
180     }
181         
182 }
183
Popular Tags