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

@ -14,9 +14,11 @@
package google.registry.dns;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistActiveSubordinateHost;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.testing.TaskQueueHelper.assertDnsTasksEnqueued;
import static google.registry.testing.TaskQueueHelper.assertNoDnsTasksEnqueued;
import static org.mockito.Mockito.mock;
@ -36,7 +38,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;
@ -50,9 +51,6 @@ public final class DnsInjectionTest {
.withTaskQueue()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final InjectRule inject = new InjectRule();
@ -97,9 +95,9 @@ public final class DnsInjectionTest {
public void testRefreshDns_missingDomain_throwsNotFound() throws Exception {
when(req.getParameter("type")).thenReturn("domain");
when(req.getParameter("name")).thenReturn("example.lol");
thrown.expect(NotFoundException.class);
thrown.expectMessage("domain example.lol not found");
component.refreshDns().run();
NotFoundException thrown =
expectThrows(NotFoundException.class, () -> component.refreshDns().run());
assertThat(thrown).hasMessageThat().contains("domain example.lol not found");
}
@Test
@ -115,8 +113,8 @@ public final class DnsInjectionTest {
public void testRefreshDns_missingHost_throwsNotFound() throws Exception {
when(req.getParameter("type")).thenReturn("host");
when(req.getParameter("name")).thenReturn("ns1.example.lol");
thrown.expect(NotFoundException.class);
thrown.expectMessage("host ns1.example.lol not found");
component.refreshDns().run();
NotFoundException thrown =
expectThrows(NotFoundException.class, () -> component.refreshDns().run());
assertThat(thrown).hasMessageThat().contains("host ns1.example.lol not found");
}
}

View file

@ -14,7 +14,9 @@
package google.registry.dns;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.testing.TaskQueueHelper.assertNoTasksEnqueued;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
@ -23,7 +25,6 @@ import google.registry.testing.TaskQueueHelper.TaskMatcher;
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;
@ -36,10 +37,6 @@ public class DnsQueueTest {
.withDatastore()
.withTaskQueue()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private DnsQueue dnsQueue;
@Before
@ -62,13 +59,19 @@ public class DnsQueueTest {
@Test
public void test_addHostRefreshTask_failsOnUnknownTld() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("octopus.notatld is not a subordinate host to a known tld");
try {
dnsQueue.addHostRefreshTask("octopus.notatld");
} finally {
assertNoTasksEnqueued("dns-pull");
}
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> {
try {
dnsQueue.addHostRefreshTask("octopus.notatld");
} finally {
assertNoTasksEnqueued("dns-pull");
}
});
assertThat(thrown)
.hasMessageThat()
.contains("octopus.notatld is not a subordinate host to a known tld");
}
@Test
@ -85,13 +88,17 @@ public class DnsQueueTest {
@Test
public void test_addDomainRefreshTask_failsOnUnknownTld() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("TLD notatld does not exist");
try {
dnsQueue.addDomainRefreshTask("fake.notatld");
} finally {
assertNoTasksEnqueued("dns-pull");
}
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> {
try {
dnsQueue.addDomainRefreshTask("fake.notatld");
} finally {
assertNoTasksEnqueued("dns-pull");
}
});
assertThat(thrown).hasMessageThat().contains("TLD notatld does not exist");
}
}

View file

