1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
package com.esotericsoftware.kryo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import junit.framework.TestCase;
import org.junit.Assert;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.minlog.Log;
/** Convenience methods for round tripping objects.
* @author Nathan Sweet <misc@n4te.com> */
abstract public class KryoTestCase extends TestCase {
protected Kryo kryo;
protected Output output;
protected Input input;
protected Object object1, object2;
protected boolean supportsCopy;
protected void setUp () throws Exception {
Log.TRACE();
kryo = new Kryo();
kryo.setReferences(false);
kryo.setRegistrationRequired(true);
}
public <T> T roundTrip (int length, T object1) {
this.object1 = object1;
// Test output to stream, large buffer.
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
output = new Output(outStream, 4096);
kryo.writeClassAndObject(output, object1);
output.flush();
// Test input from stream, large buffer.
input = new Input(new ByteArrayInputStream(outStream.toByteArray()), 4096);
object2 = kryo.readClassAndObject(input);
assertEquals(object1, object2);
assertEquals("Incorrect number of bytes read.", length, input.total());
assertEquals("Incorrect number of bytes read.", length, output.total());
// Test output to stream, small buffer.
outStream = new ByteArrayOutputStream();
output = new Output(outStream, 10);
kryo.writeClassAndObject(output, object1);
output.flush();
// Test input from stream, small buffer.
input = new Input(new ByteArrayInputStream(outStream.toByteArray()), 10);
object2 = kryo.readClassAndObject(input);
assertEquals(object1, object2);
assertEquals("Incorrect number of bytes read.", length, input.total());
if (object1 != null) {
// Test null with serializer.
Serializer serializer = kryo.getRegistration(object1.getClass()).getSerializer();
output.clear();
outStream.reset();
kryo.writeObjectOrNull(output, null, serializer);
output.flush();
// Test null from byte array with and without serializer.
input = new Input(new ByteArrayInputStream(outStream.toByteArray()), 10);
assertEquals(null, kryo.readObjectOrNull(input, object1.getClass(), serializer));
input = new Input(new ByteArrayInputStream(outStream.toByteArray()), 10);
assertEquals(null, kryo.readObjectOrNull(input, object1.getClass()));
}
// Test output to byte array.
output = new Output(length * 2, -1);
kryo.writeClassAndObject(output, object1);
output.flush();
// Test input from byte array.
input = new Input(output.toBytes());
object2 = kryo.readClassAndObject(input);
assertEquals(object1, object2);
assertEquals("Incorrect length.", length, output.total());
assertEquals("Incorrect number of bytes read.", length, input.total());
input.rewind();
if (supportsCopy) {
// Test copy.
T copy = kryo.copy(object1);
assertEquals(object1, copy);
copy = kryo.copyShallow(object1);
assertEquals(object1, copy);
}
return (T)object2;
}
static public void assertEquals (Object object1, Object object2) {
Assert.assertEquals(arrayToList(object1), arrayToList(object2));
}
static public Object arrayToList (Object array) {
if (array == null || !array.getClass().isArray()) return array;
ArrayList list = new ArrayList(Array.getLength(array));
for (int i = 0, n = Array.getLength(array); i < n; i++)
list.add(arrayToList(Array.get(array, i)));
return list;
}
static public ArrayList list (Object... items) {
ArrayList list = new ArrayList();
for (Object item : items)
list.add(item);
return list;
}
}
|