Simplified serialisation format.

This commit is contained in:
akwizgran
2012-05-08 11:02:49 +02:00
parent a82ae82108
commit 78e18fb373
6 changed files with 85 additions and 82 deletions

View File

@@ -178,7 +178,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadList() throws Exception {
setContents("F5" + "03" + "01" + "83666F6F" + "FC0080");
setContents("F5" + "01" + "83666F6F" + "FC0080" + "F3");
List<Object> l = r.readList(Object.class);
assertNotNull(l);
assertEquals(3, l.size());
@@ -213,7 +213,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadShortMap() throws Exception {
setContents("B" + "2" + "83666F6F" + "7B" + "90" + "F0");
setContents("B" + "2" + "83666F6F" + "7B" + "90" + "F2");
Map<Object, Object> m = r.readMap(Object.class, Object.class);
assertNotNull(m);
assertEquals(2, m.size());
@@ -226,7 +226,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadMap() throws Exception {
setContents("F4" + "02" + "83666F6F" + "7B" + "90" + "F0");
setContents("F4" + "83666F6F" + "7B" + "90" + "F2" + "F3");
Map<Object, Object> m = r.readMap(Object.class, Object.class);
assertNotNull(m);
assertEquals(2, m.size());
@@ -239,7 +239,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadMapTypeSafe() throws Exception {
setContents("B" + "2" + "83666F6F" + "7B" + "80" + "F0");
setContents("B" + "2" + "83666F6F" + "7B" + "80" + "F2");
Map<String, Byte> m = r.readMap(String.class, Byte.class);
assertNotNull(m);
assertEquals(2, m.size());
@@ -268,7 +268,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadDelimitedList() throws Exception {
setContents("F3" + "01" + "83666F6F" + "FC0080" + "F1");
setContents("F5" + "01" + "83666F6F" + "FC0080" + "F3");
List<Object> l = r.readList(Object.class);
assertNotNull(l);
assertEquals(3, l.size());
@@ -280,7 +280,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadDelimitedListElements() throws Exception {
setContents("F3" + "01" + "83666F6F" + "FC0080" + "F1");
setContents("F5" + "01" + "83666F6F" + "FC0080" + "F3");
assertTrue(r.hasListStart());
r.readListStart();
assertFalse(r.hasListEnd());
@@ -296,7 +296,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadDelimitedListTypeSafe() throws Exception {
setContents("F3" + "01" + "02" + "03" + "F1");
setContents("F5" + "01" + "02" + "03" + "F3");
List<Byte> l = r.readList(Byte.class);
assertNotNull(l);
assertEquals(3, l.size());
@@ -308,7 +308,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadDelimitedMap() throws Exception {
setContents("F2" + "83666F6F" + "7B" + "90" + "F0" + "F1");
setContents("F4" + "83666F6F" + "7B" + "90" + "F2" + "F3");
Map<Object, Object> m = r.readMap(Object.class, Object.class);
assertNotNull(m);
assertEquals(2, m.size());
@@ -321,7 +321,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadDelimitedMapEntries() throws Exception {
setContents("F2" + "83666F6F" + "7B" + "90" + "F0" + "F1");
setContents("F4" + "83666F6F" + "7B" + "90" + "F2" + "F3");
assertTrue(r.hasMapStart());
r.readMapStart();
assertFalse(r.hasMapEnd());
@@ -340,7 +340,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadDelimitedMapTypeSafe() throws Exception {
setContents("F2" + "83666F6F" + "7B" + "80" + "F0" + "F1");
setContents("F4" + "83666F6F" + "7B" + "80" + "F2" + "F3");
Map<String, Byte> m = r.readMap(String.class, Byte.class);
assertNotNull(m);
assertEquals(2, m.size());
@@ -372,7 +372,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadStruct() throws Exception {
setContents("C0" + "83666F6F" + "EF" + "FF" + "83666F6F");
setContents("C0" + "83666F6F" + "F1" + "FF" + "83666F6F");
// Add object readers for two structs
r.addObjectReader(0, new ObjectReader<Foo>() {
public Foo readObject(Reader r) throws IOException {
@@ -395,7 +395,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadStructWithConsumer() throws Exception {
setContents("C0" + "83666F6F" + "EF" + "FF" + "83666F6F");
setContents("C0" + "83666F6F" + "F1" + "FF" + "83666F6F");
// Add object readers for two structs
r.addObjectReader(0, new ObjectReader<Foo>() {
public Foo readObject(Reader r) throws IOException {
@@ -427,7 +427,7 @@ public class ReaderImplTest extends BriarTestCase {
assertTrue(r.hasStruct(255));
assertEquals("foo", r.readStruct(255, Bar.class).s);
// Check that everything was passed to the consumer
assertEquals("C0" + "83666F6F" + "EF" + "FF" + "83666F6F",
assertEquals("C0" + "83666F6F" + "F1" + "FF" + "83666F6F",
StringUtils.toHexString(out.toByteArray()));
}

View File

@@ -179,8 +179,8 @@ public class WriterImplTest extends BriarTestCase {
List<Object> l = new ArrayList<Object>();
for(int i = 0; i < 16; i++) l.add(i);
w.writeList(l);
// LIST tag, length as uint7, elements as uint7
checkContents("F5" + "10" + "000102030405060708090A0B0C0D0E0F");
// LIST tag, elements as uint7, END tag
checkContents("F5" + "000102030405060708090A0B0C0D0E0F" + "F3");
}
@Test
@@ -191,7 +191,7 @@ public class WriterImplTest extends BriarTestCase {
l.add(2);
w.writeList(l);
// SHORT_LIST tag, length, 1 as uint7, null, 2 as uint7
checkContents("A" + "3" + "01" + "F0" + "02");
checkContents("A" + "3" + "01" + "F2" + "02");
}
@Test
@@ -212,10 +212,10 @@ public class WriterImplTest extends BriarTestCase {
Map<Object, Object> m = new LinkedHashMap<Object, Object>();
for(int i = 0; i < 16; i++) m.put(i, i + 1);
w.writeMap(m);
// MAP tag, size as uint7, entries as uint7
checkContents("F4" + "10" + "0001" + "0102" + "0203" + "0304" + "0405"
// MAP tag, entries as uint7, END tag
checkContents("F4" + "0001" + "0102" + "0203" + "0304" + "0405"
+ "0506" + "0607" + "0708" + "0809" + "090A" + "0A0B" + "0B0C"
+ "0C0D" + "0D0E" + "0E0F" + "0F10");
+ "0C0D" + "0D0E" + "0E0F" + "0F10" + "F3");
}
@Test
@@ -225,9 +225,9 @@ public class WriterImplTest extends BriarTestCase {
w.writeString("foo"); // Written as short string
w.writeIntAny(128L); // Written as an int16
w.writeListEnd();
// LIST_START tag, 1 as uint7, "foo" as short string, 128 as int16,
// LIST tag, 1 as uint7, "foo" as short string, 128 as int16,
// END tag
checkContents("F3" + "01" + "83666F6F" + "FC0080" + "F1");
checkContents("F5" + "01" + "83666F6F" + "FC0080" + "F3");
}
@Test
@@ -238,9 +238,9 @@ public class WriterImplTest extends BriarTestCase {
w.writeBytes(new byte[] {}); // Written as short bytes
w.writeNull();
w.writeMapEnd();
// MAP_START tag, "foo" as short string, 123 as uint7,
// MAP tag, "foo" as short string, 123 as uint7,
// byte[] {} as short bytes, NULL tag, END tag
checkContents("F2" + "83666F6F" + "7B" + "90" + "F0" + "F1");
checkContents("F4" + "83666F6F" + "7B" + "90" + "F2" + "F3");
}
@Test
@@ -261,7 +261,7 @@ public class WriterImplTest extends BriarTestCase {
@Test
public void testWriteNull() throws IOException {
w.writeNull();
checkContents("F0");
checkContents("F2");
}
@Test
@@ -278,7 +278,7 @@ public class WriterImplTest extends BriarTestCase {
w.writeStructId(32);
w.writeStructId(255);
// STRUCT tag, 32 as uint8, STRUCT tag, 255 as uint8
checkContents("EF" + "20" + "EF" + "FF");
checkContents("F1" + "20" + "F1" + "FF");
}
private void checkContents(String hex) throws IOException {