@Test(expected = ClassCastException.class) public void testDeserializeClassCastException() { final String value = "Hello"; final byte[] serialized = SerializationUtils.serialize(value); Assert.assertEquals(value, SerializationUtils.deserialize(serialized)); // Causes ClassCastException in call site, not in SerializationUtils.deserialize @SuppressWarnings("unused") // needed to cause Exception final Integer i = SerializationUtils.deserialize(serialized); }
public void testDeserializeBytesBadStream() throws Exception { try { SerializationUtils.deserialize(new byte[0]); } catch (SerializationException ex) { return; } fail(); }
public void testDeserializeBytesNull() throws Exception { try { SerializationUtils.deserialize((byte[]) null); } catch (IllegalArgumentException ex) { return; } fail(); }
public void testDeserializeStreamNull() throws Exception { try { SerializationUtils.deserialize((InputStream) null); } catch (IllegalArgumentException ex) { return; } fail(); }
/** * Protect a specific value if it is considered mutable * * @param <S> the type of the value, which must be {@link Serializable} * @param value the value to protect if it is mutable (may be <tt>null</tt>) * @param immutableClasses a set of classes that can be considered immutable over and above the * {@link #DEFAULT_IMMUTABLE_CLASSES default set} * @return a cloned instance (via serialization) or the instance itself, if immutable */ @SuppressWarnings("unchecked") public static <S extends Serializable> S protectValue(S value, Set<Class<?>> immutableClasses) { if (!mustProtectValue(value, immutableClasses)) { return value; } // We have to clone it // No worries about the return type; it has to be the same as we put into the serializer return (S) SerializationUtils.deserialize(SerializationUtils.serialize(value)); }
@Test public void testDeserializeStreamBadStream() throws Exception { try { SerializationUtils.deserialize(new ByteArrayInputStream(new byte[0])); } catch (final SerializationException ex) { return; } fail(); }
public void testDeserializeBytesOfNull() throws Exception { ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(null); oos.flush(); oos.close(); Object test = SerializationUtils.deserialize(streamReal.toByteArray()); assertNull(test); }
public void testDeserializeStreamClassNotFound() throws Exception { ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(new ClassNotFoundSerialization()); oos.flush(); oos.close(); ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray()); try { Object test = SerializationUtils.deserialize(inTest); } catch (SerializationException se) { assertEquals("java.lang.ClassNotFoundException: " + CLASS_NOT_FOUND_MESSAGE, se.getMessage()); } }
public void testDeserializeBytes() throws Exception { ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(iMap); oos.flush(); oos.close(); Object test = SerializationUtils.deserialize(streamReal.toByteArray()); assertNotNull(test); assertTrue(test instanceof HashMap); assertTrue(test != iMap); HashMap testMap = (HashMap) test; assertEquals(iString, testMap.get("FOO")); assertTrue(iString != testMap.get("FOO")); assertEquals(iInteger, testMap.get("BAR")); assertTrue(iInteger != testMap.get("BAR")); assertEquals(iMap, testMap); }