Get rid of custom ExceptionRule methods

The only remaining methods on ExceptionRule after this are methods that
also exist on ExpectedException, which will allow us to, in the next CL,
swap out the one for the other and then run the automated refactoring to
turn it all into assertThrows/expectThrows.

Note that there were some assertions about root causes that couldn't
easily be turned into ExpectedException invocations, so I simply
converted them directly to usages of assertThrows/expectThrows.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178623431
This commit is contained in:
mcilwain 2017-12-11 08:47:27 -08:00 committed by jianglai
parent 68a26f5b6e
commit b825a2b5a8
144 changed files with 1176 additions and 894 deletions

View file

@ -206,7 +206,8 @@ public class PosixTarHeaderTest {
byte[] bytes = header.getBytes();
bytes[150] = '0';
bytes[151] = '0';
thrown.expect(IllegalArgumentException.class, "chksum invalid");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("chksum invalid");
PosixTarHeader.from(bytes);
}

View file

@ -86,13 +86,15 @@ public class RetrierTest {
@Test
public void testRetryableException() throws Exception {
thrown.expect(CountingException.class, "3");
thrown.expect(CountingException.class);
thrown.expectMessage("3");
retrier.callWithRetry(new CountingThrower(3), CountingException.class);
}
@Test
public void testUnretryableException() throws Exception {
thrown.expect(CountingException.class, "1");
thrown.expect(CountingException.class);
thrown.expectMessage("1");
retrier.callWithRetry(new CountingThrower(5), IllegalArgumentException.class);
}
@ -104,7 +106,8 @@ public class RetrierTest {
@Test
public void testRetryFailed_withReporter() throws Exception {
thrown.expect(CountingException.class, "3");
thrown.expect(CountingException.class);
thrown.expectMessage("3");
TestReporter reporter = new TestReporter();
try {
retrier.callWithRetry(new CountingThrower(3), reporter, CountingException.class);

View file

@ -55,13 +55,15 @@ public class SerializeUtilsTest {
@Test
public void testSerialize_objectDoesntImplementSerialize_hasInformativeError() throws Exception {
thrown.expect(IllegalArgumentException.class, "Unable to serialize: LOL_VALUE");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Unable to serialize: LOL_VALUE");
serialize(new Lol());
}
@Test
public void testDeserialize_badValue_hasInformativeError() throws Exception {
thrown.expect(IllegalArgumentException.class, "Unable to deserialize: objectBytes=FF");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Unable to deserialize: objectBytes=FF");
deserialize(String.class, new byte[] { (byte) 0xff });
}
}

View file

@ -52,7 +52,8 @@ public class SqlTemplateTest {
@Test
public void testFillSqlTemplate_substitutionButNoVariables() throws Exception {
thrown.expect(IllegalArgumentException.class, "Not found in template: ONE");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Not found in template: ONE");
SqlTemplate.create("")
.put("ONE", "1")
.build();
@ -60,7 +61,8 @@ public class SqlTemplateTest {
@Test
public void testFillSqlTemplate_substitutionButMissingVariables() throws Exception {
thrown.expect(IllegalArgumentException.class, "Not found in template: TWO");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Not found in template: TWO");
SqlTemplate.create("%ONE%")
.put("ONE", "1")
.put("TWO", "2")
@ -69,7 +71,8 @@ public class SqlTemplateTest {
@Test
public void testFillSqlTemplate_sameKeyTwice_failsEarly() throws Exception {
thrown.expect(IllegalArgumentException.class, "");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("");
SqlTemplate.create("%ONE%")
.put("ONE", "1")
.put("ONE", "2");
@ -77,7 +80,8 @@ public class SqlTemplateTest {
@Test
public void testFillSqlTemplate_variablesButNotEnoughSubstitutions() throws Exception {
thrown.expect(IllegalArgumentException.class, "%TWO% found in template but no substitution");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("%TWO% found in template but no substitution");
SqlTemplate.create("%ONE% %TWO%")
.put("ONE", "1")
.build();
@ -85,7 +89,8 @@ public class SqlTemplateTest {
@Test
public void testFillSqlTemplate_mismatchedVariableAndSubstitution() throws Exception {
thrown.expect(IllegalArgumentException.class, "%TWO% found in template but no substitution");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("%TWO% found in template but no substitution");
SqlTemplate.create("%TWO%")
.put("TOO", "2")
.build();
@ -93,14 +98,16 @@ public class SqlTemplateTest {
@Test
public void testFillSqlTemplate_missingKeyVals_whatsThePoint() throws Exception {
thrown.expect(IllegalArgumentException.class, "%TWO% found in template but no substitution");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("%TWO% found in template but no substitution");
SqlTemplate.create("%TWO%")
.build();
}
@Test
public void testFillSqlTemplate_lowercaseKey_notAllowed() throws Exception {
thrown.expect(IllegalArgumentException.class, "Bad substitution key: test");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Bad substitution key: test");
SqlTemplate.create("%test%")
.put("test", "hello world")
.build();
@ -108,21 +115,24 @@ public class SqlTemplateTest {
@Test
public void testFillSqlTemplate_substitution_disallowsSingleQuotes() throws Exception {
thrown.expect(IllegalArgumentException.class, "Illegal characters in foo'bar");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Illegal characters in foo'bar");
SqlTemplate.create("The words are '%LOS%' and baz")
.put("LOS", "foo'bar");
}
@Test
public void testFillSqlTemplate_substitution_disallowsDoubleQuotes() throws Exception {
thrown.expect(IllegalArgumentException.class, "Illegal characters in foo\"bar");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Illegal characters in foo\"bar");
SqlTemplate.create("The words are '%LOS%' and baz")
.put("LOS", "foo\"bar");
}
@Test
public void testFillSqlTemplate_quoteMismatch_throwsError() throws Exception {
thrown.expect(IllegalArgumentException.class, "Quote mismatch: \"%LOS%'");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Quote mismatch: \"%LOS%'");
SqlTemplate.create("The words are \"%LOS%' and baz")
.put("LOS", "foobar")
.build();
@ -130,7 +140,8 @@ public class SqlTemplateTest {
@Test
public void testFillSqlTemplate_extendedQuote_throwsError() throws Exception {
thrown.expect(IllegalArgumentException.class, "Quote mismatch: '%LOS%");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Quote mismatch: '%LOS%");
SqlTemplate.create("The words are '%LOS%-lol' and baz")
.put("LOS", "roid")
.build();

View file

@ -95,7 +95,8 @@ public final class TaskEnqueuerTest {
.thenThrow(new TransientFailureException("two"))
.thenThrow(new TransientFailureException("three"))
.thenThrow(new TransientFailureException("four"));
thrown.expect(TransientFailureException.class, "three");
thrown.expect(TransientFailureException.class);
thrown.expectMessage("three");
taskEnqueuer.enqueue(queue, task);
}

View file

@ -68,7 +68,8 @@ public class TeeOutputStreamTest {
public void testWriteInteger_failsAfterClose() throws Exception {
OutputStream tee = new TeeOutputStream(asList(outputA));
tee.close();
thrown.expect(IllegalStateException.class, "outputstream closed");
thrown.expect(IllegalStateException.class);
thrown.expectMessage("outputstream closed");
tee.write(1);
}
@ -76,7 +77,8 @@ public class TeeOutputStreamTest {
public void testWriteByteArray_failsAfterClose() throws Exception {
OutputStream tee = new TeeOutputStream(asList(outputA));
tee.close();
thrown.expect(IllegalStateException.class, "outputstream closed");
thrown.expect(IllegalStateException.class);
thrown.expectMessage("outputstream closed");
tee.write("hello".getBytes(UTF_8));
}
@ -84,7 +86,8 @@ public class TeeOutputStreamTest {
public void testWriteByteSubarray_failsAfterClose() throws Exception {
OutputStream tee = new TeeOutputStream(asList(outputA));
tee.close();
thrown.expect(IllegalStateException.class, "outputstream closed");
thrown.expect(IllegalStateException.class);
thrown.expectMessage("outputstream closed");
tee.write("hello".getBytes(UTF_8), 1, 3);
}
}

View file

@ -40,14 +40,15 @@ public class TypeUtilsTest {
@Test
public void test_getClassFromString_notAssignableFrom() {
thrown.expect(IllegalArgumentException.class, "ArrayList does not implement/extend Integer");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("ArrayList does not implement/extend Integer");
TypeUtils.getClassFromString("java.util.ArrayList", Integer.class);
}
@Test
public void test_getClassFromString_unknownClass() {
thrown.expect(
IllegalArgumentException.class, "Failed to load class com.fake.company.nonexistent.Class");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Failed to load class com.fake.company.nonexistent.Class");
TypeUtils.getClassFromString("com.fake.company.nonexistent.Class", Object.class);
}
}

View file

@ -98,9 +98,8 @@ public class UrlFetchUtilsTest {
public void testSetPayloadMultipart_boundaryInPayload() throws Exception {
HTTPRequest request = mock(HTTPRequest.class);
String payload = "I screamed------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHHH";
thrown.expect(
IllegalStateException.class,
"Multipart data contains autogenerated boundary: "
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Multipart data contains autogenerated boundary: "
+ "------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
setPayloadMultipart(request, "lol", "cat", CSV_UTF_8, payload);
}