@Test
public void testSerialization() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
Tuple t = new Tuple();
t.addAttribute("Hello world!");
try {
t.write(new OutputViewDataOutputStreamWrapper(dos));
} catch (IOException e1) {
e1.printStackTrace();
}
t.addAttribute("2ndAttribute");
try {
t.write(new OutputViewDataOutputStreamWrapper(dos));
} catch (IOException e) {
e.printStackTrace();
}
byte[] ba = "attr1|attr2|3|4|attr5|thisdoesnotbelongtothetuple".getBytes();
int[] of2 = {0,6,12,14,16,22};
t = new Tuple(ba, of2, 5);
try {
t.write(new OutputViewDataOutputStreamWrapper(dos));
} catch (IOException e) {
e.printStackTrace();
}
try {
dos.flush();
baos.flush();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
try {
dos.close();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
t = new Tuple();
try {
t.read(new InputViewDataInputStreamWrapper(dis));
Assert.assertTrue(t.getNumberOfColumns() == 1);
Assert.assertTrue(t.getStringValueAt(0).equals("Hello world!"));
Assert.assertTrue(t.toString().equals("Hello world!|"));
} catch (IOException e) {
e.printStackTrace();
}
t = new Tuple();
try {
t.read(new InputViewDataInputStreamWrapper(dis));
Assert.assertTrue(t.getNumberOfColumns() == 2);
Assert.assertTrue(t.getStringValueAt(0).equals("Hello world!"));
Assert.assertTrue(t.getStringValueAt(1).equals("2ndAttribute"));
Assert.assertTrue(t.toString().equals("Hello world!|2ndAttribute|"));
} catch (IOException e) {
e.printStackTrace();
}
t = new Tuple();
try {
t.read(new InputViewDataInputStreamWrapper(dis));
Assert.assertTrue(t.getNumberOfColumns() == 5);
Assert.assertTrue(t.getStringValueAt(0).equals("attr1"));
Assert.assertTrue(t.getStringValueAt(1).equals("attr2"));
Assert.assertTrue(t.getLongValueAt(2) == 3);
Assert.assertTrue(t.getLongValueAt(3) == 4);
Assert.assertTrue(t.getStringValueAt(4).equals("attr5"));
Assert.assertTrue(t.toString().equals("attr1|attr2|3|4|attr5|"));
} catch (IOException e) {
e.printStackTrace();
}
try {