1 package org.sapia.regis.cache; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.ObjectInputStream ; 6 import java.io.ObjectOutputStream ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 10 import junit.framework.TestCase; 11 12 public class RemoteMapTest extends TestCase { 13 14 private Map remote; 15 private Map delegate; 16 17 protected void setUp() throws Exception { 18 delegate = new HashMap (); 19 delegate.put("prop1", "value1"); 20 delegate.put("prop2", "value2"); 21 remote = new RemoteMap(delegate); 22 } 23 24 27 public void testSerializeEntrySet() throws Exception { 28 serialize(remote.entrySet()); 29 try{ 30 serialize(delegate.entrySet()); 31 fail("Should have thrown NotSerializableException"); 32 }catch(Exception e){} 33 34 super.assertEquals(2, remote.entrySet().size()); 35 super.assertTrue(remote.entrySet().contains(new SerializableEntry("prop1", "value1"))); 36 } 37 38 41 public void testSerializeKeySet() throws Exception { 42 serialize(remote.keySet()); 43 try{ 44 serialize(delegate.keySet()); 45 fail("Should have thrown NotSerializableException"); 46 }catch(Exception e){} 47 } 48 49 52 public void testSerializeValues() throws Exception { 53 serialize(remote.values()); 54 try{ 55 serialize(delegate.values()); 56 fail("Should have thrown NotSerializableException"); 57 }catch(Exception e){} 58 } 59 60 private void serialize(Object o) throws Exception { 61 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 62 ObjectOutputStream os = new ObjectOutputStream (bos); 63 os.writeObject(o); 64 os.flush(); 65 os.close(); 66 ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (bos.toByteArray())); 67 ois.readObject(); 68 69 } 70 71 } 72 | Popular Tags |