Automatically refactor some exception testing to use new JUnit rules

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=176550995
This commit is contained in:
mcilwain 2017-11-21 13:15:12 -08:00 committed by jianglai
parent f041b1bac0
commit c7484b25e0
13 changed files with 199 additions and 194 deletions

View file

@ -19,6 +19,9 @@ import static com.google.common.collect.Range.atMost;
import static com.google.common.collect.Range.closed;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import com.google.common.base.CharMatcher;
@ -33,9 +36,7 @@ import com.google.common.testing.NullPointerTester;
import com.google.re2j.Pattern;
import java.util.List;
import java.util.Set;
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 +46,6 @@ public class FormFieldTest {
private enum ICanHazEnum { LOL, CAT }
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testConvert_nullString_notPresent() {
assertThat(FormField.named("lol").build().convert(null)).isEmpty();
@ -77,14 +75,17 @@ public class FormFieldTest {
@Test
public void testEmptyToNullRequired_emptyString_throwsFfe() {
thrown.expect(equalTo(new FormFieldException("This field is required.").propagate("lol")));
FormField.named("lol").emptyToNull().required().build().convert("");
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() -> FormField.named("lol").emptyToNull().required().build().convert(""));
assertThat(thrown, equalTo(new FormFieldException("This field is required.").propagate("lol")));
}
@Test
public void testEmptyToNull_typeMismatch() {
thrown.expect(IllegalStateException.class);
FormField.named("lol", Object.class).emptyToNull();
assertThrows(
IllegalStateException.class, () -> FormField.named("lol", Object.class).emptyToNull());
}
@Test
@ -130,8 +131,8 @@ public class FormFieldTest {
FormField<String, String> field = FormField.named("lol")
.in(ImmutableSet.of("foo", "bar"))
.build();
thrown.expect(equalTo(new FormFieldException("Unrecognized value.").propagate("lol")));
field.convert("omfg");
FormFieldException thrown = expectThrows(FormFieldException.class, () -> field.convert("omfg"));
assertThat(thrown, equalTo(new FormFieldException("Unrecognized value.").propagate("lol")));
}
@Test
@ -146,9 +147,11 @@ public class FormFieldTest {
@Test
public void testRange_minimum_stringLengthShorterThanMinimum_throwsFfe() {
thrown.expect(FormFieldException.class);
thrown.expectMessage("Number of characters (3) not in range [4");
FormField.named("lol").range(atLeast(4)).build().convert("lol");
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() -> FormField.named("lol").range(atLeast(4)).build().convert("lol"));
assertThat(thrown).hasMessageThat().contains("Number of characters (3) not in range [4");
}
@Test
@ -163,9 +166,11 @@ public class FormFieldTest {
@Test
public void testRange_maximum_stringLengthShorterThanMaximum_throwsFfe() {
thrown.expect(FormFieldException.class);
thrown.expectMessage("Number of characters (6) not in range");
FormField.named("lol").range(atMost(5)).build().convert("omgomg");
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() -> FormField.named("lol").range(atMost(5)).build().convert("omgomg"));
assertThat(thrown).hasMessageThat().contains("Number of characters (6) not in range");
}
@Test
@ -180,8 +185,8 @@ public class FormFieldTest {
@Test
public void testRange_typeMismatch() {
thrown.expect(IllegalStateException.class);
FormField.named("lol", Object.class).range(atMost(5));
assertThrows(
IllegalStateException.class, () -> FormField.named("lol", Object.class).range(atMost(5)));
}
@Test
@ -192,14 +197,23 @@ public class FormFieldTest {
@Test
public void testMatches_mismatch_throwsFfeAndShowsDefaultErrorMessageWithPattern() {
thrown.expect(equalTo(new FormFieldException("Must match pattern: [a-z]+").propagate("lol")));
FormField.named("lol").matches(Pattern.compile("[a-z]+")).build().convert("123abc456");
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() ->
FormField.named("lol")
.matches(Pattern.compile("[a-z]+"))
.build()
.convert("123abc456"));
assertThat(
thrown, equalTo(new FormFieldException("Must match pattern: [a-z]+").propagate("lol")));
}
@Test
public void testMatches_typeMismatch() {
thrown.expect(IllegalStateException.class);
FormField.named("lol", Object.class).matches(Pattern.compile("."));
assertThrows(
IllegalStateException.class,
() -> FormField.named("lol", Object.class).matches(Pattern.compile(".")));
}
@Test
@ -246,13 +260,17 @@ public class FormFieldTest {
@Test
public void testAsListEmptyToNullRequired_empty_throwsFfe() {
thrown.expect(equalTo(new FormFieldException("This field is required.").propagate("lol")));
FormField.named("lol")
.asList()
.emptyToNull()
.required()
.build()
.convert(ImmutableList.of());
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() ->
FormField.named("lol")
.asList()
.emptyToNull()
.required()
.build()
.convert(ImmutableList.of()));
assertThat(thrown, equalTo(new FormFieldException("This field is required.").propagate("lol")));
}
@Test
@ -288,9 +306,12 @@ public class FormFieldTest {
FormField<String, ICanHazEnum> omgField = FormField.named("omg")
.asEnum(ICanHazEnum.class)
.build();
thrown.expect(equalTo(new FormFieldException("Enum ICanHazEnum does not contain 'helo'")
.propagate("omg")));
omgField.convert("helo");
FormFieldException thrown =
expectThrows(FormFieldException.class, () -> omgField.convert("helo"));
assertThat(
thrown,
equalTo(
new FormFieldException("Enum ICanHazEnum does not contain 'helo'").propagate("omg")));
}
@Test
@ -352,38 +373,53 @@ public class FormFieldTest {
@Test
public void testAsListRequiredElements_nullElement_throwsFfeWithIndex() {
thrown.expect(equalTo(new FormFieldException("This field is required.")
.propagate(1)
.propagate("lol")));
FormField.named("lol")
.emptyToNull()
.required()
.asList()
.build()
.convert(ImmutableList.of("omg", ""));
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() ->
FormField.named("lol")
.emptyToNull()
.required()
.asList()
.build()
.convert(ImmutableList.of("omg", "")));
assertThat(
thrown,
equalTo(new FormFieldException("This field is required.").propagate(1).propagate("lol")));
}
@Test
public void testMapAsListRequiredElements_nullElement_throwsFfeWithIndexAndKey() {
thrown.expect(equalTo(new FormFieldException("This field is required.")
.propagate("cat")
.propagate(0)
.propagate("lol")));
FormField.mapNamed("lol")
.transform(
String.class,
input ->
FormField.named("cat").emptyToNull().required().build().extractUntyped(input).get())
.asList()
.build()
.convert(ImmutableList.of(ImmutableMap.of("cat", "")));
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() ->
FormField.mapNamed("lol")
.transform(
String.class,
input ->
FormField.named("cat")
.emptyToNull()
.required()
.build()
.extractUntyped(input)
.get())
.asList()
.build()
.convert(ImmutableList.of(ImmutableMap.of("cat", ""))));
assertThat(
thrown,
equalTo(
new FormFieldException("This field is required.")
.propagate("cat")
.propagate(0)
.propagate("lol")));
}
@Test
public void testAsListTrimmed_typeMismatch() {
FormField.named("lol").trimmed().asList();
thrown.expect(IllegalStateException.class);
FormField.named("lol").asList().trimmed();
assertThrows(IllegalStateException.class, () -> FormField.named("lol").asList().trimmed());
}
@Test
@ -423,8 +459,7 @@ public class FormFieldTest {
@Test
public void testTrimmed_typeMismatch() {
thrown.expect(IllegalStateException.class);
FormField.named("lol", Object.class).trimmed();
assertThrows(IllegalStateException.class, () -> FormField.named("lol", Object.class).trimmed());
}
@Test

View file

@ -14,13 +14,14 @@
package google.registry.ui.forms;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import com.google.common.testing.NullPointerTester;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -28,9 +29,6 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class FormFieldsTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testXsToken_collapsesAndTrimsWhitespace() {
assertThat(FormFields.XS_TOKEN.convert(" hello \r\n\t there\n")).hasValue("hello there");
@ -48,10 +46,15 @@ public class FormFieldsTest {
@Test
public void testXsNormalizedString_containsNonSpaceWhitespace_fails() {
thrown.expect(equalTo(
new FormFieldException("Must not contain tabs or multiple lines.")
.propagate("xsNormalizedString")));
FormFields.XS_NORMALIZED_STRING.convert(" hello \r\n\t there\n");
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() -> FormFields.XS_NORMALIZED_STRING.convert(" hello \r\n\t there\n"));
assertThat(
thrown,
equalTo(
new FormFieldException("Must not contain tabs or multiple lines.")
.propagate("xsNormalizedString")));
}
@Test
@ -67,9 +70,12 @@ public class FormFieldsTest {
@Test
public void testXsEppE164PhoneNumber_localizedNumber_fails() {
thrown.expect(FormFieldException.class);
thrown.expectMessage("Must be a valid +E.164 phone number, e.g. +1.2125650000");
FormFields.PHONE_NUMBER.convert("(212) 565-0000");
FormFieldException thrown =
expectThrows(
FormFieldException.class, () -> FormFields.PHONE_NUMBER.convert("(212) 565-0000"));
assertThat(thrown)
.hasMessageThat()
.contains("Must be a valid +E.164 phone number, e.g. +1.2125650000");
}
@Test
@ -89,9 +95,9 @@ public class FormFieldsTest {
@Test
public void testXsEppRoid_missingHyphen_fails() {
thrown.expect(FormFieldException.class);
thrown.expectMessage("Please enter a valid EPP ROID, e.g. SH8013-REP");
FormFields.ROID.convert("SH8013REP");
FormFieldException thrown =
expectThrows(FormFieldException.class, () -> FormFields.ROID.convert("SH8013REP"));
assertThat(thrown).hasMessageThat().contains("Please enter a valid EPP ROID, e.g. SH8013-REP");
}
@Test

View file

@ -15,23 +15,19 @@
package google.registry.ui.server;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import google.registry.testing.CertificateSamples;
import google.registry.ui.forms.FormFieldException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link RegistrarFormFields}. */
@RunWith(JUnit4.class)
public class RegistrarFormFieldsTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testValidCertificate_doesntThrowError() {
assertThat(RegistrarFormFields.CLIENT_CERTIFICATE_FIELD.convert(CertificateSamples.SAMPLE_CERT))
@ -40,10 +36,15 @@ public class RegistrarFormFieldsTest {
@Test
public void testBadCertificate_throwsFfe() {
thrown.expect(equalTo(
new FormFieldException("Invalid X.509 PEM certificate")
.propagate("clientCertificate")));
RegistrarFormFields.CLIENT_CERTIFICATE_FIELD.convert("palfun");
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() -> RegistrarFormFields.CLIENT_CERTIFICATE_FIELD.convert("palfun"));
assertThat(
thrown,
equalTo(
new FormFieldException("Invalid X.509 PEM certificate")
.propagate("clientCertificate")));
}
@Test
@ -56,9 +57,14 @@ public class RegistrarFormFieldsTest {
@Test
public void testBadCertificateHash_throwsFfe() {
thrown.expect(equalTo(
new FormFieldException("Field must contain a base64 value.")
.propagate("clientCertificateHash")));
RegistrarFormFields.CLIENT_CERTIFICATE_HASH_FIELD.convert("~~~");
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() -> RegistrarFormFields.CLIENT_CERTIFICATE_HASH_FIELD.convert("~~~"));
assertThat(
thrown,
equalTo(
new FormFieldException("Field must contain a base64 value.")
.propagate("clientCertificateHash")));
}
}

View file

@ -16,11 +16,11 @@ package google.registry.ui.server.registrar;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.loadRegistrar;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.TaskQueueHelper.assertNoTasksEnqueued;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static google.registry.util.ResourceUtils.readResourceUtf8;
import static java.util.Arrays.asList;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.never;
@ -73,12 +73,8 @@ public class RegistrarSettingsActionTest extends RegistrarSettingsActionTestCase
when(sessionUtils.getRegistrarForAuthResult(
any(HttpServletRequest.class), any(AuthResult.class)))
.thenThrow(new ForbiddenException("Not authorized to access Registrar Console"));
try {
action.handleJsonRequest(ImmutableMap.of());
fail("expected ForbiddenException");
} catch (ForbiddenException ex) {
assertNoTasksEnqueued("sheet");
}
assertThrows(ForbiddenException.class, () -> action.handleJsonRequest(ImmutableMap.of()));
assertNoTasksEnqueued("sheet");
}
/**