alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (SerializationTestUtils.java)

This example Spring Framework source code file (SerializationTestUtils.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

awt, bytearrayinputstream, bytearrayoutputstream, bytearrayoutputstream, io, ioexception, ioexception, notserializableexception, object, objectoutputstream, objectoutputstream, point, point, serializable, serializationtestutils, testbean

The Spring Framework SerializationTestUtils.java source code

/*
 * The Spring Framework is published under the terms
 * of the Apache Software License.
 */

package org.springframework.util;

import java.awt.Point;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import junit.framework.TestCase;

import org.springframework.beans.TestBean;

/**
 * Utilities for testing serializability of objects.
 * Exposes static methods for use in other test cases.
 * Extends TestCase only to test itself.
 *
 * @author Rod Johnson
 */
public class SerializationTestUtils extends TestCase {
	
	public static void testSerialization(Object o) throws IOException {
		OutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(baos);
		oos.writeObject(o);
	}
	
	public static boolean isSerializable(Object o) throws IOException {
		try {
			testSerialization(o);
			return true;
		}
		catch (NotSerializableException ex) {
			return false;
		}
	}
	
	public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(baos);
		oos.writeObject(o);
		oos.flush();
		baos.flush();
		byte[] bytes = baos.toByteArray();
		
		ByteArrayInputStream is = new ByteArrayInputStream(bytes);
		ObjectInputStream ois = new ObjectInputStream(is);
		Object o2 = ois.readObject();
		
		return o2;
	}
	
	public SerializationTestUtils(String s) {
		super(s);
	}
	
	public void testWithNonSerializableObject() throws IOException {
		TestBean o = new TestBean();
		assertFalse(o instanceof Serializable);
		
		assertFalse(isSerializable(o));
		
		try {
			testSerialization(o);
			fail();
		}
		catch (NotSerializableException ex) {
			// Ok
		}
	}
	
	public void testWithSerializableObject() throws Exception {
		int x = 5;
		int y = 10;
		Point p = new Point(x, y);
		assertTrue(p instanceof Serializable);
	
		testSerialization(p);
		
		assertTrue(isSerializable(p));
		
		Point p2 = (Point) serializeAndDeserialize(p);
		assertNotSame(p, p2);
		assertEquals(x, (int) p2.getX());
		assertEquals(y, (int) p2.getY());
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework SerializationTestUtils.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.