Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions src/test/java/com/dampcake/bencode/Assert.java

This file was deleted.

95 changes: 15 additions & 80 deletions src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import java.util.List;
import java.util.Map;

import static com.dampcake.bencode.Assert.assertThrows;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -108,38 +108,23 @@ public void testReadStringEmpty() throws Exception {
public void testReadStringNaN() throws Exception {
instantiate("1c3:Testing");

assertThrows(InvalidObjectException.class, new Runnable() {
public void run() throws Exception {
instance.readString();
}
});

assertThrows(InvalidObjectException.class, instance::readString);
assertEquals(10, instance.available());
}

@Test
public void testReadStringEOF() throws Exception {
instantiate("123456");

assertThrows(EOFException.class, new Runnable() {
public void run() throws Exception {
instance.readString();
}
});

assertThrows(EOFException.class, instance::readString);
assertEquals(0, instance.available());
}

@Test
public void testReadStringEmptyStream() throws Exception {
instantiate("");

assertThrows(EOFException.class, new Runnable() {
public void run() throws Exception {
instance.readString();
}
});

assertThrows(EOFException.class, instance::readString);
assertEquals(0, instance.available());
}

Expand All @@ -163,38 +148,23 @@ public void testReadStringEmptyByteArray() throws Exception {
public void testReadStringNaNByteArray() throws Exception {
instantiate("1c3:Testing");

assertThrows(InvalidObjectException.class, new Runnable() {
public void run() throws Exception {
instance.readStringBytes();
}
});

assertThrows(InvalidObjectException.class, instance::readStringBytes);
assertEquals(10, instance.available());
}

@Test
public void testReadStringEOFByteArray() throws Exception {
instantiate("123456");

assertThrows(EOFException.class, new Runnable() {
public void run() throws Exception {
instance.readStringBytes();
}
});

assertThrows(EOFException.class, instance::readStringBytes);
assertEquals(0, instance.available());
}

@Test
public void testReadStringEmptyStreamByteArray() throws Exception {
instantiate("");

assertThrows(EOFException.class, new Runnable() {
public void run() throws Exception {
instance.readStringBytes();
}
});

assertThrows(EOFException.class, instance::readStringBytes);
assertEquals(0, instance.available());
}

Expand All @@ -210,38 +180,23 @@ public void testReadNumber() throws Exception {
public void testReadNumberNaN() throws Exception {
instantiate("i123cbve1");

assertThrows(NumberFormatException.class, new Runnable() {
public void run() throws Exception {
instance.readNumber();
}
});

assertThrows(NumberFormatException.class, instance::readNumber);
assertEquals(1, instance.available());
}

@Test
public void testReadNumberEOF() throws Exception {
instantiate("i123");

assertThrows(EOFException.class, new Runnable() {
public void run() throws Exception {
instance.readNumber();
}
});

assertThrows(EOFException.class, instance::readNumber);
assertEquals(0, instance.available());
}

@Test
public void testReadNumberEmptyStream() throws Exception {
instantiate("");

assertThrows(EOFException.class, new Runnable() {
public void run() throws Exception {
instance.readNumber();
}
});

assertThrows(EOFException.class, instance::readNumber);
assertEquals(0, instance.available());
}

Expand Down Expand Up @@ -304,25 +259,15 @@ public void testReadListEmpty() throws Exception {
public void testReadListInvalidItem() throws Exception {
instantiate("l2:Worlde");

assertThrows(InvalidObjectException.class, new Runnable() {
public void run() throws Exception {
instance.readList();
}
});

assertThrows(InvalidObjectException.class, instance::readList);
assertEquals(4, instance.available());
}

@Test
public void testReadListEOF() throws Exception {
instantiate("l5:Hello");

assertThrows(EOFException.class, new Runnable() {
public void run() throws Exception {
instance.readList();
}
});

assertThrows(EOFException.class, instance::readList);
assertEquals(0, instance.available());
}

Expand Down Expand Up @@ -393,25 +338,15 @@ public void testReadDictionaryEmpty() throws Exception {
public void testReadDictionaryInvalidItem() throws Exception {
instantiate("d4:item5:value3:testing");

assertThrows(InvalidObjectException.class, new Runnable() {
public void run() throws Exception {
instance.readDictionary();
}
});

assertThrows(InvalidObjectException.class, instance::readDictionary);
assertEquals(4, instance.available());
}

@Test
public void testReadDictionaryEOF() throws Exception {
instantiate("d4:item5:test");

assertThrows(EOFException.class, new Runnable() {
public void run() throws Exception {
instance.readDictionary();
}
});

assertThrows(EOFException.class, instance::readDictionary);
assertEquals(0, instance.available());
}
}
77 changes: 22 additions & 55 deletions src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.Before;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
Expand All @@ -11,8 +12,8 @@
import java.util.TreeMap;
import java.util.concurrent.ConcurrentSkipListMap;

import static com.dampcake.bencode.Assert.assertThrows;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

/**
* Unit tests for BencodeOutputStream.
Expand Down Expand Up @@ -58,12 +59,7 @@ public void testWriteStringEmpty() throws Exception {

@Test
public void testWriteStringNull() throws Exception {
assertThrows(NullPointerException.class, new Runnable() {
public void run() throws Exception {
instance.writeString((String) null);
}
});

assertThrows(NullPointerException.class, () -> instance.writeString((String) null));
assertEquals(0, out.toByteArray().length);
}

Expand Down Expand Up @@ -97,12 +93,7 @@ public void testWriteStringEmptyByteArray() throws Exception {

@Test
public void testWriteStringNullByteArray() throws Exception {
assertThrows(NullPointerException.class, new Runnable() {
public void run() throws Exception {
instance.writeString((ByteBuffer) null);
}
});

assertThrows(NullPointerException.class, () -> instance.writeString((ByteBuffer) null));
assertEquals(0, out.toByteArray().length);
}

Expand All @@ -122,12 +113,7 @@ public void testWriteNumberDecimal() throws Exception {

@Test
public void testWriteNumberNull() throws Exception {
assertThrows(NullPointerException.class, new Runnable() {
public void run() throws Exception {
instance.writeNumber(null);
}
});

assertThrows(NullPointerException.class, () -> instance.writeNumber(null));
assertEquals(0, out.toByteArray().length);
}

Expand Down Expand Up @@ -155,35 +141,25 @@ public void testWriteListEmpty() throws Exception {

@Test
public void testWriteListNullItem() throws Exception {
assertThrows(NullPointerException.class, new Runnable() {
public void run() throws Exception {
instance.writeList(new ArrayList<Object>() {{
add("Hello");
add(ByteBuffer.wrap("World!".getBytes()));
add(new ArrayList<Object>() {{
add(null);
add(456);
}});
}});
}
});

ThrowingRunnable runnable = () -> instance.writeList(new ArrayList<Object>() {{
add("Hello");
add(ByteBuffer.wrap("World!".getBytes()));
add(new ArrayList<Object>() {{
add(null);
add(456);
}});
}});
assertThrows(NullPointerException.class, runnable);
assertEquals(0, out.toByteArray().length);
}

@Test
public void testWriteListNull() throws Exception {
assertThrows(NullPointerException.class, new Runnable() {
public void run() throws Exception {
instance.writeList(null);
}
});

assertThrows(NullPointerException.class, () -> instance.writeList(null));
assertEquals(0, out.toByteArray().length);
}

@Test
@SuppressWarnings("unchecked")
public void testWriteDictionary() throws Exception {
instance.writeDictionary(new LinkedHashMap<Object, Object>() {{
put("string", "value");
Expand All @@ -192,7 +168,7 @@ public void testWriteDictionary() throws Exception {
add("list-item-1");
add("list-item-2");
}});
put("dict", new ConcurrentSkipListMap() {{
put("dict", new ConcurrentSkipListMap<Integer, Object>() {{
put(123, ByteBuffer.wrap("test".getBytes()));
put(456, "thing");
}});
Expand All @@ -211,26 +187,17 @@ public void testWriteDictionaryEmpty() throws Exception {

@Test
public void testWriteDictionaryKeyCastException() throws Exception {
assertThrows(ClassCastException.class, new Runnable() {
public void run() throws Exception {
instance.writeDictionary(new TreeMap<Object, Object>() {{
put("string", "value");
put(123, "number-key");
}});
}
});

ThrowingRunnable runnable = () -> instance.writeDictionary(new TreeMap<Object, Object>() {{
put("string", "value");
put(123, "number-key");
}});
assertThrows(ClassCastException.class, runnable);
assertEquals(0, out.toByteArray().length);
}

@Test
public void testWriteDictionaryNull() throws Exception {
assertThrows(NullPointerException.class, new Runnable() {
public void run() throws Exception {
instance.writeDictionary(null);
}
});

assertThrows(NullPointerException.class, () -> instance.writeDictionary(null));
assertEquals(0, out.toByteArray().length);
}
}
10 changes: 0 additions & 10 deletions src/test/java/com/dampcake/bencode/Runnable.java

This file was deleted.