Skip to content

Commit 3f32524

Browse files
authored
Cleanup test code (#22)
1 parent 6673c58 commit 3f32524

File tree

4 files changed

+37
-166
lines changed

4 files changed

+37
-166
lines changed

src/test/java/com/dampcake/bencode/Assert.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java

Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import java.util.List;
1010
import java.util.Map;
1111

12-
import static com.dampcake.bencode.Assert.assertThrows;
1312
import static org.hamcrest.CoreMatchers.instanceOf;
13+
import static org.hamcrest.MatcherAssert.assertThat;
1414
import static org.junit.Assert.assertEquals;
15-
import static org.junit.Assert.assertThat;
15+
import static org.junit.Assert.assertThrows;
1616
import static org.junit.Assert.assertTrue;
1717

1818
/**
@@ -108,38 +108,23 @@ public void testReadStringEmpty() throws Exception {
108108
public void testReadStringNaN() throws Exception {
109109
instantiate("1c3:Testing");
110110

111-
assertThrows(InvalidObjectException.class, new Runnable() {
112-
public void run() throws Exception {
113-
instance.readString();
114-
}
115-
});
116-
111+
assertThrows(InvalidObjectException.class, instance::readString);
117112
assertEquals(10, instance.available());
118113
}
119114

120115
@Test
121116
public void testReadStringEOF() throws Exception {
122117
instantiate("123456");
123118

124-
assertThrows(EOFException.class, new Runnable() {
125-
public void run() throws Exception {
126-
instance.readString();
127-
}
128-
});
129-
119+
assertThrows(EOFException.class, instance::readString);
130120
assertEquals(0, instance.available());
131121
}
132122

133123
@Test
134124
public void testReadStringEmptyStream() throws Exception {
135125
instantiate("");
136126

137-
assertThrows(EOFException.class, new Runnable() {
138-
public void run() throws Exception {
139-
instance.readString();
140-
}
141-
});
142-
127+
assertThrows(EOFException.class, instance::readString);
143128
assertEquals(0, instance.available());
144129
}
145130

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

166-
assertThrows(InvalidObjectException.class, new Runnable() {
167-
public void run() throws Exception {
168-
instance.readStringBytes();
169-
}
170-
});
171-
151+
assertThrows(InvalidObjectException.class, instance::readStringBytes);
172152
assertEquals(10, instance.available());
173153
}
174154

175155
@Test
176156
public void testReadStringEOFByteArray() throws Exception {
177157
instantiate("123456");
178158

179-
assertThrows(EOFException.class, new Runnable() {
180-
public void run() throws Exception {
181-
instance.readStringBytes();
182-
}
183-
});
184-
159+
assertThrows(EOFException.class, instance::readStringBytes);
185160
assertEquals(0, instance.available());
186161
}
187162

188163
@Test
189164
public void testReadStringEmptyStreamByteArray() throws Exception {
190165
instantiate("");
191166

192-
assertThrows(EOFException.class, new Runnable() {
193-
public void run() throws Exception {
194-
instance.readStringBytes();
195-
}
196-
});
197-
167+
assertThrows(EOFException.class, instance::readStringBytes);
198168
assertEquals(0, instance.available());
199169
}
200170

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

213-
assertThrows(NumberFormatException.class, new Runnable() {
214-
public void run() throws Exception {
215-
instance.readNumber();
216-
}
217-
});
218-
183+
assertThrows(NumberFormatException.class, instance::readNumber);
219184
assertEquals(1, instance.available());
220185
}
221186

222187
@Test
223188
public void testReadNumberEOF() throws Exception {
224189
instantiate("i123");
225190

226-
assertThrows(EOFException.class, new Runnable() {
227-
public void run() throws Exception {
228-
instance.readNumber();
229-
}
230-
});
231-
191+
assertThrows(EOFException.class, instance::readNumber);
232192
assertEquals(0, instance.available());
233193
}
234194

235195
@Test
236196
public void testReadNumberEmptyStream() throws Exception {
237197
instantiate("");
238198

239-
assertThrows(EOFException.class, new Runnable() {
240-
public void run() throws Exception {
241-
instance.readNumber();
242-
}
243-
});
244-
199+
assertThrows(EOFException.class, instance::readNumber);
245200
assertEquals(0, instance.available());
246201
}
247202

@@ -304,25 +259,15 @@ public void testReadListEmpty() throws Exception {
304259
public void testReadListInvalidItem() throws Exception {
305260
instantiate("l2:Worlde");
306261

307-
assertThrows(InvalidObjectException.class, new Runnable() {
308-
public void run() throws Exception {
309-
instance.readList();
310-
}
311-
});
312-
262+
assertThrows(InvalidObjectException.class, instance::readList);
313263
assertEquals(4, instance.available());
314264
}
315265

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

320-
assertThrows(EOFException.class, new Runnable() {
321-
public void run() throws Exception {
322-
instance.readList();
323-
}
324-
});
325-
270+
assertThrows(EOFException.class, instance::readList);
326271
assertEquals(0, instance.available());
327272
}
328273

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

396-
assertThrows(InvalidObjectException.class, new Runnable() {
397-
public void run() throws Exception {
398-
instance.readDictionary();
399-
}
400-
});
401-
341+
assertThrows(InvalidObjectException.class, instance::readDictionary);
402342
assertEquals(4, instance.available());
403343
}
404344

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

409-
assertThrows(EOFException.class, new Runnable() {
410-
public void run() throws Exception {
411-
instance.readDictionary();
412-
}
413-
});
414-
349+
assertThrows(EOFException.class, instance::readDictionary);
415350
assertEquals(0, instance.available());
416351
}
417352
}

src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Before;
44
import org.junit.Test;
5+
import org.junit.function.ThrowingRunnable;
56

67
import java.io.ByteArrayOutputStream;
78
import java.nio.ByteBuffer;
@@ -11,8 +12,8 @@
1112
import java.util.TreeMap;
1213
import java.util.concurrent.ConcurrentSkipListMap;
1314

14-
import static com.dampcake.bencode.Assert.assertThrows;
1515
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertThrows;
1617

1718
/**
1819
* Unit tests for BencodeOutputStream.
@@ -58,12 +59,7 @@ public void testWriteStringEmpty() throws Exception {
5859

5960
@Test
6061
public void testWriteStringNull() throws Exception {
61-
assertThrows(NullPointerException.class, new Runnable() {
62-
public void run() throws Exception {
63-
instance.writeString((String) null);
64-
}
65-
});
66-
62+
assertThrows(NullPointerException.class, () -> instance.writeString((String) null));
6763
assertEquals(0, out.toByteArray().length);
6864
}
6965

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

9894
@Test
9995
public void testWriteStringNullByteArray() throws Exception {
100-
assertThrows(NullPointerException.class, new Runnable() {
101-
public void run() throws Exception {
102-
instance.writeString((ByteBuffer) null);
103-
}
104-
});
105-
96+
assertThrows(NullPointerException.class, () -> instance.writeString((ByteBuffer) null));
10697
assertEquals(0, out.toByteArray().length);
10798
}
10899

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

123114
@Test
124115
public void testWriteNumberNull() throws Exception {
125-
assertThrows(NullPointerException.class, new Runnable() {
126-
public void run() throws Exception {
127-
instance.writeNumber(null);
128-
}
129-
});
130-
116+
assertThrows(NullPointerException.class, () -> instance.writeNumber(null));
131117
assertEquals(0, out.toByteArray().length);
132118
}
133119

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

156142
@Test
157143
public void testWriteListNullItem() throws Exception {
158-
assertThrows(NullPointerException.class, new Runnable() {
159-
public void run() throws Exception {
160-
instance.writeList(new ArrayList<Object>() {{
161-
add("Hello");
162-
add(ByteBuffer.wrap("World!".getBytes()));
163-
add(new ArrayList<Object>() {{
164-
add(null);
165-
add(456);
166-
}});
167-
}});
168-
}
169-
});
170-
144+
ThrowingRunnable runnable = () -> instance.writeList(new ArrayList<Object>() {{
145+
add("Hello");
146+
add(ByteBuffer.wrap("World!".getBytes()));
147+
add(new ArrayList<Object>() {{
148+
add(null);
149+
add(456);
150+
}});
151+
}});
152+
assertThrows(NullPointerException.class, runnable);
171153
assertEquals(0, out.toByteArray().length);
172154
}
173155

174156
@Test
175157
public void testWriteListNull() throws Exception {
176-
assertThrows(NullPointerException.class, new Runnable() {
177-
public void run() throws Exception {
178-
instance.writeList(null);
179-
}
180-
});
181-
158+
assertThrows(NullPointerException.class, () -> instance.writeList(null));
182159
assertEquals(0, out.toByteArray().length);
183160
}
184161

185162
@Test
186-
@SuppressWarnings("unchecked")
187163
public void testWriteDictionary() throws Exception {
188164
instance.writeDictionary(new LinkedHashMap<Object, Object>() {{
189165
put("string", "value");
@@ -192,7 +168,7 @@ public void testWriteDictionary() throws Exception {
192168
add("list-item-1");
193169
add("list-item-2");
194170
}});
195-
put("dict", new ConcurrentSkipListMap() {{
171+
put("dict", new ConcurrentSkipListMap<Integer, Object>() {{
196172
put(123, ByteBuffer.wrap("test".getBytes()));
197173
put(456, "thing");
198174
}});
@@ -211,26 +187,17 @@ public void testWriteDictionaryEmpty() throws Exception {
211187

212188
@Test
213189
public void testWriteDictionaryKeyCastException() throws Exception {
214-
assertThrows(ClassCastException.class, new Runnable() {
215-
public void run() throws Exception {
216-
instance.writeDictionary(new TreeMap<Object, Object>() {{
217-
put("string", "value");
218-
put(123, "number-key");
219-
}});
220-
}
221-
});
222-
190+
ThrowingRunnable runnable = () -> instance.writeDictionary(new TreeMap<Object, Object>() {{
191+
put("string", "value");
192+
put(123, "number-key");
193+
}});
194+
assertThrows(ClassCastException.class, runnable);
223195
assertEquals(0, out.toByteArray().length);
224196
}
225197

226198
@Test
227199
public void testWriteDictionaryNull() throws Exception {
228-
assertThrows(NullPointerException.class, new Runnable() {
229-
public void run() throws Exception {
230-
instance.writeDictionary(null);
231-
}
232-
});
233-
200+
assertThrows(NullPointerException.class, () -> instance.writeDictionary(null));
234201
assertEquals(0, out.toByteArray().length);
235202
}
236203
}

src/test/java/com/dampcake/bencode/Runnable.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)