forked from rkapsi/bee-encode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some tests and fixed String decoding.
- Loading branch information
Showing
2 changed files
with
86 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,71 @@ | ||
package org.ardverk.coding; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
import static org.junit.Assert.*; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class BencodingTest { | ||
|
||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); | ||
BencodingOutputStream bEncoder = new BencodingOutputStream(byteStream); | ||
|
||
private BencodingInputStream decoder() { | ||
return decoder(false); | ||
} | ||
|
||
private BencodingInputStream decoder(boolean decodeAsString) { | ||
final byte[] bytes = byteStream.toByteArray(); | ||
System.out.println(new String(bytes)); | ||
return new BencodingInputStream(new ByteArrayInputStream(bytes), decodeAsString); | ||
} | ||
|
||
@Before | ||
public void setup() { | ||
byteStream = new ByteArrayOutputStream(); | ||
bEncoder = new BencodingOutputStream(byteStream); | ||
} | ||
|
||
@Test | ||
public void testString() throws IOException { | ||
bEncoder.writeString("test"); | ||
String result = decoder().readString(); | ||
assertEquals("test", result); | ||
} | ||
|
||
@Test | ||
public void testInt() throws IOException { | ||
bEncoder.writeInt(1); | ||
int result = decoder().readInt(); | ||
assertEquals(1, result); | ||
} | ||
|
||
@Test | ||
public void testCollectionsDecoding() throws IOException { | ||
ArrayList<Object> list = new ArrayList<Object>(); | ||
list.add("testA"); | ||
list.add("testB"); | ||
bEncoder.writeCollection(list); | ||
|
||
List<Object> result = decoder(true).readList(); | ||
assertEquals(list, result); | ||
} | ||
|
||
@Test | ||
public void testMapDecoding() throws IOException { | ||
Map<String, String> map = new TreeMap<String, String>(); | ||
map.put("keyA", "valueA"); | ||
map.put("keyB", "valueB"); | ||
bEncoder.writeMap(map); | ||
|
||
Map<String, Object> result = decoder(true).readMap(); | ||
assertEquals(map, result); | ||
} | ||
} |