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

@ -15,6 +15,7 @@
package google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.util.CollectionUtils.nullToEmpty;
import static google.registry.util.CollectionUtils.partitionMap;
@ -22,9 +23,7 @@ import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -32,9 +31,6 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class CollectionUtilsTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testNullToEmptyMap_leavesNonNullAlone() {
Map<String, Integer> map = ImmutableMap.of("hello", 1);
@ -65,14 +61,12 @@ public class CollectionUtilsTest {
@Test
public void testPartitionMap_negativePartitionSize() {
thrown.expect(IllegalArgumentException.class);
partitionMap(ImmutableMap.of("A", "b"), -2);
assertThrows(IllegalArgumentException.class, () -> partitionMap(ImmutableMap.of("A", "b"), -2));
}
@Test
public void testPartitionMap_nullMap() {
thrown.expect(NullPointerException.class);
partitionMap(null, 100);
assertThrows(NullPointerException.class, () -> partitionMap(null, 100));
}
@Test

View file

@ -15,6 +15,7 @@
package google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static google.registry.util.DateTimeUtils.earliestOf;
@ -25,9 +26,7 @@ import static google.registry.util.DateTimeUtils.leapSafeAddYears;
import com.google.common.collect.ImmutableList;
import org.joda.time.DateTime;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -38,9 +37,6 @@ public class DateTimeUtilsTest {
ImmutableList<DateTime> sampleDates = ImmutableList.of(
START_OF_TIME, START_OF_TIME.plusDays(1), END_OF_TIME, END_OF_TIME);
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testSuccess_earliestOf() {
assertThat(earliestOf(START_OF_TIME, END_OF_TIME)).isEqualTo(START_OF_TIME);
@ -76,13 +72,11 @@ public class DateTimeUtilsTest {
@Test
public void testFailure_earliestOfEmpty() {
thrown.expect(IllegalArgumentException.class);
earliestOf(ImmutableList.of());
assertThrows(IllegalArgumentException.class, () -> earliestOf(ImmutableList.of()));
}
@Test
public void testFailure_latestOfEmpty() {
thrown.expect(IllegalArgumentException.class);
earliestOf(ImmutableList.of());
assertThrows(IllegalArgumentException.class, () -> earliestOf(ImmutableList.of()));
}
}

View file

@ -15,21 +15,16 @@
package google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
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 DomainNameUtils}. */
@RunWith(JUnit4.class)
public class DomainNameUtilsTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testCanonicalizeDomainName() throws Exception {
assertThat(canonicalizeDomainName("foo")).isEqualTo("foo");
@ -46,7 +41,6 @@ public class DomainNameUtilsTest {
@Test
public void testCanonicalizeDomainName_acePrefixUnicodeChars() throws Exception {
thrown.expect(IllegalArgumentException.class);
canonicalizeDomainName("xn--みんな");
assertThrows(IllegalArgumentException.class, () -> canonicalizeDomainName("xn--みんな"));
}
}

View file

@ -15,12 +15,11 @@
package google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.StringWriter;
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 +27,6 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class HexDumperTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testEmpty() throws Exception {
String input = "";
@ -169,37 +165,32 @@ public class HexDumperTest {
@Test
public void testBadArgumentPerLineZero() throws Exception {
HexDumper.dumpHex(new byte[1], 1, 0);
thrown.expect(IllegalArgumentException.class);
HexDumper.dumpHex(new byte[1], 0, 0);
assertThrows(IllegalArgumentException.class, () -> HexDumper.dumpHex(new byte[1], 0, 0));
}
@Test
public void testBadArgumentPerLineNegative() throws Exception {
HexDumper.dumpHex(new byte[1], 1, 0);
thrown.expect(IllegalArgumentException.class);
HexDumper.dumpHex(new byte[1], -1, 0);
assertThrows(IllegalArgumentException.class, () -> HexDumper.dumpHex(new byte[1], -1, 0));
}
@Test
public void testBadArgumentPerGroupNegative() throws Exception {
HexDumper.dumpHex(new byte[1], 1, 0);
thrown.expect(IllegalArgumentException.class);
HexDumper.dumpHex(new byte[1], 1, -1);
assertThrows(IllegalArgumentException.class, () -> HexDumper.dumpHex(new byte[1], 1, -1));
}
@Test
public void testBadArgumentPerGroupGreaterThanOrEqualToPerLine() throws Exception {
HexDumper.dumpHex(new byte[1], 1, 0);
HexDumper.dumpHex(new byte[1], 2, 1);
thrown.expect(IllegalArgumentException.class);
HexDumper.dumpHex(new byte[1], 1, 1);
assertThrows(IllegalArgumentException.class, () -> HexDumper.dumpHex(new byte[1], 1, 1));
}
@Test
public void testBadArgumentBytesIsNull() throws Exception {
HexDumper.dumpHex(new byte[1]);
thrown.expect(NullPointerException.class);
HexDumper.dumpHex(null);
assertThrows(NullPointerException.class, () -> HexDumper.dumpHex(null));
}
@Test

View file

@ -17,6 +17,7 @@ package google.registry.util;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.testing.JUnitBackports.expectThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.testing.EqualsTester;
@ -27,19 +28,13 @@ import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
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 PosixTarHeader}. */
@RunWith(JUnit4.class)
public class PosixTarHeaderTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testGnuTarBlob() throws Exception {
// This data was generated as follows:
@ -206,9 +201,9 @@ public class PosixTarHeaderTest {
byte[] bytes = header.getBytes();
bytes[150] = '0';
bytes[151] = '0';
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("chksum invalid");
PosixTarHeader.from(bytes);
IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> PosixTarHeader.from(bytes));
assertThat(thrown).hasMessageThat().contains("chksum invalid");
}
@Test

View file

@ -15,14 +15,13 @@
package google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeSleeper;
import google.registry.util.Retrier.FailureReporter;
import java.util.concurrent.Callable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -30,9 +29,6 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class RetrierTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
Retrier retrier = new Retrier(new FakeSleeper(new FakeClock()), 3);
/** An exception to throw from {@link CountingThrower}. */
@ -86,16 +82,20 @@ public class RetrierTest {
@Test
public void testRetryableException() throws Exception {
thrown.expect(CountingException.class);
thrown.expectMessage("3");
retrier.callWithRetry(new CountingThrower(3), CountingException.class);
CountingException thrown =
expectThrows(
CountingException.class,
() -> retrier.callWithRetry(new CountingThrower(3), CountingException.class));
assertThat(thrown).hasMessageThat().contains("3");
}
@Test
public void testUnretryableException() throws Exception {
thrown.expect(CountingException.class);
thrown.expectMessage("1");
retrier.callWithRetry(new CountingThrower(5), IllegalArgumentException.class);
CountingException thrown =
expectThrows(
CountingException.class,
() -> retrier.callWithRetry(new CountingThrower(5), IllegalArgumentException.class));
assertThat(thrown).hasMessageThat().contains("1");
}
@Test
@ -106,15 +106,19 @@ public class RetrierTest {
@Test
public void testRetryFailed_withReporter() throws Exception {
thrown.expect(CountingException.class);
thrown.expectMessage("3");
TestReporter reporter = new TestReporter();
try {
retrier.callWithRetry(new CountingThrower(3), reporter, CountingException.class);
} catch (CountingException expected) {
reporter.assertNumbers(2, 1);
throw expected;
}
CountingException thrown =
expectThrows(
CountingException.class,
() -> {
TestReporter reporter = new TestReporter();
try {
retrier.callWithRetry(new CountingThrower(3), reporter, CountingException.class);
} catch (CountingException expected) {
reporter.assertNumbers(2, 1);
throw expected;
}
});
assertThat(thrown).hasMessageThat().contains("3");
}
@Test

View file

@ -15,12 +15,11 @@
package google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.util.SerializeUtils.deserialize;
import static google.registry.util.SerializeUtils.serialize;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -34,10 +33,6 @@ public class SerializeUtilsTest {
return "LOL_VALUE";
}
}
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testSerialize_nullValue_returnsNull() throws Exception {
assertThat(serialize(null)).isNull();
@ -55,15 +50,17 @@ public class SerializeUtilsTest {
@Test
public void testSerialize_objectDoesntImplementSerialize_hasInformativeError() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Unable to serialize: LOL_VALUE");
serialize(new Lol());
IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> serialize(new Lol()));
assertThat(thrown).hasMessageThat().contains("Unable to serialize: LOL_VALUE");
}
@Test
public void testDeserialize_badValue_hasInformativeError() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Unable to deserialize: objectBytes=FF");
deserialize(String.class, new byte[] { (byte) 0xff });
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> deserialize(String.class, new byte[] {(byte) 0xff}));
assertThat(thrown).hasMessageThat().contains("Unable to deserialize: objectBytes=FF");
}
}

View file

@ -15,10 +15,9 @@
package google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -26,9 +25,6 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class SqlTemplateTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testFillSqlTemplate() throws Exception {
assertThat(
@ -52,98 +48,99 @@ public class SqlTemplateTest {
@Test
public void testFillSqlTemplate_substitutionButNoVariables() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Not found in template: ONE");
SqlTemplate.create("")
.put("ONE", "1")
.build();
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class, () -> SqlTemplate.create("").put("ONE", "1").build());
assertThat(thrown).hasMessageThat().contains("Not found in template: ONE");
}
@Test
public void testFillSqlTemplate_substitutionButMissingVariables() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Not found in template: TWO");
SqlTemplate.create("%ONE%")
.put("ONE", "1")
.put("TWO", "2")
.build();
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> SqlTemplate.create("%ONE%").put("ONE", "1").put("TWO", "2").build());
assertThat(thrown).hasMessageThat().contains("Not found in template: TWO");
}
@Test
public void testFillSqlTemplate_sameKeyTwice_failsEarly() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("");
SqlTemplate.create("%ONE%")
.put("ONE", "1")
.put("ONE", "2");
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> SqlTemplate.create("%ONE%").put("ONE", "1").put("ONE", "2"));
assertThat(thrown).hasMessageThat().contains("");
}
@Test
public void testFillSqlTemplate_variablesButNotEnoughSubstitutions() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("%TWO% found in template but no substitution");
SqlTemplate.create("%ONE% %TWO%")
.put("ONE", "1")
.build();
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> SqlTemplate.create("%ONE% %TWO%").put("ONE", "1").build());
assertThat(thrown).hasMessageThat().contains("%TWO% found in template but no substitution");
}
@Test
public void testFillSqlTemplate_mismatchedVariableAndSubstitution() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("%TWO% found in template but no substitution");
SqlTemplate.create("%TWO%")
.put("TOO", "2")
.build();
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> SqlTemplate.create("%TWO%").put("TOO", "2").build());
assertThat(thrown).hasMessageThat().contains("%TWO% found in template but no substitution");
}
@Test
public void testFillSqlTemplate_missingKeyVals_whatsThePoint() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("%TWO% found in template but no substitution");
SqlTemplate.create("%TWO%")
.build();
IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> SqlTemplate.create("%TWO%").build());
assertThat(thrown).hasMessageThat().contains("%TWO% found in template but no substitution");
}
@Test
public void testFillSqlTemplate_lowercaseKey_notAllowed() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Bad substitution key: test");
SqlTemplate.create("%test%")
.put("test", "hello world")
.build();
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> SqlTemplate.create("%test%").put("test", "hello world").build());
assertThat(thrown).hasMessageThat().contains("Bad substitution key: test");
}
@Test
public void testFillSqlTemplate_substitution_disallowsSingleQuotes() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Illegal characters in foo'bar");
SqlTemplate.create("The words are '%LOS%' and baz")
.put("LOS", "foo'bar");
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> SqlTemplate.create("The words are '%LOS%' and baz").put("LOS", "foo'bar"));
assertThat(thrown).hasMessageThat().contains("Illegal characters in foo'bar");
}
@Test
public void testFillSqlTemplate_substitution_disallowsDoubleQuotes() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Illegal characters in foo\"bar");
SqlTemplate.create("The words are '%LOS%' and baz")
.put("LOS", "foo\"bar");
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> SqlTemplate.create("The words are '%LOS%' and baz").put("LOS", "foo\"bar"));
assertThat(thrown).hasMessageThat().contains("Illegal characters in foo\"bar");
}
@Test
public void testFillSqlTemplate_quoteMismatch_throwsError() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Quote mismatch: \"%LOS%'");
SqlTemplate.create("The words are \"%LOS%' and baz")
.put("LOS", "foobar")
.build();
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
SqlTemplate.create("The words are \"%LOS%' and baz").put("LOS", "foobar").build());
assertThat(thrown).hasMessageThat().contains("Quote mismatch: \"%LOS%'");
}
@Test
public void testFillSqlTemplate_extendedQuote_throwsError() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Quote mismatch: '%LOS%");
SqlTemplate.create("The words are '%LOS%-lol' and baz")
.put("LOS", "roid")
.build();
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
SqlTemplate.create("The words are '%LOS%-lol' and baz").put("LOS", "roid").build());
assertThat(thrown).hasMessageThat().contains("Quote mismatch: '%LOS%");
}
}

