From 558375f8536852022f3fe1220b39c65f076c9fb6 Mon Sep 17 00:00:00 2001 From: Adam Peck Date: Thu, 27 Mar 2025 22:23:32 -0600 Subject: [PATCH] Cleanup test code --- .../java/com/dampcake/bencode/Assert.java | 21 ---- .../bencode/BencodeInputStreamTest.java | 95 +++---------------- .../bencode/BencodeOutputStreamTest.java | 77 +++++---------- .../java/com/dampcake/bencode/Runnable.java | 10 -- 4 files changed, 37 insertions(+), 166 deletions(-) delete mode 100644 src/test/java/com/dampcake/bencode/Assert.java delete mode 100644 src/test/java/com/dampcake/bencode/Runnable.java diff --git a/src/test/java/com/dampcake/bencode/Assert.java b/src/test/java/com/dampcake/bencode/Assert.java deleted file mode 100644 index b173e75..0000000 --- a/src/test/java/com/dampcake/bencode/Assert.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.dampcake.bencode; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -/** - * Custom assertions. - * - * @author Adam Peck - */ -class Assert { - - public static void assertThrows(Class expected, Runnable block) { - try { - block.run(); - fail("No exception thrown"); - } catch (Throwable t) { - assertEquals("Unexpected exception thrown: " + t.getClass() + "\n" + t.getMessage(), expected, t.getClass()); - } - } -} diff --git a/src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java b/src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java index 8cf35ec..1dd579a 100644 --- a/src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java +++ b/src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java @@ -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; /** @@ -108,12 +108,7 @@ 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()); } @@ -121,12 +116,7 @@ public void run() throws Exception { 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()); } @@ -134,12 +124,7 @@ public void run() throws Exception { 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()); } @@ -163,12 +148,7 @@ 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()); } @@ -176,12 +156,7 @@ public void run() throws Exception { 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()); } @@ -189,12 +164,7 @@ public void run() throws Exception { 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()); } @@ -210,12 +180,7 @@ 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()); } @@ -223,12 +188,7 @@ public void run() throws Exception { 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()); } @@ -236,12 +196,7 @@ public void run() throws Exception { 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()); } @@ -304,12 +259,7 @@ 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()); } @@ -317,12 +267,7 @@ public void run() throws Exception { 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()); } @@ -393,12 +338,7 @@ 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()); } @@ -406,12 +346,7 @@ public void run() throws Exception { 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()); } } diff --git a/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java b/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java index 76bacfd..697ab78 100644 --- a/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java +++ b/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java @@ -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; @@ -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. @@ -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); } @@ -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); } @@ -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); } @@ -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() {{ - add("Hello"); - add(ByteBuffer.wrap("World!".getBytes())); - add(new ArrayList() {{ - add(null); - add(456); - }}); - }}); - } - }); - + ThrowingRunnable runnable = () -> instance.writeList(new ArrayList() {{ + add("Hello"); + add(ByteBuffer.wrap("World!".getBytes())); + add(new ArrayList() {{ + 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() {{ put("string", "value"); @@ -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() {{ put(123, ByteBuffer.wrap("test".getBytes())); put(456, "thing"); }}); @@ -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() {{ - put("string", "value"); - put(123, "number-key"); - }}); - } - }); - + ThrowingRunnable runnable = () -> instance.writeDictionary(new TreeMap() {{ + 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); } } diff --git a/src/test/java/com/dampcake/bencode/Runnable.java b/src/test/java/com/dampcake/bencode/Runnable.java deleted file mode 100644 index 64f7ac5..0000000 --- a/src/test/java/com/dampcake/bencode/Runnable.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.dampcake.bencode; - -/** - * Runnable interface for testing. - * - * @author Adam Peck - */ -interface Runnable { - void run() throws Exception; -}