Skip to content

Commit da0f689

Browse files
authored
Add test of debian bttracker (#18)
1 parent bd30fa2 commit da0f689

File tree

1 file changed

+65
-96
lines changed

1 file changed

+65
-96
lines changed

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

Lines changed: 65 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.dampcake.bencode;
22

33
import org.junit.Before;
4-
import org.junit.Rule;
54
import org.junit.Test;
6-
import org.junit.rules.ExpectedException;
5+
import org.junit.function.ThrowingRunnable;
76

87
import java.io.EOFException;
98
import java.io.InvalidObjectException;
@@ -17,22 +16,18 @@
1716
import java.util.TreeMap;
1817
import java.util.concurrent.ConcurrentSkipListMap;
1918

20-
import static org.hamcrest.core.IsInstanceOf.any;
2119
import static org.hamcrest.CoreMatchers.instanceOf;
20+
import static org.hamcrest.MatcherAssert.assertThat;
2221
import static org.junit.Assert.assertEquals;
2322
import static org.junit.Assert.assertSame;
24-
import static org.junit.Assert.assertThat;
23+
import static org.junit.Assert.assertThrows;
2524
import static org.junit.Assert.assertTrue;
2625

2726
/**
2827
* Unit tests for Bencode.
2928
*/
3029
@SuppressWarnings("unchecked")
3130
public class BencodeTest {
32-
33-
@Rule
34-
public ExpectedException exception = ExpectedException.none();
35-
3631
private Bencode instance;
3732

3833
@Before
@@ -42,10 +37,8 @@ public void setUp() {
4237

4338
@Test
4439
public void testConstructorNullCharset() {
45-
exception.expect(NullPointerException.class);
46-
exception.expectMessage("charset cannot be null");
47-
48-
new Bencode(null);
40+
NullPointerException exception = assertThrows(NullPointerException.class, () -> new Bencode(null));
41+
assertEquals("charset cannot be null", exception.getMessage());
4942
}
5043

5144
@Test
@@ -82,42 +75,32 @@ public void testTypeUnknown() {
8275

8376
@Test
8477
public void testTypeEmpty() {
85-
exception.expect(BencodeException.class);
86-
exception.expectCause(any(EOFException.class));
87-
88-
instance.type(new byte[0]);
78+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.type(new byte[0]));
79+
assertThat(exception.getCause(), instanceOf(EOFException.class));
8980
}
9081

9182
@Test
9283
public void testTypeNullBytes() {
93-
exception.expect(NullPointerException.class);
94-
exception.expectMessage("bytes cannot be null");
95-
96-
instance.type(null);
84+
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.type(null));
85+
assertEquals("bytes cannot be null", exception.getMessage());
9786
}
9887

9988
@Test
10089
public void testDecodeNullBytes() {
101-
exception.expect(NullPointerException.class);
102-
exception.expectMessage("bytes cannot be null");
103-
104-
instance.decode(null, Type.STRING);
90+
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.decode(null, Type.STRING));
91+
assertEquals("bytes cannot be null", exception.getMessage());
10592
}
10693

10794
@Test
10895
public void testDecodeNullType() {
109-
exception.expect(NullPointerException.class);
110-
exception.expectMessage("type cannot be null");
111-
112-
instance.decode("12:Hello World!".getBytes(), null);
96+
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.decode("12:Hello World!".getBytes(), null));
97+
assertEquals("type cannot be null", exception.getMessage());
11398
}
11499

115100
@Test
116101
public void testDecodeUnknownType() {
117-
exception.expect(IllegalArgumentException.class);
118-
exception.expectMessage("type cannot be UNKNOWN");
119-
120-
instance.decode("12:Hello World!".getBytes(), Type.UNKNOWN);
102+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> instance.decode("12:Hello World!".getBytes(), Type.UNKNOWN));
103+
assertEquals("type cannot be UNKNOWN", exception.getMessage());
121104
}
122105

123106
@Test
@@ -143,26 +126,20 @@ public void testDecodeEmptyString() {
143126

144127
@Test
145128
public void testDecodeStringNaN() throws Exception {
146-
exception.expect(BencodeException.class);
147-
exception.expectCause(any(InvalidObjectException.class));
148-
149-
instance.decode("1c3:Testing".getBytes(), Type.STRING);
129+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("1c3:Testing".getBytes(), Type.STRING));
130+
assertThat(exception.getCause(), instanceOf(InvalidObjectException.class));
150131
}
151132

