diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 27b2cc71a..eba6f7900 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + Add SerializationUtils.roundtrip(T extends Serializable) to serialize then deserialize Please add number to byte[] methods org.apache.commons.lang3.reflect.FieldUtils.removeFinalModifier(Field) does not clean up after itself FastDateParser javadoc incorrectly states that SimpleDateFormat is used internally diff --git a/src/main/java/org/apache/commons/lang3/SerializationUtils.java b/src/main/java/org/apache/commons/lang3/SerializationUtils.java index 6b4e6c415..f8429aca9 100644 --- a/src/main/java/org/apache/commons/lang3/SerializationUtils.java +++ b/src/main/java/org/apache/commons/lang3/SerializationUtils.java @@ -111,6 +111,18 @@ public class SerializationUtils { } } + /** + * Performs a serialization roundtrip. Serializes and deserializes the given object, great for testing objects that + * implement {@link Serializable}. + * + * @param msg + * the object to roundtrip + * @return the serialized and deseralized object + */ + public static T roundtrip(final T msg) { + return SerializationUtils.deserialize(SerializationUtils.serialize(msg)); + } + // Serialize //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java index 5a0a2f6bb..8a898574f 100644 --- a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java @@ -271,6 +271,12 @@ public class SerializationUtilsTest { } } + @Test + public void testRoundtrip() { + HashMap newMap = SerializationUtils.roundtrip(iMap); + assertEquals(iMap, newMap); + } + //----------------------------------------------------------------------- @Test