1 16 package org.apache.commons.collections.map; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.util.Map ; 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 38 public class TestFlat3Map extends AbstractTestIterableMap { 39 40 private static final Integer ONE = new Integer (1); 41 private static final Integer TWO = new Integer (2); 42 private static final String TEN = "10"; 43 private static final String TWENTY = "20"; 44 45 public TestFlat3Map(String testName) { 46 super(testName); 47 } 48 49 public static void main(String [] args) { 50 TestRunner.run(suite()); 51 } 52 53 public static Test suite() { 54 return BulkTest.makeSuite(TestFlat3Map.class); 55 } 56 57 public Map makeEmptyMap() { 58 return new Flat3Map(); 59 } 60 61 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 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 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 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 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 { 127 Flat3Map map = new Flat3Map(); 128 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 129 ObjectOutputStream out = new ObjectOutputStream (bout); 130 out.writeObject(map); 131 byte[] bytes = bout.toByteArray(); 132 out.close(); 133 ByteArrayInputStream bin = new ByteArrayInputStream (bytes); 134 ObjectInputStream in = new ObjectInputStream (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 { 142 Flat3Map map = new Flat3Map(); 143 map.put(ONE, TEN); 144 map.put(TWO, TWENTY); 145 146 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 147 ObjectOutputStream out = new ObjectOutputStream (bout); 148 out.writeObject(map); 149 byte[] bytes = bout.toByteArray(); 150 out.close(); 151 ByteArrayInputStream bin = new ByteArrayInputStream (bytes); 152 ObjectInputStream in = new ObjectInputStream (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 { 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 bout = new ByteArrayOutputStream (); 171 ObjectOutputStream out = new ObjectOutputStream (bout); 172 out.writeObject(map); 173 byte[] bytes = bout.toByteArray(); 174 out.close(); 175 ByteArrayInputStream bin = new ByteArrayInputStream (bytes); 176 ObjectInputStream in = new ObjectInputStream (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 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 [] 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 getMap() { 224 return TestFlat3Map.this.map; 226 } 227 228 public Map getConfirmedMap() { 229 return TestFlat3Map.this.confirmed; 231 } 232 233 public void verify() { 234 super.verify(); 235 TestFlat3Map.this.verify(); 236 } 237 } 238 239 public String getCompatibilityVersion() { 240 return "3.1"; 241 } 242 243 } 254 | Popular Tags |