152133
@Test
153134
public void testDecodeStringEOF() throws Exception {
154-
exception.expect(BencodeException.class);
155-
exception.expectCause(any(EOFException.class));
156-
157-
instance.decode("123456".getBytes(), Type.STRING);
135+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("123456".getBytes(), Type.STRING));
136+
assertThat(exception.getCause(), instanceOf(EOFException.class));
158137
}
159138

160139
@Test
161140
public void testDecodeStringEmpty() throws Exception {
162-
exception.expect(BencodeException.class);
163-
exception.expectCause(any(EOFException.class));
164-
165-
instance.decode("".getBytes(), Type.STRING);
141+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("".getBytes(), Type.STRING));
142+
assertThat(exception.getCause(), instanceOf(EOFException.class));
166143
}
167144

168145
@Test
@@ -174,26 +151,20 @@ public void testDecodeNumber() throws Exception {
174151

175152
@Test
176153
public void testDecodeNumberNaN() throws Exception {
177-
exception.expect(BencodeException.class);
178-
exception.expectCause(any(NumberFormatException.class));
179-
180-
instance.decode("i123cbve1".getBytes(), Type.NUMBER);
154+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("i123cbve1".getBytes(), Type.NUMBER));
155+
assertThat(exception.getCause(), instanceOf(NumberFormatException.class));
181156
}
182157

183158
@Test
184159
public void testDecodeNumberEOF() throws Exception {
185-
exception.expect(BencodeException.class);
186-
exception.expectCause(any(EOFException.class));
187-
188-
instance.decode("i123".getBytes(), Type.NUMBER);
160+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("i123".getBytes(), Type.NUMBER));
161+
assertThat(exception.getCause(), instanceOf(EOFException.class));
189162
}
190163

191164
@Test
192165
public void testDecodeNumberEmpty() throws Exception {
193-
exception.expect(BencodeException.class);
194-
exception.expectCause(any(EOFException.class));
195-
196-
instance.decode("".getBytes(), Type.NUMBER);
166+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("".getBytes(), Type.NUMBER));
167+
assertThat(exception.getCause(), instanceOf(EOFException.class));
197168
}
198169

199170
@Test
@@ -236,18 +207,14 @@ public void testDecodeListEmpty() throws Exception {
236207

237208
@Test
238209
public void testDecodeListInvalidItem() throws Exception {
239-
exception.expect(BencodeException.class);
240-
exception.expectCause(any(InvalidObjectException.class));
241-
242-
instance.decode("l2:Worlde".getBytes(), Type.LIST);
210+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("l2:Worlde".getBytes(), Type.LIST));
211+
assertThat(exception.getCause(), instanceOf(InvalidObjectException.class));
243212
}
244213

245214
@Test
246215
public void testDecodeListEOF() throws Exception {
247-
exception.expect(BencodeException.class);
248-
exception.expectCause(any(EOFException.class));
249-
250-
instance.decode("l5:Hello".getBytes(), Type.LIST);
216+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("l5:Hello".getBytes(), Type.LIST));
217+
assertThat(exception.getCause(), instanceOf(EOFException.class));
251218
}
252219

253220
@Test
@@ -270,6 +237,22 @@ public void testDecodeDictionary() throws Exception {
270237
assertEquals("thing", map.get("456"));
271238
}
272239

