Skip to content

Commit

Permalink
Prefer assertThrows to try/fail (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
kingle authored and ibauersachs committed Jun 17, 2019
1 parent 25fc5e0 commit 0ae8fea
Show file tree
Hide file tree
Showing 35 changed files with 476 additions and 1,471 deletions.
48 changes: 12 additions & 36 deletions src/test/java/org/xbill/DNS/A6RecordTest.java
Expand Up @@ -34,19 +34,19 @@
// //
package org.xbill.DNS; package org.xbill.DNS;


import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;


import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class A6RecordTest class A6RecordTest
{ {
private Name m_an; private Name m_an;
Expand Down Expand Up @@ -115,25 +115,13 @@ void test_ctor_6arg()
assertEquals(m_an2, ar.getPrefix()); assertEquals(m_an2, ar.getPrefix());


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


// a relative prefix name // a relative prefix name
try { assertThrows(RelativeNameException.class, () -> new A6Record(m_an, DClass.IN, m_ttl, m_prefix_bits, m_addr, m_rn));
new A6Record(m_an, DClass.IN, m_ttl, m_prefix_bits, m_addr, m_rn);
fail("RelativeNameException not thrown");
}
catch( RelativeNameException e ){}


// invalid prefix bits // invalid prefix bits
try { assertThrows(RelativeNameException.class, () -> new A6Record(m_rn, DClass.IN, m_ttl, 0x100, m_addr, null));
new A6Record(m_rn, DClass.IN, m_ttl, 0x100, m_addr, null);
fail("IllegalArgumentException not thrown");
}
catch( RelativeNameException e ){}


// an IPv4 address // an IPv4 address
try { try {
Expand Down Expand Up @@ -200,22 +188,10 @@ void test_rdataFromString() throws
assertEquals(m_an2, ar.getPrefix()); assertEquals(m_an2, ar.getPrefix());


// record with invalid prefixBits // record with invalid prefixBits
t = new Tokenizer("129"); assertThrows(TextParseException.class, () -> new A6Record().rdataFromString(new Tokenizer("129"), null));
ar = new A6Record();
try {
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}


// record with invalid ipv6 address // record with invalid ipv6 address
t = new Tokenizer("0 " + m_addr_string.substring(4)); assertThrows(TextParseException.class, () -> new A6Record().rdataFromString(new Tokenizer("0 " + m_addr_string.substring(4)), null));
ar = new A6Record();
try {
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
Expand Down
28 changes: 9 additions & 19 deletions src/test/java/org/xbill/DNS/AAAARecordTest.java
Expand Up @@ -34,19 +34,19 @@
// //
package org.xbill.DNS; package org.xbill.DNS;


import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;


import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class AAAARecordTest class AAAARecordTest
{ {
private Name m_an; private Name m_an;
Expand Down Expand Up @@ -97,11 +97,7 @@ void test_ctor_4arg()
assertEquals(m_addr, ar.getAddress()); assertEquals(m_addr, ar.getAddress());


// a relative name // a relative name
try { assertThrows(RelativeNameException.class, () -> new AAAARecord(m_rn, DClass.IN, m_ttl, m_addr));
new AAAARecord(m_rn, DClass.IN, m_ttl, m_addr);
fail("RelativeNameException not thrown");
}
catch( RelativeNameException e ){}


// an IPv4 address // an IPv4 address
try { try {
Expand Down Expand Up @@ -135,13 +131,7 @@ void test_rdataFromString() throws IOException
assertEquals(m_addr, ar.getAddress()); assertEquals(m_addr, ar.getAddress());


// invalid address // invalid address
t = new Tokenizer("193.160.232.1"); assertThrows(TextParseException.class, () -> new AAAARecord().rdataFromString(new Tokenizer("193.160.232.1"), null));
ar = new AAAARecord();
try {
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
Expand Down
116 changes: 26 additions & 90 deletions src/test/java/org/xbill/DNS/APLRecordTest.java
Expand Up @@ -34,23 +34,23 @@
// //
package org.xbill.DNS; package org.xbill.DNS;


import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.xbill.DNS.APLRecord.Element;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;


import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xbill.DNS.APLRecord.Element;

public class APLRecordTest public class APLRecordTest
{ {
static class Test_Element_init static class Test_Element_init
Expand Down Expand Up @@ -79,11 +79,7 @@ void test_valid_IPv4()
@Test @Test
void test_invalid_IPv4() void test_invalid_IPv4()
{ {
try { assertThrows(IllegalArgumentException.class, () -> new Element(true, m_addr4, 33));
new Element(true, m_addr4, 33);
fail("IllegalArgumentException not thrown");
}
catch( IllegalArgumentException e ){}
} }


@Test @Test
Expand All @@ -99,11 +95,7 @@ void test_valid_IPv6()
@Test @Test
void test_invalid_IPv6() void test_invalid_IPv6()
{ {
try { assertThrows(IllegalArgumentException.class, () -> new Element(true, m_addr6, 129));
new Element(true, m_addr6, 129);
fail("IllegalArgumentException not thrown");
}
catch( IllegalArgumentException e ){}
} }
} }


Expand Down Expand Up @@ -181,11 +173,7 @@ void test_4arg_empty_elements()
@Test @Test
void test_4arg_relative_name() void test_4arg_relative_name()
{ {
try { assertThrows(RelativeNameException.class, () -> new APLRecord(m_rn, DClass.IN, m_ttl, m_elements));
new APLRecord(m_rn, DClass.IN, m_ttl, m_elements);
fail("RelativeNameException not thrown");
}
catch( RelativeNameException e ){}
} }
} }


Expand Down Expand Up @@ -250,11 +238,7 @@ void test_invalid_IPv4_prefix() throws IOException


DNSInput di = new DNSInput(raw); DNSInput di = new DNSInput(raw);
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(WireParseException.class, () -> ar.rrFromWire(di));
ar.rrFromWire(di);
fail("WireParseException not thrown");
}
catch( WireParseException e ){}
} }


@Test @Test
Expand All @@ -266,11 +250,7 @@ void test_invalid_IPv4_length() throws IOException


DNSInput di = new DNSInput(raw); DNSInput di = new DNSInput(raw);
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(WireParseException.class, () -> ar.rrFromWire(di));
ar.rrFromWire(di);
fail("WireParseException not thrown");
}
catch( WireParseException e ){}
} }


@Test @Test
Expand Down Expand Up @@ -410,131 +390,87 @@ void test_no_colon() throws IOException
{ {
Tokenizer t = new Tokenizer("!1192.68.0.1/20"); Tokenizer t = new Tokenizer("!1192.68.0.1/20");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_colon_and_slash_swapped() throws IOException void test_colon_and_slash_swapped() throws IOException
{ {
Tokenizer t = new Tokenizer("!1/192.68.0.1:20"); Tokenizer t = new Tokenizer("!1/192.68.0.1:20");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_no_slash() throws IOException void test_no_slash() throws IOException
{ {
Tokenizer t = new Tokenizer("!1:192.68.0.1|20"); Tokenizer t = new Tokenizer("!1:192.68.0.1|20");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_empty_family() throws IOException void test_empty_family() throws IOException
{ {
Tokenizer t = new Tokenizer("!:192.68.0.1/20"); Tokenizer t = new Tokenizer("!:192.68.0.1/20");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_malformed_family() throws IOException void test_malformed_family() throws IOException
{ {
Tokenizer t = new Tokenizer("family:192.68.0.1/20"); Tokenizer t = new Tokenizer("family:192.68.0.1/20");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_invalid_family() throws IOException void test_invalid_family() throws IOException
{ {
Tokenizer t = new Tokenizer("3:192.68.0.1/20"); Tokenizer t = new Tokenizer("3:192.68.0.1/20");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_empty_prefix() throws IOException void test_empty_prefix() throws IOException
{ {
Tokenizer t = new Tokenizer("1:192.68.0.1/"); Tokenizer t = new Tokenizer("1:192.68.0.1/");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_malformed_prefix() throws IOException void test_malformed_prefix() throws IOException
{ {
Tokenizer t = new Tokenizer("1:192.68.0.1/prefix"); Tokenizer t = new Tokenizer("1:192.68.0.1/prefix");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_invalid_prefix() throws IOException void test_invalid_prefix() throws IOException
{ {
Tokenizer t = new Tokenizer("1:192.68.0.1/33"); Tokenizer t = new Tokenizer("1:192.68.0.1/33");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_empty_address() throws IOException void test_empty_address() throws IOException
{ {
Tokenizer t = new Tokenizer("1:/33"); Tokenizer t = new Tokenizer("1:/33");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }


@Test @Test
void test_malformed_address() throws IOException void test_malformed_address() throws IOException
{ {
Tokenizer t = new Tokenizer("1:A.B.C.D/33"); Tokenizer t = new Tokenizer("1:A.B.C.D/33");
APLRecord ar = new APLRecord(); APLRecord ar = new APLRecord();
try { assertThrows(TextParseException.class, () -> ar.rdataFromString(t, null));
ar.rdataFromString(t, null);
fail("TextParseException not thrown");
}
catch( TextParseException e ){}
} }
} }


Expand Down

0 comments on commit 0ae8fea

Please sign in to comment.