@Before public void createMap() { map = new SerializationSafeHashMap<Object, Object>(); map.put("key1", "value1"); map.put(new NonSerializableValue("key2"), "value2"); map.put("key3", new NonSerializableValue("value3")); }
@Test @SuppressWarnings("unchecked") public void serialization() throws Exception { ByteArrayOutputStream target = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(target); out.writeObject(map); out.close(); byte[] bytes = target.toByteArray(); assertTrue("Serialized data must not be empty", bytes.length > 0); assertTrue("Original Map must not be modified", map.size() == 3); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes)); Object obj = in.readObject(); in.close(); assertTrue( "Deserialized object must be of same type", obj instanceof SerializationSafeHashMap<?, ?>); SerializationSafeHashMap<Object, Object> map2 = (SerializationSafeHashMap) obj; assertTrue("Deserialized map must contain 'key1'", map2.containsKey("key1")); assertEquals("Size of the seserialized map must be 1", map2.size(), 1); }