From c046419acd4d00d4ccf53b78cdabe0f99c9ec4ad Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 11 Oct 2010 17:36:18 +0000 Subject: [PATCH] SPR-7627 added Serializer and Deserializer strategies, Converter adapters, and default implementations --- .../core/serializer/DefaultDeserializer.java | 49 ++++++++++ .../core/serializer/DefaultSerializer.java | 47 ++++++++++ .../DeserializationFailureException.java | 46 ++++++++++ .../core/serializer/Deserializer.java | 39 ++++++++ .../serializer/DeserializingConverter.java | 71 +++++++++++++++ .../serializer/SerializationException.java | 48 ++++++++++ .../SerializationFailureException.java | 46 ++++++++++ .../core/serializer/SerializationUtils.java | 74 +++++++++++++++ .../core/serializer/Serializer.java | 39 ++++++++ .../core/serializer/SerializingConverter.java | 66 ++++++++++++++ .../core/serializer/package-info.java | 6 ++ .../SerializationConverterTests.java | 90 +++++++++++++++++++ .../serializer/SerializationUtilsTests.java | 74 +++++++++++++++ 13 files changed, 695 insertions(+) create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/DefaultDeserializer.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/DefaultSerializer.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/DeserializationFailureException.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/Deserializer.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/DeserializingConverter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationException.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationFailureException.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationUtils.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/Serializer.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/SerializingConverter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/serializer/package-info.java create mode 100644 org.springframework.core/src/test/java/org/springframework/core/serializer/SerializationConverterTests.java create mode 100644 org.springframework.core/src/test/java/org/springframework/core/serializer/SerializationUtilsTests.java diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/DefaultDeserializer.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/DefaultDeserializer.java new file mode 100644 index 00000000000..123a25292fd --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/DefaultDeserializer.java @@ -0,0 +1,49 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; + +/** + * Deserializer that reads an input stream using Java Serialization. + * + * @author Gary Russell + * @author Mark Fisher + * @since 3.0.5 + */ +public class DefaultDeserializer implements Deserializer { + + /** + * Reads the input stream and deserializes into an object. + */ + public Object deserialize(InputStream inputStream) throws IOException { + ObjectInputStream objectInputStream = null; + try { + objectInputStream = new ObjectInputStream(inputStream); + return objectInputStream.readObject(); + } + catch (ClassNotFoundException e) { + if (objectInputStream != null) { + objectInputStream.close(); + } + throw new IOException(e.getMessage()); + } + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/DefaultSerializer.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/DefaultSerializer.java new file mode 100644 index 00000000000..96680e1f39c --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/DefaultSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.Serializable; + +import org.springframework.util.Assert; + +/** + * Serializer that writes an object to an output stream using Java Serialization. + * + * @author Gary Russell + * @author Mark Fisher + * @since 3.0.5 + */ +public class DefaultSerializer implements Serializer { + + /** + * Writes the source object to an output stream using Java Serialization. + * Source object must implement {@link Serializable}. + */ + public void serialize(Object object, OutputStream outputStream) throws IOException { + Assert.isTrue(object instanceof Serializable, this.getClass().getName() + + " requires a Serializable payload, but received [" + object.getClass().getName() + "]"); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); + objectOutputStream.writeObject(object); + objectOutputStream.flush(); + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/DeserializationFailureException.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/DeserializationFailureException.java new file mode 100644 index 00000000000..c0e0f3da7a8 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/DeserializationFailureException.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +/** + * Exception to be thrown when a failure occurs while deserializing an object. + * + * @author Gary Russell + * @since 3.0.5 + */ +@SuppressWarnings("serial") +public class DeserializationFailureException extends SerializationException { + + /** + * Construct a DeserializationFailureException with the specified detail message. + * @param message the detail message + */ + public DeserializationFailureException(String message) { + super(message); + } + + /** + * Construct a DeserializationFailureException with the specified detail message + * and nested exception. + * @param message the detail message + * @param cause the nested exception + */ + public DeserializationFailureException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/Deserializer.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/Deserializer.java new file mode 100644 index 00000000000..bdf3dc85d9c --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/Deserializer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import java.io.IOException; +import java.io.InputStream; + +/** + * A strategy interface for converting from data in an InputStream to an Object. + * + * @author Gary Russell + * @author Mark Fisher + * @since 3.0.5 + */ +public interface Deserializer { + + /** + * Read (assemble an object of type T) from an InputStream. + * @param inputStream The InputStream. + * @return the deserialized object + * @throws IOException in case of errors reading the stream + */ + public T deserialize(InputStream inputStream) throws IOException; + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/DeserializingConverter.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/DeserializingConverter.java new file mode 100644 index 00000000000..9b787aea987 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/DeserializingConverter.java @@ -0,0 +1,71 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.util.Assert; + +/** + * A {@link Converter} that delegates to a {@link Deserializer} + * to convert data in a byte array to an object. + * + * @author Gary Russell + * @author Mark Fisher + * @since 3.0.5 + */ +public class DeserializingConverter implements Converter { + + private final Deserializer deserializer; + + + /** + * Create a default DeserializingConverter that uses standard Java deserialization. + */ + public DeserializingConverter() { + this.deserializer = new DefaultDeserializer(); + } + + /** + * Create a DeserializingConverter that delegates to the provided {@link Deserializer} + */ + public DeserializingConverter(Deserializer deserializer) { + Assert.notNull(deserializer, "deserializer must not be null"); + this.deserializer = deserializer; + } + + + public Object convert(byte[] source) { + ByteArrayInputStream byteStream = new ByteArrayInputStream(source); + try { + return this.deserializer.deserialize(byteStream); + } + catch (Exception e) { + try { + byteStream.close(); + } + catch (IOException e1) { /* ignore */ } + throw new DeserializationFailureException( + "Failed to deserialize payload. " + + "Is the byte array a result of corresponding serialization for " + + this.deserializer.getClass().getName() + "?", e); + } + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationException.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationException.java new file mode 100644 index 00000000000..1a7a3543716 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationException.java @@ -0,0 +1,48 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import org.springframework.core.NestedRuntimeException; + +/** + * Root exception for serialization and deserialization exceptions. + * + * @author Gary Russell + * @since 3.0.5 + */ +@SuppressWarnings("serial") +public abstract class SerializationException extends NestedRuntimeException { + + /** + * Construct a SerializationException with the specified detail message. + * @param message the detail message + */ + public SerializationException(String message) { + super(message); + } + + /** + * Construct a SerializationException with the specified detail message + * and nested exception. + * @param message the detail message + * @param cause the nested exception + */ + public SerializationException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationFailureException.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationFailureException.java new file mode 100644 index 00000000000..9b7c1d14176 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationFailureException.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +/** + * Exception to be thrown when a failure occurs while serializing an object. + * + * @author Gary Russell + * @since 3.0.5 + */ +@SuppressWarnings("serial") +public class SerializationFailureException extends SerializationException { + + /** + * Construct a SerializationFailureException with the specified detail message. + * @param message the detail message + */ + public SerializationFailureException(String message) { + super(message); + } + + /** + * Construct a SerializationFailureException with the specified detail message + * and nested exception. + * @param message the detail message + * @param cause the nested exception + */ + public SerializationFailureException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationUtils.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationUtils.java new file mode 100644 index 00000000000..16594843650 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializationUtils.java @@ -0,0 +1,74 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +/** + * Static utility for serialization and deserialization. + * + * @author Dave Syer + * @since 3.0.5 + */ +public abstract class SerializationUtils { + + /** + * Serialize the object provided. + * + * @param object the object to serialize + * @return an array of bytes representing the object in a portable fashion + */ + public static byte[] serialize(Object object) { + if (object == null) { + return null; + } + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + try { + new ObjectOutputStream(stream).writeObject(object); + } + catch (IOException e) { + throw new IllegalArgumentException("failed to serialize object of type: " + object.getClass(), e); + } + return stream.toByteArray(); + } + + /** + * Deserialize the byte array into an object. + * + * @param bytes a serialized object + * @return the result of deserializing the bytes + */ + public static Object deserialize(byte[] bytes) { + if (bytes == null) { + return null; + } + try { + return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject(); + } + catch (IOException e) { + throw new IllegalArgumentException("failed to deserialize object", e); + } + catch (ClassNotFoundException e) { + throw new IllegalStateException("failed to deserialize object type", e); + } + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/Serializer.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/Serializer.java new file mode 100644 index 00000000000..df42b27ef05 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/Serializer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * A strategy interface for streaming an object to an OutputStream. + * + * @author Gary Russell + * @author Mark Fisher + * @since 3.0.5 + */ +public interface Serializer { + + /** + * Write an object of type T to the outputSream. + * @param object The object. + * @param outputStream the outputStream + * @throws IOException in case of errors writing to the stream + */ + public void serialize(T object, OutputStream outputStream) throws IOException; + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializingConverter.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializingConverter.java new file mode 100644 index 00000000000..d2cc7ee4442 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/SerializingConverter.java @@ -0,0 +1,66 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import java.io.ByteArrayOutputStream; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.util.Assert; + +/** + * A {@Link Converter} that delegates to a {@link Serializer} to convert an object to a byte array. + * + * @author Gary Russell + * @author Mark Fisher + * @since 3.0.5 + */ +public class SerializingConverter implements Converter { + + private final Serializer serializer; + + + /** + * Create a default SerializingConverter that uses standard Java serialization. + */ + public SerializingConverter() { + this.serializer = new DefaultSerializer(); + } + + /** + * Create a SerializingConverter that delegates to the provided {@link Serializer} + */ + public SerializingConverter(Serializer serializer) { + Assert.notNull(serializer, "serializer must not be null"); + this.serializer = serializer; + } + + + /** + * Serializes the source object and returns the byte array result. + */ + public byte[] convert(Object source) { + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128); + try { + this.serializer.serialize(source, byteStream); + return byteStream.toByteArray(); + } + catch (Exception e) { + throw new SerializationFailureException("failed to serialize object", e); + } + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/serializer/package-info.java b/org.springframework.core/src/main/java/org/springframework/core/serializer/package-info.java new file mode 100644 index 00000000000..97087713de8 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/serializer/package-info.java @@ -0,0 +1,6 @@ +/** + * Root package for Spring's serializer interfaces and implementations. + * Provides an abstraction over various serialization techniques. + * Includes exceptions for serialization and deserialization failures. + */ +package org.springframework.core.serializer; diff --git a/org.springframework.core/src/test/java/org/springframework/core/serializer/SerializationConverterTests.java b/org.springframework.core/src/test/java/org/springframework/core/serializer/SerializationConverterTests.java new file mode 100644 index 00000000000..40f5c1728a5 --- /dev/null +++ b/org.springframework.core/src/test/java/org/springframework/core/serializer/SerializationConverterTests.java @@ -0,0 +1,90 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.NotSerializableException; +import java.io.Serializable; + +import org.junit.Test; + +import org.springframework.core.serializer.DeserializationFailureException; +import org.springframework.core.serializer.DeserializingConverter; +import org.springframework.core.serializer.SerializationFailureException; +import org.springframework.core.serializer.SerializingConverter; + +/** + * @author Gary Russell + * @author Mark Fisher + * @since 3.0.5 + */ +public class SerializationConverterTests { + + @Test + public void serializeAndDeserializeString() { + SerializingConverter toBytes = new SerializingConverter(); + byte[] bytes = toBytes.convert("Testing"); + DeserializingConverter fromBytes = new DeserializingConverter(); + assertEquals("Testing", fromBytes.convert(bytes)); + } + + @Test + public void nonSerializableObject() { + SerializingConverter toBytes = new SerializingConverter(); + try { + toBytes.convert(new Object()); + fail("Expected IllegalArgumentException"); + } + catch (SerializationFailureException e) { + assertNotNull(e.getCause()); + assertTrue(e.getCause() instanceof IllegalArgumentException); + } + } + + @Test + public void nonSerializableField() { + SerializingConverter toBytes = new SerializingConverter(); + try { + toBytes.convert(new UnSerializable()); + fail("Expected SerializationFailureException"); + } + catch (SerializationFailureException e) { + assertNotNull(e.getCause()); + assertTrue(e.getCause() instanceof NotSerializableException); + } + } + + @Test(expected = DeserializationFailureException.class) + public void deserializationFailure() { + DeserializingConverter fromBytes = new DeserializingConverter(); + fromBytes.convert("Junk".getBytes()); + } + + + class UnSerializable implements Serializable { + + private static final long serialVersionUID = 1L; + + @SuppressWarnings("unused") + private Object object; + } + +} diff --git a/org.springframework.core/src/test/java/org/springframework/core/serializer/SerializationUtilsTests.java b/org.springframework.core/src/test/java/org/springframework/core/serializer/SerializationUtilsTests.java new file mode 100644 index 00000000000..82cc577361a --- /dev/null +++ b/org.springframework.core/src/test/java/org/springframework/core/serializer/SerializationUtilsTests.java @@ -0,0 +1,74 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.serializer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.math.BigInteger; + +import org.junit.Test; + +import org.springframework.core.serializer.SerializationUtils; + +/** + * Test for static utility to help with serialization. + * + * @author Dave Syer + * @since 3.0.5 + */ +public class SerializationUtilsTests { + + private static BigInteger FOO = new BigInteger( + "-9702942423549012526722364838327831379660941553432801565505143675386108883970811292563757558516603356009681061" + + "5697574744209306031461371833798723505120163874786203211176873686513374052845353833564048"); + + + @Test + public void serializeCycleSunnyDay() throws Exception { + assertEquals("foo", SerializationUtils.deserialize(SerializationUtils.serialize("foo"))); + } + + @Test(expected = IllegalStateException.class) + public void deserializeUndefined() throws Exception { + byte[] bytes = FOO.toByteArray(); + Object foo = SerializationUtils.deserialize(bytes); + assertNotNull(foo); + } + + @Test(expected = IllegalArgumentException.class) + public void serializeNonSerializable() throws Exception { + SerializationUtils.serialize(new Object()); + } + + @Test(expected = IllegalArgumentException.class) + public void deserializeNonSerializable() throws Exception { + SerializationUtils.deserialize("foo".getBytes()); + } + + @Test + public void serializeNull() throws Exception { + assertNull(SerializationUtils.serialize(null)); + } + + @Test + public void deserializeNull() throws Exception { + assertNull(SerializationUtils.deserialize(null)); + } + +}