From 16a1d6d196c693a20ffb8be76f572885f4fda599 Mon Sep 17 00:00:00 2001 From: mcilwain Date: Thu, 14 Dec 2017 14:15:10 -0800 Subject: [PATCH] Refactor the last usages of ExpectedException to assert/expectThrows ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=179095403 --- .../rde/imports/RdeContactReaderTest.java | 20 ++-- .../rde/imports/RdeDomainReaderTest.java | 20 ++-- .../rde/imports/RdeHostInputTest.java | 18 ++-- .../rde/imports/RdeHostReaderTest.java | 20 ++-- .../rde/imports/RdeImportUtilsTest.java | 94 ++++++++----------- .../registry/rde/imports/RdeParserTest.java | 62 +++++++----- .../XjcToDomainResourceConverterTest.java | 47 +++++----- 7 files changed, 127 insertions(+), 154 deletions(-) diff --git a/javatests/google/registry/rde/imports/RdeContactReaderTest.java b/javatests/google/registry/rde/imports/RdeContactReaderTest.java index 4d13b3511..b6f5c3deb 100644 --- a/javatests/google/registry/rde/imports/RdeContactReaderTest.java +++ b/javatests/google/registry/rde/imports/RdeContactReaderTest.java @@ -15,6 +15,7 @@ package google.registry.rde.imports; import static com.google.common.truth.Truth.assertThat; +import static google.registry.testing.JUnitBackports.assertThrows; import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsService; @@ -38,7 +39,6 @@ import java.io.OutputStream; import java.util.NoSuchElementException; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -61,9 +61,6 @@ public class RdeContactReaderTest { GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); - - @Rule public final ExpectedException thrown = ExpectedException.none(); - /** Reads at least one result at 0 offset 1 maxResults */ @Test public void testZeroOffsetOneResult_readsOne() throws Exception { @@ -78,8 +75,7 @@ public class RdeContactReaderTest { pushToGcs(DEPOSIT_3_CONTACT); RdeContactReader reader = getReader(0, 1); reader.next(); - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Skips already-processed records after rehydration */ @@ -93,8 +89,7 @@ public class RdeContactReaderTest { reader = cloneReader(reader); reader.beginSlice(); // reader will not advance any further - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Reads three contacts */ @@ -115,8 +110,7 @@ public class RdeContactReaderTest { for (int i = 0; i < 3; i++) { reader.next(); } - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Reads one contact from file then stops at end of file */ @@ -125,8 +119,7 @@ public class RdeContactReaderTest { pushToGcs(DEPOSIT_1_CONTACT); RdeContactReader reader = getReader(0, 3); reader.next(); - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Skips three contacts with offset of three */ @@ -173,8 +166,7 @@ public class RdeContactReaderTest { reader.beginSlice(); reader.next(); reader.next(); - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } private void pushToGcs(ByteSource source) throws IOException { diff --git a/javatests/google/registry/rde/imports/RdeDomainReaderTest.java b/javatests/google/registry/rde/imports/RdeDomainReaderTest.java index 7ababc98c..b351d69a9 100644 --- a/javatests/google/registry/rde/imports/RdeDomainReaderTest.java +++ b/javatests/google/registry/rde/imports/RdeDomainReaderTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; import static google.registry.rde.imports.RdeImportsTestData.loadBytes; import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.persistActiveContact; +import static google.registry.testing.JUnitBackports.assertThrows; import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsService; @@ -42,7 +43,6 @@ import java.util.NoSuchElementException; 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; @@ -65,9 +65,6 @@ public class RdeDomainReaderTest { .withDatastore() .build(); - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Before public void before() { createTld("test"); @@ -89,8 +86,7 @@ public class RdeDomainReaderTest { pushToGcs(DEPOSIT_3_DOMAIN); RdeDomainReader reader = getReader(0, 1); reader.next(); - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Skips already-processed records after rehydration */ @@ -104,8 +100,7 @@ public class RdeDomainReaderTest { reader = cloneObject(reader); reader.beginSlice(); // reader will not advance any further - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Reads three domains */ @@ -126,8 +121,7 @@ public class RdeDomainReaderTest { for (int i = 0; i < 3; i++) { reader.next(); } - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Reads one domain from file then stops at end of file */ @@ -136,8 +130,7 @@ public class RdeDomainReaderTest { pushToGcs(DEPOSIT_1_DOMAIN); RdeDomainReader reader = getReader(0, 3); reader.next(); - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Skips three domains with offset of three */ @@ -184,8 +177,7 @@ public class RdeDomainReaderTest { reader.beginSlice(); reader.next(); reader.next(); - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } private void pushToGcs(ByteSource source) throws IOException { diff --git a/javatests/google/registry/rde/imports/RdeHostInputTest.java b/javatests/google/registry/rde/imports/RdeHostInputTest.java index e43b2f4dd..de6d0b45e 100644 --- a/javatests/google/registry/rde/imports/RdeHostInputTest.java +++ b/javatests/google/registry/rde/imports/RdeHostInputTest.java @@ -16,6 +16,7 @@ package google.registry.rde.imports; import static com.google.common.truth.Truth.assertThat; import static google.registry.rde.imports.RdeImportsTestData.loadBytes; +import static google.registry.testing.JUnitBackports.expectThrows; import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsService; @@ -33,7 +34,6 @@ import java.util.List; import java.util.Optional; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -57,24 +57,20 @@ public class RdeHostInputTest { public final AppEngineRule appEngine = AppEngineRule.builder() .withDatastore() .build(); - - @Rule - public final ExpectedException thrown = ExpectedException.none(); - /** Number of shards cannot be negative */ @Test public void testNegativeShards_throwsIllegalArgumentException() throws Exception { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Number of shards must be greater than zero"); - getInput(Optional.of(-1)); + IllegalArgumentException thrown = + expectThrows(IllegalArgumentException.class, () -> getInput(Optional.of(-1))); + assertThat(thrown).hasMessageThat().contains("Number of shards must be greater than zero"); } /** Number of shards cannot be zero */ @Test public void testZeroShards_throwsIllegalArgumentException() throws Exception { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Number of shards must be greater than zero"); - getInput(Optional.of(0)); + IllegalArgumentException thrown = + expectThrows(IllegalArgumentException.class, () -> getInput(Optional.of(0))); + assertThat(thrown).hasMessageThat().contains("Number of shards must be greater than zero"); } /** Escrow file with zero hosts results in one reader */ diff --git a/javatests/google/registry/rde/imports/RdeHostReaderTest.java b/javatests/google/registry/rde/imports/RdeHostReaderTest.java index ac7e20e24..08e6c5546 100644 --- a/javatests/google/registry/rde/imports/RdeHostReaderTest.java +++ b/javatests/google/registry/rde/imports/RdeHostReaderTest.java @@ -16,6 +16,7 @@ package google.registry.rde.imports; import static com.google.common.truth.Truth.assertThat; import static google.registry.rde.imports.RdeImportsTestData.loadBytes; +import static google.registry.testing.JUnitBackports.assertThrows; import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsService; @@ -39,7 +40,6 @@ import java.io.OutputStream; import java.util.NoSuchElementException; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -58,9 +58,6 @@ public class RdeHostReaderTest { GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); - - @Rule public final ExpectedException thrown = ExpectedException.none(); - /** Reads at least one result at 0 offset 1 maxResults */ @Test public void testZeroOffsetOneResult_readsOne() throws Exception { @@ -75,8 +72,7 @@ public class RdeHostReaderTest { pushToGcs(DEPOSIT_3_HOST); RdeHostReader reader = getReader(0, 1); reader.next(); - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Skips already-processed records after rehydration */ @@ -90,8 +86,7 @@ public class RdeHostReaderTest { reader = cloneReader(reader); reader.beginSlice(); // reader will not advance any further - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Reads three hosts */ @@ -112,8 +107,7 @@ public class RdeHostReaderTest { for (int i = 0; i < 3; i++) { reader.next(); } - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Reads one host from file then stops at end of file */ @@ -122,8 +116,7 @@ public class RdeHostReaderTest { pushToGcs(DEPOSIT_1_HOST); RdeHostReader reader = getReader(0, 3); reader.next(); - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } /** Skips three hosts with offset of three */ @@ -170,8 +163,7 @@ public class RdeHostReaderTest { reader.beginSlice(); reader.next(); reader.next(); - thrown.expect(NoSuchElementException.class); - reader.next(); + assertThrows(NoSuchElementException.class, reader::next); } private void pushToGcs(ByteSource source) throws IOException { diff --git a/javatests/google/registry/rde/imports/RdeImportUtilsTest.java b/javatests/google/registry/rde/imports/RdeImportUtilsTest.java index f75ed25d9..bc6d44f64 100644 --- a/javatests/google/registry/rde/imports/RdeImportUtilsTest.java +++ b/javatests/google/registry/rde/imports/RdeImportUtilsTest.java @@ -21,9 +21,9 @@ import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.persistActiveContact; import static google.registry.testing.DatastoreHelper.persistNewRegistrar; import static google.registry.testing.DatastoreHelper.persistResource; +import static google.registry.testing.JUnitBackports.assertThrows; import static google.registry.testing.JUnitBackports.expectThrows; import static org.joda.time.DateTimeZone.UTC; -import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -78,8 +78,7 @@ public class RdeImportUtilsTest extends ShardableTestCase { private InputStream xmlInput; - @Rule - public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); + @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); private final GcsUtils gcsUtils = mock(GcsUtils.class); @@ -103,7 +102,7 @@ public class RdeImportUtilsTest extends ShardableTestCase { } } - /** Verifies import of a contact that has not been previously imported */ + /** Verifies import of a contact that has not been previously imported. */ @Test public void testImportNewContact() { final ContactResource newContact = buildNewContact(); @@ -111,7 +110,7 @@ public class RdeImportUtilsTest extends ShardableTestCase { assertEppResourceIndexEntityFor(newContact); assertForeignKeyIndexFor(newContact); - // verify the new contact was saved + // Verify that the new contact was saved. ContactResource saved = getContact("TEST-123"); assertThat(saved).isNotNull(); assertThat(saved.getContactId()).isEqualTo(newContact.getContactId()); @@ -119,7 +118,7 @@ public class RdeImportUtilsTest extends ShardableTestCase { assertThat(saved.getLastEppUpdateTime()).isEqualTo(newContact.getLastEppUpdateTime()); } - /** Verifies that a contact will not be imported more than once */ + /** Verifies that a contact will not be imported more than once. */ @Test public void testImportExistingContact() { ContactResource newContact = buildNewContact(); @@ -129,20 +128,16 @@ public class RdeImportUtilsTest extends ShardableTestCase { .asBuilder() .setLastEppUpdateTime(newContact.getLastEppUpdateTime().plusSeconds(1)) .build(); - try { - importResourceInTransaction(updatedContact); - fail("Expected ResourceExistsException"); - } catch (ResourceExistsException expected) { - // verify the updated contact was not saved - ContactResource saved = getContact("TEST-123"); - assertThat(saved).isNotNull(); - assertThat(saved.getContactId()).isEqualTo(newContact.getContactId()); - assertThat(saved.getEmailAddress()).isEqualTo(newContact.getEmailAddress()); - assertThat(saved.getLastEppUpdateTime()).isEqualTo(newContact.getLastEppUpdateTime()); - } + assertThrows(ResourceExistsException.class, () -> importResourceInTransaction(updatedContact)); + // Verify that the updated contact was not saved. + ContactResource saved = getContact("TEST-123"); + assertThat(saved).isNotNull(); + assertThat(saved.getContactId()).isEqualTo(newContact.getContactId()); + assertThat(saved.getEmailAddress()).isEqualTo(newContact.getEmailAddress()); + assertThat(saved.getLastEppUpdateTime()).isEqualTo(newContact.getLastEppUpdateTime()); } - /** Verifies import of a host that has not been previously imported */ + /** Verifies import of a host that has not been previously imported. */ @Test public void testImportNewHost() throws UnknownHostException { final HostResource newHost = buildNewHost(); @@ -151,7 +146,7 @@ public class RdeImportUtilsTest extends ShardableTestCase { assertEppResourceIndexEntityFor(newHost); assertForeignKeyIndexFor(newHost); - // verify the new contact was saved + // Verify that the new contact was saved. HostResource saved = getHost("FOO_ROID"); assertThat(saved).isNotNull(); assertThat(saved.getFullyQualifiedHostName()).isEqualTo(newHost.getFullyQualifiedHostName()); @@ -159,7 +154,7 @@ public class RdeImportUtilsTest extends ShardableTestCase { assertThat(saved.getLastEppUpdateTime()).isEqualTo(newHost.getLastEppUpdateTime()); } - /** Verifies that a host will not be imported more than once */ + /** Verifies that a host will not be imported more than once. */ @Test public void testImportExistingHost() throws UnknownHostException { HostResource newHost = buildNewHost(); @@ -169,17 +164,13 @@ public class RdeImportUtilsTest extends ShardableTestCase { .asBuilder() .setLastEppUpdateTime(newHost.getLastEppUpdateTime().plusSeconds(1)) .build(); - try { - importResourceInTransaction(updatedHost); - fail("Expected ResourceExistsException"); - } catch (ResourceExistsException expected) { - // verify the contact was not updated - HostResource saved = getHost("FOO_ROID"); - assertThat(saved).isNotNull(); - assertThat(saved.getFullyQualifiedHostName()).isEqualTo(newHost.getFullyQualifiedHostName()); - assertThat(saved.getInetAddresses()).isEqualTo(newHost.getInetAddresses()); - assertThat(saved.getLastEppUpdateTime()).isEqualTo(newHost.getLastEppUpdateTime()); - } + assertThrows(ResourceExistsException.class, () -> importResourceInTransaction(updatedHost)); + // Verify that the contact was not updated. + HostResource saved = getHost("FOO_ROID"); + assertThat(saved).isNotNull(); + assertThat(saved.getFullyQualifiedHostName()).isEqualTo(newHost.getFullyQualifiedHostName()); + assertThat(saved.getInetAddresses()).isEqualTo(newHost.getInetAddresses()); + assertThat(saved.getLastEppUpdateTime()).isEqualTo(newHost.getLastEppUpdateTime()); } @Test @@ -209,23 +200,18 @@ public class RdeImportUtilsTest extends ShardableTestCase { .asBuilder() .setFullyQualifiedDomainName("1" + newDomain.getFullyQualifiedDomainName()) .build(); - try { - importResourceInTransaction(updatedDomain); - fail("Expected ResourceExistsException"); - } catch (ResourceExistsException expected) { - DomainResource saved = getDomain("Dexample1-TEST"); - assertThat(saved.getFullyQualifiedDomainName()) - .isEqualTo(newDomain.getFullyQualifiedDomainName()); - assertThat(saved.getStatusValues()).isEqualTo(newDomain.getStatusValues()); - assertThat(saved.getRegistrant()).isEqualTo(newDomain.getRegistrant()); - assertThat(saved.getContacts()).isEqualTo(newDomain.getContacts()); - assertThat(saved.getCurrentSponsorClientId()) - .isEqualTo(newDomain.getCurrentSponsorClientId()); - assertThat(saved.getCreationClientId()).isEqualTo(newDomain.getCreationClientId()); - assertThat(saved.getCreationTime()).isEqualTo(newDomain.getCreationTime()); - assertThat(saved.getRegistrationExpirationTime()) - .isEqualTo(newDomain.getRegistrationExpirationTime()); - } + assertThrows(ResourceExistsException.class, () -> importResourceInTransaction(updatedDomain)); + DomainResource saved = getDomain("Dexample1-TEST"); + assertThat(saved.getFullyQualifiedDomainName()) + .isEqualTo(newDomain.getFullyQualifiedDomainName()); + assertThat(saved.getStatusValues()).isEqualTo(newDomain.getStatusValues()); + assertThat(saved.getRegistrant()).isEqualTo(newDomain.getRegistrant()); + assertThat(saved.getContacts()).isEqualTo(newDomain.getContacts()); + assertThat(saved.getCurrentSponsorClientId()).isEqualTo(newDomain.getCurrentSponsorClientId()); + assertThat(saved.getCreationClientId()).isEqualTo(newDomain.getCreationClientId()); + assertThat(saved.getCreationTime()).isEqualTo(newDomain.getCreationTime()); + assertThat(saved.getRegistrationExpirationTime()) + .isEqualTo(newDomain.getRegistrationExpirationTime()); } private static ContactResource buildNewContact() { @@ -258,9 +244,10 @@ public class RdeImportUtilsTest extends ShardableTestCase { .setRepoId("Dexample1-TEST") .setStatusValues(ImmutableSet.of(StatusValue.OK)) .setRegistrant(Key.create(registrant)) - .setContacts(ImmutableSet.of( - DesignatedContact.create(Type.ADMIN, Key.create(admin)), - DesignatedContact.create(Type.TECH, Key.create(admin)))) + .setContacts( + ImmutableSet.of( + DesignatedContact.create(Type.ADMIN, Key.create(admin)), + DesignatedContact.create(Type.TECH, Key.create(admin)))) .setPersistedCurrentSponsorClientId("registrarx") .setCreationClientId("registrarx") .setCreationTime(DateTime.parse("1999-04-03T22:00:00.0Z")) @@ -275,8 +262,9 @@ public class RdeImportUtilsTest extends ShardableTestCase { when(gcsUtils.openInputStream(any(GcsFilename.class))).thenReturn(xmlInput); rdeImportUtils.validateEscrowFileForImport("valid-deposit-file.xml"); // stored to avoid an error in FOSS build, marked "CheckReturnValue" - InputStream unusedResult = verify(gcsUtils).openInputStream( - new GcsFilename("import-bucket", "valid-deposit-file.xml")); + InputStream unusedResult = + verify(gcsUtils) + .openInputStream(new GcsFilename("import-bucket", "valid-deposit-file.xml")); } /** Verifies thrown error when tld in escrow file is not in the registry */ diff --git a/javatests/google/registry/rde/imports/RdeParserTest.java b/javatests/google/registry/rde/imports/RdeParserTest.java index dfe92a093..7a40d43e3 100644 --- a/javatests/google/registry/rde/imports/RdeParserTest.java +++ b/javatests/google/registry/rde/imports/RdeParserTest.java @@ -16,6 +16,7 @@ package google.registry.rde.imports; import static com.google.common.truth.Truth.assertThat; import static google.registry.rde.imports.RdeImportsTestData.loadBytes; +import static google.registry.testing.JUnitBackports.expectThrows; import com.google.common.io.ByteSource; import google.registry.rde.imports.RdeParser.RdeHeader; @@ -31,9 +32,7 @@ import java.io.IOException; import java.io.InputStream; import org.junit.After; 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,9 +43,6 @@ public class RdeParserTest { private static final ByteSource DEPOSIT_XML = loadBytes("deposit_full_parser.xml"); private InputStream xml; - - @Rule public final ExpectedException thrown = ExpectedException.none(); - private void checkHeader(RdeHeader header) { assertThat(header.getTld()).isEqualTo("test"); assertThat(header.getContactCount()).isEqualTo(1L); @@ -78,9 +74,11 @@ public class RdeParserTest { @Test public void testGetContactNotAtElement_throwsIllegalStateException() throws Exception { try (RdeParser parser = new RdeParser(xml)) { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Not at element urn:ietf:params:xml:ns:rdeContact-1.0:contact"); - parser.getContact(); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> parser.getContact()); + assertThat(thrown) + .hasMessageThat() + .contains("Not at element urn:ietf:params:xml:ns:rdeContact-1.0:contact"); } } @@ -161,9 +159,11 @@ public class RdeParserTest { @Test public void testGetDomainNotAtElement_throwsIllegalStateException() throws Exception { try (RdeParser parser = new RdeParser(xml)) { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Not at element urn:ietf:params:xml:ns:rdeDomain-1.0:domain"); - parser.getDomain(); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> parser.getDomain()); + assertThat(thrown) + .hasMessageThat() + .contains("Not at element urn:ietf:params:xml:ns:rdeDomain-1.0:domain"); } } @@ -272,9 +272,11 @@ public class RdeParserTest { @Test public void testGetHostNotAtElement_throwsIllegalStateException() throws Exception { try (RdeParser parser = new RdeParser(xml)) { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Not at element urn:ietf:params:xml:ns:rdeHost-1.0:host"); - parser.getHost(); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> parser.getHost()); + assertThat(thrown) + .hasMessageThat() + .contains("Not at element urn:ietf:params:xml:ns:rdeHost-1.0:host"); } } @@ -384,9 +386,11 @@ public class RdeParserTest { @Test public void testGetRegistrarNotAtElement_throwsIllegalStateException() throws Exception { try (RdeParser parser = new RdeParser(xml)) { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Not at element urn:ietf:params:xml:ns:rdeRegistrar-1.0:registrar"); - parser.getRegistrar(); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> parser.getRegistrar()); + assertThat(thrown) + .hasMessageThat() + .contains("Not at element urn:ietf:params:xml:ns:rdeRegistrar-1.0:registrar"); } } @@ -422,9 +426,11 @@ public class RdeParserTest { @Test public void testGetNndnNotAtElement_throwsIllegalStateException() throws Exception { try (RdeParser parser = new RdeParser(xml)) { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Not at element urn:ietf:params:xml:ns:rdeNNDN-1.0:NNDN"); - parser.getNndn(); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> parser.getNndn()); + assertThat(thrown) + .hasMessageThat() + .contains("Not at element urn:ietf:params:xml:ns:rdeNNDN-1.0:NNDN"); } } @@ -459,9 +465,11 @@ public class RdeParserTest { @Test public void testGetIdnNotAtElement_throwsIllegalStateException() throws Exception { try (RdeParser parser = new RdeParser(xml)) { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Not at element urn:ietf:params:xml:ns:rdeIDN-1.0:idnTableRef"); - parser.getIdn(); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> parser.getIdn()); + assertThat(thrown) + .hasMessageThat() + .contains("Not at element urn:ietf:params:xml:ns:rdeIDN-1.0:idnTableRef"); } } @@ -498,9 +506,11 @@ public class RdeParserTest { @Test public void testGetEppParamsNotAtElement_throwsIllegalStateException() throws Exception { try (RdeParser parser = new RdeParser(xml)) { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Not at element urn:ietf:params:xml:ns:rdeEppParams-1.0:eppParams"); - parser.getEppParams(); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> parser.getEppParams()); + assertThat(thrown) + .hasMessageThat() + .contains("Not at element urn:ietf:params:xml:ns:rdeEppParams-1.0:eppParams"); } } diff --git a/javatests/google/registry/rde/imports/XjcToDomainResourceConverterTest.java b/javatests/google/registry/rde/imports/XjcToDomainResourceConverterTest.java index 3c2f7ebc8..ce77d7ca5 100644 --- a/javatests/google/registry/rde/imports/XjcToDomainResourceConverterTest.java +++ b/javatests/google/registry/rde/imports/XjcToDomainResourceConverterTest.java @@ -26,6 +26,7 @@ import static google.registry.testing.DatastoreHelper.getHistoryEntries; import static google.registry.testing.DatastoreHelper.persistActiveContact; import static google.registry.testing.DatastoreHelper.persistActiveHost; import static google.registry.testing.DatastoreHelper.persistResource; +import static google.registry.testing.JUnitBackports.expectThrows; import static google.registry.util.DateTimeUtils.END_OF_TIME; import static java.util.Arrays.asList; @@ -65,7 +66,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; @@ -90,9 +90,6 @@ public class XjcToDomainResourceConverterTest { "google.registry.xjc.smd")); @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); - - @Rule public final ExpectedException thrown = ExpectedException.none(); - @Rule public final InjectRule inject = new InjectRule(); private Unmarshaller unmarshaller; @@ -216,9 +213,11 @@ public class XjcToDomainResourceConverterTest { persistActiveContact("jd1234"); persistActiveContact("sh8013"); final XjcRdeDomain xjcDomain = loadDomainFromRdeXml("domain_fragment_pendingRestorePeriod.xml"); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Unsupported grace period status: PENDING_RESTORE"); - convertDomainInTransaction(xjcDomain); + IllegalArgumentException thrown = + expectThrows(IllegalArgumentException.class, () -> convertDomainInTransaction(xjcDomain)); + assertThat(thrown) + .hasMessageThat() + .contains("Unsupported grace period status: PENDING_RESTORE"); } @Test @@ -270,9 +269,9 @@ public class XjcToDomainResourceConverterTest { persistActiveHost("ns1.example.net"); persistActiveHost("ns2.example.net"); final XjcRdeDomain xjcDomain = loadDomainFromRdeXml("domain_fragment_host_attrs.xml"); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Host attributes are not yet supported"); - convertDomainInTransaction(xjcDomain); + IllegalArgumentException thrown = + expectThrows(IllegalArgumentException.class, () -> convertDomainInTransaction(xjcDomain)); + assertThat(thrown).hasMessageThat().contains("Host attributes are not yet supported"); } @Test @@ -281,18 +280,20 @@ public class XjcToDomainResourceConverterTest { persistActiveContact("sh8013"); persistActiveHost("ns1.example.net"); final XjcRdeDomain xjcDomain = loadDomainFromRdeXml("domain_fragment_host_objs.xml"); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("HostResource not found with name 'ns2.example.net'"); - convertDomainInTransaction(xjcDomain); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> convertDomainInTransaction(xjcDomain)); + assertThat(thrown) + .hasMessageThat() + .contains("HostResource not found with name 'ns2.example.net'"); } @Test public void testConvertDomainResourceRegistrantNotFound() throws Exception { persistActiveContact("sh8013"); final XjcRdeDomain xjcDomain = loadDomainFromRdeXml("domain_fragment.xml"); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Registrant not found: 'jd1234'"); - convertDomainInTransaction(xjcDomain); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> convertDomainInTransaction(xjcDomain)); + assertThat(thrown).hasMessageThat().contains("Registrant not found: 'jd1234'"); } @Test @@ -300,18 +301,20 @@ public class XjcToDomainResourceConverterTest { persistActiveContact("jd1234"); persistActiveContact("sh8013"); final XjcRdeDomain xjcDomain = loadDomainFromRdeXml("domain_fragment_registrant_missing.xml"); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Registrant is missing for domain 'example1.example'"); - convertDomainInTransaction(xjcDomain); + IllegalArgumentException thrown = + expectThrows(IllegalArgumentException.class, () -> convertDomainInTransaction(xjcDomain)); + assertThat(thrown) + .hasMessageThat() + .contains("Registrant is missing for domain 'example1.example'"); } @Test public void testConvertDomainResourceAdminNotFound() throws Exception { persistActiveContact("jd1234"); final XjcRdeDomain xjcDomain = loadDomainFromRdeXml("domain_fragment.xml"); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Contact not found: 'sh8013'"); - convertDomainInTransaction(xjcDomain); + IllegalStateException thrown = + expectThrows(IllegalStateException.class, () -> convertDomainInTransaction(xjcDomain)); + assertThat(thrown).hasMessageThat().contains("Contact not found: 'sh8013'"); } @Test