Automatically refactor more exception testing to use new JUnit rules

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178911894
This commit is contained in:
mcilwain 2017-08-14 09:20:03 -04:00 committed by Ben McIlwain
parent 36ad38e5df
commit 7dc224627f
125 changed files with 1970 additions and 1982 deletions

View file

@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.joda.time.Duration.standardDays;
import static org.joda.time.Duration.standardSeconds;
import static org.mockito.Mockito.mock;
@ -37,7 +38,6 @@ import org.joda.time.DateTimeZone;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -45,9 +45,6 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class EscrowTaskRunnerTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder()
.withDatastore()
@ -99,10 +96,13 @@ public class EscrowTaskRunnerTest {
clock.setTo(DateTime.parse("2006-06-06T00:30:00Z"));
persistResource(
Cursor.create(CursorType.RDE_STAGING, DateTime.parse("2006-06-07TZ"), registry));
thrown.expect(NoContentException.class);
thrown.expectMessage("Already completed");
runner.lockRunAndRollForward(
task, registry, standardSeconds(30), CursorType.RDE_STAGING, standardDays(1));
NoContentException thrown =
expectThrows(
NoContentException.class,
() ->
runner.lockRunAndRollForward(
task, registry, standardSeconds(30), CursorType.RDE_STAGING, standardDays(1)));
assertThat(thrown).hasMessageThat().contains("Already completed");
}
@Test
@ -111,10 +111,14 @@ public class EscrowTaskRunnerTest {
clock.setTo(DateTime.parse("2006-06-06T00:30:00Z"));
persistResource(
Cursor.create(CursorType.RDE_STAGING, DateTime.parse("2006-06-06TZ"), registry));
thrown.expect(ServiceUnavailableException.class);
thrown.expectMessage("Lock in use: " + lockName + " for TLD: lol");
runner.lockHandler = new FakeLockHandler(false);
runner.lockRunAndRollForward(
task, registry, standardSeconds(30), CursorType.RDE_STAGING, standardDays(1));
ServiceUnavailableException thrown =
expectThrows(
ServiceUnavailableException.class,
() -> {
runner.lockHandler = new FakeLockHandler(false);
runner.lockRunAndRollForward(
task, registry, standardSeconds(30), CursorType.RDE_STAGING, standardDays(1));
});
assertThat(thrown).hasMessageThat().contains("Lock in use: " + lockName + " for TLD: lol");
}
}

View file

@ -17,6 +17,8 @@ package google.registry.rde;
import static com.google.common.base.Strings.repeat;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.keyring.api.PgpHelper.KeyRequirement.ENCRYPT;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
@ -42,7 +44,6 @@ import org.junit.Test;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
/** Unit tests for {@link Ghostryde}. */
@ -50,9 +51,6 @@ import org.junit.runner.RunWith;
@SuppressWarnings("resource")
public class GhostrydeTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final BouncyCastleProviderRule bouncy = new BouncyCastleProviderRule();
@ -194,11 +192,15 @@ public class GhostrydeTest {
korruption(ciphertext, ciphertext.length / 2);
ByteArrayInputStream bsIn = new ByteArrayInputStream(ciphertext);
thrown.expect(IllegalStateException.class);
thrown.expectMessage("tampering");
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream());
}
IllegalStateException thrown =
expectThrows(
IllegalStateException.class,
() -> {
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream());
}
});
assertThat(thrown).hasMessageThat().contains("tampering");
}
@Theory
@ -223,10 +225,13 @@ public class GhostrydeTest {
korruption(ciphertext, ciphertext.length / 2);
ByteArrayInputStream bsIn = new ByteArrayInputStream(ciphertext);
thrown.expect(PGPException.class);
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream());
}
assertThrows(
PGPException.class,
() -> {
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream());
}
});
}
@Test
@ -248,12 +253,17 @@ public class GhostrydeTest {
}
ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray());
thrown.expect(PGPException.class);
thrown.expectMessage(
"Message was encrypted for keyid a59c132f3589a1d5 but ours is c9598c84ec70b9fd");
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream());
}
PGPException thrown =
expectThrows(
PGPException.class,
() -> {
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream());
}
});
assertThat(thrown)
.hasMessageThat()
.contains("Message was encrypted for keyid a59c132f3589a1d5 but ours is c9598c84ec70b9fd");
}
@Test

View file

