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.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.util.Map;
023:
024: import junit.framework.Test;
025: import junit.textui.TestRunner;
026:
027: import org.apache.commons.collections.BulkTest;
028: import org.apache.commons.collections.MapIterator;
029: import org.apache.commons.collections.iterators.AbstractTestMapIterator;
030:
031: /**
032: * JUnit tests.
033: *
034: * @version $Revision: 170210 $ $Date: 2005-05-15 10:22:50 +0100 (Sun, 15 May 2005) $
035: *
036: * @author Stephen Colebourne
037: */
038: public class TestFlat3Map extends AbstractTestIterableMap {
039:
040: private static final Integer ONE = new Integer(1);
041: private static final Integer TWO = new Integer(2);
042: private static final String TEN = "10";
043: private static final String TWENTY = "20";
044:
045: public TestFlat3Map(String testName) {
046: super (testName);
047: }
048:
049: public static void main(String[] args) {
050: TestRunner.run(suite());
051: }
052:
053: public static Test suite() {
054: return BulkTest.makeSuite(TestFlat3Map.class);
055: }
056:
057: public Map makeEmptyMap() {
058: return new Flat3Map();
059: }
060:
061: //-----------------------------------------------------------------------
062: public void testEquals1() {
063: Flat3Map map1 = new Flat3Map();
064: map1.put("a", "testA");
065: map1.put("b", "testB");
066: Flat3Map map2 = new Flat3Map();
067: map2.put("a", "testB");
068: map2.put("b", "testA");
069: assertEquals(false, map1.equals(map2));
070: }
071:
072: public void testEquals2() {
073: Flat3Map map1 = new Flat3Map();
074: map1.put("a", "testA");
075: map1.put("b", "testB");
076: Flat3Map map2 = new Flat3Map();
077: map2.put("a", "testB");
078: map2.put("c", "testA");
079: assertEquals(false, map1.equals(map2));
080: }
081:
082: public void testClone2() {
083: Flat3Map map = new Flat3Map();
084: assertEquals(0, map.size());
085: map.put(ONE, TEN);
086: map.put(TWO, TWENTY);
087: assertEquals(2, map.size());
088: assertEquals(true, map.containsKey(ONE));
089: assertEquals(true, map.containsKey(TWO));
090: assertSame(TEN, map.get(ONE));
091: assertSame(TWENTY, map.get(TWO));
092:
093: // clone works (size = 2)
094: Flat3Map cloned = (Flat3Map) map.clone();
095: assertEquals(2, cloned.size());
096: assertEquals(true, cloned.containsKey(ONE));
097: assertEquals(true, cloned.containsKey(TWO));
098: assertSame(TEN, cloned.get(ONE));
099: assertSame(TWENTY, cloned.get(TWO));
100:
101: // change original doesn't change clone
102: map.put(TEN, ONE);
103: map.put(TWENTY, TWO);
104: assertEquals(4, map.size());
105: assertEquals(2, cloned.size());
106: assertEquals(true, cloned.containsKey(ONE));
107: assertEquals(true, cloned.containsKey(TWO));
108: assertSame(TEN, cloned.get(ONE));
109: assertSame(TWENTY, cloned.get(TWO));
110: }
111:
112: public void testClone4() {
113: Flat3Map map = new Flat3Map();
114: assertEquals(0, map.size());
115: map.put(ONE, TEN);
116: map.put(TWO, TWENTY);
117: map.put(TEN, ONE);
118: map.put(TWENTY, TWO);
119:
120: // clone works (size = 4)
121: Flat3Map cloned = (Flat3Map) map.clone();
122: assertEquals(4, map.size());
123: assertEquals(4, cloned.size());
124: assertEquals(true, cloned.containsKey(ONE));
125: assertEquals(true, cloned.containsKey(TWO));
126: assertEquals(true, cloned.containsKey(TEN));
127: assertEquals(true, cloned.containsKey(TWENTY));
128: assertSame(TEN, cloned.get(ONE));
129: assertSame(TWENTY, cloned.get(TWO));
130: assertSame(ONE, cloned.get(TEN));
131: assertSame(TWO, cloned.get(TWENTY));
132:
133: // change original doesn't change clone
134: map.clear();
135: assertEquals(0, map.size());
136: assertEquals(4, cloned.size());
137: assertEquals(true, cloned.containsKey(ONE));
138: assertEquals(true, cloned.containsKey(TWO));
139: assertEquals(true, cloned.containsKey(TEN));
140: assertEquals(true, cloned.containsKey(TWENTY));
141: assertSame(TEN, cloned.get(ONE));
142: assertSame(TWENTY, cloned.get(TWO));
143: assertSame(ONE, cloned.get(TEN));
144: assertSame(TWO, cloned.get(TWENTY));
145: }
146:
147: public void testSerialisation0() throws Exception {
148: Flat3Map map = new Flat3Map();
149: ByteArrayOutputStream bout = new ByteArrayOutputStream();
150: ObjectOutputStream out = new ObjectOutputStream(bout);
151: out.writeObject(map);
152: byte[] bytes = bout.toByteArray();
153: out.close();
154: ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
155: ObjectInputStream in = new ObjectInputStream(bin);
156: Flat3Map ser = (Flat3Map) in.readObject();
157: in.close();
158: assertEquals(0, map.size());
159: assertEquals(0, ser.size());
160: }
161:
162: public void testSerialisation2() throws Exception {
163: Flat3Map map = new Flat3Map();
164: map.put(ONE, TEN);
165: map.put(TWO, TWENTY);
166:
167: ByteArrayOutputStream bout = new ByteArrayOutputStream();
168: ObjectOutputStream out = new ObjectOutputStream(bout);
169: out.writeObject(map);
170: byte[] bytes = bout.toByteArray();
171: out.close();
172: ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
173: ObjectInputStream in = new ObjectInputStream(bin);
174: Flat3Map ser = (Flat3Map) in.readObject();
175: in.close();
176: assertEquals(2, map.size());
177: assertEquals(2, ser.size());
178: assertEquals(true, ser.containsKey(ONE));
179: assertEquals(true, ser.containsKey(TWO));
180: assertEquals(TEN, ser.get(ONE));
181: assertEquals(TWENTY, ser.get(TWO));
182: }
183:
184: public void testSerialisation4() throws Exception {
185: Flat3Map map = new Flat3Map();
186: map.put(ONE, TEN);
187: map.put(TWO, TWENTY);
188: map.put(TEN, ONE);
189: map.put(TWENTY, TWO);
190:
191: ByteArrayOutputStream bout = new ByteArrayOutputStream();
192: ObjectOutputStream out = new ObjectOutputStream(bout);
193: out.writeObject(map);
194: byte[] bytes = bout.toByteArray();
195: out.close();
196: ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
197: ObjectInputStream in = new ObjectInputStream(bin);
198: Flat3Map ser = (Flat3Map) in.readObject();
199: in.close();
200: assertEquals(4, map.size());
201: assertEquals(4, ser.size());
202: assertEquals(true, ser.containsKey(ONE));
203: assertEquals(true, ser.containsKey(TWO));
204: assertEquals(true, ser.containsKey(TEN));
205: assertEquals(true, ser.containsKey(TWENTY));
206: assertEquals(TEN, ser.get(ONE));
207: assertEquals(TWENTY, ser.get(TWO));
208: assertEquals(ONE, ser.get(TEN));
209: assertEquals(TWO, ser.get(TWENTY));
210: }
211:
212: //-----------------------------------------------------------------------
213: public BulkTest bulkTestMapIterator() {
214: return new TestFlatMapIterator();
215: }
216:
217: public class TestFlatMapIterator extends AbstractTestMapIterator {
218: public TestFlatMapIterator() {
219: super ("TestFlatMapIterator");
220: }
221:
222: public Object[] addSetValues() {
223: return TestFlat3Map.this .getNewSampleValues();
224: }
225:
226: public boolean supportsRemove() {
227: return TestFlat3Map.this .isRemoveSupported();
228: }
229:
230: public boolean supportsSetValue() {
231: return TestFlat3Map.this .isSetValueSupported();
232: }
233:
234: public MapIterator makeEmptyMapIterator() {
235: resetEmpty();
236: return ((Flat3Map) TestFlat3Map.this .map).mapIterator();
237: }
238:
239: public MapIterator makeFullMapIterator() {
240: resetFull();
241: return ((Flat3Map) TestFlat3Map.this .map).mapIterator();
242: }
243:
244: public Map getMap() {
245: // assumes makeFullMapIterator() called first
246: return TestFlat3Map.this .map;
247: }
248:
249: public Map getConfirmedMap() {
250: // assumes makeFullMapIterator() called first
251: return TestFlat3Map.this .confirmed;
252: }
253:
254: public void verify() {
255: super .verify();
256: TestFlat3Map.this .verify();
257: }
258: }
259:
260: public String getCompatibilityVersion() {
261: return "3.1";
262: }
263:
264: // public void testCreate() throws Exception {
265: // resetEmpty();
266: // writeExternalFormToDisk(
267: // (java.io.Serializable) map,
268: // "D:/dev/collections/data/test/Flat3Map.emptyCollection.version3.1.obj");
269: // resetFull();
270: // writeExternalFormToDisk(
271: // (java.io.Serializable) map,
272: // "D:/dev/collections/data/test/Flat3Map.fullCollection.version3.1.obj");
273: // }
274: }
|