240+
@Test
241+
public void testDecodeDebianTracker() throws Exception {
242+
Map<String, Object> decoded = instance.decode("d8:intervali900e5:peersld2:ip12:146.71.73.514:porti63853eeee".getBytes(), Type.DICTIONARY);
243+
244+
assertEquals(2, decoded.size());
245+
246+
assertEquals(900L, decoded.get("interval"));
247+
248+
List<Object> list = (List<Object>) decoded.get("peers");
249+
assertEquals(1, list.size());
250+
251+
Map<String, Object> map = (Map<String, Object>) list.get(0);
252+
assertEquals("146.71.73.51", map.get("ip"));
253+
assertEquals(63853L, map.get("port"));
254+
}
255+
273256
@Test
274257
public void testDecodeDictionaryByteArray() throws Exception {
275258
instance = new Bencode(true);
@@ -305,18 +288,14 @@ public void testDecodeDictionaryEmpty() throws Exception {
305288

306289
@Test
307290
public void testDecodeDictionaryInvalidItem() throws Exception {
308-
exception.expect(BencodeException.class);
309-
exception.expectCause(any(InvalidObjectException.class));
310-
311-
instance.decode("d4:item5:value3:testing".getBytes(), Type.DICTIONARY);
291+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("d4:item5:value3:testing".getBytes(), Type.DICTIONARY));
292+
assertThat(exception.getCause(), instanceOf(InvalidObjectException.class));
312293
}
313294

314295
@Test
315296
public void testDecodeDictionaryEOF() throws Exception {
316-
exception.expect(BencodeException.class);
317-
exception.expectCause(any(EOFException.class));
318-
319-
instance.decode("d4:item5:test".getBytes(), Type.DICTIONARY);
297+
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("d4:item5:test".getBytes(), Type.DICTIONARY));
298+
assertThat(exception.getCause(), instanceOf(EOFException.class));
320299
}
321300

322301
@Test
@@ -341,10 +320,8 @@ public void testWriteStringEmpty() throws Exception {
341320

342321
@Test
343322
public void testWriteStringNull() throws Exception {
344-
exception.expect(NullPointerException.class);
345-
exception.expectMessage("s cannot be null");
346-
347-
instance.encode((String) null);
323+
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.encode((String) null));
324+
assertEquals("s cannot be null", exception.getMessage());
348325
}
349326

350327
@Test
@@ -363,10 +340,8 @@ public void testWriteNumberDecimal() throws Exception {
363340

364341
@Test
365342
public void testWriteNumberNull() throws Exception {
366-
exception.expect(NullPointerException.class);
367-
exception.expectMessage("n cannot be null");
368-
369-
instance.encode((Number) null);
343+
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.encode((Number) null));
344+
assertEquals("n cannot be null", exception.getMessage());
370345
}
371346

372347
@Test
@@ -392,25 +367,22 @@ public void testWriteListEmpty() throws Exception {
392367

393368
@Test
394369
public void testWriteListNullItem() throws Exception {
395-
exception.expect(BencodeException.class);
396-
exception.expectCause(any(NullPointerException.class));
397-
398-
instance.encode(new ArrayList<Object>() {{
370+
ThrowingRunnable runnable = () -> instance.encode(new ArrayList<Object>() {{
399371
add("Hello");
400372
add("World!");
401373
add(new ArrayList<Object>() {{
402374
add(null);
403375
add(456);
404376
}});
405377
}});
378+
BencodeException exception = assertThrows(BencodeException.class, runnable);
379+
assertThat(exception.getCause(), instanceOf(NullPointerException.class));
406380
}
407381

408382
@Test
409383
public void testWriteListNull() throws Exception {
410-
exception.expect(NullPointerException.class);
411-
exception.expectMessage("l cannot be null");
412-
413-
instance.encode((List) null);
384+
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.encode((List<?>) null));
385+
assertEquals("l cannot be null", exception.getMessage());
414386
}
415387

416388
@Test
@@ -422,7 +394,7 @@ public void testWriteDictionary() throws Exception {
422394
add("list-item-1");
423395
add("list-item-2");
424396
}});
425-
put("dict", new ConcurrentSkipListMap() {{
397+
put("dict", new ConcurrentSkipListMap<Integer, String>() {{
426398
put(123, "test");
427399
put(456, "thing");
428400
}});
@@ -441,19 +413,16 @@ public void testWriteDictionaryEmpty() throws Exception {
441413

442414
@Test
443415
public void testWriteDictionaryKeyCastException() throws Exception {
444-
exception.expect(any(ClassCastException.class));
445-
446-
instance.encode(new TreeMap<Object, Object>() {{
416+
ThrowingRunnable runnable = () -> instance.encode(new TreeMap<Object, Object>() {{
447417
put("string", "value");
448418
put(123, "number-key");
449419
}});
420+
assertThrows(ClassCastException.class, runnable);
450421
}
451422

452423
@Test
453424
public void testWriteDictionaryNull() throws Exception {
454-
exception.expect(NullPointerException.class);
455-
exception.expectMessage("m cannot be null");
456-
457-
instance.encode((Map) null);
425+
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.encode((Map<?, ?>) null));
426+
assertEquals("m cannot be null", exception.getMessage());
458427
}
459428
}

0 commit comments

Comments
 (0)