KickJava   Java API By Example, From Geeks To Geeks.

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


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

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