001: /*
002: * Copyright 2001-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.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import junit.framework.Test;
024: import junit.textui.TestRunner;
025:
026: import org.apache.commons.collections.BulkTest;
027: import org.apache.commons.collections.MapIterator;
028: import org.apache.commons.collections.OrderedMap;
029: import org.apache.commons.collections.ResettableIterator;
030: import org.apache.commons.collections.list.AbstractTestList;
031:
032: /**
033: * JUnit tests.
034: *
035: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
036: *
037: * @author Stephen Colebourne
038: */
039: public class TestLinkedMap extends AbstractTestOrderedMap {
040:
041: public TestLinkedMap(String testName) {
042: super (testName);
043: }
044:
045: public static void main(String[] args) {
046: TestRunner.run(suite());
047: }
048:
049: public static Test suite() {
050: return BulkTest.makeSuite(TestLinkedMap.class);
051: }
052:
053: public Map makeEmptyMap() {
054: return new LinkedMap();
055: }
056:
057: public String getCompatibilityVersion() {
058: return "3";
059: }
060:
061: //-----------------------------------------------------------------------
062: public void testReset() {
063: resetEmpty();
064: OrderedMap ordered = (OrderedMap) map;
065: ((ResettableIterator) ordered.mapIterator()).reset();
066:
067: resetFull();
068: ordered = (OrderedMap) map;
069: List list = new ArrayList(ordered.keySet());
070: ResettableIterator it = (ResettableIterator) ordered
071: .mapIterator();
072: assertSame(list.get(0), it.next());
073: assertSame(list.get(1), it.next());
074: it.reset();
075: assertSame(list.get(0), it.next());
076: }
077:
078: //-----------------------------------------------------------------------
079: public void testInsertionOrder() {
080: if (isPutAddSupported() == false
081: || isPutChangeSupported() == false)
082: return;
083: Object[] keys = getSampleKeys();
084: Object[] values = getSampleValues();
085: Iterator it = null;
086:
087: resetEmpty();
088: map.put(keys[0], values[0]);
089: map.put(keys[1], values[1]);
090: it = map.keySet().iterator();
091: assertSame(keys[0], it.next());
092: assertSame(keys[1], it.next());
093: it = map.values().iterator();
094: assertSame(values[0], it.next());
095: assertSame(values[1], it.next());
096:
097: // no change to order
098: map.put(keys[1], values[1]);
099: it = map.keySet().iterator();
100: assertSame(keys[0], it.next());
101: assertSame(keys[1], it.next());
102: it = map.values().iterator();
103: assertSame(values[0], it.next());
104: assertSame(values[1], it.next());
105:
106: // no change to order
107: map.put(keys[1], values[2]);
108: it = map.keySet().iterator();
109: assertSame(keys[0], it.next());
110: assertSame(keys[1], it.next());
111: it = map.values().iterator();
112: assertSame(values[0], it.next());
113: assertSame(values[2], it.next());
114:
115: // no change to order
116: map.put(keys[0], values[3]);
117: it = map.keySet().iterator();
118: assertSame(keys[0], it.next());
119: assertSame(keys[1], it.next());
120: it = map.values().iterator();
121: assertSame(values[3], it.next());
122: assertSame(values[2], it.next());
123: }
124:
125: //-----------------------------------------------------------------------
126: public void testGetByIndex() {
127: resetEmpty();
128: LinkedMap lm = (LinkedMap) map;
129: try {
130: lm.get(0);
131: } catch (IndexOutOfBoundsException ex) {
132: }
133: try {
134: lm.get(-1);
135: } catch (IndexOutOfBoundsException ex) {
136: }
137:
138: resetFull();
139: lm = (LinkedMap) map;
140: try {
141: lm.get(-1);
142: } catch (IndexOutOfBoundsException ex) {
143: }
144: try {
145: lm.get(lm.size());
146: } catch (IndexOutOfBoundsException ex) {
147: }
148:
149: int i = 0;
150: for (MapIterator it = lm.mapIterator(); it.hasNext(); i++) {
151: assertSame(it.next(), lm.get(i));
152: }
153: }
154:
155: public void testGetValueByIndex() {
156: resetEmpty();
157: LinkedMap lm = (LinkedMap) map;
158: try {
159: lm.getValue(0);
160: } catch (IndexOutOfBoundsException ex) {
161: }
162: try {
163: lm.getValue(-1);
164: } catch (IndexOutOfBoundsException ex) {
165: }
166:
167: resetFull();
168: lm = (LinkedMap) map;
169: try {
170: lm.getValue(-1);
171: } catch (IndexOutOfBoundsException ex) {
172: }
173: try {
174: lm.getValue(lm.size());
175: } catch (IndexOutOfBoundsException ex) {
176: }
177:
178: int i = 0;
179: for (MapIterator it = lm.mapIterator(); it.hasNext(); i++) {
180: it.next();
181: assertSame(it.getValue(), lm.getValue(i));
182: }
183: }
184:
185: public void testIndexOf() {
186: resetEmpty();
187: LinkedMap lm = (LinkedMap) map;
188: assertEquals(-1, lm.indexOf(getOtherKeys()));
189:
190: resetFull();
191: lm = (LinkedMap) map;
192: List list = new ArrayList();
193: for (MapIterator it = lm.mapIterator(); it.hasNext();) {
194: list.add(it.next());
195: }
196: for (int i = 0; i < list.size(); i++) {
197: assertEquals(i, lm.indexOf(list.get(i)));
198: }
199: }
200:
201: public void testRemoveByIndex() {
202: resetEmpty();
203: LinkedMap lm = (LinkedMap) map;
204: try {
205: lm.remove(0);
206: } catch (IndexOutOfBoundsException ex) {
207: }
208: try {
209: lm.remove(-1);
210: } catch (IndexOutOfBoundsException ex) {
211: }
212:
213: resetFull();
214: lm = (LinkedMap) map;
215: try {
216: lm.remove(-1);
217: } catch (IndexOutOfBoundsException ex) {
218: }
219: try {
220: lm.remove(lm.size());
221: } catch (IndexOutOfBoundsException ex) {
222: }
223:
224: List list = new ArrayList();
225: for (MapIterator it = lm.mapIterator(); it.hasNext();) {
226: list.add(it.next());
227: }
228: for (int i = 0; i < list.size(); i++) {
229: Object key = list.get(i);
230: Object value = lm.get(key);
231: assertEquals(value, lm.remove(i));
232: list.remove(i);
233: assertEquals(false, lm.containsKey(key));
234: }
235: }
236:
237: public BulkTest bulkTestListView() {
238: return new TestListView();
239: }
240:
241: public class TestListView extends AbstractTestList {
242:
243: TestListView() {
244: super ("TestListView");
245: }
246:
247: public List makeEmptyList() {
248: return ((LinkedMap) TestLinkedMap.this .makeEmptyMap())
249: .asList();
250: }
251:
252: public List makeFullList() {
253: return ((LinkedMap) TestLinkedMap.this .makeFullMap())
254: .asList();
255: }
256:
257: public Object[] getFullElements() {
258: return TestLinkedMap.this .getSampleKeys();
259: }
260:
261: public boolean isAddSupported() {
262: return false;
263: }
264:
265: public boolean isRemoveSupported() {
266: return false;
267: }
268:
269: public boolean isSetSupported() {
270: return false;
271: }
272:
273: public boolean isNullSupported() {
274: return TestLinkedMap.this .isAllowNullKey();
275: }
276:
277: public boolean isTestSerialization() {
278: return false;
279: }
280: }
281:
282: public void testClone() {
283: LinkedMap map = new LinkedMap(10);
284: map.put("1", "1");
285: Map cloned = (Map) map.clone();
286: assertEquals(map.size(), cloned.size());
287: assertSame(map.get("1"), cloned.get("1"));
288: }
289:
290: // public void testCreate() throws Exception {
291: // resetEmpty();
292: // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/LinkedMap.emptyCollection.version3.obj");
293: // resetFull();
294: // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/LinkedMap.fullCollection.version3.obj");
295: // }
296: }
|