KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > AbstractTestOrderedMap


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.map;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.TreeMap JavaDoc;
26
27 import org.apache.commons.collections.BulkTest;
28 import org.apache.commons.collections.MapIterator;
29 import org.apache.commons.collections.OrderedMap;
30 import org.apache.commons.collections.comparators.NullComparator;
31 import org.apache.commons.collections.iterators.AbstractTestOrderedMapIterator;
32
33 /**
34  * Abstract test class for {@link OrderedMap} methods and contracts.
35  *
36  * @version $Revision: 1.7 $ $Date: 2004/02/18 01:20:38 $
37  *
38  * @author Stephen Colebourne
39  */

40 public abstract class AbstractTestOrderedMap extends AbstractTestIterableMap {
41
42     /**
43      * JUnit constructor.
44      *
45      * @param testName the test name
46      */

47     public AbstractTestOrderedMap(String JavaDoc testName) {
48         super(testName);
49     }
50     
51     //-----------------------------------------------------------------------
52
/**
53      * OrderedMap uses TreeMap as its known comparison.
54      *
55      * @return a map that is known to be valid
56      */

57     public Map JavaDoc makeConfirmedMap() {
58         return new TreeMap JavaDoc(new NullComparator());
59     }
60     
61     /**
62      * The only confirmed collection we have that is ordered is the sorted one.
63      * Thus, sort the keys.
64      */

65     public Object JavaDoc[] getSampleKeys() {
66         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(super.getSampleKeys()));
67         Collections.sort(list, new NullComparator());
68         return list.toArray();
69     }
70
71     //-----------------------------------------------------------------------
72
public void testFirstKey() {
73         resetEmpty();
74         OrderedMap ordered = (OrderedMap) map;
75         try {
76             ordered.firstKey();
77             fail();
78         } catch (NoSuchElementException JavaDoc ex) {}
79         
80         resetFull();
81         ordered = (OrderedMap) map;
82         Object JavaDoc confirmedFirst = confirmed.keySet().iterator().next();
83         assertEquals(confirmedFirst, ordered.firstKey());
84     }
85     
86     public void testLastKey() {
87         resetEmpty();
88         OrderedMap ordered = (OrderedMap) map;
89         try {
90             ordered.lastKey();
91             fail();
92         } catch (NoSuchElementException JavaDoc ex) {}
93         
94         resetFull();
95         ordered = (OrderedMap) map;
96         Object JavaDoc confirmedLast = null;
97         for (Iterator JavaDoc it = confirmed.keySet().iterator(); it.hasNext();) {
98             confirmedLast = it.next();
99         }
100         assertEquals(confirmedLast, ordered.lastKey());
101     }
102
103     //-----------------------------------------------------------------------
104
public void testNextKey() {
105         resetEmpty();
106         OrderedMap ordered = (OrderedMap) map;
107         assertEquals(null, ordered.nextKey(getOtherKeys()[0]));
108         if (isAllowNullKey() == false) {
109             try {
110                 assertEquals(null, ordered.nextKey(null)); // this is allowed too
111
} catch (NullPointerException JavaDoc ex) {}
112         } else {
113             assertEquals(null, ordered.nextKey(null));
114         }
115         
116         resetFull();
117         ordered = (OrderedMap) map;
118         Iterator JavaDoc it = confirmed.keySet().iterator();
119         Object JavaDoc confirmedLast = it.next();
120         while (it.hasNext()) {
121             Object JavaDoc confirmedObject = it.next();
122             assertEquals(confirmedObject, ordered.nextKey(confirmedLast));
123             confirmedLast = confirmedObject;
124         }
125         assertEquals(null, ordered.nextKey(confirmedLast));
126         
127         if (isAllowNullKey() == false) {
128             try {
129                 ordered.nextKey(null);
130                 fail();
131             } catch (NullPointerException JavaDoc ex) {}
132         } else {
133             assertEquals(null, ordered.nextKey(null));
134         }
135     }
136     
137     public void testPreviousKey() {
138         resetEmpty();
139         OrderedMap ordered = (OrderedMap) map;
140         assertEquals(null, ordered.previousKey(getOtherKeys()[0]));
141         if (isAllowNullKey() == false) {
142             try {
143                 assertEquals(null, ordered.previousKey(null)); // this is allowed too
144
} catch (NullPointerException JavaDoc ex) {}
145         } else {
146             assertEquals(null, ordered.previousKey(null));
147         }
148         
149         resetFull();
150         ordered = (OrderedMap) map;
151         List JavaDoc list = new ArrayList JavaDoc(confirmed.keySet());
152         Collections.reverse(list);
153         Iterator JavaDoc it = list.iterator();
154         Object JavaDoc confirmedLast = it.next();
155         while (it.hasNext()) {
156             Object JavaDoc confirmedObject = it.next();
157             assertEquals(confirmedObject, ordered.previousKey(confirmedLast));
158             confirmedLast = confirmedObject;
159         }
160         assertEquals(null, ordered.previousKey(confirmedLast));
161         
162         if (isAllowNullKey() == false) {
163             try {
164                 ordered.previousKey(null);
165                 fail();
166             } catch (NullPointerException JavaDoc ex) {}
167         } else {
168             if (isAllowNullKey() == false) {
169                 assertEquals(null, ordered.previousKey(null));
170             }
171         }
172     }
173     
174     //-----------------------------------------------------------------------
175
public BulkTest bulkTestOrderedMapIterator() {
176         return new InnerTestOrderedMapIterator();
177     }
178     
179     public class InnerTestOrderedMapIterator extends AbstractTestOrderedMapIterator {
180         public InnerTestOrderedMapIterator() {
181             super("InnerTestOrderedMapIterator");
182         }
183         
184         public boolean supportsRemove() {
185             return AbstractTestOrderedMap.this.isRemoveSupported();
186         }
187
188         public boolean isGetStructuralModify() {
189             return AbstractTestOrderedMap.this.isGetStructuralModify();
190         }
191         
192         public boolean supportsSetValue() {
193             return AbstractTestOrderedMap.this.isSetValueSupported();
194         }
195
196         public MapIterator makeEmptyMapIterator() {
197             resetEmpty();
198             return ((OrderedMap) AbstractTestOrderedMap.this.map).orderedMapIterator();
199         }
200
201         public MapIterator makeFullMapIterator() {
202             resetFull();
203             return ((OrderedMap) AbstractTestOrderedMap.this.map).orderedMapIterator();
204         }
205         
206         public Map JavaDoc getMap() {
207             // assumes makeFullMapIterator() called first
208
return AbstractTestOrderedMap.this.map;
209         }
210         
211         public Map JavaDoc getConfirmedMap() {
212             // assumes makeFullMapIterator() called first
213
return AbstractTestOrderedMap.this.confirmed;
214         }
215         
216         public void verify() {
217             super.verify();
218             AbstractTestOrderedMap.this.verify();
219         }
220     }
221     
222 }
223
Popular Tags