@ -14,10 +14,12 @@
package google.registry.dns;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistActiveSubordinateHost;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@ -40,7 +42,6 @@ import org.joda.time.Duration;
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;
@ -56,10 +57,6 @@ public class PublishDnsUpdatesActionTest {
@Rule
public final InjectRule inject = new InjectRule();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final FakeClock clock = new FakeClock(DateTime.parse("1971-01-01TZ"));
private final FakeLockHandler lockHandler = new FakeLockHandler(true);
private final DnsWriter dnsWriter = mock(DnsWriter.class);
@ -187,19 +184,24 @@ public class PublishDnsUpdatesActionTest {
@Test
public void testLockIsntAvailable() throws Exception {
thrown.expect(ServiceUnavailableException.class);
thrown.expectMessage("Lock failure");
action = createAction("xn--q9jyb4c");
action.domains = ImmutableSet.of("example.com", "example2.com");
action.hosts = ImmutableSet.of("ns1.example.com", "ns2.example.com", "ns1.example2.com");
action.lockHandler = new FakeLockHandler(false);
action.run();
ServiceUnavailableException thrown =
expectThrows(
ServiceUnavailableException.class,
() -> {
action = createAction("xn--q9jyb4c");
action.domains = ImmutableSet.of("example.com", "example2.com");
action.hosts =
ImmutableSet.of("ns1.example.com", "ns2.example.com", "ns1.example2.com");
action.lockHandler = new FakeLockHandler(false);
action.run();
verifyNoMoreInteractions(dnsWriter);
verifyNoMoreInteractions(dnsWriter);
verifyNoMoreInteractions(dnsMetrics);
verifyNoMoreInteractions(dnsMetrics);
verifyNoMoreInteractions(dnsQueue);
verifyNoMoreInteractions(dnsQueue);
});
assertThat(thrown).hasMessageThat().contains("Lock failure");
}
@Test

View file

@ -14,10 +14,13 @@
package google.registry.dns;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistActiveHost;
import static google.registry.testing.DatastoreHelper.persistActiveSubordinateHost;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@ -31,7 +34,6 @@ import google.registry.testing.FakeClock;
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;
@ -44,10 +46,6 @@ public class RefreshDnsActionTest {
.withDatastore()
.withTaskQueue()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final DnsQueue dnsQueue = mock(DnsQueue.class);
private final FakeClock clock = new FakeClock();
@ -78,13 +76,19 @@ public class RefreshDnsActionTest {
public void testSuccess_externalHostNotEnqueued() throws Exception {
persistActiveDomain("example.xn--q9jyb4c");
persistActiveHost("ns1.example.xn--q9jyb4c");
thrown.expect(BadRequestException.class);
thrown.expectMessage("ns1.example.xn--q9jyb4c isn't a subordinate hostname");
try {
run(TargetType.HOST, "ns1.example.xn--q9jyb4c");
} finally {
verifyNoMoreInteractions(dnsQueue);
}
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() -> {
try {
run(TargetType.HOST, "ns1.example.xn--q9jyb4c");
} finally {
verifyNoMoreInteractions(dnsQueue);
}
});
assertThat(thrown)
.hasMessageThat()
.contains("ns1.example.xn--q9jyb4c isn't a subordinate hostname");
}
@Test
@ -97,19 +101,16 @@ public class RefreshDnsActionTest {
@Test
public void testFailure_unqualifiedName() throws Exception {
thrown.expect(BadRequestException.class);
run(TargetType.DOMAIN, "example");
assertThrows(BadRequestException.class, () -> run(TargetType.DOMAIN, "example"));
}
@Test
public void testFailure_hostDoesNotExist() throws Exception {
thrown.expect(NotFoundException.class);
run(TargetType.HOST, "ns1.example.xn--q9jyb4c");
assertThrows(NotFoundException.class, () -> run(TargetType.HOST, "ns1.example.xn--q9jyb4c"));
}
@Test
public void testFailure_domainDoesNotExist() throws Exception {
thrown.expect(NotFoundException.class);
run(TargetType.DOMAIN, "example.xn--q9jyb4c");
assertThrows(NotFoundException.class, () -> run(TargetType.DOMAIN, "example.xn--q9jyb4c"));
}
}

View file

@ -56,7 +56,6 @@ import org.joda.time.Duration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
@ -85,8 +84,6 @@ public class CloudDnsWriterTest {
private CloudDnsWriter writer;
private ImmutableSet<ResourceRecordSet> stubZone;
@Rule public final ExpectedException thrown = ExpectedException.none();
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
/*

View file

@ -17,6 +17,7 @@ package google.registry.dns.writer.dnsupdate;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -34,9 +35,7 @@ import java.util.Arrays;
import javax.net.SocketFactory;
import org.joda.time.Duration;
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.xbill.DNS.ARecord;
@ -62,10 +61,6 @@ public class DnsMessageTransportTest {
private Message simpleQuery;
private Message expectedResponse;
private DnsMessageTransport resolver;
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Before
public void before() throws Exception {
simpleQuery =
@ -114,9 +109,7 @@ public class DnsMessageTransportTest {
new ByteArrayInputStream(Arrays.copyOf(messageBytes, messageBytes.length - 1));
when(mockSocket.getInputStream()).thenReturn(inputStream);
when(mockSocket.getOutputStream()).thenReturn(new ByteArrayOutputStream());
thrown.expect(EOFException.class);
resolver.send(new Message());
assertThrows(EOFException.class, () -> resolver.send(new Message()));
}
@Test
@ -145,10 +138,9 @@ public class DnsMessageTransportTest {
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
when(mockSocket.getOutputStream()).thenReturn(outputStream);
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("message larger than maximum");
resolver.send(oversize);
IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> resolver.send(oversize));
assertThat(thrown).hasMessageThat().contains("message larger than maximum");
}
@Test
@ -157,13 +149,14 @@ public class DnsMessageTransportTest {
when(mockSocket.getInputStream())
.thenReturn(new ByteArrayInputStream(messageToBytesWithLength(expectedResponse)));
when(mockSocket.getOutputStream()).thenReturn(new ByteArrayOutputStream());
thrown.expect(VerifyException.class);
thrown.expectMessage("response ID "
+ expectedResponse.getHeader().getID()
+ " does not match query ID "
+ simpleQuery.getHeader().getID());
resolver.send(simpleQuery);
VerifyException thrown = expectThrows(VerifyException.class, () -> resolver.send(simpleQuery));
assertThat(thrown)
.hasMessageThat()
.contains(
"response ID "
+ expectedResponse.getHeader().getID()
+ " does not match query ID "
+ simpleQuery.getHeader().getID());
}
@Test
@ -173,10 +166,10 @@ public class DnsMessageTransportTest {
when(mockSocket.getInputStream())
.thenReturn(new ByteArrayInputStream(messageToBytesWithLength(expectedResponse)));
when(mockSocket.getOutputStream()).thenReturn(new ByteArrayOutputStream());
thrown.expect(VerifyException.class);
thrown.expectMessage("response opcode 'STATUS' does not match query opcode 'QUERY'");
resolver.send(simpleQuery);
VerifyException thrown = expectThrows(VerifyException.class, () -> resolver.send(simpleQuery));
assertThat(thrown)
.hasMessageThat()
.contains("response opcode 'STATUS' does not match query opcode 'QUERY'");
}
private Message responseMessageWithCode(Message query, int responseCode) {

View file

@ -26,6 +26,7 @@ import static google.registry.testing.DatastoreHelper.persistActiveSubordinateHo
import static google.registry.testing.DatastoreHelper.persistDeletedDomain;
import static google.registry.testing.DatastoreHelper.persistDeletedHost;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
@ -52,7 +53,6 @@ import org.joda.time.Duration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
@ -76,9 +76,6 @@ public class DnsUpdateWriterTest {
public final AppEngineRule appEngine =
AppEngineRule.builder().withDatastore().withTaskQueue().build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final InjectRule inject = new InjectRule();
@ -390,11 +387,14 @@ public class DnsUpdateWriterTest {
.build();
persistResource(domain);
when(mockResolver.send(any(Message.class))).thenReturn(messageWithResponseCode(Rcode.SERVFAIL));
thrown.expect(VerifyException.class);
thrown.expectMessage("SERVFAIL");
writer.publishDomain("example.tld");
writer.commit();
VerifyException thrown =
expectThrows(
VerifyException.class,
() -> {
writer.publishDomain("example.tld");
writer.commit();
});
assertThat(thrown).hasMessageThat().contains("SERVFAIL");
}
@Test
@ -406,11 +406,14 @@ public class DnsUpdateWriterTest {
.build();
persistResource(host);
when(mockResolver.send(any(Message.class))).thenReturn(messageWithResponseCode(Rcode.SERVFAIL));
thrown.expect(VerifyException.class);
thrown.expectMessage("SERVFAIL");
writer.publishHost("ns1.example.tld");
writer.commit();
VerifyException thrown =
expectThrows(
VerifyException.class,
() -> {
writer.publishHost("ns1.example.tld");
writer.commit();
});
assertThat(thrown).hasMessageThat().contains("SERVFAIL");
}
private void assertThatUpdatedZoneIs(Update update, String zoneName) {