From ccbcefdf48bb83b30118d8c5b2358d4588cf07d6 Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 21 Mar 2022 05:02:46 -0700 Subject: [PATCH] Automatic code cleanup. PiperOrigin-RevId: 436175027 --- .../build/docgen/RuleLinkExpanderTest.java | 17 +++-- .../build/lib/actions/ResourceSetTest.java | 17 ++--- .../lib/remote/http/HttpCacheClientTest.java | 38 ++++++----- .../build/lib/util/LongArrayListTest.java | 13 ++-- .../coverageoutputgenerator/CoverageTest.java | 23 ++++--- .../LcovMergerFlagsTest.java | 66 ++++++++++++------- 6 files changed, 106 insertions(+), 68 deletions(-) diff --git a/src/test/java/com/google/devtools/build/docgen/RuleLinkExpanderTest.java b/src/test/java/com/google/devtools/build/docgen/RuleLinkExpanderTest.java index 853302d9540094..09922bcb0a75cc 100644 --- a/src/test/java/com/google/devtools/build/docgen/RuleLinkExpanderTest.java +++ b/src/test/java/com/google/devtools/build/docgen/RuleLinkExpanderTest.java @@ -14,6 +14,7 @@ package com.google.devtools.build.docgen; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableMap; import java.util.Map; @@ -120,16 +121,24 @@ private void checkExpandMulti(String docs, String expected) { "Common Definitions"); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRefNotFound() { String docs = "bar"; - multiPageExpander.expand(docs); + assertThrows( + IllegalArgumentException.class, + () -> { + multiPageExpander.expand(docs); + }); } - @Test(expected = IllegalArgumentException.class) + @Test public void testIncorrectStaticPageHeadingLink() { String docs = "Label Expansion"; - multiPageExpander.expand(docs); + assertThrows( + IllegalArgumentException.class, + () -> { + multiPageExpander.expand(docs); + }); } @Test public void testRuleHeadingLink() { diff --git a/src/test/java/com/google/devtools/build/lib/actions/ResourceSetTest.java b/src/test/java/com/google/devtools/build/lib/actions/ResourceSetTest.java index 757a7188967288..7b1987c410dc15 100644 --- a/src/test/java/com/google/devtools/build/lib/actions/ResourceSetTest.java +++ b/src/test/java/com/google/devtools/build/lib/actions/ResourceSetTest.java @@ -14,7 +14,7 @@ package com.google.devtools.build.lib.actions; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.devtools.build.lib.actions.ResourceSet.ResourceSetConverter; import com.google.devtools.common.options.OptionsParsingException; @@ -44,21 +44,18 @@ public void testConverterParsesExpectedFormat() throws Exception { assertThat(resources.getLocalTestCount()).isEqualTo(Integer.MAX_VALUE); } - @Test(expected = OptionsParsingException.class) + @Test public void testConverterThrowsWhenGivenInsufficientInputs() throws Exception { - converter.convert("0,0,"); - fail(); + assertThrows(OptionsParsingException.class, () -> converter.convert("0,0,")); } - @Test(expected = OptionsParsingException.class) + @Test public void testConverterThrowsWhenGivenTooManyInputs() throws Exception { - converter.convert("0,0,0,"); - fail(); + assertThrows(OptionsParsingException.class, () -> converter.convert("0,0,0,")); } - @Test(expected = OptionsParsingException.class) + @Test public void testConverterThrowsWhenGivenNegativeInputs() throws Exception { - converter.convert("-1,0,0"); - fail(); + assertThrows(OptionsParsingException.class, () -> converter.convert("-1,0,0")); } } diff --git a/src/test/java/com/google/devtools/build/lib/remote/http/HttpCacheClientTest.java b/src/test/java/com/google/devtools/build/lib/remote/http/HttpCacheClientTest.java index d80996e00c26e6..41cddce1827849 100644 --- a/src/test/java/com/google/devtools/build/lib/remote/http/HttpCacheClientTest.java +++ b/src/test/java/com/google/devtools/build/lib/remote/http/HttpCacheClientTest.java @@ -338,7 +338,7 @@ public void testUpload() throws Exception { } } - @Test(expected = ConnectException.class, timeout = 30000) + @Test(timeout = 30000) public void connectTimeout() throws Exception { ServerChannel server = testServer.start(new ChannelInboundHandlerAdapter() {}); testServer.stop(server); @@ -347,13 +347,15 @@ public void connectTimeout() throws Exception { AuthAndTLSOptions authAndTlsOptions = Options.getDefaults(AuthAndTLSOptions.class); HttpCacheClient blobStore = createHttpBlobStore(server, /* timeoutSeconds= */ 1, credentials, authAndTlsOptions); - getFromFuture( - blobStore.downloadBlob(remoteActionExecutionContext, DIGEST, new ByteArrayOutputStream())); - - fail("Exception expected"); + assertThrows( + ConnectException.class, + () -> + getFromFuture( + blobStore.downloadBlob( + remoteActionExecutionContext, DIGEST, new ByteArrayOutputStream()))); } - @Test(expected = UploadTimeoutException.class, timeout = 30000) + @Test(timeout = 30000) public void uploadTimeout() throws Exception { ServerChannel server = null; try { @@ -372,16 +374,20 @@ protected void channelRead0( HttpCacheClient blobStore = createHttpBlobStore(server, /* timeoutSeconds= */ 1, credentials, authAndTlsOptions); byte[] data = "File Contents".getBytes(Charsets.US_ASCII); - getFromFuture( - blobStore.uploadBlob( - remoteActionExecutionContext, DIGEST_UTIL.compute(data), ByteString.copyFrom(data))); - fail("Exception expected"); + assertThrows( + UploadTimeoutException.class, + () -> + getFromFuture( + blobStore.uploadBlob( + remoteActionExecutionContext, + DIGEST_UTIL.compute(data), + ByteString.copyFrom(data)))); } finally { testServer.stop(server); } } - @Test(expected = DownloadTimeoutException.class, timeout = 30000) + @Test(timeout = 30000) public void downloadTimeout() throws Exception { ServerChannel server = null; try { @@ -399,10 +405,12 @@ protected void channelRead0( AuthAndTLSOptions authAndTlsOptions = Options.getDefaults(AuthAndTLSOptions.class); HttpCacheClient blobStore = createHttpBlobStore(server, /* timeoutSeconds= */ 1, credentials, authAndTlsOptions); - getFromFuture( - blobStore.downloadBlob( - remoteActionExecutionContext, DIGEST, new ByteArrayOutputStream())); - fail("Exception expected"); + assertThrows( + DownloadTimeoutException.class, + () -> + getFromFuture( + blobStore.downloadBlob( + remoteActionExecutionContext, DIGEST, new ByteArrayOutputStream()))); } finally { testServer.stop(server); } diff --git a/src/test/java/com/google/devtools/build/lib/util/LongArrayListTest.java b/src/test/java/com/google/devtools/build/lib/util/LongArrayListTest.java index 14befff84d8fd8..39b3b07d643fda 100644 --- a/src/test/java/com/google/devtools/build/lib/util/LongArrayListTest.java +++ b/src/test/java/com/google/devtools/build/lib/util/LongArrayListTest.java @@ -14,6 +14,7 @@ package com.google.devtools.build.lib.util; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import org.junit.Before; import org.junit.Test; @@ -147,24 +148,24 @@ public void testEnsureCapacity() throws Exception { assertThat(list.get(last)).isEqualTo(last); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testRemoveExceptionEmpty() throws Exception { - list.remove(0); + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0)); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testRemoveExceptionFilled() throws Exception { for (int i = 0; i < 15; i++) { list.add(i); } - list.remove(15); + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(15)); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testGetException() throws Exception { for (int i = 0; i < 15; i++) { list.add(i); } - list.get(15); + assertThrows(IndexOutOfBoundsException.class, () -> list.get(15)); } } diff --git a/tools/test/CoverageOutputGenerator/javatests/com/google/devtools/coverageoutputgenerator/CoverageTest.java b/tools/test/CoverageOutputGenerator/javatests/com/google/devtools/coverageoutputgenerator/CoverageTest.java index 188c2f3a2d128b..4e0f91dcfc4d28 100644 --- a/tools/test/CoverageOutputGenerator/javatests/com/google/devtools/coverageoutputgenerator/CoverageTest.java +++ b/tools/test/CoverageOutputGenerator/javatests/com/google/devtools/coverageoutputgenerator/CoverageTest.java @@ -21,6 +21,7 @@ import static com.google.devtools.coverageoutputgenerator.LcovMergerTestUtils.createLinesExecution2; import static com.google.devtools.coverageoutputgenerator.LcovMergerTestUtils.createSourceFile1; import static com.google.devtools.coverageoutputgenerator.LcovMergerTestUtils.createSourceFile2; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -158,14 +159,18 @@ public void testFilterSourcesNoFilter() throws Exception { assertThat(filteredSources).containsExactly(validSource1, validSource2); } - @Test(expected = IllegalArgumentException.class) + @Test public void testFilterSourcesNullCoverage() { - Coverage.filterOutMatchingSources(null, ImmutableList.of()); + assertThrows( + IllegalArgumentException.class, + () -> Coverage.filterOutMatchingSources(null, ImmutableList.of())); } - @Test(expected = IllegalArgumentException.class) + @Test public void testFilterSourcesNullRegex() { - Coverage.filterOutMatchingSources(new Coverage(), null); + assertThrows( + IllegalArgumentException.class, + () -> Coverage.filterOutMatchingSources(new Coverage(), null)); } private List getSourceFileNames( @@ -195,14 +200,16 @@ public void testGetOnlyTheseSources() throws Exception { .containsExactly("source/common/protobuf/utility.cc", "source/common/grpc/common.cc"); } - @Test(expected = IllegalArgumentException.class) + @Test public void testGetOnlyTheseSourcesNullCoverage() { - Coverage.getOnlyTheseSources(null, new HashSet<>()); + assertThrows( + IllegalArgumentException.class, () -> Coverage.getOnlyTheseSources(null, new HashSet<>())); } - @Test(expected = IllegalArgumentException.class) + @Test public void testGetOnlyTheseSourcesNullSources() { - Coverage.getOnlyTheseSources(new Coverage(), null); + assertThrows( + IllegalArgumentException.class, () -> Coverage.getOnlyTheseSources(new Coverage(), null)); } @Test diff --git a/tools/test/CoverageOutputGenerator/javatests/com/google/devtools/coverageoutputgenerator/LcovMergerFlagsTest.java b/tools/test/CoverageOutputGenerator/javatests/com/google/devtools/coverageoutputgenerator/LcovMergerFlagsTest.java index 1599e855c91e5b..dda8864a3f19bd 100644 --- a/tools/test/CoverageOutputGenerator/javatests/com/google/devtools/coverageoutputgenerator/LcovMergerFlagsTest.java +++ b/tools/test/CoverageOutputGenerator/javatests/com/google/devtools/coverageoutputgenerator/LcovMergerFlagsTest.java @@ -15,6 +15,7 @@ package com.google.devtools.coverageoutputgenerator; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import org.junit.Test; import org.junit.runner.RunWith; @@ -79,46 +80,61 @@ public void parseFlagsTestReportsFileOutputFileMultipleFilterSources() { assertThat(flags.filterSources()).containsExactly("first_filter", "second_filter"); } - @Test(expected = IllegalArgumentException.class) + @Test public void parseFlagsTestCoverageDirAndReportsFile() { - LcovMergerFlags.parseFlags( - new String[] {"--reports_file=my_reports_file", "--coverage_dir=my_coverage_dir"}); + assertThrows( + IllegalArgumentException.class, + () -> + LcovMergerFlags.parseFlags( + new String[] {"--reports_file=my_reports_file", "--coverage_dir=my_coverage_dir"})); } - @Test(expected = IllegalArgumentException.class) + @Test public void parseFlagsTestEmptyFlags() { - LcovMergerFlags.parseFlags(new String[] {}); + assertThrows(IllegalArgumentException.class, () -> LcovMergerFlags.parseFlags(new String[] {})); } - @Test(expected = IllegalArgumentException.class) + @Test public void parseFlagsTestNoOutputFile() { - LcovMergerFlags.parseFlags( - new String[] { - "--reports_file=my_reports_file", - }); + assertThrows( + IllegalArgumentException.class, + () -> + LcovMergerFlags.parseFlags( + new String[] { + "--reports_file=my_reports_file", + })); } - @Test(expected = IllegalArgumentException.class) + @Test public void parseFlagsTestUnknownFlag() { - LcovMergerFlags.parseFlags( - new String[] { - "--fake_flag=my_reports_file", - }); + assertThrows( + IllegalArgumentException.class, + () -> + LcovMergerFlags.parseFlags( + new String[] { + "--fake_flag=my_reports_file", + })); } - @Test(expected = IllegalArgumentException.class) + @Test public void parseFlagsTestInvalidFlagValue() { - LcovMergerFlags.parseFlags( - new String[] { - "--reports_file", "--output_file=my_file", - }); + assertThrows( + IllegalArgumentException.class, + () -> + LcovMergerFlags.parseFlags( + new String[] { + "--reports_file", "--output_file=my_file", + })); } - @Test(expected = IllegalArgumentException.class) + @Test public void parseFlagsTestInvalidFlagValueWithoutDashes() { - LcovMergerFlags.parseFlags( - new String[] { - "reports_file", "--output_file=my_file", - }); + assertThrows( + IllegalArgumentException.class, + () -> + LcovMergerFlags.parseFlags( + new String[] { + "reports_file", "--output_file=my_file", + })); } }