You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dl...@apache.org on 2002/03/11 09:05:20 UTC
cvs commit: jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang ObjectsTest.java
dlr 02/03/11 00:05:20
Modified: lang build.xml
lang/src/java/org/apache/commons/lang Objects.java
Added: lang/src/test/org/apache/commons/lang ObjectsTest.java
Log:
Patch by John McNally <jm...@collab.net>:
"Here is a patch to add a serialize method to Objects to complement
the deserialize method. Also included are unit tests for Objects."
Revision Changes Path
1.5 +11 -1 jakarta-commons-sandbox/lang/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/build.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -u -r1.4 -r1.5
--- build.xml 9 Mar 2002 17:11:38 -0000 1.4
+++ build.xml 11 Mar 2002 08:05:20 -0000 1.5
@@ -3,7 +3,7 @@
<!--
"Lang" component of the Jakarta Commons Subproject
- $Id: build.xml,v 1.4 2002/03/09 17:11:38 dlr Exp $
+ $Id: build.xml,v 1.5 2002/03/11 08:05:20 dlr Exp $
-->
@@ -224,6 +224,7 @@
<target name="test" depends="compile.tests,
test.strings,
+ test.objects,
test.numberRange,
test.exception
"
@@ -236,6 +237,15 @@
<java classname="${test.runner}" fork="yes"
failonerror="${test.failonerror}">
<arg value="org.apache.commons.lang.StringsTest"/>
+ <classpath refid="test.classpath"/>
+ </java>
+ </target>
+
+ <target name="test.objects" depends="compile.tests">
+ <echo message="Running Objects tests ..."/>
+ <java classname="${test.runner}" fork="yes"
+ failonerror="${test.failonerror}">
+ <arg value="org.apache.commons.lang.ObjectsTest"/>
<classpath refid="test.classpath"/>
</java>
</target>
1.2 +38 -2 jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Objects.java
Index: Objects.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Objects.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -u -r1.1 -r1.2
--- Objects.java 22 Feb 2002 05:57:15 -0000 1.1
+++ Objects.java 11 Mar 2002 08:05:20 -0000 1.2
@@ -54,11 +54,14 @@
* <http://www.apache.org/>.
*/
-import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
+import java.io.Serializable;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.IOException;
/**
* Common <code>Object</code> manipulation routines.
@@ -66,7 +69,7 @@
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
* @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
- * @version $Id: Objects.java,v 1.1 2002/02/22 05:57:15 bayard Exp $
+ * @version $Id: Objects.java,v 1.2 2002/03/11 08:05:20 dlr Exp $
*/
public class Objects
{
@@ -121,6 +124,39 @@
}
}
return object;
+ }
+
+
+ /**
+ * Converts a object to a byte array for storage/serialization.
+ *
+ * @param obj The Serializable to convert.
+ * @return A byte[] with the converted Serializable.
+ * @exception IOException, if conversion to a byte[] fails.
+ */
+ public static byte[] serialize(Serializable obj)
+ throws IOException
+ {
+ byte[] byteArray = null;
+ ByteArrayOutputStream baos = null;
+ ObjectOutputStream out = null;
+ try
+ {
+ // These objects are closed in the finally.
+ baos = new ByteArrayOutputStream();
+ out = new ObjectOutputStream(baos);
+
+ out.writeObject(obj);
+ byteArray = baos.toByteArray();
+ }
+ finally
+ {
+ if (out != null)
+ {
+ out.close();
+ }
+ }
+ return byteArray;
}
/**
1.1 jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/ObjectsTest.java
Index: ObjectsTest.java
===================================================================
package org.apache.commons.lang;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" 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",
* "Apache Turbine", nor may "Apache" appear in their name, 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.util.HashMap;
import java.io.IOException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit tests {@link org.apache.commons.lang.Objects}.
*
* @author <a href="mailto:jmcnally@collab.net">John McNally</a>
*/
public class ObjectsTest extends TestCase
{
private static final String FOO = "foo";
private static final String BAR = "bar";
private static final String MAP_ERROR =
"Map did not serialize and deserialize to equivalent map";
public ObjectsTest(String name)
{
super(name);
}
public void testIsNull()
{
Object o = FOO;
Object dflt = BAR;
assertEquals("dflt was not returned when o was null", dflt,
Objects.isNull(null, dflt));
assertEquals("dflt was returned when o was not null", o,
Objects.isNull(o, dflt));
}
public void testDeserialize()
throws IOException
{
serializeDeserialize();
}
public void testSerialize()
throws IOException
{
serializeDeserialize();
}
private void serializeDeserialize()
throws IOException
{
HashMap original = new HashMap();
original.put(FOO, BAR);
original.put(BAR, FOO);
byte[] ser = Objects.serialize(original);
HashMap deser = (HashMap)Objects.deserialize(ser);
assertEquals(MAP_ERROR, original.get(FOO), deser.get(FOO));
assertEquals(MAP_ERROR, original.get(BAR), deser.get(BAR));
}
public void testEquals()
{
assertTrue("Objects.equals(null, null) returned false",
Objects.equals(null, null));
assertTrue("Objects.equals(\"foo\", null) returned true",
!Objects.equals(FOO, null));
assertTrue("Objects.equals(null, \"bar\") returned true",
!Objects.equals(null, BAR));
assertTrue("Objects.equals(\"foo\", \"bar\") returned true",
!Objects.equals(FOO, BAR));
assertTrue("Objects.equals(\"foo\", \"foo\") returned false",
Objects.equals(FOO, FOO));
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>