001: /*
002: * Copyright 2001-2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.map;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import junit.framework.Test;
024:
025: import org.apache.commons.collections.BulkTest;
026: import org.apache.commons.collections.MapIterator;
027: import org.apache.commons.collections.list.AbstractTestList;
028:
029: /**
030: * Extension of {@link TestMap} for exercising the {@link ListOrderedMap}
031: * implementation.
032: *
033: * @since Commons Collections 3.0
034: * @version $Revision: 365406 $ $Date: 2006-01-02 19:34:53 +0000 (Mon, 02 Jan 2006) $
035: *
036: * @author Henri Yandell
037: * @author Stephen Colebourne
038: * @author Matt Benson
039: */
040: public class TestListOrderedMap extends AbstractTestOrderedMap {
041:
042: public TestListOrderedMap(String testName) {
043: super (testName);
044: }
045:
046: public static Test suite() {
047: return BulkTest.makeSuite(TestListOrderedMap.class);
048: }
049:
050: public static void main(String args[]) {
051: String[] testCaseName = { TestListOrderedMap.class.getName() };
052: junit.textui.TestRunner.main(testCaseName);
053: }
054:
055: public Map makeEmptyMap() {
056: return ListOrderedMap.decorate(new HashMap());
057: }
058:
059: //-----------------------------------------------------------------------
060: public void testGetByIndex() {
061: resetEmpty();
062: ListOrderedMap lom = (ListOrderedMap) map;
063: try {
064: lom.get(0);
065: } catch (IndexOutOfBoundsException ex) {
066: }
067: try {
068: lom.get(-1);
069: } catch (IndexOutOfBoundsException ex) {
070: }
071:
072: resetFull();
073: lom = (ListOrderedMap) map;
074: try {
075: lom.get(-1);
076: } catch (IndexOutOfBoundsException ex) {
077: }
078: try {
079: lom.get(lom.size());
080: } catch (IndexOutOfBoundsException ex) {
081: }
082:
083: int i = 0;
084: for (MapIterator it = lom.mapIterator(); it.hasNext(); i++) {
085: assertSame(it.next(), lom.get(i));
086: }
087: }
088:
089: public void testGetValueByIndex() {
090: resetEmpty();
091: ListOrderedMap lom = (ListOrderedMap) map;
092: try {
093: lom.getValue(0);
094: } catch (IndexOutOfBoundsException ex) {
095: }
096: try {
097: lom.getValue(-1);
098: } catch (IndexOutOfBoundsException ex) {
099: }
100:
101: resetFull();
102: lom = (ListOrderedMap) map;
103: try {
104: lom.getValue(-1);
105: } catch (IndexOutOfBoundsException ex) {
106: }
107: try {
108: lom.getValue(lom.size());
109: } catch (IndexOutOfBoundsException ex) {
110: }
111:
112: int i = 0;
113: for (MapIterator it = lom.mapIterator(); it.hasNext(); i++) {
114: it.next();
115: assertSame(it.getValue(), lom.getValue(i));
116: }
117: }
118:
119: public void testIndexOf() {
120: resetEmpty();
121: ListOrderedMap lom = (ListOrderedMap) map;
122: assertEquals(-1, lom.indexOf(getOtherKeys()));
123:
124: resetFull();
125: lom = (ListOrderedMap) map;
126: List list = new ArrayList();
127: for (MapIterator it = lom.mapIterator(); it.hasNext();) {
128: list.add(it.next());
129: }
130: for (int i = 0; i < list.size(); i++) {
131: assertEquals(i, lom.indexOf(list.get(i)));
132: }
133: }
134:
135: public void testSetValueByIndex() {
136: resetEmpty();
137: ListOrderedMap lom = (ListOrderedMap) map;
138: try {
139: lom.setValue(0, "");
140: } catch (IndexOutOfBoundsException ex) {
141: }
142: try {
143: lom.setValue(-1, "");
144: } catch (IndexOutOfBoundsException ex) {
145: }
146:
147: resetFull();
148: lom = (ListOrderedMap) map;
149: try {
150: lom.setValue(-1, "");
151: } catch (IndexOutOfBoundsException ex) {
152: }
153: try {
154: lom.setValue(lom.size(), "");
155: } catch (IndexOutOfBoundsException ex) {
156: }
157:
158: for (int i = 0; i < lom.size(); i++) {
159: Object value = lom.getValue(i);
160: Object input = new Integer(i);
161: assertEquals(value, lom.setValue(i, input));
162: assertEquals(input, lom.getValue(i));
163: }
164: }
165:
166: public void testRemoveByIndex() {
167: resetEmpty();
168: ListOrderedMap lom = (ListOrderedMap) map;
169: try {
170: lom.remove(0);
171: } catch (IndexOutOfBoundsException ex) {
172: }
173: try {
174: lom.remove(-1);
175: } catch (IndexOutOfBoundsException ex) {
176: }
177:
178: resetFull();
179: lom = (ListOrderedMap) map;
180: try {
181: lom.remove(-1);
182: } catch (IndexOutOfBoundsException ex) {
183: }
184: try {
185: lom.remove(lom.size());
186: } catch (IndexOutOfBoundsException ex) {
187: }
188:
189: List list = new ArrayList();
190: for (MapIterator it = lom.mapIterator(); it.hasNext();) {
191: list.add(it.next());
192: }
193: for (int i = 0; i < list.size(); i++) {
194: Object key = list.get(i);
195: Object value = lom.get(key);
196: assertEquals(value, lom.remove(i));
197: list.remove(i);
198: assertEquals(false, lom.containsKey(key));
199: }
200: }
201:
202: public void testPut_intObjectObject() {
203: resetEmpty();
204: ListOrderedMap lom = (ListOrderedMap) map;
205:
206: try {
207: lom.put(1, "testInsert1", "testInsert1v");
208: fail("should not be able to insert at pos 1 in empty Map");
209: } catch (IndexOutOfBoundsException ex) {
210: }
211: try {
212: lom.put(-1, "testInsert-1", "testInsert-1v");
213: fail("should not be able to insert at pos -1 in empty Map");
214: } catch (IndexOutOfBoundsException ex) {
215: }
216:
217: // put where key doesn't exist
218: lom.put(0, "testInsert1", "testInsert1v");
219: assertEquals("testInsert1v", lom.getValue(0));
220:
221: lom.put("testInsertPut", "testInsertPutv");
222: assertEquals("testInsert1v", lom.getValue(0));
223: assertEquals("testInsertPutv", lom.getValue(1));
224:
225: lom.put(0, "testInsert0", "testInsert0v");
226: assertEquals("testInsert0v", lom.getValue(0));
227: assertEquals("testInsert1v", lom.getValue(1));
228: assertEquals("testInsertPutv", lom.getValue(2));
229:
230: lom.put(3, "testInsert3", "testInsert3v");
231: assertEquals("testInsert0v", lom.getValue(0));
232: assertEquals("testInsert1v", lom.getValue(1));
233: assertEquals("testInsertPutv", lom.getValue(2));
234: assertEquals("testInsert3v", lom.getValue(3));
235:
236: // put in a full map
237: resetFull();
238: lom = (ListOrderedMap) map;
239: ListOrderedMap lom2 = new ListOrderedMap();
240: lom2.putAll(lom);
241:
242: lom2.put(0, "testInsert0", "testInsert0v");
243: assertEquals("testInsert0v", lom2.getValue(0));
244: for (int i = 0; i < lom.size(); i++) {
245: assertEquals(lom2.getValue(i + 1), lom.getValue(i));
246: }
247:
248: // put where key does exist
249: Integer i1 = new Integer(1);
250: Integer i1b = new Integer(1);
251: Integer i2 = new Integer(2);
252: Integer i3 = new Integer(3);
253:
254: resetEmpty();
255: lom = (ListOrderedMap) map;
256: lom.put(i1, "1");
257: lom.put(i2, "2");
258: lom.put(i3, "3");
259: lom.put(0, i1, "One");
260: assertEquals(3, lom.size());
261: assertEquals(3, lom.map.size());
262: assertEquals(3, lom.insertOrder.size());
263: assertEquals("One", lom.getValue(0));
264: assertSame(i1, lom.get(0));
265:
266: resetEmpty();
267: lom = (ListOrderedMap) map;
268: lom.put(i1, "1");
269: lom.put(i2, "2");
270: lom.put(i3, "3");
271: lom.put(0, i1b, "One");
272: assertEquals(3, lom.size());
273: assertEquals(3, lom.map.size());
274: assertEquals(3, lom.insertOrder.size());
275: assertEquals("One", lom.getValue(0));
276: assertEquals("2", lom.getValue(1));
277: assertEquals("3", lom.getValue(2));
278: assertSame(i1b, lom.get(0));
279:
280: resetEmpty();
281: lom = (ListOrderedMap) map;
282: lom.put(i1, "1");
283: lom.put(i2, "2");
284: lom.put(i3, "3");
285: lom.put(1, i1b, "One");
286: assertEquals(3, lom.size());
287: assertEquals(3, lom.map.size());
288: assertEquals(3, lom.insertOrder.size());
289: assertEquals("One", lom.getValue(0));
290: assertEquals("2", lom.getValue(1));
291: assertEquals("3", lom.getValue(2));
292:
293: resetEmpty();
294: lom = (ListOrderedMap) map;
295: lom.put(i1, "1");
296: lom.put(i2, "2");
297: lom.put(i3, "3");
298: lom.put(2, i1b, "One");
299: assertEquals(3, lom.size());
300: assertEquals(3, lom.map.size());
301: assertEquals(3, lom.insertOrder.size());
302: assertEquals("2", lom.getValue(0));
303: assertEquals("One", lom.getValue(1));
304: assertEquals("3", lom.getValue(2));
305:
306: resetEmpty();
307: lom = (ListOrderedMap) map;
308: lom.put(i1, "1");
309: lom.put(i2, "2");
310: lom.put(i3, "3");
311: lom.put(3, i1b, "One");
312: assertEquals(3, lom.size());
313: assertEquals(3, lom.map.size());
314: assertEquals(3, lom.insertOrder.size());
315: assertEquals("2", lom.getValue(0));
316: assertEquals("3", lom.getValue(1));
317: assertEquals("One", lom.getValue(2));
318: }
319:
320: //-----------------------------------------------------------------------
321: public void testValueList_getByIndex() {
322: resetFull();
323: ListOrderedMap lom = (ListOrderedMap) map;
324: for (int i = 0; i < lom.size(); i++) {
325: Object expected = lom.getValue(i);
326: assertEquals(expected, lom.valueList().get(i));
327: }
328: }
329:
330: public void testValueList_setByIndex() {
331: resetFull();
332: ListOrderedMap lom = (ListOrderedMap) map;
333: for (int i = 0; i < lom.size(); i++) {
334: Object input = new Integer(i);
335: Object expected = lom.getValue(i);
336: assertEquals(expected, lom.valueList().set(i, input));
337: assertEquals(input, lom.getValue(i));
338: assertEquals(input, lom.valueList().get(i));
339: }
340: }
341:
342: public void testValueList_removeByIndex() {
343: resetFull();
344: ListOrderedMap lom = (ListOrderedMap) map;
345: while (lom.size() > 1) {
346: Object expected = lom.getValue(1);
347: assertEquals(expected, lom.valueList().remove(1));
348: }
349: }
350:
351: //-----------------------------------------------------------------------
352: public BulkTest bulkTestKeyListView() {
353: return new TestKeyListView();
354: }
355:
356: public BulkTest bulkTestValueListView() {
357: return new TestValueListView();
358: }
359:
360: //-----------------------------------------------------------------------
361: public class TestKeyListView extends AbstractTestList {
362: TestKeyListView() {
363: super ("TestKeyListView");
364: }
365:
366: public List makeEmptyList() {
367: return ((ListOrderedMap) TestListOrderedMap.this
368: .makeEmptyMap()).keyList();
369: }
370:
371: public List makeFullList() {
372: return ((ListOrderedMap) TestListOrderedMap.this
373: .makeFullMap()).keyList();
374: }
375:
376: public Object[] getFullElements() {
377: return TestListOrderedMap.this .getSampleKeys();
378: }
379:
380: public boolean isAddSupported() {
381: return false;
382: }
383:
384: public boolean isRemoveSupported() {
385: return false;
386: }
387:
388: public boolean isSetSupported() {
389: return false;
390: }
391:
392: public boolean isNullSupported() {
393: return TestListOrderedMap.this .isAllowNullKey();
394: }
395:
396: public boolean isTestSerialization() {
397: return false;
398: }
399: }
400:
401: //-----------------------------------------------------------------------
402: public class TestValueListView extends AbstractTestList {
403: TestValueListView() {
404: super ("TestValueListView");
405: }
406:
407: public List makeEmptyList() {
408: return ((ListOrderedMap) TestListOrderedMap.this
409: .makeEmptyMap()).valueList();
410: }
411:
412: public List makeFullList() {
413: return ((ListOrderedMap) TestListOrderedMap.this
414: .makeFullMap()).valueList();
415: }
416:
417: public Object[] getFullElements() {
418: return TestListOrderedMap.this .getSampleValues();
419: }
420:
421: public boolean isAddSupported() {
422: return false;
423: }
424:
425: public boolean isRemoveSupported() {
426: return true;
427: }
428:
429: public boolean isSetSupported() {
430: return true;
431: }
432:
433: public boolean isNullSupported() {
434: return TestListOrderedMap.this .isAllowNullKey();
435: }
436:
437: public boolean isTestSerialization() {
438: return false;
439: }
440: }
441:
442: //-----------------------------------------------------------------------
443: public String getCompatibilityVersion() {
444: return "3.1";
445: }
446:
447: // public void testCreate() throws Exception {
448: // resetEmpty();
449: // writeExternalFormToDisk(
450: // (java.io.Serializable) map,
451: // "D:/dev/collections/data/test/ListOrderedMap.emptyCollection.version3.1.obj");
452: // resetFull();
453: // writeExternalFormToDisk(
454: // (java.io.Serializable) map,
455: // "D:/dev/collections/data/test/ListOrderedMap.fullCollection.version3.1.obj");
456: // }
457: }
|