KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import junit.framework.Test;
24 import junit.textui.TestRunner;
25
26 import org.apache.commons.collections.BulkTest;
27 import org.apache.commons.collections.MapIterator;
28 import org.apache.commons.collections.OrderedMap;
29 import org.apache.commons.collections.ResettableIterator;
30 import org.apache.commons.collections.list.AbstractTestList;
31
32 /**
33  * JUnit tests.
34  *
35  * @version $Revision: 1.8 $ $Date: 2004/05/31 22:39:20 $
36  *
37  * @author Stephen Colebourne
38  */

39 public class TestLinkedMap extends AbstractTestOrderedMap {
40
41     public TestLinkedMap(String JavaDoc testName) {
42         super(testName);
43     }
44
45     public static void main(String JavaDoc[] args) {
46         TestRunner.run(suite());
47     }
48     
49     public static Test suite() {
50         return BulkTest.makeSuite(TestLinkedMap.class);
51     }
52
53     public Map JavaDoc makeEmptyMap() {
54         return new LinkedMap();
55     }
56
57     public String JavaDoc getCompatibilityVersion() {
58         return "3";
59     }
60
61     //-----------------------------------------------------------------------
62
public void testReset() {
63         resetEmpty();
64         OrderedMap ordered = (OrderedMap) map;
65         ((ResettableIterator) ordered.mapIterator()).reset();
66         
67         resetFull();
68         ordered = (OrderedMap) map;
69         List JavaDoc list = new ArrayList JavaDoc(ordered.keySet());
70         ResettableIterator it = (ResettableIterator) ordered.mapIterator();
71         assertSame(list.get(0), it.next());
72         assertSame(list.get(1), it.next());
73         it.reset();
74         assertSame(list.get(0), it.next());
75     }
76     
77     //-----------------------------------------------------------------------
78
public void testInsertionOrder() {
79         if (isPutAddSupported() == false || isPutChangeSupported() == false) return;
80         Object JavaDoc[] keys = getSampleKeys();
81         Object JavaDoc[] values = getSampleValues();
82         Iterator JavaDoc it = null;
83         
84         resetEmpty();
85         map.put(keys[0], values[0]);
86         map.put(keys[1], values[1]);
87         it = map.keySet().iterator();
88         assertSame(keys[0], it.next());
89         assertSame(keys[1], it.next());
90         it = map.values().iterator();
91         assertSame(values[0], it.next());
92         assertSame(values[1], it.next());
93
94         // no change to order
95
map.put(keys[1], values[1]);
96         it = map.keySet().iterator();
97         assertSame(keys[0], it.next());
98         assertSame(keys[1], it.next());
99         it = map.values().iterator();
100         assertSame(values[0], it.next());
101         assertSame(values[1], it.next());
102
103         // no change to order
104
map.put(keys[1], values[2]);
105         it = map.keySet().iterator();
106         assertSame(keys[0], it.next());
107         assertSame(keys[1], it.next());
108         it = map.values().iterator();
109         assertSame(values[0], it.next());
110         assertSame(values[2], it.next());
111
112         // no change to order
113
map.put(keys[0], values[3]);
114         it = map.keySet().iterator();
115         assertSame(keys[0], it.next());
116         assertSame(keys[1], it.next());
117         it = map.values().iterator();
118         assertSame(values[3], it.next());
119         assertSame(values[2], it.next());
120     }
121     
122     //-----------------------------------------------------------------------
123
public void testGetByIndex() {
124         resetEmpty();
125         LinkedMap lm = (LinkedMap) map;
126         try {
127             lm.get(0);
128         } catch (IndexOutOfBoundsException JavaDoc ex) {}
129         try {
130             lm.get(-1);
131         } catch (IndexOutOfBoundsException JavaDoc ex) {}
132         
133         resetFull();
134         lm = (LinkedMap) map;
135         try {
136             lm.get(-1);
137         } catch (IndexOutOfBoundsException JavaDoc ex) {}
138         try {
139             lm.get(lm.size());
140         } catch (IndexOutOfBoundsException JavaDoc ex) {}
141         
142         int i = 0;
143         for (MapIterator it = lm.mapIterator(); it.hasNext(); i++) {
144             assertSame(it.next(), lm.get(i));
145         }
146     }
147
148     public void testGetValueByIndex() {
149         resetEmpty();
150         LinkedMap lm = (LinkedMap) map;
151         try {
152             lm.getValue(0);
153         } catch (IndexOutOfBoundsException JavaDoc ex) {}
154         try {
155             lm.getValue(-1);
156         } catch (IndexOutOfBoundsException JavaDoc ex) {}
157         
158         resetFull();
159         lm = (LinkedMap) map;
160         try {
161             lm.getValue(-1);
162         } catch (IndexOutOfBoundsException JavaDoc ex) {}
163         try {
164             lm.getValue(lm.size());
165         } catch (IndexOutOfBoundsException JavaDoc ex) {}
166         
167         int i = 0;
168         for (MapIterator it = lm.mapIterator(); it.hasNext(); i++) {
169             it.next();
170             assertSame(it.getValue(), lm.getValue(i));
171         }
172     }
173
174     public void testIndexOf() {
175         resetEmpty();
176         LinkedMap lm = (LinkedMap) map;
177         assertEquals(-1, lm.indexOf(getOtherKeys()));
178         
179         resetFull();
180         lm = (LinkedMap) map;
181         List JavaDoc list = new ArrayList JavaDoc();
182         for (MapIterator it = lm.mapIterator(); it.hasNext();) {
183             list.add(it.next());
184         }
185         for (int i = 0; i < list.size(); i++) {
186             assertEquals(i, lm.indexOf(list.get(i)));
187         }
188     }
189
190     public void testRemoveByIndex() {
191         resetEmpty();
192         LinkedMap lm = (LinkedMap) map;
193         try {
194             lm.remove(0);
195         } catch (IndexOutOfBoundsException JavaDoc ex) {}
196         try {
197             lm.remove(-1);
198         } catch (IndexOutOfBoundsException JavaDoc ex) {}
199         
200         resetFull();
201         lm = (LinkedMap) map;
202         try {
203             lm.remove(-1);
204         } catch (IndexOutOfBoundsException JavaDoc ex) {}
205         try {
206             lm.remove(lm.size());
207         } catch (IndexOutOfBoundsException JavaDoc ex) {}
208         
209         List JavaDoc list = new ArrayList JavaDoc();
210         for (MapIterator it = lm.mapIterator(); it.hasNext();) {
211             list.add(it.next());
212         }
213         for (int i = 0; i < list.size(); i++) {
214             Object JavaDoc key = list.get(i);
215             Object JavaDoc value = lm.get(key);
216             assertEquals(value, lm.remove(i));
217             list.remove(i);
218             assertEquals(false, lm.containsKey(key));
219         }
220     }
221     
222     public BulkTest bulkTestListView() {
223         return new TestListView();
224     }
225     
226     public class TestListView extends AbstractTestList {
227         
228         TestListView() {
229             super("TestListView");
230         }
231
232         public List JavaDoc makeEmptyList() {
233             return ((LinkedMap) TestLinkedMap.this.makeEmptyMap()).asList();
234         }
235         
236         public List JavaDoc makeFullList() {
237             return ((LinkedMap) TestLinkedMap.this.makeFullMap()).asList();
238         }
239         
240         public Object JavaDoc[] getFullElements() {
241             return TestLinkedMap.this.getSampleKeys();
242         }
243         public boolean isAddSupported() {
244             return false;
245         }
246         public boolean isRemoveSupported() {
247             return false;
248         }
249         public boolean isSetSupported() {
250             return false;
251         }
252         public boolean isNullSupported() {
253             return TestLinkedMap.this.isAllowNullKey();
254         }
255         public boolean isTestSerialization() {
256             return false;
257         }
258     }
259
260     public void testClone() {
261         LinkedMap map = new LinkedMap(10);
262         map.put("1", "1");
263         Map JavaDoc cloned = (Map JavaDoc) map.clone();
264         assertEquals(map.size(), cloned.size());
265         assertSame(map.get("1"), cloned.get("1"));
266     }
267     
268 // public void testCreate() throws Exception {
269
// resetEmpty();
270
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/LinkedMap.emptyCollection.version3.obj");
271
// resetFull();
272
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/LinkedMap.fullCollection.version3.obj");
273
// }
274
}
275
Popular Tags