KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import junit.framework.Test;
25 import junit.textui.TestRunner;
26
27 import org.apache.commons.collections.BulkTest;
28 import org.apache.commons.collections.MapIterator;
29 import org.apache.commons.collections.iterators.AbstractTestMapIterator;
30
31 /**
32  * JUnit tests.
33  *
34  * @version $Revision: 1.7 $ $Date: 2004/03/31 23:18:56 $
35  *
36  * @author Stephen Colebourne
37  */

38 public class TestFlat3Map extends AbstractTestIterableMap {
39
40     private static final Integer JavaDoc ONE = new Integer JavaDoc(1);
41     private static final Integer JavaDoc TWO = new Integer JavaDoc(2);
42     private static final String JavaDoc TEN = "10";
43     private static final String JavaDoc TWENTY = "20";
44         
45     public TestFlat3Map(String JavaDoc testName) {
46         super(testName);
47     }
48
49     public static void main(String JavaDoc[] args) {
50         TestRunner.run(suite());
51     }
52     
53     public static Test suite() {
54         return BulkTest.makeSuite(TestFlat3Map.class);
55     }
56
57     public Map JavaDoc makeEmptyMap() {
58         return new Flat3Map();
59     }
60
61     //-----------------------------------------------------------------------
62
public void testClone2() {
63         Flat3Map map = new Flat3Map();
64         assertEquals(0, map.size());
65         map.put(ONE, TEN);
66         map.put(TWO, TWENTY);
67         assertEquals(2, map.size());
68         assertEquals(true, map.containsKey(ONE));
69         assertEquals(true, map.containsKey(TWO));
70         assertSame(TEN, map.get(ONE));
71         assertSame(TWENTY, map.get(TWO));
72
73         // clone works (size = 2)
74
Flat3Map cloned = (Flat3Map) map.clone();
75         assertEquals(2, cloned.size());
76         assertEquals(true, cloned.containsKey(ONE));
77         assertEquals(true, cloned.containsKey(TWO));
78         assertSame(TEN, cloned.get(ONE));
79         assertSame(TWENTY, cloned.get(TWO));
80         
81         // change original doesn't change clone
82
map.put(TEN, ONE);
83         map.put(TWENTY, TWO);
84         assertEquals(4, map.size());
85         assertEquals(2, cloned.size());
86         assertEquals(true, cloned.containsKey(ONE));
87         assertEquals(true, cloned.containsKey(TWO));
88         assertSame(TEN, cloned.get(ONE));
89         assertSame(TWENTY, cloned.get(TWO));
90     }
91     public void testClone4() {
92         Flat3Map map = new Flat3Map();
93         assertEquals(0, map.size());
94         map.put(ONE, TEN);
95         map.put(TWO, TWENTY);
96         map.put(TEN, ONE);
97         map.put(TWENTY, TWO);
98         
99         // clone works (size = 4)
100
Flat3Map cloned = (Flat3Map) map.clone();
101         assertEquals(4, map.size());
102         assertEquals(4, cloned.size());
103         assertEquals(true, cloned.containsKey(ONE));
104         assertEquals(true, cloned.containsKey(TWO));
105         assertEquals(true, cloned.containsKey(TEN));
106         assertEquals(true, cloned.containsKey(TWENTY));
107         assertSame(TEN, cloned.get(ONE));
108         assertSame(TWENTY, cloned.get(TWO));
109         assertSame(ONE, cloned.get(TEN));
110         assertSame(TWO, cloned.get(TWENTY));
111         
112         // change original doesn't change clone
113
map.clear();
114         assertEquals(0, map.size());
115         assertEquals(4, cloned.size());
116         assertEquals(true, cloned.containsKey(ONE));
117         assertEquals(true, cloned.containsKey(TWO));
118         assertEquals(true, cloned.containsKey(TEN));
119         assertEquals(true, cloned.containsKey(TWENTY));
120         assertSame(TEN, cloned.get(ONE));
121         assertSame(TWENTY, cloned.get(TWO));
122         assertSame(ONE, cloned.get(TEN));
123         assertSame(TWO, cloned.get(TWENTY));
124     }
125     
126     public void testSerialisation0() throws Exception JavaDoc {
127         Flat3Map map = new Flat3Map();
128         ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
129         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(bout);
130         out.writeObject(map);
131         byte[] bytes = bout.toByteArray();
132         out.close();
133         ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(bytes);
134         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bin);
135         Flat3Map ser = (Flat3Map) in.readObject();
136         in.close();
137         assertEquals(0, map.size());
138         assertEquals(0, ser.size());
139     }
140     
141     public void testSerialisation2() throws Exception JavaDoc {
142         Flat3Map map = new Flat3Map();
143         map.put(ONE, TEN);
144         map.put(TWO, TWENTY);
145         
146         ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
147         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(bout);
148         out.writeObject(map);
149         byte[] bytes = bout.toByteArray();
150         out.close();
151         ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(bytes);
152         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bin);
153         Flat3Map ser = (Flat3Map) in.readObject();
154         in.close();
155         assertEquals(2, map.size());
156         assertEquals(2, ser.size());
157         assertEquals(true, ser.containsKey(ONE));
158         assertEquals(true, ser.containsKey(TWO));
159         assertEquals(TEN, ser.get(ONE));
160         assertEquals(TWENTY, ser.get(TWO));
161     }
162     
163     public void testSerialisation4() throws Exception JavaDoc {
164         Flat3Map map = new Flat3Map();
165         map.put(ONE, TEN);
166         map.put(TWO, TWENTY);
167         map.put(TEN, ONE);
168         map.put(TWENTY, TWO);
169         
170         ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
171         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(bout);
172         out.writeObject(map);
173         byte[] bytes = bout.toByteArray();
174         out.close();
175         ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(bytes);
176         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bin);
177         Flat3Map ser = (Flat3Map) in.readObject();
178         in.close();
179         assertEquals(4, map.size());
180         assertEquals(4, ser.size());
181         assertEquals(true, ser.containsKey(ONE));
182         assertEquals(true, ser.containsKey(TWO));
183         assertEquals(true, ser.containsKey(TEN));
184         assertEquals(true, ser.containsKey(TWENTY));
185         assertEquals(TEN, ser.get(ONE));
186         assertEquals(TWENTY, ser.get(TWO));
187         assertEquals(ONE, ser.get(TEN));
188         assertEquals(TWO, ser.get(TWENTY));
189     }
190     
191     //-----------------------------------------------------------------------
192
public BulkTest bulkTestMapIterator() {
193         return new TestFlatMapIterator();
194     }
195     
196     public class TestFlatMapIterator extends AbstractTestMapIterator {
197         public TestFlatMapIterator() {
198             super("TestFlatMapIterator");
199         }
200         
201         public Object JavaDoc[] addSetValues() {
202             return TestFlat3Map.this.getNewSampleValues();
203         }
204         
205         public boolean supportsRemove() {
206             return TestFlat3Map.this.isRemoveSupported();
207         }
208
209         public boolean supportsSetValue() {
210             return TestFlat3Map.this.isSetValueSupported();
211         }
212
213         public MapIterator makeEmptyMapIterator() {
214             resetEmpty();
215             return ((Flat3Map) TestFlat3Map.this.map).mapIterator();
216         }
217
218         public MapIterator makeFullMapIterator() {
219             resetFull();
220             return ((Flat3Map) TestFlat3Map.this.map).mapIterator();
221         }
222         
223         public Map JavaDoc getMap() {
224             // assumes makeFullMapIterator() called first
225
return TestFlat3Map.this.map;
226         }
227         
228         public Map JavaDoc getConfirmedMap() {
229             // assumes makeFullMapIterator() called first
230
return TestFlat3Map.this.confirmed;
231         }
232         
233         public void verify() {
234             super.verify();
235             TestFlat3Map.this.verify();
236         }
237     }
238     
239     public String JavaDoc getCompatibilityVersion() {
240         return "3.1";
241     }
242
243 // public void testCreate() throws Exception {
244
// resetEmpty();
245
// writeExternalFormToDisk(
246
// (java.io.Serializable) map,
247
// "D:/dev/collections/data/test/Flat3Map.emptyCollection.version3.1.obj");
248
// resetFull();
249
// writeExternalFormToDisk(
250
// (java.io.Serializable) map,
251
// "D:/dev/collections/data/test/Flat3Map.fullCollection.version3.1.obj");
252
// }
253
}
254
Popular Tags