@Test public void serializingEmptyDictionaryShouldBeCorrect() throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(100); BencodeDictionary bencodeDictionary = new BencodeDictionary(); bencodeDictionary.serialize(byteArrayOutputStream); String s = byteArrayOutputStream.toString(); Assert.assertEquals("de", s); }
@Test public void serializingDictionaryShouldBeCorrect() throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(100); BencodeDictionary bencodeDictionary = new BencodeDictionary(); bencodeDictionary.addItem("key1", new BencodeInteger(5)); bencodeDictionary.addItem("key2", new BencodeString("hello")); bencodeDictionary.serialize(byteArrayOutputStream); String s = byteArrayOutputStream.toString(); Assert.assertEquals("d4:key1i5e4:key25:helloe", s); }
@Test public void serializingNestedListInDictionaryShouldBeCorrect() throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(100); BencodeDictionary bencodeDictionary = new BencodeDictionary(); BencodeList bencodeList = new BencodeList(); bencodeList.addItem(new BencodeInteger(5)); bencodeList.addItem(new BencodeInteger(2)); bencodeDictionary.addItem("key", bencodeList); bencodeDictionary.serialize(byteArrayOutputStream); String s = byteArrayOutputStream.toString(); Assert.assertEquals("d3:keyli5ei2eee", s); }
@Test public void parsingDictionaryShouldBeCorrect() throws IOException, BadBencodingException { byte[] buffer; buffer = "d4:key15:Hello4:key2i5ee".getBytes(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer); BencodeParser parser = new BencodeParser(byteArrayInputStream); BencodeDictionary dictionary = (BencodeDictionary) parser.parse(); Map<String, BencodeItem> map = dictionary.getMap(); Assert.assertNotNull(map.get("key1")); Assert.assertNotNull(map.get("key2")); Assert.assertEquals("Hello", ((BencodeString) map.get("key1")).getString()); Assert.assertEquals(5, ((BencodeInteger) map.get("key2")).getInteger()); }