001: /*
002: * Copyright 2004 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.List;
020: import java.util.Map;
021:
022: import junit.framework.Test;
023:
024: import org.apache.commons.collections.BulkTest;
025: import org.apache.commons.collections.MapIterator;
026: import org.apache.commons.collections.list.AbstractTestList;
027:
028: /**
029: * Extension of {@link TestMap} for exercising the {@link ListOrderedMap}
030: * implementation.
031: *
032: * @since Commons Collections 3.1
033: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
034: *
035: * @author Henri Yandell
036: * @author Stephen Colebourne
037: */
038: public class TestListOrderedMap2 extends AbstractTestOrderedMap {
039:
040: public TestListOrderedMap2(String testName) {
041: super (testName);
042: }
043:
044: public static Test suite() {
045: return BulkTest.makeSuite(TestListOrderedMap2.class);
046: }
047:
048: public static void main(String args[]) {
049: String[] testCaseName = { TestListOrderedMap2.class.getName() };
050: junit.textui.TestRunner.main(testCaseName);
051: }
052:
053: public Map makeEmptyMap() {
054: return new ListOrderedMap();
055: }
056:
057: //-----------------------------------------------------------------------
058: public void testGetByIndex() {
059: resetEmpty();
060: ListOrderedMap lom = (ListOrderedMap) map;
061: try {
062: lom.get(0);
063: } catch (IndexOutOfBoundsException ex) {
064: }
065: try {
066: lom.get(-1);
067: } catch (IndexOutOfBoundsException ex) {
068: }
069:
070: resetFull();
071: lom = (ListOrderedMap) map;
072: try {
073: lom.get(-1);
074: } catch (IndexOutOfBoundsException ex) {
075: }
076: try {
077: lom.get(lom.size());
078: } catch (IndexOutOfBoundsException ex) {
079: }
080:
081: int i = 0;
082: for (MapIterator it = lom.mapIterator(); it.hasNext(); i++) {
083: assertSame(it.next(), lom.get(i));
084: }
085: }
086:
087: public void testGetValueByIndex() {
088: resetEmpty();
089: ListOrderedMap lom = (ListOrderedMap) map;
090: try {
091: lom.getValue(0);
092: } catch (IndexOutOfBoundsException ex) {
093: }
094: try {
095: lom.getValue(-1);
096: } catch (IndexOutOfBoundsException ex) {
097: }
098:
099: resetFull();
100: lom = (ListOrderedMap) map;
101: try {
102: lom.getValue(-1);
103: } catch (IndexOutOfBoundsException ex) {
104: }
105: try {
106: lom.getValue(lom.size());
107: } catch (IndexOutOfBoundsException ex) {
108: }
109:
110: int i = 0;
111: for (MapIterator it = lom.mapIterator(); it.hasNext(); i++) {
112: it.next();
113: assertSame(it.getValue(), lom.getValue(i));
114: }
115: }
116:
117: public void testIndexOf() {
118: resetEmpty();
119: ListOrderedMap lom = (ListOrderedMap) map;
120: assertEquals(-1, lom.indexOf(getOtherKeys()));
121:
122: resetFull();
123: lom = (ListOrderedMap) map;
124: List list = new ArrayList();
125: for (MapIterator it = lom.mapIterator(); it.hasNext();) {
126: list.add(it.next());
127: }
128: for (int i = 0; i < list.size(); i++) {
129: assertEquals(i, lom.indexOf(list.get(i)));
130: }
131: }
132:
133: public void testRemoveByIndex() {
134: resetEmpty();
135: ListOrderedMap lom = (ListOrderedMap) map;
136: try {
137: lom.remove(0);
138: } catch (IndexOutOfBoundsException ex) {
139: }
140: try {
141: lom.remove(-1);
142: } catch (IndexOutOfBoundsException ex) {
143: }
144:
145: resetFull();
146: lom = (ListOrderedMap) map;
147: try {
148: lom.remove(-1);
149: } catch (IndexOutOfBoundsException ex) {
150: }
151: try {
152: lom.remove(lom.size());
153: } catch (IndexOutOfBoundsException ex) {
154: }
155:
156: List list = new ArrayList();
157: for (MapIterator it = lom.mapIterator(); it.hasNext();) {
158: list.add(it.next());
159: }
160: for (int i = 0; i < list.size(); i++) {
161: Object key = list.get(i);
162: Object value = lom.get(key);
163: assertEquals(value, lom.remove(i));
164: list.remove(i);
165: assertEquals(false, lom.containsKey(key));
166: }
167: }
168:
169: public BulkTest bulkTestListView() {
170: return new TestListView();
171: }
172:
173: public class TestListView extends AbstractTestList {
174:
175: TestListView() {
176: super ("TestListView");
177: }
178:
179: public List makeEmptyList() {
180: return ((ListOrderedMap) TestListOrderedMap2.this
181: .makeEmptyMap()).asList();
182: }
183:
184: public List makeFullList() {
185: return ((ListOrderedMap) TestListOrderedMap2.this
186: .makeFullMap()).asList();
187: }
188:
189: public Object[] getFullElements() {
190: return TestListOrderedMap2.this .getSampleKeys();
191: }
192:
193: public boolean isAddSupported() {
194: return false;
195: }
196:
197: public boolean isRemoveSupported() {
198: return false;
199: }
200:
201: public boolean isSetSupported() {
202: return false;
203: }
204:
205: public boolean isNullSupported() {
206: return TestListOrderedMap2.this .isAllowNullKey();
207: }
208:
209: public boolean isTestSerialization() {
210: return false;
211: }
212: }
213:
214: public String getCompatibilityVersion() {
215: return "3.1";
216: }
217:
218: // public void testCreate() throws Exception {
219: // resetEmpty();
220: // writeExternalFormToDisk(
221: // (java.io.Serializable) map,
222: // "D:/dev/collections/data/test/ListOrderedMap.emptyCollection.version3.1.obj");
223: // resetFull();
224: // writeExternalFormToDisk(
225: // (java.io.Serializable) map,
226: // "D:/dev/collections/data/test/ListOrderedMap.fullCollection.version3.1.obj");
227: // }
228: }
|