@ -17,6 +17,7 @@ package google.registry.rde;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.xjc.XjcXmlTransformer.marshalStrict;
import static java.nio.charset.StandardCharsets.UTF_8;
@ -36,7 +37,6 @@ import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -54,9 +54,6 @@ public class HostResourceToXjcConverterTest {
.withDatastore()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Before
public void init() {
createTld("foobar");
@ -192,20 +189,22 @@ public class HostResourceToXjcConverterTest {
@Test
public void testHostStatusValueIsInvalid() throws Exception {
thrown.expect(IllegalArgumentException.class);
HostResourceToXjcConverter.convertExternalHost(
new HostResource.Builder()
.setCreationClientId("LawyerCat")
.setCreationTimeForTest(DateTime.parse("1900-01-01T00:00:00Z"))
.setPersistedCurrentSponsorClientId("BusinessCat")
.setFullyQualifiedHostName("ns1.love.lol")
.setInetAddresses(ImmutableSet.of(InetAddresses.forString("cafe::abba")))
.setLastTransferTime(DateTime.parse("1910-01-01T00:00:00Z"))
.setLastEppUpdateClientId("CeilingCat")
.setLastEppUpdateTime(DateTime.parse("1920-01-01T00:00:00Z"))
.setRepoId("2-LOL")
.setStatusValues(ImmutableSet.of(StatusValue.SERVER_HOLD)) // <-- OOPS
.build());
assertThrows(
IllegalArgumentException.class,
() ->
HostResourceToXjcConverter.convertExternalHost(
new HostResource.Builder()
.setCreationClientId("LawyerCat")
.setCreationTimeForTest(DateTime.parse("1900-01-01T00:00:00Z"))
.setPersistedCurrentSponsorClientId("BusinessCat")
.setFullyQualifiedHostName("ns1.love.lol")
.setInetAddresses(ImmutableSet.of(InetAddresses.forString("cafe::abba")))
.setLastTransferTime(DateTime.parse("1910-01-01T00:00:00Z"))
.setLastEppUpdateClientId("CeilingCat")
.setLastEppUpdateTime(DateTime.parse("1920-01-01T00:00:00Z"))
.setRepoId("2-LOL")
.setStatusValues(ImmutableSet.of(StatusValue.SERVER_HOLD)) // <-- OOPS
.build()));
}
@Test

View file

@ -21,6 +21,8 @@ import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.GcsTestingUtils.writeGcsFile;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.joda.time.DateTimeZone.UTC;
@ -65,7 +67,6 @@ import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
@ -77,10 +78,6 @@ public class RdeReportActionTest {
private static final ByteSource REPORT_XML = RdeTestData.loadBytes("report.xml");
private static final ByteSource IIRDEA_BAD_XML = RdeTestData.loadBytes("iirdea_bad.xml");
private static final ByteSource IIRDEA_GOOD_XML = RdeTestData.loadBytes("iirdea_good.xml");
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final BouncyCastleProviderRule bouncy = new BouncyCastleProviderRule();
@ -175,17 +172,19 @@ public class RdeReportActionTest {
when(httpResponse.getResponseCode()).thenReturn(SC_BAD_REQUEST);
when(httpResponse.getContent()).thenReturn(IIRDEA_BAD_XML.read());
when(urlFetchService.fetch(request.capture())).thenReturn(httpResponse);
thrown.expect(InternalServerErrorException.class);
thrown.expectMessage("The structure of the report is invalid.");
createAction().runWithLock(loadRdeReportCursor());
InternalServerErrorException thrown =
expectThrows(
InternalServerErrorException.class,
() -> createAction().runWithLock(loadRdeReportCursor()));
assertThat(thrown).hasMessageThat().contains("The structure of the report is invalid.");
}
@Test
public void testRunWithLock_fetchFailed_throwsRuntimeException() throws Exception {
class ExpectedThrownException extends RuntimeException {}
when(urlFetchService.fetch(any(HTTPRequest.class))).thenThrow(new ExpectedThrownException());
thrown.expect(ExpectedThrownException.class);
createAction().runWithLock(loadRdeReportCursor());
assertThrows(
ExpectedThrownException.class, () -> createAction().runWithLock(loadRdeReportCursor()));
}
@Test

View file

@ -26,6 +26,7 @@ import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.DatastoreHelper.persistResourceWithCommitLog;
import static google.registry.testing.GcsTestingUtils.readGcsFile;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.TaskQueueHelper.assertAtLeastOneTaskIsEnqueued;
import static google.registry.testing.TaskQueueHelper.assertNoTasksEnqueued;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
@ -92,7 +93,6 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -108,9 +108,6 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
@Rule
public final InjectRule inject = new InjectRule();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final FakeClock clock = new FakeClock();
private final FakeResponse response = new FakeResponse();
private final GcsService gcsService = GcsServiceFactory.createGcsService();
@ -162,8 +159,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
createTldWithEscrowEnabled("lol");
clock.setTo(DateTime.parse("2000-01-01TZ"));
action.modeStrings = ImmutableSet.of("full");
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test
@ -171,8 +167,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
createTldWithEscrowEnabled("lol");
clock.setTo(DateTime.parse("2000-01-01TZ"));
action.tlds = ImmutableSet.of("tld");
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test
@ -180,8 +175,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
createTldWithEscrowEnabled("lol");
clock.setTo(DateTime.parse("2000-01-01TZ"));
action.watermarks = ImmutableSet.of(clock.nowUtc());
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test
@ -189,8 +183,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
createTldWithEscrowEnabled("lol");
clock.setTo(DateTime.parse("2000-01-01TZ"));
action.revision = Optional.of(42);
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test
@ -248,8 +241,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.modeStrings = ImmutableSet.of();
action.tlds = ImmutableSet.of("lol");
action.watermarks = ImmutableSet.of(clock.nowUtc());
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test
@ -261,8 +253,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.modeStrings = ImmutableSet.of("full", "thing");
action.tlds = ImmutableSet.of("lol");
action.watermarks = ImmutableSet.of(clock.nowUtc());
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test
@ -274,8 +265,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.modeStrings = ImmutableSet.of("full");
action.tlds = ImmutableSet.of();
action.watermarks = ImmutableSet.of(clock.nowUtc());
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test
@ -287,8 +277,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.modeStrings = ImmutableSet.of("full");
action.tlds = ImmutableSet.of("lol");
action.watermarks = ImmutableSet.of();
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test
@ -300,8 +289,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.modeStrings = ImmutableSet.of("full");
action.tlds = ImmutableSet.of("lol");
action.watermarks = ImmutableSet.of(DateTime.parse("2001-01-01T01:36:45Z"));
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test
@ -314,8 +302,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.tlds = ImmutableSet.of("lol");
action.watermarks = ImmutableSet.of(DateTime.parse("2001-01-01T00:00:00Z"));
action.revision = Optional.of(-1);
thrown.expect(BadRequestException.class);
action.run();
assertThrows(BadRequestException.class, () -> action.run());
}
@Test

View file

@ -24,6 +24,7 @@ import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.DatastoreHelper.persistSimpleResource;
import static google.registry.testing.GcsTestingUtils.readGcsFile;
import static google.registry.testing.GcsTestingUtils.writeGcsFile;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.testing.SystemInfo.hasCommand;
import static google.registry.testing.TaskQueueHelper.assertNoTasksEnqueued;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
@ -86,7 +87,6 @@ import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -113,10 +113,6 @@ public class RdeUploadActionTest {
new GcsFilename("bucket", "tld_2010-10-17_full_S1_R1.xml.length");
private static final GcsFilename REPORT_R1_FILE =
new GcsFilename("bucket", "tld_2010-10-17_full_S1_R1-report.xml.ghostryde");
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final SftpServerRule sftpd = new SftpServerRule();
@ -312,9 +308,9 @@ public class RdeUploadActionTest {
Cursor.create(CursorType.RDE_STAGING, stagingCursor, Registry.get("tld")));
RdeUploadAction action = createAction(uploadUrl);
action.lazyJsch = Lazies.of(createThrowingJSchSpy(action.lazyJsch.get(), 3));
thrown.expect(RuntimeException.class);
thrown.expectMessage("The crow flies in square circles.");
action.runWithLock(uploadCursor);
RuntimeException thrown =
expectThrows(RuntimeException.class, () -> action.runWithLock(uploadCursor));
assertThat(thrown).hasMessageThat().contains("The crow flies in square circles.");
}
@Test
@ -387,9 +383,10 @@ public class RdeUploadActionTest {
DateTime uploadCursor = DateTime.parse("2010-10-17TZ");
persistResource(
Cursor.create(CursorType.RDE_STAGING, stagingCursor, Registry.get("tld")));
thrown.expect(ServiceUnavailableException.class);
thrown.expectMessage("Waiting for RdeStagingAction to complete");
createAction(null).runWithLock(uploadCursor);
ServiceUnavailableException thrown =
expectThrows(
ServiceUnavailableException.class, () -> createAction(null).runWithLock(uploadCursor));
assertThat(thrown).hasMessageThat().contains("Waiting for RdeStagingAction to complete");
}
private String slurp(InputStream is) throws FileNotFoundException, IOException {