public void testSerializeStreamNullNull() throws Exception { try { SerializationUtils.serialize(null, 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(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); }
@Test public void testSerializeStreamObjNull() throws Exception { try { SerializationUtils.serialize(iMap, null); } catch (final IllegalArgumentException ex) { return; } fail(); }
public void testSerializeBytesUnserializable() throws Exception { try { iMap.put(new Object(), new Object()); SerializationUtils.serialize(iMap); } catch (SerializationException ex) { return; } fail(); }
public void testSerializeStreamUnserializable() throws Exception { ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); try { iMap.put(new Object(), new Object()); SerializationUtils.serialize(iMap, streamTest); } catch (SerializationException ex) { return; } fail(); }
public void testSerializeBytesNull() throws Exception { byte[] testBytes = SerializationUtils.serialize(null); ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(null); oos.flush(); oos.close(); byte[] realBytes = streamReal.toByteArray(); assertEquals(testBytes.length, realBytes.length); for (int i = 0; i < realBytes.length; i++) { assertEquals(realBytes[i], testBytes[i]); } }
public void testSerializeIOException() throws Exception { // forces an IOException when the ObjectOutputStream is created, to test not closing the stream // in the finally block OutputStream streamTest = new OutputStream() { public void write(int arg0) throws IOException { throw new IOException(SERIALIZE_IO_EXCEPTION_MESSAGE); } }; try { SerializationUtils.serialize(iMap, streamTest); } catch (SerializationException e) { assertEquals("java.io.IOException: " + SERIALIZE_IO_EXCEPTION_MESSAGE, e.getMessage()); } }
@Test public void testSerializeStream() throws Exception { final ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); SerializationUtils.serialize(iMap, streamTest); final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(iMap); oos.flush(); oos.close(); final byte[] testBytes = streamTest.toByteArray(); final byte[] realBytes = streamReal.toByteArray(); assertEquals(testBytes.length, realBytes.length); for (int i = 0; i < realBytes.length; i++) { assertEquals(realBytes[i], testBytes[i]); } }