8000 Prefer assertThrows to try/fail (#62) · codesplode/dnsjava@0ae8fea · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ae8fea

Browse files
kingleibauersachs
authored andcommitted
Prefer assertThrows to try/fail (dnsjava#62)
1 parent 25fc5e0 commit 0ae8fea

35 files changed

+476
-1471
lines changed

src/test/java/org/xbill/DNS/A6RecordTest.java

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@
3434
//
3535
package org.xbill.DNS;
3636

37-
import org.junit.jupiter.api.BeforeEach;
38-
import org.junit.jupiter.api.Test;
39-
40-
import java.io.IOException;
41-
import java.net.InetAddress;
42-
import java.net.UnknownHostException;
43-
37+
import static org.junit.jupiter.api.Assertions.assertThrows;
4438
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4539
import static org.junit.jupiter.api.Assertions.assertEquals;
4640
import static org.junit.jupiter.api.Assertions.assertNull;
4741
import static org.junit.jupiter.api.Assertions.assertTrue;
4842
import static org.junit.jupiter.api.Assertions.fail;
4943

44+
import java.io.IOException;
45+
import java.net.InetAddress;
46+
import java.net.UnknownHostException;
47+
import org.junit.jupiter.api.BeforeEach;
48+
import org.junit.jupiter.api.Test;
49+
5050
class A6RecordTest
5151
{
5252
private Name m_an;
@@ -115,25 +115,13 @@ void test_ctor_6arg()
115115
assertEquals(m_an2, ar.getPrefix());
116116

117117
// a relative name
118-
try {
119-
new A6Record(m_rn, DClass.IN, m_ttl, m_prefix_bits, m_addr, null);
120-
fail("RelativeNameException not thrown");
121-
}
122-
catch( RelativeNameException e ){}
118+
assertThrows(RelativeNameException.class, () -> new A6Record(m_rn, DClass.IN, m_ttl, m_prefix_bits, m_addr, null));
123119

124120
// a relative prefix name
125-
try {
126-
new A6Record(m_an, DClass.IN, m_ttl, m_prefix_bits, m_addr, m_rn);
127-
fail("RelativeNameException not thrown");
128-
}
129-
catch( RelativeNameException e ){}
121+
assertThrows(RelativeNameException.class, () -> new A6Record(m_an, DClass.IN, m_ttl, m_prefix_bits, m_addr, m_rn));
130122

131123
// invalid prefix bits
132-
try {
133-
new A6Record(m_rn, DClass.IN, m_ttl, 0x100, m_addr, null);
134-
fail("IllegalArgumentException not thrown");
135-
}
136-
catch( RelativeNameException e ){}
124+
assertThrows(RelativeNameException.class, () -> new A6Record(m_rn, DClass.IN, m_ttl, 0x100, m_addr, null));
137125

138126
// an IPv4 address
139127
try {
@@ -200,22 +188,10 @@ void test_rdataFromString() throws
200188
assertEquals(m_an2, ar.getPrefix());
201189

202190
// record with invalid prefixBits
203-
t = new Tokenizer("129");
204-
ar = new A6Record();
205-
try {
206-
ar.rdataFromString(t, null);
207-
fail("TextParseException not thrown");
208-
}
209-
catch( TextParseException e ){}
191+
assertThrows(TextParseException.class, () -> new A6Record().rdataFromString(new Tokenizer("129"), null));
210192

211193
// record with invalid ipv6 address
212-
t = new Tokenizer("0 " + m_addr_string.substring(4));
213-
ar = new A6Record();
214-
try {
215-
ar.rdataFromString(t, null);
216-
fail("TextParseException not thrown");
217-
}
218-
catch( TextParseException e ){}
194+
assertThrows(TextParseException.class, () -> new A6Record().rdataFromString(new Tokenizer("0 " + m_addr_string.substring(4)), null));
219195
}
220196

221197
@Test

src/test/java/org/xbill/DNS/AAAARecordTest.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@
3434
//
3535
package org.xbill.DNS;
3636

37-
import org.junit.jupiter.api.BeforeEach;
38-
import org.junit.jupiter.api.Test;
39-
40-
import java.io.IOException;
41-
import java.net.InetAddress;
42-
import java.net.UnknownHostException;
43-
37+
import static org.junit.jupiter.api.Assertions.assertThrows;
4438
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4539
import static org.junit.jupiter.api.Assertions.assertEquals;
4640
import static org.junit.jupiter.api.Assertions.assertNull;
4741
import static org.junit.jupiter.api.Assertions.assertTrue;
4842
import static org.junit.jupiter.api.Assertions.fail;
4943

44+
import java.io.IOException;
45+
import java.net.InetAddress;
46+
import java.net.UnknownHostException;
47+
import org.junit.jupiter.api.BeforeEach;
48+
import org.junit.jupiter.api.Test;
49+
5050
class AAAARecordTest
5151
{
5252
private Name m_an;
@@ -97,11 +97,7 @@ void test_ctor_4arg()
9797
assertEquals(m_addr, ar.getAddress());
9898

9999
// a relative name
100-
try {
101-
new AAAARecord(m_rn, DClass.IN, m_ttl, m_addr);
102-
fail("RelativeNameException not thrown");
103-
}
104-
catch( RelativeNameException e ){}
100+
assertThrows(RelativeNameException.class, () -> new AAAARecord(m_rn, DClass.IN, m_ttl, m_addr));
105101

106102
// an IPv4 address
107103
try {
@@ -135,13 +131,7 @@ void test_rdataFromString() throws IOException
135131
assertEquals(m_addr, ar.getAddress());
136132

137133
// invalid address
138-
t = new Tokenizer("193.160.232.1");
139-
ar = new AAAARecord();
140-
try {
141-
ar.rdataFromString(t, null);
142-
fail("TextParseException not thrown");
143-
}
144-
catch( TextParseException e ){}
134+
assertThrows(TextParseException.class, () -> new AAAARecord().rdataFromString(new Tokenizer("193.160.232.1&q 558 uot;), null));
145135
}
146136

147137
@Test

src/test/java/org/xbill/DNS/APLRecordTest.java

Lines changed: 26 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@
3434
//
3535
package org.xbill.DNS;
3636

37-
import org.junit.jupiter.api.BeforeEach;
38-
import org.junit.jupiter.api.Test;
39-
import org.xbill.DNS.APLRecord.Element;
40-
41-
import java.io.IOException;
42-
import java.net.InetAddress;
43-
import java.net.UnknownHostException;
44-
import java.util.ArrayList;
45-
import java.util.List;
46-
37+
import static org.junit.jupiter.api.Assertions.assertThrows;
4738
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4839
import static org.junit.jupiter.api.Assertions.assertEquals;
4940
import static org.junit.jupiter.api.Assertions.assertFalse;
5041
import static org.junit.jupiter.api.Assertions.assertNull;
5142
import static org.junit.jupiter.api.Assertions.assertTrue;
5243
import static org.junit.jupiter.api.Assertions.fail;
5344

45+
import java.io.IOException;
46+
import java.net.InetAddress;
47+
import java.net.UnknownHostException;
48+
import java.util.ArrayList;
49+
import java.util.List;
50+
import org.junit.jupiter.api.BeforeEach;
51+
import org.junit.jupiter.api.Test;
52+
import org.xbill.DNS.APLRecord.Element;
53+
5454
public class APLRecordTest
5555
{
5656
static class Test_Element_init
@@ -79,11 +79,7 @@ void test_valid_IPv4()
7979
@Test
8080
void test_invalid_IPv4()
8181
{
82-
try {
83-
new Element(true, m_addr4, 33);
84-
fail("IllegalArgumentException not thrown");
85-
}
86-
catch( IllegalArgumentException e ){}
82+
assertThrows(IllegalArgumentException.class, () -> new Element(true, m_addr4, 33));
8783
}
8884

8985
@Test
@@ -99,11 +95,7 @@ void test_valid_IPv6()
9995
@Test
10096
void test_invalid_IPv6()
10197
{
102-
try {
103-
new Element(true, m_addr6, 129);
104-
fail("IllegalArgumentException not thrown");
105-
}
106-
catch( IllegalArgumentException e ){}
98+
assertThrows(IllegalArgumentException.class, () -> new Element(true, m_addr6, 129));
10799
}
108100
}
109101

@@ -181,11 +173,7 @@ void test_4arg_empty_elements()
181173
@Test
182174
void test_4arg_relative_name()
183175
{
184-
try {
185-
new APLRecord(m_rn, DClass.IN, m_ttl, m_elements);
186-
fail("RelativeNameException not thrown");
187-
}
188-
catch( RelativeNameException e ){}
176+
assertThrows(RelativeNameException.class, () -> new APLRecord(m_rn, DClass.IN, m_ttl, m_elements));
189177
}
190178
}
191179

@@ -250,11 +238,7 @@ void test_invalid_IPv4_prefix() throws IOException
250238

251239
DNSInput di = new DNSInput(raw);
252240
APLRecord ar = new APLRecord();
253-
try {
254-
ar.rrFromWire(di);
255-
fail("WireParseException not thrown");
256-
}
257-
catch( WireParseException e ){}
241+
assertThrows(WireParseException.class, () -> ar.rrFromWire(di));
258242
}
259243

260244
@Test
@@ -266,11 +250,7 @@ void test_invalid_IPv4_length() throws IOException
266250

267251
DNSInput di = new DNSInput(raw);
268252
APLRecord ar = new APLRecord();
269-
try {
270-
ar.rrFromWire(di);
271-
fail("WireParseException not thrown");
272-
}
273-
catch( WireParseException e ){}
253+
assertThrows(WireParseException.class, () -> ar.rrFromWire(di));
274254
}
275255

276256
@Test
@@ -410,131 +390,87 @@ void test_no_colon() throws IOException
410390
{
411391
Tokenizer t = new Tokenizer("!1192.68.0.1/20");
412392
APLRecord ar = new APLRecord();
413-
try {
414-
ar.rdataFromString(t, null);
415-
fail("TextParseException not thrown");
416-
}
417-
catch( TextParseException e ){}
393+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
418394
}
419395

420396
@Test
421397
void test_colon_and_slash_swapped() throws IOException
422398
{
423399
Tokenizer t = new Tokenizer("!1/192.68.0.1:20");
424400
APLRecord ar = new APLRecord();
425-
try {
426-
ar.rdataFromString(t, null);
427-
fail("TextParseException not thrown");
428-
}
429-
catch( TextParseException e ){}
401+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
430402
}
431403

432404
@Test
433405
void test_no_slash() throws IOException
434406
{
435407
Tokenizer t = new Tokenizer("!1:192.68.0.1|20");
436408
APLRecord ar = new APLRecord();
437-
try {
438-
ar.rdataFromString(t, null);
439-
fail("TextParseException not thrown");
440-
}
441-
catch( TextParseException e ){}
409+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
442410
}
443411

444412
@Test
445413
void test_empty_family() throws IOException
446414
{
447415
Tokenizer t = new Tokenizer("!:192.68.0.1/20");
448416
APLRecord ar = new APLRecord();
449-
try {
450-
ar.rdataFromString(t, null);
451-
fail("TextParseException not thrown");
452-
}
453-
catch( TextParseException e ){}
417+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
454418
}
455419

456420
@Test
457421
void test_malformed_family() throws IOException
458422
{
459423
Tokenizer t = new Tokenizer("family:192.68.0.1/20");
460424
APLRecord ar = new APLRecord();
461-
try {
462-
ar.rdataFromString(t, null);
463-
fail("TextParseException not thrown");
464-
}
465-
catch( TextParseException e ){}
425+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
466426
}
467427

468428
@Test
469429
void test_invalid_family() throws IOException
470430
{
471431
Tokenizer t = new Tokenizer("3:192.68.0.1/20");
472432
APLRecord ar = new APLRecord();
473-
try {
474-
ar.rdataFromString(t, null);
475-
fail("TextParseException not thrown");
476-
}
477-
catch( TextParseException e ){}
433+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
478434
}
479435

480436
@Test
481437
void test_empty_prefix() throws IOException
482438
{
483439
Tokenizer t = new Tokenizer("1:192.68.0.1/");
484440
APLRecord ar = new APLRecord();
485-
try {
486-
ar.rdataFromString(t, null);
487-
fail("TextParseException not thrown");
488-
}
489-
catch( TextParseException e ){}
441+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
490442
}
491443

492444
@Test
493445
void test_malformed_prefix() throws IOException
494446
{
495447
Tokenizer t = new Tokenizer("1:192.68.0.1/prefix");
496448
APLRecord ar = new APLRecord();
497-
try {
498-
ar.rdataFromString(t, null);
499-
fail("TextParseException not thrown");
500-
}
501-
catch( TextParseException e ){}
449+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
502450
}
503451

504452
@Test
505453
void test_invalid_prefix() throws IOException
506454
{
507455
Tokenizer t = new Tokenizer("1:192.68.0.1/33");
508456
APLRecord ar = new APLRecord();
509-
try {
510-
ar.rdataFromString(t, null);
511-
fail("TextParseException not thrown");
512-
}
513-
catch( TextParseException e ){}
457+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
514458
}
515459

516460
@Test
517461
void test_empty_address() throws IOException
518462
{
519463
Tokenizer t = new Tokenizer("1:/33");
520464
APLRecord ar = new APLRecord();
521-
try {
522-
ar.rdataFromString(t, null);
523-
fail("TextParseException not thrown");
524-
}
525-
catch( TextParseException e ){}
465+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
526466
}
527467

528468
@Test
529469
void test_malformed_address() throws IOException
530470
{
531471
Tokenizer t = new Tokenizer("1:A.B.C.D/33");
532472
APLRecord ar = new APLRecord();
533-
try {
534-
ar.rdataFromString(t, null);
535-
fail("TextParseException not thrown");
536-
}
537-
catch( TextParseException e ){}
473+
assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
538474
}
539475
}
540476

0 commit comments

Comments
 (0)
0