| package org.apache.commons.lang; |
| |
| /* ==================================================================== |
| * The Apache Software License, Version 1.1 |
| * |
| * Copyright (c) 2002 The Apache Software Foundation. All rights |
| * reserved. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions |
| * are met: |
| * |
| * 1. Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * |
| * 2. Redistributions in binary form must reproduce the above copyright |
| * notice, this list of conditions and the following disclaimer in |
| * the documentation and/or other materials provided with the |
| * distribution. |
| * |
| * 3. The end-user documentation included with the redistribution, if |
| * any, must include the following acknowlegement: |
| * "This product includes software developed by the |
| * Apache Software Foundation (http://www.apache.org/)." |
| * Alternately, this acknowlegement may appear in the software itself, |
| * if and wherever such third-party acknowlegements normally appear. |
| * |
| * 4. The names "The Jakarta Project", "Commons", and "Apache Software |
| * Foundation" must not be used to endorse or promote products derived |
| * from this software without prior written permission. For written |
| * permission, please contact apache@apache.org. |
| * |
| * 5. Products derived from this software may not be called "Apache" |
| * nor may "Apache" appear in their names without prior written |
| * permission of the Apache Software Foundation. |
| * |
| * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| * SUCH DAMAGE. |
| * ==================================================================== |
| * |
| * This software consists of voluntary contributions made by many |
| * individuals on behalf of the Apache Software Foundation. For more |
| * information on the Apache Software Foundation, please see |
| * <http://www.apache.org/>. |
| */ |
| |
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| import java.io.InputStream; |
| import java.io.ObjectOutputStream; |
| import java.util.HashMap; |
| |
| import junit.framework.Test; |
| import junit.framework.TestCase; |
| import junit.framework.TestSuite; |
| import junit.textui.TestRunner; |
| |
| /** |
| * Unit tests {@link org.apache.commons.lang.SerializationUtils}. |
| * |
| * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a> |
| * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a> |
| * @version $Id: SerializationUtilsTest.java,v 1.1 2002/07/19 03:35:55 bayard Exp $ |
| */ |
| public class SerializationUtilsTest extends TestCase { |
| private String iString; |
| private Integer iInteger; |
| private HashMap iMap; |
| |
| public SerializationUtilsTest(String name) { |
| super(name); |
| } |
| |
| public static void main(String[] args) { |
| TestRunner.run(suite()); |
| } |
| |
| public static Test suite() { |
| TestSuite suite = new TestSuite(SerializationUtilsTest.class); |
| suite.setName("SerializationUtils Tests"); |
| return suite; |
| } |
| |
| protected void setUp() throws Exception { |
| super.setUp(); |
| |
| iString = "foo"; |
| iInteger = new Integer(7); |
| iMap = new HashMap(); |
| iMap.put("FOO", iString); |
| iMap.put("BAR", iInteger); |
| } |
| |
| protected void tearDown() throws Exception { |
| super.tearDown(); |
| } |
| |
| //----------------------------------------------------------------------- |
| |
| public void testSerializeStream() throws Exception { |
| ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); |
| SerializationUtils.serialize(iMap, streamTest); |
| |
| ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); |
| ObjectOutputStream oos = new ObjectOutputStream(streamReal); |
| oos.writeObject(iMap); |
| oos.flush(); |
| oos.close(); |
| |
| byte[] testBytes = streamTest.toByteArray(); |
| byte[] realBytes = streamReal.toByteArray(); |
| assertEquals(testBytes.length, realBytes.length); |
| for (int i = 0; i < realBytes.length; i++) { |
| assertEquals(realBytes[i], testBytes[i]); |
| } |
| } |
| |
| 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 testSerializeStreamNullObj() throws Exception { |
| ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); |
| SerializationUtils.serialize(null, streamTest); |
| |
| ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); |
| ObjectOutputStream oos = new ObjectOutputStream(streamReal); |
| oos.writeObject(null); |
| oos.flush(); |
| oos.close(); |
| |
| byte[] testBytes = streamTest.toByteArray(); |
| byte[] realBytes = streamReal.toByteArray(); |
| assertEquals(testBytes.length, realBytes.length); |
| for (int i = 0; i < realBytes.length; i++) { |
| assertEquals(realBytes[i], testBytes[i]); |
| } |
| } |
| |
| public void testSerializeStreamObjNull() throws Exception { |
| ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); |
| try { |
| SerializationUtils.serialize(iMap, null); |
| } catch (NullPointerException ex) { |
| return; |
| } |
| fail(); |
| } |
| |
| public void testSerializeStreamNullNull() throws Exception { |
| ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); |
| try { |
| SerializationUtils.serialize(null, null); |
| } catch (NullPointerException ex) { |
| return; |
| } |
| fail(); |
| } |
| |
| //----------------------------------------------------------------------- |
| |
| public void testDeserializeStream() throws Exception { |
| ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); |
| ObjectOutputStream oos = new ObjectOutputStream(streamReal); |
| oos.writeObject(iMap); |
| oos.flush(); |
| oos.close(); |
| |
| ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray()); |
| Object test = SerializationUtils.deserialize(inTest); |
| 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); |
| } |
| |
| public void testDeserializeStreamOfNull() throws Exception { |
| ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); |
| ObjectOutputStream oos = new ObjectOutputStream(streamReal); |
| oos.writeObject(null); |
| oos.flush(); |
| oos.close(); |
| |
| ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray()); |
| Object test = SerializationUtils.deserialize(inTest); |
| assertNull(test); |
| } |
| |
| public void testDeserializeStreamNull() throws Exception { |
| try { |
| SerializationUtils.deserialize((InputStream) null); |
| } catch (NullPointerException ex) { |
| return; |
| } |
| fail(); |
| } |
| |
| public void testDeserializeStreamBadStream() throws Exception { |
| try { |
| SerializationUtils.deserialize(new ByteArrayInputStream(new byte[0])); |
| } catch (SerializationException ex) { |
| return; |
| } |
| fail(); |
| } |
| |
| //----------------------------------------------------------------------- |
| |
| public void testSerializeBytes() throws Exception { |
| byte[] testBytes = SerializationUtils.serialize(iMap); |
| |
| ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); |
| ObjectOutputStream oos = new ObjectOutputStream(streamReal); |
| oos.writeObject(iMap); |
| 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 testSerializeBytesUnserializable() throws Exception { |
| try { |
| iMap.put(new Object(), new Object()); |
| SerializationUtils.serialize(iMap); |
| } 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 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); |
| } |
| |
| 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 testDeserializeBytesNull() throws Exception { |
| try { |
| SerializationUtils.deserialize((byte[]) null); |
| } catch (NullPointerException ex) { |
| return; |
| } |
| fail(); |
| } |
| |
| public void testDeserializeBytesBadStream() throws Exception { |
| try { |
| SerializationUtils.deserialize(new byte[0]); |
| } catch (SerializationException ex) { |
| return; |
| } |
| fail(); |
| } |
| |
| //----------------------------------------------------------------------- |
| |
| public void testClone() throws Exception { |
| Object test = SerializationUtils.clone(iMap); |
| 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); |
| } |
| |
| public void testCloneNull() throws Exception { |
| Object test = SerializationUtils.clone(null); |
| assertNull(test); |
| } |
| |
| public void testCloneUnserializable() throws Exception { |
| try { |
| iMap.put(new Object(), new Object()); |
| SerializationUtils.clone(iMap); |
| } catch (SerializationException ex) { |
| return; |
| } |
| fail(); |
| } |
| |
| } |