KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import junit.framework.Test;
24
25 import org.apache.commons.collections.BulkTest;
26 import org.apache.commons.collections.MapIterator;
27 import org.apache.commons.collections.list.AbstractTestList;
28
29 /**
30  * Extension of {@link TestMap} for exercising the {@link ListOrderedMap}
31  * implementation.
32  *
33  * @since Commons Collections 3.0
34  * @version $Revision: 1.10 $ $Date: 2004/05/31 22:39:20 $
35  *
36  * @author Henri Yandell
37  * @author Stephen Colebourne
38  */

39 public class TestListOrderedMap extends AbstractTestOrderedMap {
40
41     public TestListOrderedMap(String JavaDoc testName) {
42         super(testName);
43     }
44
45     public static Test suite() {
46         return BulkTest.makeSuite(TestListOrderedMap.class);
47     }
48
49     public static void main(String JavaDoc args[]) {
50         String JavaDoc[] testCaseName = { TestListOrderedMap.class.getName()};
51         junit.textui.TestRunner.main(testCaseName);
52     }
53
54     public Map JavaDoc makeEmptyMap() {
55         return ListOrderedMap.decorate(new HashMap JavaDoc());
56     }
57     
58     //-----------------------------------------------------------------------
59
public void testGetByIndex() {
60         resetEmpty();
61         ListOrderedMap lom = (ListOrderedMap) map;
62         try {
63             lom.get(0);
64         } catch (IndexOutOfBoundsException JavaDoc ex) {}
65         try {
66             lom.get(-1);
67         } catch (IndexOutOfBoundsException JavaDoc ex) {}
68         
69         resetFull();
70         lom = (ListOrderedMap) map;
71         try {
72             lom.get(-1);
73         } catch (IndexOutOfBoundsException JavaDoc ex) {}
74         try {
75             lom.get(lom.size());
76         } catch (IndexOutOfBoundsException JavaDoc ex) {}
77         
78         int i = 0;
79         for (MapIterator it = lom.mapIterator(); it.hasNext(); i++) {
80             assertSame(it.next(), lom.get(i));
81         }
82     }
83
84     public void testGetValueByIndex() {
85         resetEmpty();
86         ListOrderedMap lom = (ListOrderedMap) map;
87         try {
88             lom.getValue(0);
89         } catch (IndexOutOfBoundsException JavaDoc ex) {}
90         try {
91             lom.getValue(-1);
92         } catch (IndexOutOfBoundsException JavaDoc ex) {}
93         
94         resetFull();
95         lom = (ListOrderedMap) map;
96         try {
97             lom.getValue(-1);
98         } catch (IndexOutOfBoundsException JavaDoc ex) {}
99         try {
100             lom.getValue(lom.size());
101         } catch (IndexOutOfBoundsException JavaDoc ex) {}
102         
103         int i = 0;
104         for (MapIterator it = lom.mapIterator(); it.hasNext(); i++) {
105             it.next();
106             assertSame(it.getValue(), lom.getValue(i));
107         }
108     }
109
110     public void testIndexOf() {
111         resetEmpty();
112         ListOrderedMap lom = (ListOrderedMap) map;
113         assertEquals(-1, lom.indexOf(getOtherKeys()));
114         
115         resetFull();
116         lom = (ListOrderedMap) map;
117         List JavaDoc list = new ArrayList JavaDoc();
118         for (MapIterator it = lom.mapIterator(); it.hasNext();) {
119             list.add(it.next());
120         }
121         for (int i = 0; i < list.size(); i++) {
122             assertEquals(i, lom.indexOf(list.get(i)));
123         }
124     }
125
126     public void testRemoveByIndex() {
127         resetEmpty();
128         ListOrderedMap lom = (ListOrderedMap) map;
129         try {
130             lom.remove(0);
131         } catch (IndexOutOfBoundsException JavaDoc ex) {}
132         try {
133             lom.remove(-1);
134         } catch (IndexOutOfBoundsException JavaDoc ex) {}
135         
136         resetFull();
137         lom = (ListOrderedMap) map;
138         try {
139             lom.remove(-1);
140         } catch (IndexOutOfBoundsException JavaDoc ex) {}
141         try {
142             lom.remove(lom.size());
143         } catch (IndexOutOfBoundsException JavaDoc ex) {}
144         
145         List JavaDoc list = new ArrayList JavaDoc();
146         for (MapIterator it = lom.mapIterator(); it.hasNext();) {
147             list.add(it.next());
148         }
149         for (int i = 0; i < list.size(); i++) {
150             Object JavaDoc key = list.get(i);
151             Object JavaDoc value = lom.get(key);
152             assertEquals(value, lom.remove(i));
153             list.remove(i);
154             assertEquals(false, lom.containsKey(key));
155         }
156     }
157     
158     public BulkTest bulkTestListView() {
159         return new TestListView();
160     }
161     
162     public class TestListView extends AbstractTestList {
163         
164         TestListView() {
165             super("TestListView");
166         }
167
168         public List JavaDoc makeEmptyList() {
169             return ((ListOrderedMap) TestListOrderedMap.this.makeEmptyMap()).asList();
170         }
171         
172         public List JavaDoc makeFullList() {
173             return ((ListOrderedMap) TestListOrderedMap.this.makeFullMap()).asList();
174         }
175         
176         public Object JavaDoc[] getFullElements() {
177             return TestListOrderedMap.this.getSampleKeys();
178         }
179         public boolean isAddSupported() {
180             return false;
181         }
182         public boolean isRemoveSupported() {
183             return false;
184         }
185         public boolean isSetSupported() {
186             return false;
187         }
188         public boolean isNullSupported() {
189             return TestListOrderedMap.this.isAllowNullKey();
190         }
191         public boolean isTestSerialization() {
192             return false;
193         }
194     }
195
196     public String JavaDoc getCompatibilityVersion() {
197         return "3.1";
198     }
199
200 // public void testCreate() throws Exception {
201
// resetEmpty();
202
// writeExternalFormToDisk(
203
// (java.io.Serializable) map,
204
// "D:/dev/collections/data/test/ListOrderedMap.emptyCollection.version3.1.obj");
205
// resetFull();
206
// writeExternalFormToDisk(
207
// (java.io.Serializable) map,
208
// "D:/dev/collections/data/test/ListOrderedMap.fullCollection.version3.1.obj");
209
// }
210
}
211
Popular Tags