1 package org.jboss.cache.util; 2 3 import junit.framework.TestCase; 4 5 import java.io.ByteArrayInputStream ; 6 import java.io.ByteArrayOutputStream ; 7 import java.io.ObjectInputStream ; 8 import java.io.ObjectOutputStream ; 9 import java.util.HashMap ; 10 import java.util.Map ; 11 12 public class MapCopyTest extends TestCase 13 { 14 15 public void testSerializable() throws Exception 16 { 17 HashMap hm = new HashMap (); 18 hm.put(null, null); 19 hm.put("y", "z"); 20 MapCopy mc = new MapCopy(hm); 21 assertEquals(hm, mc); 22 ByteArrayOutputStream os = new ByteArrayOutputStream (); 23 ObjectOutputStream oos = new ObjectOutputStream (os); 24 oos.writeObject(mc); 25 ByteArrayInputStream is = new ByteArrayInputStream (os.toByteArray()); 26 ObjectInputStream ois = new ObjectInputStream (is); 27 Object o = ois.readObject(); 28 assertEquals(hm, o); 29 } 30 31 public void testNull() 32 { 33 HashMap hm = new HashMap (); 34 hm.put(null, null); 35 MapCopy mc = new MapCopy(hm); 36 assertEquals(hm, mc); 37 assertEquals(hm.toString(), mc.toString()); 38 39 hm.put(null, "x"); 40 hm.put("y", null); 41 mc = new MapCopy(hm); 42 mc.toString(); 43 assertEquals(true, mc.containsKey("y")); 44 } 45 46 public void testAll() 47 { 48 HashMap hm = new HashMap (); 49 hm.put("a", "b"); 50 hm.put("b", "c"); 51 MapCopy mc = new MapCopy(hm); 52 assertEquals(hm, mc); 53 assertEquals(hm.size(), mc.size()); 54 try 55 { 56 mc.clear(); 57 fail("read only"); 58 } 59 catch (UnsupportedOperationException e) 60 { 61 } 62 HashMap bhm = new HashMap (hm); 63 hm.put("b", "d"); 64 assertEquals(bhm, mc); 65 Map.Entry me = (Map.Entry ) mc.entrySet().iterator().next(); 66 try 67 { 68 me.setValue("arg"); 69 fail("read only"); 70 } 71 catch (UnsupportedOperationException e) 72 { 73 } 74 } 75 76 public void testModifications() 77 { 78 Map hm = new HashMap (); 79 hm.put("a", "b"); 80 Map mc = new MapCopy(hm); 81 82 try 83 { 84 mc.put("x", "y"); 85 fail("should fail"); 86 } 87 catch (UnsupportedOperationException uoe) 88 { 89 } 91 92 try 93 { 94 mc.remove("a"); 95 fail("should fail"); 96 } 97 catch (UnsupportedOperationException uoe) 98 { 99 } 101 102 try 103 { 104 mc.keySet().iterator().remove(); 105 fail("should fail"); 106 } 107 catch (UnsupportedOperationException uoe) 108 { 109 } 111 112 try 113 { 114 mc.entrySet().iterator().remove(); 115 fail("should fail"); 116 } 117 catch (UnsupportedOperationException uoe) 118 { 119 } 121 122 try 123 { 124 mc.values().iterator().remove(); 125 fail("should fail"); 126 } 127 catch (UnsupportedOperationException uoe) 128 { 129 } 131 132 133 } 134 } 135 | Popular Tags |