View file

@ -16,6 +16,8 @@ package google.registry.util;
import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;
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.times;
import static org.mockito.Mockito.verify;
@ -32,7 +34,6 @@ import google.registry.testing.FakeSleeper;
import org.joda.time.DateTime;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -41,10 +42,6 @@ import org.junit.runners.JUnit4;
public final class TaskEnqueuerTest {
private static final int MAX_RETRIES = 3;
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder()
.withDatastore()
@ -95,9 +92,9 @@ public final class TaskEnqueuerTest {
.thenThrow(new TransientFailureException("two"))
.thenThrow(new TransientFailureException("three"))
.thenThrow(new TransientFailureException("four"));
thrown.expect(TransientFailureException.class);
thrown.expectMessage("three");
taskEnqueuer.enqueue(queue, task);
TransientFailureException thrown =
expectThrows(TransientFailureException.class, () -> taskEnqueuer.enqueue(queue, task));
assertThat(thrown).hasMessageThat().contains("three");
}
@Test
@ -105,8 +102,7 @@ public final class TaskEnqueuerTest {
when(queue.add(ImmutableList.of(task))).thenThrow(new TransientFailureException(""));
try {
Thread.currentThread().interrupt();
thrown.expect(TransientFailureException.class);
taskEnqueuer.enqueue(queue, task);
assertThrows(TransientFailureException.class, () -> taskEnqueuer.enqueue(queue, task));
} finally {
Thread.interrupted(); // Clear interrupt state so it doesn't pwn other tests.
}

View file

@ -15,25 +15,21 @@
package google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import com.google.common.collect.ImmutableSet;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
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 TeeOutputStream}. */
@RunWith(JUnit4.class)
public class TeeOutputStreamTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private final ByteArrayOutputStream outputA = new ByteArrayOutputStream();
private final ByteArrayOutputStream outputB = new ByteArrayOutputStream();
private final ByteArrayOutputStream outputC = new ByteArrayOutputStream();
@ -60,34 +56,32 @@ public class TeeOutputStreamTest {
@Test
@SuppressWarnings("resource")
public void testConstructor_failsWithEmptyIterable() {
thrown.expect(IllegalArgumentException.class);
new TeeOutputStream(ImmutableSet.of());
assertThrows(IllegalArgumentException.class, () -> new TeeOutputStream(ImmutableSet.of()));
}
@Test
public void testWriteInteger_failsAfterClose() throws Exception {
OutputStream tee = new TeeOutputStream(asList(outputA));
tee.close();
thrown.expect(IllegalStateException.class);
thrown.expectMessage("outputstream closed");
tee.write(1);
IllegalStateException thrown = expectThrows(IllegalStateException.class, () -> tee.write(1));
assertThat(thrown).hasMessageThat().contains("outputstream closed");
}
@Test
public void testWriteByteArray_failsAfterClose() throws Exception {
OutputStream tee = new TeeOutputStream(asList(outputA));
tee.close();
thrown.expect(IllegalStateException.class);
thrown.expectMessage("outputstream closed");
tee.write("hello".getBytes(UTF_8));
IllegalStateException thrown =
expectThrows(IllegalStateException.class, () -> tee.write("hello".getBytes(UTF_8)));
assertThat(thrown).hasMessageThat().contains("outputstream closed");
}
@Test
public void testWriteByteSubarray_failsAfterClose() throws Exception {
OutputStream tee = new TeeOutputStream(asList(outputA));
tee.close();
thrown.expect(IllegalStateException.class);
thrown.expectMessage("outputstream closed");
tee.write("hello".getBytes(UTF_8), 1, 3);
IllegalStateException thrown =
expectThrows(IllegalStateException.class, () -> tee.write("hello".getBytes(UTF_8), 1, 3));
assertThat(thrown).hasMessageThat().contains("outputstream closed");
}
}

View file

@ -15,22 +15,17 @@
package google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import java.io.Serializable;
import java.util.ArrayList;
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 TypeUtils}. */
@RunWith(JUnit4.class)
public class TypeUtilsTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void test_getClassFromString_validClass() {
Class<? extends Serializable> clazz =
@ -40,15 +35,21 @@ public class TypeUtilsTest {
@Test
public void test_getClassFromString_notAssignableFrom() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("ArrayList does not implement/extend Integer");
TypeUtils.getClassFromString("java.util.ArrayList", Integer.class);
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> TypeUtils.getClassFromString("java.util.ArrayList", Integer.class));
assertThat(thrown).hasMessageThat().contains("ArrayList does not implement/extend Integer");
}
@Test
public void test_getClassFromString_unknownClass() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Failed to load class com.fake.company.nonexistent.Class");
TypeUtils.getClassFromString("com.fake.company.nonexistent.Class", Object.class);
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> TypeUtils.getClassFromString("com.fake.company.nonexistent.Class", Object.class));
assertThat(thrown)
.hasMessageThat()
.contains("Failed to load class com.fake.company.nonexistent.Class");
}
}

View file

@ -18,6 +18,7 @@ import static com.google.common.net.HttpHeaders.CONTENT_LENGTH;
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.google.common.net.MediaType.CSV_UTF_8;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.util.UrlFetchUtils.setPayloadMultipart;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Matchers.any;
@ -37,7 +38,6 @@ import java.util.Random;
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;
@ -49,10 +49,6 @@ public class UrlFetchUtilsTest {
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final InjectRule inject = new InjectRule();
@ -98,9 +94,14 @@ public class UrlFetchUtilsTest {
public void testSetPayloadMultipart_boundaryInPayload() throws Exception {
HTTPRequest request = mock(HTTPRequest.class);
String payload = "I screamed------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHHH";
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Multipart data contains autogenerated boundary: "
+ "------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
setPayloadMultipart(request, "lol", "cat", CSV_UTF_8, payload);
IllegalStateException thrown =
expectThrows(
IllegalStateException.class,
() -> setPayloadMultipart(request, "lol", "cat", CSV_UTF_8, payload));
assertThat(thrown)
.hasMessageThat()
.contains(
"Multipart data contains autogenerated boundary: "
+ "------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
}
}