KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > AbstractTestSortedSet


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.set;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.SortedSet JavaDoc;
22 import java.util.TreeSet JavaDoc;
23
24 import org.apache.commons.collections.BulkTest;
25
26 /**
27  * Abstract test class for {@link SortedSet} methods and contracts.
28  * <p>
29  * To use, subclass and override the {@link #makeEmptySet()}
30  * method. You may have to override other protected methods if your
31  * set is not modifiable, or if your set restricts what kinds of
32  * elements may be added; see {@link AbstractTestCollection} for more details.
33  *
34  * @since Commons Collections 3.0
35  * @version $Revision: 1.6 $ $Date: 2004/05/31 22:39:20 $
36  *
37  * @author Stephen Colebourne
38  * @author Dieter Wimberger
39  */

40 public abstract class AbstractTestSortedSet extends AbstractTestSet {
41
42     /**
43      * JUnit constructor.
44      *
45      * @param name name for test
46      */

47     public AbstractTestSortedSet(String JavaDoc name) {
48         super(name);
49     }
50
51     //-----------------------------------------------------------------------
52
/**
53      * Verification extension, will check the order of elements,
54      * the sets should already be verified equal.
55      */

56     public void verify() {
57         super.verify();
58         
59         // Check that iterator returns elements in order and first() and last()
60
// are consistent
61
Iterator JavaDoc colliter = collection.iterator();
62         Iterator JavaDoc confiter = confirmed.iterator();
63         Object JavaDoc first = null;
64         Object JavaDoc last = null;
65         while (colliter.hasNext()) {
66             if (first == null) {
67                 first = colliter.next();
68                 last = first;
69             } else {
70               last = colliter.next();
71             }
72             assertEquals("Element appears to be out of order.", last, confiter.next());
73         }
74         if (collection.size() > 0) {
75             assertEquals("Incorrect element returned by first().", first,
76                 ((SortedSet JavaDoc) collection).first());
77             assertEquals("Incorrect element returned by last().", last,
78                 ((SortedSet JavaDoc) collection).last());
79         }
80     }
81
82     //-----------------------------------------------------------------------
83
/**
84      * Overridden because SortedSets don't allow null elements (normally).
85      * @return false
86      */

87     public boolean isNullSupported() {
88         return false;
89     }
90
91     //-----------------------------------------------------------------------
92
/**
93      * Returns an empty {@link TreeSet} for use in modification testing.
94      *
95      * @return a confirmed empty collection
96      */

97     public Collection JavaDoc makeConfirmedCollection() {
98         return new TreeSet JavaDoc();
99     }
100
101     //-----------------------------------------------------------------------
102
/**
103      * Return the {@link AbstractTestCollection#confirmed} fixture, but cast as a
104      * SortedSet.
105      */

106     public SortedSet JavaDoc getConfirmedSortedSet() {
107         return (SortedSet JavaDoc) confirmed;
108     }
109
110     //-----------------------------------------------------------------------
111
/**
112      * Override to return comparable objects.
113      */

114     public Object JavaDoc[] getFullNonNullElements() {
115         Object JavaDoc[] elements = new Object JavaDoc[30];
116
117         for (int i = 0; i < 30; i++) {
118             elements[i] = new Integer JavaDoc(i + i + 1);
119         }
120         return elements;
121     }
122
123     /**
124      * Override to return comparable objects.
125      */

126     public Object JavaDoc[] getOtherNonNullElements() {
127         Object JavaDoc[] elements = new Object JavaDoc[30];
128         for (int i = 0; i < 30; i++) {
129             elements[i] = new Integer JavaDoc(i + i + 2);
130         }
131         return elements;
132     }
133
134     //-----------------------------------------------------------------------
135
/**
136      * Bulk test {@link SortedSet#subSet(Object, Object)}. This method runs through all of
137      * the tests in {@link AbstractTestSortedSet}.
138      * After modification operations, {@link #verify()} is invoked to ensure
139      * that the set and the other collection views are still valid.
140      *
141      * @return a {@link AbstractTestSet} instance for testing a subset.
142      */

143     public BulkTest bulkTestSortedSetSubSet() {
144         int length = getFullElements().length;
145
146         int lobound = length / 3;
147         int hibound = lobound * 2;
148         return new TestSortedSetSubSet(lobound, hibound);
149
150     }
151
152     /**
153      * Bulk test {@link SortedSet#headSet(Object)}. This method runs through all of
154      * the tests in {@link AbstractTestSortedSet}.
155      * After modification operations, {@link #verify()} is invoked to ensure
156      * that the set and the other collection views are still valid.
157      *
158      * @return a {@link AbstractTestSet} instance for testing a headset.
159      */

160     public BulkTest bulkTestSortedSetHeadSet() {
161         int length = getFullElements().length;
162
163         int lobound = length / 3;
164         int hibound = lobound * 2;
165         return new TestSortedSetSubSet(hibound, true);
166
167     }
168
169     /**
170      * Bulk test {@link SortedSet#tailSet(Object)}. This method runs through all of
171      * the tests in {@link AbstractTestSortedSet}.
172      * After modification operations, {@link #verify()} is invoked to ensure
173      * that the set and the other collection views are still valid.
174      *
175      * @return a {@link AbstractTestSet} instance for testing a tailset.
176      */

177     public BulkTest bulkTestSortedSetTailSet() {
178         int length = getFullElements().length;
179         int lobound = length / 3;
180         return new TestSortedSetSubSet(lobound, false);
181     }
182
183     public class TestSortedSetSubSet extends AbstractTestSortedSet {
184
185         private int m_Type;
186         private int m_LowBound;
187         private int m_HighBound;
188         private Object JavaDoc[] m_FullElements;
189         private Object JavaDoc[] m_OtherElements;
190
191         public TestSortedSetSubSet(int bound, boolean head) {
192             super("TestSortedSetSubSet");
193             if (head) {
194                 //System.out.println("HEADSET");
195
m_Type = TYPE_HEADSET;
196                 m_HighBound = bound;
197                 m_FullElements = new Object JavaDoc[bound];
198                 System.arraycopy(AbstractTestSortedSet.this.getFullElements(), 0, m_FullElements, 0, bound);
199                 m_OtherElements = new Object JavaDoc[bound - 1];
200                 System.arraycopy(//src src_pos dst dst_pos length
201
AbstractTestSortedSet.this.getOtherElements(), 0, m_OtherElements, 0, bound - 1);
202                 //System.out.println(new TreeSet(Arrays.asList(m_FullElements)));
203
//System.out.println(new TreeSet(Arrays.asList(m_OtherElements)));
204
} else {
205                 //System.out.println("TAILSET");
206
m_Type = TYPE_TAILSET;
207                 m_LowBound = bound;
208                 Object JavaDoc[] allelements = AbstractTestSortedSet.this.getFullElements();
209                 //System.out.println("bound = "+bound +"::length="+allelements.length);
210
m_FullElements = new Object JavaDoc[allelements.length - bound];
211                 System.arraycopy(allelements, bound, m_FullElements, 0, allelements.length - bound);
212                 m_OtherElements = new Object JavaDoc[allelements.length - bound - 1];
213                 System.arraycopy(//src src_pos dst dst_pos length
214
AbstractTestSortedSet.this.getOtherElements(), bound, m_OtherElements, 0, allelements.length - bound - 1);
215                 //System.out.println(new TreeSet(Arrays.asList(m_FullElements)));
216
//System.out.println(new TreeSet(Arrays.asList(m_OtherElements)));
217
//resetFull();
218
//System.out.println(collection);
219
//System.out.println(confirmed);
220

221             }
222
223         } //type
224

225         public TestSortedSetSubSet(int lobound, int hibound) {
226             super("TestSortedSetSubSet");
227             //System.out.println("SUBSET");
228
m_Type = TYPE_SUBSET;
229             m_LowBound = lobound;
230             m_HighBound = hibound;
231             int length = hibound - lobound;
232             //System.out.println("Low=" + lobound + "::High=" + hibound + "::Length=" + length);
233
m_FullElements = new Object JavaDoc[length];
234             System.arraycopy(AbstractTestSortedSet.this.getFullElements(), lobound, m_FullElements, 0, length);
235             m_OtherElements = new Object JavaDoc[length - 1];
236             System.arraycopy(//src src_pos dst dst_pos length
237
AbstractTestSortedSet.this.getOtherElements(), lobound, m_OtherElements, 0, length - 1);
238
239             //System.out.println(new TreeSet(Arrays.asList(m_FullElements)));
240
//System.out.println(new TreeSet(Arrays.asList(m_OtherElements)));
241

242         }
243
244         public boolean isNullSupported() {
245             return AbstractTestSortedSet.this.isNullSupported();
246         }
247         public boolean isAddSupported() {
248             return AbstractTestSortedSet.this.isAddSupported();
249         }
250         public boolean isRemoveSupported() {
251             return AbstractTestSortedSet.this.isRemoveSupported();
252         }
253         public boolean isFailFastSupported() {
254             return AbstractTestSortedSet.this.isFailFastSupported();
255         }
256
257         public Object JavaDoc[] getFullElements() {
258             return m_FullElements;
259         }
260         public Object JavaDoc[] getOtherElements() {
261             return m_OtherElements;
262         }
263
264         private SortedSet JavaDoc getSubSet(SortedSet JavaDoc set) {
265             Object JavaDoc[] elements = AbstractTestSortedSet.this.getFullElements();
266             switch (m_Type) {
267                 case TYPE_SUBSET :
268                     return set.subSet(elements[m_LowBound], elements[m_HighBound]);
269                 case TYPE_HEADSET :
270                     return set.headSet(elements[m_HighBound]);
271                 case TYPE_TAILSET :
272                     return set.tailSet(elements[m_LowBound]);
273                 default :
274                     return null;
275             }
276         }
277
278         public Set JavaDoc makeEmptySet() {
279             SortedSet JavaDoc s = (SortedSet JavaDoc) AbstractTestSortedSet.this.makeEmptySet();
280             return getSubSet(s);
281         }
282
283         public Set JavaDoc makeFullSet() {
284             SortedSet JavaDoc s = (SortedSet JavaDoc) AbstractTestSortedSet.this.makeFullCollection();
285             return getSubSet(s);
286         }
287         
288         public boolean isTestSerialization() {
289             return false;
290         }
291         
292         public BulkTest bulkTestSortedSetSubSet() {
293             return null; // prevent infinite recursion
294
}
295         public BulkTest bulkTestSortedSetHeadSet() {
296             return null; // prevent infinite recursion
297
}
298         public BulkTest bulkTestSortedSetTailSet() {
299             return null; // prevent infinite recursion
300
}
301
302         static final int TYPE_SUBSET = 0;
303         static final int TYPE_TAILSET = 1;
304         static final int TYPE_HEADSET = 2;
305
306     }
307
308 }
309
Popular Tags