Use @Test annotation to test for exceptions being thrown

Please note that this commit only uses the @Test annotation
where exceptions are thrown at the end of the test,
because otherwise the test would not be executed completely.

Examples for this are in DatabaseComponentImplTest where many exceptions
are thrown in close succession or in ConnectionRegistryImplTest where an
exception is thrown in the middle of the test.
This commit is contained in:
Torsten Grote
2016-01-12 15:16:34 -02:00
parent aa1b9328c4
commit b837e8b035
5 changed files with 48 additions and 140 deletions

View File

@@ -82,7 +82,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
assertEquals(-1, s.readFrame(buffer));
}
@Test
@Test(expected = IOException.class)
public void testTruncatedFrameThrowsException() throws Exception {
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
int payloadLength = 123, paddingLength = 234;
@@ -105,15 +105,10 @@ public class StreamDecrypterImplTest extends BriarTestCase {
// Try to read the truncated frame
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
try {
s.readFrame(buffer);
fail();
} catch (IOException expected) {
// Expected
}
s.readFrame(buffer);
}
@Test
@Test(expected = IOException.class)
public void testInvalidPayloadAndPaddingLengthThrowsException()
throws Exception {
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
@@ -139,15 +134,10 @@ public class StreamDecrypterImplTest extends BriarTestCase {
// Try to read the invalid frame
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
try {
s.readFrame(buffer);
fail();
} catch (IOException expected) {
// Expected
}
s.readFrame(buffer);
}
@Test
@Test(expected = IOException.class)
public void testNonZeroPaddingThrowsException() throws Exception {
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
int payloadLength = 123, paddingLength = 234;
@@ -174,12 +164,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
// Try to read the invalid frame
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
try {
s.readFrame(buffer);
fail();
} catch (IOException expected) {
// Expected
}
s.readFrame(buffer);
}
@Test

View File

@@ -165,18 +165,13 @@ public class BdfReaderImplTest extends BriarTestCase {
assertTrue(r.eof());
}
@Test
@Test(expected = FormatException.class)
public void testReadString8ChecksMaxLength() throws Exception {
// "foo" twice
setContents("41" + "03" + "666F6F" + "41" + "03" + "666F6F");
assertEquals("foo", r.readString(3));
assertTrue(r.hasString());
try {
r.readString(2);
fail();
} catch (FormatException expected) {
// Expected
}
r.readString(2);
}
@Test
@@ -205,7 +200,7 @@ public class BdfReaderImplTest extends BriarTestCase {
assertTrue(r.eof());
}
@Test
@Test(expected = FormatException.class)
public void testReadString16ChecksMaxLength() throws Exception {
String shortest = TestUtils.createRandomString(Byte.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
@@ -213,12 +208,7 @@ public class BdfReaderImplTest extends BriarTestCase {
setContents("42" + "0080" + shortHex + "42" + "0080" + shortHex);
assertEquals(shortest, r.readString(Byte.MAX_VALUE + 1));
assertTrue(r.hasString());
try {
r.readString(Byte.MAX_VALUE);
fail();
} catch (FormatException expected) {
// Expected
}
r.readString(Byte.MAX_VALUE);
}
@Test
@@ -244,7 +234,7 @@ public class BdfReaderImplTest extends BriarTestCase {
assertTrue(r.eof());
}
@Test
@Test(expected = FormatException.class)
public void testReadString32ChecksMaxLength() throws Exception {
String shortest = TestUtils.createRandomString(Short.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
@@ -253,12 +243,7 @@ public class BdfReaderImplTest extends BriarTestCase {
"44" + "00008000" + shortHex);
assertEquals(shortest, r.readString(Short.MAX_VALUE + 1));
assertTrue(r.hasString());
try {
r.readString(Short.MAX_VALUE);
fail();
} catch (FormatException expected) {
// Expected
}
r.readString(Short.MAX_VALUE);
}
@Test
@@ -286,18 +271,13 @@ public class BdfReaderImplTest extends BriarTestCase {
assertTrue(r.eof());
}
@Test
@Test(expected = FormatException.class)
public void testReadRaw8ChecksMaxLength() throws Exception {
// {1, 2, 3} twice
setContents("51" + "03" + "010203" + "51" + "03" + "010203");
assertArrayEquals(new byte[] {1, 2, 3}, r.readRaw(3));
assertTrue(r.hasRaw());
try {
r.readRaw(2);
fail();
} catch (FormatException expected) {
// Expected
}
r.readRaw(2);
}
@Test
@@ -326,7 +306,7 @@ public class BdfReaderImplTest extends BriarTestCase {
assertTrue(r.eof());
}
@Test
@Test(expected = FormatException.class)
public void testReadRaw16ChecksMaxLength() throws Exception {
byte[] shortest = new byte[Byte.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest);
@@ -334,12 +314,7 @@ public class BdfReaderImplTest extends BriarTestCase {
setContents("52" + "0080" + shortHex + "52" + "0080" + shortHex);
assertArrayEquals(shortest, r.readRaw(Byte.MAX_VALUE + 1));
assertTrue(r.hasRaw());
try {
r.readRaw(Byte.MAX_VALUE);
fail();
} catch (FormatException expected) {
// Expected
}
r.readRaw(Byte.MAX_VALUE);
}
@Test
@@ -365,7 +340,7 @@ public class BdfReaderImplTest extends BriarTestCase {
assertTrue(r.eof());
}
@Test
@Test(expected = FormatException.class)
public void testReadRaw32ChecksMaxLength() throws Exception {
byte[] shortest = new byte[Short.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest);
@@ -374,12 +349,7 @@ public class BdfReaderImplTest extends BriarTestCase {
"54" + "00008000" + shortHex);
assertArrayEquals(shortest, r.readRaw(Short.MAX_VALUE + 1));
assertTrue(r.hasRaw());
try {
r.readRaw(Short.MAX_VALUE);
fail();
} catch (FormatException expected) {
// Expected
}
r.readRaw(Short.MAX_VALUE);
}
@Test

View File

@@ -31,7 +31,7 @@ public class ConsumersTest extends BriarTestCase {
assertArrayEquals(dig, dig1);
}
@Test
@Test(expected = FormatException.class)
public void testCountingConsumer() throws Exception {
byte[] data = new byte[1234];
CountingConsumer cc = new CountingConsumer(data.length);
@@ -39,12 +39,7 @@ public class ConsumersTest extends BriarTestCase {
cc.write(data, 1, data.length - 2);
cc.write(data[data.length - 1]);
assertEquals(data.length, cc.getCount());
try {
cc.write((byte) 0);
fail();
} catch (FormatException expected) {
// Expected
}
cc.write((byte) 0);
}
@Test

View File

@@ -39,18 +39,13 @@ public class PacketReaderImplTest extends BriarTestCase {
bdfWriterFactory = i.getInstance(BdfWriterFactory.class);
}
@Test
@Test(expected = FormatException.class)
public void testFormatExceptionIfAckIsTooLarge() throws Exception {
byte[] b = createAck(true);
ByteArrayInputStream in = new ByteArrayInputStream(b);
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
null, in);
try {
reader.readAck();
fail();
} catch (FormatException expected) {
// Expected
}
reader.readAck();
}
@Test
@@ -62,32 +57,22 @@ public class PacketReaderImplTest extends BriarTestCase {
reader.readAck();
}
@Test
@Test(expected = FormatException.class)
public void testEmptyAck() throws Exception {
byte[] b = createEmptyAck();
ByteArrayInputStream in = new ByteArrayInputStream(b);
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
null, in);
try {
reader.readAck();
fail();
} catch (FormatException expected) {
// Expected
}
reader.readAck();
}
@Test
@Test(expected = FormatException.class)
public void testFormatExceptionIfOfferIsTooLarge() throws Exception {
byte[] b = createOffer(true);
ByteArrayInputStream in = new ByteArrayInputStream(b);
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
null, in);
try {
reader.readOffer();
fail();
} catch (FormatException expected) {
// Expected
}
reader.readOffer();
}
@Test
@@ -101,32 +86,22 @@ public class PacketReaderImplTest extends BriarTestCase {
reader.readOffer();
}
@Test
@Test(expected = FormatException.class)
public void testEmptyOffer() throws Exception {
byte[] b = createEmptyOffer();
ByteArrayInputStream in = new ByteArrayInputStream(b);
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
null, in);
try {
reader.readOffer();
fail();
} catch (FormatException expected) {
// Expected
}
reader.readOffer();
}
@Test
@Test(expected = FormatException.class)
public void testFormatExceptionIfRequestIsTooLarge() throws Exception {
byte[] b = createRequest(true);
ByteArrayInputStream in = new ByteArrayInputStream(b);
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
null, in);
try {
reader.readRequest();
fail();
} catch (FormatException expected) {
// Expected
}
reader.readRequest();
}
@Test
@@ -138,18 +113,13 @@ public class PacketReaderImplTest extends BriarTestCase {
reader.readRequest();
}
@Test
@Test(expected = FormatException.class)
public void testEmptyRequest() throws Exception {
byte[] b = createEmptyRequest();
ByteArrayInputStream in = new ByteArrayInputStream(b);
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
null, in);
try {
reader.readRequest();
fail();
} catch (FormatException expected) {
// Expected
}
reader.readRequest();
}
private byte[] createAck(boolean tooBig) throws Exception {

View File

@@ -20,20 +20,14 @@ public class ByteUtilsTest extends BriarTestCase {
assertEquals(65535, ByteUtils.readUint16(b, 1));
}
@Test
public void testReadUint16ValidatesArguments() {
try {
ByteUtils.readUint16(new byte[1], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.readUint16(new byte[2], 1);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
@Test(expected = IllegalArgumentException.class)
public void testReadUint16ValidatesArguments1() {
ByteUtils.readUint16(new byte[1], 0);
}
@Test(expected = IllegalArgumentException.class)
public void testReadUint16ValidatesArguments2() {
ByteUtils.readUint16(new byte[2], 1);
}
@Test
@@ -46,20 +40,14 @@ public class ByteUtilsTest extends BriarTestCase {
assertEquals(4294967295L, ByteUtils.readUint32(b, 1));
}
@Test
public void testReadUint32ValidatesArguments() {
try {
ByteUtils.readUint32(new byte[3], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.readUint32(new byte[4], 1);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
@Test(expected = IllegalArgumentException.class)
public void testReadUint32ValidatesArguments1() {
ByteUtils.readUint32(new byte[3], 0);
}
@Test(expected = IllegalArgumentException.class)
public void testReadUint32ValidatesArguments2() {
ByteUtils.readUint32(new byte[4], 1);
}
@Test