KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > primitives > adapters > BaseTestList


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.collections.primitives.adapters;
18
19 import java.util.Arrays JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ListIterator JavaDoc;
23
24 import org.apache.commons.collections.BulkTest;
25 import org.apache.commons.collections.list.AbstractTestList;
26
27 /**
28  * @version $Revision: 480451 $ $Date: 2006-11-28 23:45:08 -0800 (Tue, 28 Nov 2006) $
29  * @author Rodney Waldhoff
30  */

31 public abstract class BaseTestList extends AbstractTestList {
32
33     // conventional
34
// ------------------------------------------------------------------------
35

36     public BaseTestList(String JavaDoc testName) {
37         super(testName);
38     }
39
40     // tests
41
// ------------------------------------------------------------------------
42

43     public final void testAddAllAtIndex() {
44         List JavaDoc source = makeFullList();
45         List JavaDoc dest = makeFullList();
46         
47         dest.addAll(1,source);
48          
49         Iterator JavaDoc iter = dest.iterator();
50         assertTrue(iter.hasNext());
51         assertEquals(source.get(0),iter.next());
52         for(int i=0;i<source.size();i++) {
53             assertTrue(iter.hasNext());
54             assertEquals(source.get(i),iter.next());
55         }
56         for(int i=1;i<source.size();i++) {
57             assertTrue(iter.hasNext());
58             assertEquals(source.get(i),iter.next());
59         }
60         assertFalse(iter.hasNext());
61     }
62
63     /**
64      * Override to change assertSame to assertEquals.
65      */

66     public void testListListIteratorPreviousRemove() {
67         if (isRemoveSupported() == false) return;
68         resetFull();
69         ListIterator JavaDoc it = getList().listIterator();
70         Object JavaDoc zero = it.next();
71         Object JavaDoc one = it.next();
72         Object JavaDoc two = it.next();
73         Object JavaDoc two2 = it.previous();
74         Object JavaDoc one2 = it.previous();
75         assertEquals(one, one2);
76         assertEquals(two, two2);
77         assertEquals(zero, getList().get(0));
78         assertEquals(one, getList().get(1));
79         assertEquals(two, getList().get(2));
80         it.remove();
81         assertEquals(zero, getList().get(0));
82         assertEquals(two, getList().get(1));
83     }
84
85     /**
86      * Override to change assertSame to assertEquals.
87      */

88     public BulkTest bulkTestSubList() {
89         if (getFullElements().length - 6 < 10) return null;
90         return new PrimitiveBulkTestSubList(this);
91     }
92
93
94     /**
95      * Whole class copied as sub list constructor was package scoped in 3.1.
96      */

97     public static class PrimitiveBulkTestSubList extends BaseTestList {
98         private BaseTestList outer;
99     
100         PrimitiveBulkTestSubList(BaseTestList outer) {
101             super("");
102             this.outer = outer;
103         }
104     
105         public Object JavaDoc[] getFullElements() {
106             List JavaDoc l = Arrays.asList(outer.getFullElements());
107             return l.subList(3, l.size() - 3).toArray();
108         }
109         public Object JavaDoc[] getOtherElements() {
110             return outer.getOtherElements();
111         }
112         public boolean isAddSupported() {
113             return outer.isAddSupported();
114         }
115         public boolean isSetSupported() {
116             return outer.isSetSupported();
117         }
118         public boolean isRemoveSupported() {
119             return outer.isRemoveSupported();
120         }
121     
122         public List JavaDoc makeEmptyList() {
123             return outer.makeFullList().subList(4, 4);
124         }
125         public List JavaDoc makeFullList() {
126             int size = getFullElements().length;
127             return outer.makeFullList().subList(3, size - 3);
128         }
129         public void resetEmpty() {
130             outer.resetFull();
131             this.collection = outer.getList().subList(4, 4);
132             this.confirmed = outer.getConfirmedList().subList(4, 4);
133         }
134         public void resetFull() {
135             outer.resetFull();
136             int size = outer.confirmed.size();
137             this.collection = outer.getList().subList(3, size - 3);
138             this.confirmed = outer.getConfirmedList().subList(3, size - 3);
139         }
140         public void verify() {
141             super.verify();
142             outer.verify();
143         }
144         public boolean isTestSerialization() {
145             return false;
146         }
147         /**
148          * Override to change assertSame to assertEquals.
149          */

150         public void testListListIteratorPreviousRemove() {
151             if (isRemoveSupported() == false)
152                 return;
153             resetFull();
154             ListIterator JavaDoc it = getList().listIterator();
155             Object JavaDoc zero = it.next();
156             Object JavaDoc one = it.next();
157             Object JavaDoc two = it.next();
158             Object JavaDoc two2 = it.previous();
159             Object JavaDoc one2 = it.previous();
160             assertEquals(one, one2);
161             assertEquals(two, two2);
162             assertEquals(zero, getList().get(0));
163             assertEquals(one, getList().get(1));
164             assertEquals(two, getList().get(2));
165             it.remove();
166             assertEquals(zero, getList().get(0));
167             assertEquals(two, getList().get(1));
168         }
169     }
170 }
171
Popular Tags