import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Random;
import junit.framework.TestCase;
import dijjer.io.comm.Peer;
import dijjer.util.BitArray;
import dijjer.util.Buffer;
import dijjer.util.Serializer;
import dijjer.util.VeryLongInteger;
/*
* Created on Nov 27, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
/**
* @author ian
*
* To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code and
* Comments
*/
public class SerializerTest extends TestCase {
public static final String VERSION = "$Id: SerializerTest.java,v 1.8 2005/04/12 13:04:15 sanity Exp $";
public SerializerTest(String name) {
super(name);
}
public void testReadWrite() throws IOException {
Random r = new Random(0);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
Object[] written = new Object[10];
written[0] = new Boolean(true);
written[1] = new Byte((byte) r.nextInt());
written[2] = new Short((short) r.nextInt());
written[3] = new Integer(r.nextInt());
written[4] = new Long(r.nextLong());
written[5] = Long.toString(r.nextLong());
byte[] buf = new byte[r.nextInt() % 100];
r.nextBytes(buf);
written[6] = new Buffer(buf);
written[7] = new VeryLongInteger(r.nextLong(), r.nextLong());
written[8] = new Peer(InetAddress.getByName("google.com"), 1234);
BitArray ba = new BitArray(r.nextInt() % 100);
for (int x=0; x<ba.getSize(); x++) {
ba.setBit(x, r.nextBoolean());
}
written[9] = ba;
// TODO: Buffer and others, lists
for (int x=0; x<written.length; x++) {
Serializer.writeToDataOutputStream(written[x], dos);
System.out.println("Wrote "+written[x].getClass());
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
DataInputStream dis = new DataInputStream(bais);
for (int x=0; x<written.length; x++) {
Object read = Serializer.readFromDataInputStream(written[x].getClass(), dis);
assertEquals("Incorrect serialization of "+written[x].getClass(), written[x], read);
System.out.println("Read "+written[x].getClass());
}
}
}