/*
* Copyright (c) 2006 Leonardo "Bubble" Mesquita
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.bubble.serializer;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Vector;
import junit.framework.TestCase;
import com.bubble.serializer.objects.ExtremePojo;
import com.bubble.serializer.objects.Pojo;
import com.bubble.serializer.objects.PrimitivePojo;
import com.bubble.serializer.objects.PrivatePrimitivePojo;
import com.bubble.utils.ArrayUtils;
public class SerializationTest extends TestCase {
public void testPrimitivePojo() {
boolean boolValue = generateBooleanTestValue();
byte byteValue = generateByteTestValue();
short shortValue = generateShortTestValue();
int intValue = generateIntTestValue();
long longValue = generateLongTestValue();
float floatValue = generateFloatTestValue();
double doubleValue = generateDoubleTestValue();
char charValue = generateCharTestValue();
PrimitivePojo pojo = new PrimitivePojo(
boolValue,
byteValue,
shortValue,
intValue,
longValue,
floatValue,
doubleValue,
charValue
);
SerializationContext ctx = new SerializationContext();
ByteBuffer buffer = ByteBuffer.allocate(1024);
ctx.serialize(pojo, buffer);
buffer.flip();
DeserializationContext dctx = new DeserializationContext();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be a PrimitivePojo", obj instanceof PrimitivePojo);
PrimitivePojo response = (PrimitivePojo)obj;
assertEquals(boolValue, response.isBooleanField());
assertEquals(byteValue, response.getByteField());
assertEquals(shortValue, response.getShortField());
assertEquals(intValue, response.getIntField());
assertEquals(longValue, response.getLongField());
assertEquals(floatValue, response.getFloatField(), 0.0);
assertEquals(doubleValue, response.getDoubleField(), 0.0);
assertEquals(charValue, response.getCharField());
System.out.println(pojo);
System.out.println(response);
}
public void testPrivatePrimitivePojo() {
boolean boolValue = generateBooleanTestValue();
byte byteValue = generateByteTestValue();
short shortValue = generateShortTestValue();
int intValue = generateIntTestValue();
long longValue = generateLongTestValue();
float floatValue = generateFloatTestValue();
double doubleValue = generateDoubleTestValue();
char charValue = generateCharTestValue();
PrivatePrimitivePojo pojo = new PrivatePrimitivePojo(
boolValue,
byteValue,
shortValue,
intValue,
longValue,
floatValue,
doubleValue,
charValue
);
SerializationContext ctx = new SerializationContext();
ByteBuffer buffer = ByteBuffer.allocate(1024);
ctx.serialize(pojo, buffer);
buffer.flip();
DeserializationContext dctx = new DeserializationContext();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be a PrimitivePojo", obj instanceof PrivatePrimitivePojo);
PrivatePrimitivePojo response = (PrivatePrimitivePojo)obj;
assertEquals(boolValue, response.isBooleanField());
assertEquals(byteValue, response.getByteField());
assertEquals(shortValue, response.getShortField());
assertEquals(intValue, response.getIntField());
assertEquals(longValue, response.getLongField());
assertEquals(floatValue, response.getFloatField(), 0.0);
assertEquals(doubleValue, response.getDoubleField(), 0.0);
assertEquals(charValue, response.getCharField());
System.out.println(pojo);
System.out.println(response);
}
public void testPrimitiveArray() {
boolean[] boolArray = generateBooleanArrayTestValue();
byte[] byteArray = generateByteArrayTestValue();
short[] shortArray = generateShortArrayTestValue();
int[] intArray = generateIntArrayTestValue();
long[] longArray = generateLongArrayTestValue();
float[] floatArray = generateFloatArrayTestValue();
double[] doubleArray = generateDoubleArrayTestValue();
char[] charArray = generateCharArrayTestValue();
SerializationContext ctx = new SerializationContext();
DeserializationContext dctx = new DeserializationContext();
ByteBuffer buffer = ByteBuffer.allocate(1024);
ctx.serialize(boolArray, buffer);
buffer.flip();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be a boolean[]", obj instanceof boolean[]);
boolean[] boolResponse = (boolean[])obj;
assertEquals(boolArray.length, boolResponse.length);
for (int i = 0; i < boolResponse.length; i++) {
assertEquals(boolArray[i], boolResponse[i]);
}
buffer.clear();
ctx.serialize(byteArray, buffer);
buffer.flip();
obj = dctx.deserialize(buffer);
assertTrue("Object must be a byte[]", obj instanceof byte[]);
byte[] byteResponse = (byte[])obj;
assertEquals(byteArray.length, byteResponse.length);
for (int i = 0; i < byteResponse.length; i++) {
assertEquals(byteArray[i], byteResponse[i]);
}
buffer.clear();
ctx.serialize(shortArray, buffer);
buffer.flip();
obj = dctx.deserialize(buffer);
assertTrue("Object must be a short[]", obj instanceof short[]);
short[] shortResponse = (short[])obj;
assertEquals(shortArray.length, shortResponse.length);
for (int i = 0; i < shortResponse.length; i++) {
assertEquals(shortArray[i], shortResponse[i]);
}
buffer.clear();
ctx.serialize(intArray, buffer);
buffer.flip();
obj = dctx.deserialize(buffer);
assertTrue("Object must be a int[]", obj instanceof int[]);
int[] intResponse = (int[])obj;
assertEquals(intArray.length, intResponse.length);
for (int i = 0; i < intResponse.length; i++) {
assertEquals(intArray[i], intResponse[i]);
}
buffer.clear();
ctx.serialize(longArray, buffer);
buffer.flip();
obj = dctx.deserialize(buffer);
assertTrue("Object must be a long[]", obj instanceof long[]);
long[] longResponse = (long[])obj;
assertEquals(longArray.length, longResponse.length);
for (int i = 0; i < longResponse.length; i++) {
assertEquals(longArray[i], longResponse[i]);
}
buffer.clear();
ctx.serialize(floatArray, buffer);
buffer.flip();
obj = dctx.deserialize(buffer);
assertTrue("Object must be a float[]", obj instanceof float[]);
float[] floatResponse = (float[])obj;
assertEquals(floatArray.length, floatResponse.length);
for (int i = 0; i < floatResponse.length; i++) {
assertEquals(floatArray[i], floatResponse[i], 0.0);
}
buffer.clear();
ctx.serialize(doubleArray, buffer);
buffer.flip();
obj = dctx.deserialize(buffer);
assertTrue("Object must be a double[]", obj instanceof double[]);
double[] doubleResponse = (double[])obj;
assertEquals(doubleArray.length, doubleResponse.length);
for (int i = 0; i < doubleResponse.length; i++) {
assertEquals(doubleArray[i], doubleResponse[i], 0.0);
}
buffer.clear();
ctx.serialize(charArray, buffer);
buffer.flip();
obj = dctx.deserialize(buffer);
assertTrue("Object must be a char[]", obj instanceof char[]);
char[] charResponse = (char[])obj;
assertEquals(charArray.length, charResponse.length);
for (int i = 0; i < charResponse.length; i++) {
assertEquals(charArray[i], charResponse[i]);
}
System.out.println(ArrayUtils.toString(boolArray));
System.out.println(ArrayUtils.toString(boolResponse));
System.out.println(ArrayUtils.toString(byteArray));
System.out.println(ArrayUtils.toString(byteResponse));
System.out.println(ArrayUtils.toString(shortArray));
System.out.println(ArrayUtils.toString(shortResponse));
System.out.println(ArrayUtils.toString(intArray));
System.out.println(ArrayUtils.toString(intResponse));
System.out.println(ArrayUtils.toString(longArray));
System.out.println(ArrayUtils.toString(longResponse));
System.out.println(ArrayUtils.toString(floatArray));
System.out.println(ArrayUtils.toString(floatResponse));
System.out.println(ArrayUtils.toString(doubleArray));
System.out.println(ArrayUtils.toString(doubleResponse));
System.out.println(ArrayUtils.toString(charArray));
System.out.println(ArrayUtils.toString(charResponse));
}
private static final int DIM_SIZE = 5;
public void testMultiDimensionalArray() {
int[][][] intArray = new int[DIM_SIZE][DIM_SIZE][];
for (int i = 0; i < intArray.length; i++) {
for (int j = 0; j < intArray[i].length; j++) {
intArray[i][j] = generateIntArrayTestValue();
}
}
SerializationContext ctx = new SerializationContext();
DeserializationContext dctx = new DeserializationContext();
ByteBuffer buffer = ByteBuffer.allocate(4096);
ctx.serialize(intArray, buffer);
buffer.flip();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be a int[][][]", obj instanceof int[][][]);
int[][][] response = (int[][][])obj;
assertEquals(intArray.length, response.length);
for (int i = 0; i < response.length; i++) {
assertEquals(intArray[i].length, response[i].length);
for (int j = 0; j < response[i].length; j++) {
assertEquals(intArray[i][j].length, response[i][j].length);
for (int k = 0; k < response.length; k++) {
assertEquals(intArray[i][j][k], response[i][j][k]);
}
}
}
}
public void testObjectArray() {
int len = (int)generateTestValue(10, 20);
Pojo[] pojoArray = new Pojo[len];
for (int i = 0; i < pojoArray.length; i++) {
pojoArray[i] = new Pojo(generateIntTestValue());
}
SerializationContext ctx = new SerializationContext();
DeserializationContext dctx = new DeserializationContext();
ByteBuffer buffer = ByteBuffer.allocate(4096);
ctx.serialize(pojoArray, buffer);
buffer.flip();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be a Pojo[]", obj instanceof Pojo[]);
Pojo[] response = (Pojo[])obj;
assertEquals(pojoArray.length, response.length);
for (int i = 0; i < response.length; i++) {
assertEquals(pojoArray[i].getValue(), response[i].getValue());
}
}
public void testString() {
String testString = generateTestString();
SerializationContext ctx = new SerializationContext();
ByteBuffer buffer = ByteBuffer.allocate(1024);
ctx.serialize(testString, buffer);
buffer.flip();
DeserializationContext dctx = new DeserializationContext();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be a String", obj instanceof String);
String response = (String)obj;
assertEquals(testString, response);
System.out.println(testString);
System.out.println(response);
}
public void testStringArray() {
String[]testArray = generateTestStringArray();
SerializationContext ctx = new SerializationContext();
DeserializationContext dctx = new DeserializationContext();
ByteBuffer buffer = ByteBuffer.allocate(4096);
ctx.serialize(testArray, buffer);
buffer.flip();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be a String[]", obj instanceof String[]);
String[] response = (String[])obj;
assertEquals(testArray.length, response.length);
for (int i = 0; i < response.length; i++) {
assertEquals(testArray[i], response[i]);
}
}
public void testExtremePojo() {
ExtremePojo pojo = generateExtremePojoNode();
pojo = generateExtremePojoLink(pojo);
pojo = generateExtremePojoLink(pojo);
SerializationContext ctx = new SerializationContext();
DeserializationContext dctx = new DeserializationContext();
ByteBuffer buffer = ByteBuffer.allocate(1048576);
ctx.serialize(pojo, buffer);
buffer.flip();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be an ExtremePojo", obj instanceof ExtremePojo);
ExtremePojo response = (ExtremePojo)obj;
assertEquals(pojo, response);
}
public void testLightExtremePojo() {
ExtremePojo pojo = generateExtremePojoNode();
pojo = generateExtremePojoLink(pojo);
pojo = generateExtremePojoLink(pojo);
LightSerializationContext ctx = new LightSerializationContext();
LightDeserializationContext dctx = new LightDeserializationContext();
ByteBuffer buffer = ByteBuffer.allocate(1048576);
ctx.serialize(pojo, buffer);
buffer.flip();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be an ExtremePojo", obj instanceof ExtremePojo);
ExtremePojo response = (ExtremePojo)obj;
assertEquals(pojo, response);
}
public void testJavaVector() {
//NOTE: Only works with Vector because it uses default serialization underneath.
// Other java.* objects that resort to "writeObject" will not work
// in the current version.
Vector v = new Vector();
Pojo pojo = new Pojo(generateIntTestValue());
v.add(pojo);
SerializationContext ctx = new SerializationContext();
DeserializationContext dctx = new DeserializationContext();
ByteBuffer buffer = ByteBuffer.allocate(1048576);
ctx.serialize(v, buffer);
buffer.flip();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be a Vector", obj instanceof Vector);
Vector response = (Vector)obj;
assertEquals(v.size(), response.size());
assertTrue(response.get(0) instanceof Pojo);
Pojo respojo = (Pojo)response.get(0);
assertEquals(pojo.getValue(), respojo.getValue());
}
public void testUpdateArray() {
final int LEN = 10;
int[] tmp = new int[LEN];
int[] intArray1 = generateIntArrayTestValue(LEN);
int[] intArray2 = generateIntArrayTestValue(LEN);
SerializationContext ctx = new SerializationContext();
DeserializationContext dctx = new DeserializationContext();
ByteBuffer buffer = ByteBuffer.allocate(4096);
ctx.serialize(intArray1, buffer);
ctx.serialize(intArray2, buffer);
System.arraycopy(intArray1, 0, tmp, 0, LEN);
System.arraycopy(intArray2, 0, intArray1, 0, LEN);
System.arraycopy(tmp, 0, intArray2, 0, LEN);
ctx.update(intArray1, buffer);
ctx.update(intArray2, buffer);
buffer.flip();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be an int[]", obj instanceof int[]);
int[] resp1 = (int[])obj;
obj = dctx.deserialize(buffer);
assertTrue("Object must be an int[]", obj instanceof int[]);
int[] resp2 = (int[])obj;
assertTrue(Arrays.equals(resp1, intArray2));
assertTrue(Arrays.equals(resp2, intArray1));
obj = dctx.deserialize(buffer);
assertTrue("Object must be an int[]", obj instanceof int[]);
int[] resp3 = (int[])obj;
obj = dctx.deserialize(buffer);
assertTrue("Object must be an int[]", obj instanceof int[]);
int[] resp4 = (int[])obj;
assertTrue(Arrays.equals(resp3, intArray1));
assertTrue(Arrays.equals(resp4, intArray2));
assertTrue(Arrays.equals(resp1, intArray1));
assertTrue(Arrays.equals(resp2, intArray2));
assertSame(resp1, resp3);
assertSame(resp2, resp4);
}
public void testUpdate() {
final int FIRST_VALUE = generateIntTestValue();
final int SECOND_VALUE = generateIntTestValue();
Pojo pojo1 = new Pojo(FIRST_VALUE);
Pojo pojo2 = new Pojo(SECOND_VALUE);
SerializationContext ctx = new SerializationContext();
DeserializationContext dctx = new DeserializationContext();
ByteBuffer buffer = ByteBuffer.allocate(4096);
ctx.serialize(pojo1, buffer);
ctx.serialize(pojo2, buffer);
pojo1.setValue(SECOND_VALUE);
pojo2.setValue(FIRST_VALUE);
ctx.update(pojo2, buffer);
ctx.update(pojo1, buffer);
buffer.flip();
Object obj = dctx.deserialize(buffer);
assertTrue("Object must be a Pojo", obj instanceof Pojo);
Pojo resp1 = (Pojo)obj;
obj = dctx.deserialize(buffer);
assertTrue("Object must be a Pojo", obj instanceof Pojo);
Pojo resp2 = (Pojo)obj;
assertEquals(FIRST_VALUE, resp1.getValue());
assertEquals(SECOND_VALUE, resp2.getValue());
obj = dctx.deserialize(buffer);
assertTrue("Object must be a Pojo", obj instanceof Pojo);
Pojo resp3 = (Pojo)obj;
obj = dctx.deserialize(buffer);
assertTrue("Object must be a Pojo", obj instanceof Pojo);
Pojo resp4 = (Pojo)obj;
assertEquals(FIRST_VALUE, resp3.getValue());
assertEquals(SECOND_VALUE, resp4.getValue());
assertEquals(SECOND_VALUE, resp1.getValue());
assertEquals(FIRST_VALUE, resp2.getValue());
assertSame(resp1, resp4);
assertSame(resp2, resp3);
}
private boolean generateBooleanTestValue() {
return (Math.random() > 0.5);
}
private boolean[] generateBooleanArrayTestValue() {
int len = (int)generateTestValue(10, 20);
boolean[] result = new boolean[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateBooleanTestValue();
}
return result;
}
private byte[] generateByteArrayTestValue() {
int len = (int)generateTestValue(10, 20);
byte[] result = new byte[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateByteTestValue();
}
return result;
}
private short[] generateShortArrayTestValue() {
int len = (int)generateTestValue(10, 20);
short[] result = new short[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateShortTestValue();
}
return result;
}
private int[] generateIntArrayTestValue() {
int len = (int)generateTestValue(10, 20);
return generateIntArrayTestValue(len);
}
private int[] generateIntArrayTestValue(int len) {
int[] result = new int[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateIntTestValue();
}
return result;
}
private long[] generateLongArrayTestValue() {
int len = (int)generateTestValue(10, 20);
long[] result = new long[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateLongTestValue();
}
return result;
}
private float[] generateFloatArrayTestValue() {
int len = (int)generateTestValue(10, 20);
float[] result = new float[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateFloatTestValue();
}
return result;
}
private double[] generateDoubleArrayTestValue() {
int len = (int)generateTestValue(10, 20);
double[] result = new double[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateDoubleTestValue();
}
return result;
}
private char[] generateCharArrayTestValue() {
int len = (int)generateTestValue(10, 20);
char[] result = new char[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateCharTestValue();
}
return result;
}
private byte generateByteTestValue() {
return (byte)generateTestValue(Byte.MIN_VALUE, Byte.MAX_VALUE);
}
private short generateShortTestValue() {
return (short)generateTestValue(Short.MIN_VALUE, Short.MAX_VALUE);
}
private int generateIntTestValue() {
return (int)generateTestValue(Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private long generateLongTestValue() {
return (long)generateTestValue(Long.MIN_VALUE, Long.MAX_VALUE);
}
private float generateFloatTestValue() {
return (float)generateTestValue(Float.MIN_VALUE, Float.MAX_VALUE);
}
private double generateDoubleTestValue() {
return generateTestValue(Double.MIN_VALUE, Double.MAX_VALUE);
}
private char generateCharTestValue() {
char c;
// Generate 40% Unicode, 60% ASCII
if(Math.random() < 0.4) {
c = (char)generateTestValue(Character.MIN_VALUE, Character.MAX_VALUE-0x800);
// Avoids surrogate region
if(c >= 0xD800) {
c += 0x800;
}
} else {
c = (char)generateTestValue('\u0000', '\u007f');
}
return c;
}
private double generateTestValue(double min, double max) {
double x = Math.random();
return (x*max + (1-x)*min);
}
private String generateTestString() {
char[] chars = generateCharArrayTestValue();
return new String(chars);
}
private String[] generateTestStringArray() {
int len = (int)generateTestValue(10, 20);
String[] result = new String[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateTestString();
}
return result;
}
private ExtremePojo generateLeafExtremePojo() {
ExtremePojo result = new ExtremePojo();
result.booleanField = generateBooleanTestValue();
result.byteField = generateByteTestValue();
result.shortField = generateShortTestValue();
result.intField = generateIntTestValue();
result.longField = generateLongTestValue();
result.floatField = generateFloatTestValue();
result.doubleField = generateDoubleTestValue();
result.charField = generateCharTestValue();
result.booleanArrayField = generateBooleanArrayTestValue();
result.byteArrayField = generateByteArrayTestValue();
result.shortArrayField = generateShortArrayTestValue();
result.intArrayField = generateIntArrayTestValue();
result.longArrayField = generateLongArrayTestValue();
result.floatArrayField = generateFloatArrayTestValue();
result.doubleArrayField = generateDoubleArrayTestValue();
result.charArrayField = generateCharArrayTestValue();
result.stringField = generateTestString();
result.stringArrayField = generateTestStringArray();
return result;
}
private ExtremePojo[] generateExtremePojoArray() {
int len = (int)generateTestValue(10, 20);
ExtremePojo[] result = new ExtremePojo[len];
for (int i = 0; i < result.length; i++) {
result[i] = generateLeafExtremePojo();
}
return result;
}
private ExtremePojo[][] generateExtremePojoMultiArray() {
int len = (int)generateTestValue(3, 6);
ExtremePojo[][] result = new ExtremePojo[len][];
for (int i = 0; i < result.length; i++) {
result[i] = generateExtremePojoArray();
}
return result;
}
private ExtremePojo generateExtremePojoNode() {
ExtremePojo result = generateLeafExtremePojo();
result.objectField = generateLeafExtremePojo();
result.objectArrayField = generateExtremePojoArray();
result.objectMultiArrayField = generateExtremePojoMultiArray();
return result;
}
private ExtremePojo generateExtremePojoLink(ExtremePojo link) {
ExtremePojo result = generateLeafExtremePojo();
result.objectField = link;
result.objectArrayField = generateExtremePojoArray();
result.objectMultiArrayField = generateExtremePojoMultiArray();
return result;
}
}