Automatic cleanup change.
PiperOrigin-RevId: 245990334
diff --git a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/OptionFileExpanderTest.java b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/OptionFileExpanderTest.java
index 8c380d4..beb9f84 100644
--- a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/OptionFileExpanderTest.java
+++ b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/OptionFileExpanderTest.java
@@ -15,8 +15,8 @@
package com.google.devtools.build.singlejar;
import static com.google.common.truth.Truth.assertThat;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.junit.Assert.fail;
import com.google.devtools.build.singlejar.OptionFileExpander.OptionFileProvider;
import java.io.ByteArrayInputStream;
@@ -75,11 +75,6 @@
StoredOptionFileProvider provider = new StoredOptionFileProvider();
provider.addFile("options", "'missing apostrophe");
OptionFileExpander expander = new OptionFileExpander(provider);
- try {
- expander.expandArguments(Arrays.asList("@options"));
- fail();
- } catch (IOException e) {
- // Expected exception.
- }
+ assertThrows(IOException.class, () -> expander.expandArguments(Arrays.asList("@options")));
}
}
diff --git a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/SingleJarTest.java b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/SingleJarTest.java
index 3e89489..b62b11f 100644
--- a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/SingleJarTest.java
+++ b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/SingleJarTest.java
@@ -15,8 +15,8 @@
package com.google.devtools.build.singlejar;
import static com.google.common.truth.Truth.assertThat;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.junit.Assert.fail;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
@@ -591,13 +591,19 @@
MockSimpleFileSystem mockFs = new MockSimpleFileSystem("output.jar");
mockFs.addFile("a/b/c", "Test");
SingleJar singleJar = new SingleJar(mockFs);
- try {
- singleJar.run(ImmutableList.of("--output", "output.jar", "--exclude_build_data",
- "--resources", "a/b/c", "a/b/c"));
- fail();
- } catch (IllegalArgumentException e) {
- assertThat(e).hasMessageThat().contains("already contains a file named 'a/b/c'.");
- }
+ IllegalArgumentException e =
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ singleJar.run(
+ ImmutableList.of(
+ "--output",
+ "output.jar",
+ "--exclude_build_data",
+ "--resources",
+ "a/b/c",
+ "a/b/c")));
+ assertThat(e).hasMessageThat().contains("already contains a file named 'a/b/c'.");
}
@Test
diff --git a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/ZipCombinerTest.java b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/ZipCombinerTest.java
index 2ecf40e..21fa068 100644
--- a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/ZipCombinerTest.java
+++ b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/ZipCombinerTest.java
@@ -15,9 +15,9 @@
package com.google.devtools.build.singlejar;
import static com.google.common.truth.Truth.assertThat;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.junit.Assert.fail;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
@@ -493,10 +493,7 @@
};
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ZipCombiner zipCombiner = new ZipCombiner(badFilter, out)) {
- zipCombiner.addZip(sampleZip());
- fail();
- } catch (IllegalStateException e) {
- // Expected exception.
+ assertThrows(IllegalStateException.class, () -> zipCombiner.addZip(sampleZip()));
}
}
@@ -510,10 +507,7 @@
};
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ZipCombiner zipCombiner = new ZipCombiner(badFilter, out)) {
- zipCombiner.addZip(sampleZip());
- fail();
- } catch (IllegalStateException e) {
- // Expected exception.
+ assertThrows(IllegalStateException.class, () -> zipCombiner.addZip(sampleZip()));
}
}
diff --git a/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java b/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
index c5ad4da..7f9741f 100644
--- a/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
+++ b/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
@@ -14,7 +14,7 @@
package com.google.devtools.build.lib.cmdline;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester;
@@ -90,22 +90,13 @@
@Test
public void testLabelResolutionAbsolutePath() throws Exception {
- try {
- parseCommandLine("//absolute:label", "/absolute");
- fail();
- } catch (IllegalArgumentException e) {
- // Expected exception
- }
+ assertThrows(
+ IllegalArgumentException.class, () -> parseCommandLine("//absolute:label", "/absolute"));
}
@Test
public void testLabelResolutionBadSyntax() throws Exception {
- try {
- parseCommandLine("//absolute:A+bad:syntax", "");
- fail();
- } catch (LabelSyntaxException e) {
- // Expected exception
- }
+ assertThrows(LabelSyntaxException.class, () -> parseCommandLine("//absolute:A+bad:syntax", ""));
}
@Test
@@ -127,30 +118,16 @@
@Test
public void testGetRelativeWithIllegalLabel() throws Exception {
Label base = Label.parseAbsolute("//foo/bar:baz", ImmutableMap.of());
- try {
- base.getRelativeWithRemapping("/p1/p2:target", ImmutableMap.of());
- fail();
- } catch (LabelSyntaxException e) {
- /* ok */
- }
- try {
- base.getRelativeWithRemapping("quux:", ImmutableMap.of());
- fail();
- } catch (LabelSyntaxException e) {
- /* ok */
- }
- try {
- base.getRelativeWithRemapping(":", ImmutableMap.of());
- fail();
- } catch (LabelSyntaxException e) {
- /* ok */
- }
- try {
- base.getRelativeWithRemapping("::", ImmutableMap.of());
- fail();
- } catch (LabelSyntaxException e) {
- /* ok */
- }
+ assertThrows(
+ LabelSyntaxException.class,
+ () -> base.getRelativeWithRemapping("/p1/p2:target", ImmutableMap.of()));
+ assertThrows(
+ LabelSyntaxException.class,
+ () -> base.getRelativeWithRemapping("quux:", ImmutableMap.of()));
+ assertThrows(
+ LabelSyntaxException.class, () -> base.getRelativeWithRemapping(":", ImmutableMap.of()));
+ assertThrows(
+ LabelSyntaxException.class, () -> base.getRelativeWithRemapping("::", ImmutableMap.of()));
}
@Test
@@ -325,12 +302,12 @@
* @param label the label to create.
*/
private static void assertSyntaxError(String expectedError, String label) {
- try {
- Label.parseAbsolute(label, ImmutableMap.of());
- fail("Label '" + label + "' did not contain a syntax error");
- } catch (LabelSyntaxException e) {
- assertThat(e).hasMessageThat().containsMatch(Pattern.quote(expectedError));
- }
+ LabelSyntaxException e =
+ assertThrows(
+ "Label '" + label + "' did not contain a syntax error, but was expected to",
+ LabelSyntaxException.class,
+ () -> Label.parseAbsolute(label, ImmutableMap.of()));
+ assertThat(e).hasMessageThat().containsMatch(Pattern.quote(expectedError));
}
@Test
@@ -466,26 +443,23 @@
@Test
public void testInvalidRepo() throws Exception {
- try {
- Label.parseAbsolute("foo//bar/baz:bat/boo", ImmutableMap.of());
- fail();
- } catch (LabelSyntaxException e) {
- assertThat(e)
- .hasMessageThat()
- .isEqualTo("invalid repository name 'foo': workspace names must start with '@'");
- }
+ LabelSyntaxException e =
+ assertThrows(
+ LabelSyntaxException.class,
+ () -> Label.parseAbsolute("foo//bar/baz:bat/boo", ImmutableMap.of()));
+ assertThat(e)
+ .hasMessageThat()
+ .isEqualTo("invalid repository name 'foo': workspace names must start with '@'");
}
@Test
public void testInvalidRepoWithColon() throws Exception {
- try {
- Label.parseAbsolute("@foo:xyz", ImmutableMap.of());
- fail();
- } catch (LabelSyntaxException e) {
- assertThat(e)
- .hasMessageThat()
- .containsMatch("invalid repository name '@foo:xyz': workspace names may contain only");
- }
+ LabelSyntaxException e =
+ assertThrows(
+ LabelSyntaxException.class, () -> Label.parseAbsolute("@foo:xyz", ImmutableMap.of()));
+ assertThat(e)
+ .hasMessageThat()
+ .containsMatch("invalid repository name '@foo:xyz': workspace names may contain only");
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/cmdline/RepositoryNameTest.java b/src/test/java/com/google/devtools/build/lib/cmdline/RepositoryNameTest.java
index 280f4f7..a9cc14e 100644
--- a/src/test/java/com/google/devtools/build/lib/cmdline/RepositoryNameTest.java
+++ b/src/test/java/com/google/devtools/build/lib/cmdline/RepositoryNameTest.java
@@ -15,7 +15,7 @@
package com.google.devtools.build.lib.cmdline;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.devtools.build.lib.vfs.PathFragment;
import org.junit.Test;
@@ -29,12 +29,9 @@
public class RepositoryNameTest {
public void assertNotValid(String name, String expectedMessage) {
- try {
- RepositoryName.create(name);
- fail();
- } catch (LabelSyntaxException expected) {
- assertThat(expected).hasMessageThat().contains(expectedMessage);
- }
+ LabelSyntaxException expected =
+ assertThrows(LabelSyntaxException.class, () -> RepositoryName.create(name));
+ assertThat(expected).hasMessageThat().contains(expectedMessage);
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/cmdline/TargetPatternTest.java b/src/test/java/com/google/devtools/build/lib/cmdline/TargetPatternTest.java
index f48257e..0230924 100644
--- a/src/test/java/com/google/devtools/build/lib/cmdline/TargetPatternTest.java
+++ b/src/test/java/com/google/devtools/build/lib/cmdline/TargetPatternTest.java
@@ -15,7 +15,7 @@
package com.google.devtools.build.lib.cmdline;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.devtools.build.lib.cmdline.TargetPattern.ContainsTBDForTBDResult;
import org.junit.Test;
@@ -26,11 +26,7 @@
@RunWith(JUnit4.class)
public class TargetPatternTest {
private void expectError(String pattern) {
- try {
- parse(pattern);
- fail();
- } catch (TargetParsingException expected) {
- }
+ assertThrows(TargetParsingException.class, () -> parse(pattern));
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/concurrent/AbstractQueueVisitorTest.java b/src/test/java/com/google/devtools/build/lib/concurrent/AbstractQueueVisitorTest.java
index b6b5eec..bb84b66 100644
--- a/src/test/java/com/google/devtools/build/lib/concurrent/AbstractQueueVisitorTest.java
+++ b/src/test/java/com/google/devtools/build/lib/concurrent/AbstractQueueVisitorTest.java
@@ -15,7 +15,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@@ -91,11 +91,8 @@
ErrorClassifier.DEFAULT);
counter.dependOnFuture(future);
Thread.currentThread().interrupt();
- try {
- counter.awaitQuiescence(/*interruptWorkers=*/ true);
- fail();
- } catch (InterruptedException expected) {
- }
+ assertThrows(
+ InterruptedException.class, () -> counter.awaitQuiescence(/*interruptWorkers=*/ true));
assertThat(future.isCancelled()).isTrue();
}
@@ -136,14 +133,10 @@
}
});
- try {
- // The exception from the worker thread should be
- // re-thrown from the main thread.
- visitor.awaitQuiescence(/*interruptWorkers=*/ false);
- fail();
- } catch (Exception e) {
- assertThat(e).isSameAs(myException);
- }
+ // The exception from the worker thread should be re-thrown from the main thread.
+ Exception e =
+ assertThrows(Exception.class, () -> visitor.awaitQuiescence(/*interruptWorkers=*/ false));
+ assertThat(e).isSameAs(myException);
}
// Regression test for "AbstractQueueVisitor loses track of jobs if thread allocation fails".
@@ -169,12 +162,9 @@
CountingQueueVisitor counter = new CountingQueueVisitor(executor);
counter.enqueue();
- try {
- counter.awaitQuiescence(/*interruptWorkers=*/ false);
- fail();
- } catch (Error expected) {
- assertThat(expected).hasMessageThat().isEqualTo("Could not create thread (fakeout)");
- }
+ Error expected =
+ assertThrows(Error.class, () -> counter.awaitQuiescence(/*interruptWorkers=*/ false));
+ assertThat(expected).hasMessageThat().isEqualTo("Could not create thread (fakeout)");
assertThat(counter.getCount()).isSameAs(5);
executor.shutdown();
@@ -203,13 +193,10 @@
assertThat(threadStarted.await(TestUtils.WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS)).isTrue();
// Interrupt will not be processed until work starts.
Thread.currentThread().interrupt();
- try {
- visitor.awaitQuiescence(/*interruptWorkers=*/ true);
- fail();
- } catch (Exception e) {
- assertThat(e).isEqualTo(THROWABLE);
- assertThat(Thread.interrupted()).isTrue();
- }
+ Exception e =
+ assertThrows(Exception.class, () -> visitor.awaitQuiescence(/*interruptWorkers=*/ true));
+ assertThat(e).isEqualTo(THROWABLE);
+ assertThat(Thread.interrupted()).isTrue();
}
@Test
@@ -251,12 +238,8 @@
interrupterThread.start();
- try {
- visitor.awaitQuiescence(/*interruptWorkers=*/ false);
- fail();
- } catch (InterruptedException e) {
- // Expected.
- }
+ assertThrows(
+ InterruptedException.class, () -> visitor.awaitQuiescence(/*interruptWorkers=*/ false));
interrupterThread.joinAndAssertState(400);
assertThat(workerThreadCompleted[0]).isTrue();
@@ -297,12 +280,8 @@
latch1.await();
Thread.currentThread().interrupt();
- try {
- visitor.awaitQuiescence(/*interruptWorkers=*/ true);
- fail();
- } catch (InterruptedException e) {
- // Expected.
- }
+ assertThrows(
+ InterruptedException.class, () -> visitor.awaitQuiescence(/*interruptWorkers=*/ true));
assertThat(workerThreadInterrupted[0]).isTrue();
}
@@ -370,15 +349,12 @@
latchB.await();
visitor.execute(interrupt ? interruptingRunnable(Thread.currentThread()) : throwingRunnable());
- try {
- visitor.awaitQuiescence(/*interruptWorkers=*/ false);
- fail();
- } catch (Exception e) {
- if (interrupt) {
+ Exception e =
+ assertThrows(Exception.class, () -> visitor.awaitQuiescence(/*interruptWorkers=*/ false));
+ if (interrupt) {
assertThat(e).isInstanceOf(InterruptedException.class);
} else {
assertThat(e).isSameAs(THROWABLE);
- }
}
assertWithMessage("got: " + visitedList + "\nwant: " + Arrays.toString(expectedVisited))
.that(Sets.newHashSet(visitedList))
@@ -421,12 +397,9 @@
visitor.execute(throwingRunnable());
CountDownLatch exnLatch = visitor.getExceptionLatchForTestingOnly();
- try {
- visitor.awaitQuiescence(/*interruptWorkers=*/ true);
- fail();
- } catch (Exception e) {
- assertThat(e).isSameAs(THROWABLE);
- }
+ Exception e =
+ assertThrows(Exception.class, () -> visitor.awaitQuiescence(/*interruptWorkers=*/ true));
+ assertThat(e).isSameAs(THROWABLE);
assertThat(wasInterrupted.get()).isTrue();
assertThat(executor.isShutdown()).isTrue();
diff --git a/src/test/java/com/google/devtools/build/lib/concurrent/KeyedLockerTest.java b/src/test/java/com/google/devtools/build/lib/concurrent/KeyedLockerTest.java
index 086a989..b80ed80 100644
--- a/src/test/java/com/google/devtools/build/lib/concurrent/KeyedLockerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/concurrent/KeyedLockerTest.java
@@ -14,7 +14,7 @@
package com.google.devtools.build.lib.concurrent;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
@@ -110,11 +110,7 @@
protected void runDoubleUnlockOnSameAutoUnlockerNotAllowed(final Supplier<AutoUnlocker> lockFn) {
AutoUnlocker unlocker = lockFn.get();
unlocker.close();
- try {
- unlocker.close();
- fail();
- } catch (IllegalUnlockException expected) {
- }
+ assertThrows(IllegalUnlockException.class, () -> unlocker.close());
}
@Test
@@ -192,22 +188,21 @@
unlockerRefSetLatch.countDown();
}
};
- Runnable runnable2 = new Runnable() {
- @Override
- public void run() {
- try {
- unlockerRefSetLatch.await(TestUtils.WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
- } catch (InterruptedException e) {
- runnableInterrupted.set(true);
- }
- try {
- Preconditions.checkNotNull(unlockerRef.get()).close();
- fail();
- } catch (IllegalMonitorStateException expected) {
- runnable2Executed.set(true);
- }
- }
- };
+ Runnable runnable2 =
+ new Runnable() {
+ @Override
+ public void run() {
+ try {
+ unlockerRefSetLatch.await(TestUtils.WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ runnableInterrupted.set(true);
+ }
+ assertThrows(
+ IllegalMonitorStateException.class,
+ () -> Preconditions.checkNotNull(unlockerRef.get()).close());
+ runnable2Executed.set(true);
+ }
+ };
@SuppressWarnings("unused")
Future<?> possiblyIgnoredError = executorService.submit(wrapper.wrap(runnable1));
@SuppressWarnings("unused")
diff --git a/src/test/java/com/google/devtools/build/lib/concurrent/MoreFuturesTest.java b/src/test/java/com/google/devtools/build/lib/concurrent/MoreFuturesTest.java
index de3aa2e..d73865a 100644
--- a/src/test/java/com/google/devtools/build/lib/concurrent/MoreFuturesTest.java
+++ b/src/test/java/com/google/devtools/build/lib/concurrent/MoreFuturesTest.java
@@ -14,6 +14,7 @@
package com.google.devtools.build.lib.concurrent;
import static com.google.common.truth.Truth.assertThat;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import static org.junit.Assert.fail;
import com.google.common.util.concurrent.AbstractFuture;
@@ -146,14 +147,12 @@
// And run this thread in the background.
interruptThread.start();
try {
- try {
- // And then wait for all the futures to complete, interruptibly.
- MoreFutures.waitForAllInterruptiblyFailFast(futureList);
- fail();
- } catch (InterruptedException expected) {
- // Then, as expected, waitForAllInterruptiblyFailFast propagates the interrupt sent to the
- // main test thread by our background thread.
- }
+ // And then wait for all the futures to complete, interruptibly.
+ // Then, as expected, waitForAllInterruptiblyFailFast propagates the interrupt sent to the
+ // main test thread by our background thread.
+ assertThrows(
+ InterruptedException.class,
+ () -> MoreFutures.waitForAllInterruptiblyFailFast(futureList));
} finally {
// The @After-annotated shutdownExecutor method blocks on completion of all tasks. Since we
// submitted a bunch of tasks that never complete, we need to explicitly cancel them.
@@ -179,12 +178,11 @@
DelayedFuture toFail = new DelayedFuture(1000);
futureList.add(toFail);
toFail.makeItFail();
- try {
- MoreFutures.waitForAllInterruptiblyFailFast(futureList);
- fail();
- } catch (ExecutionException ee) {
- assertThat(ee).hasCauseThat().hasMessageThat().isEqualTo("I like to fail!!");
- }
+ ExecutionException ee =
+ assertThrows(
+ ExecutionException.class,
+ () -> MoreFutures.waitForAllInterruptiblyFailFast(futureList));
+ assertThat(ee).hasCauseThat().hasMessageThat().isEqualTo("I like to fail!!");
}
/**
diff --git a/src/test/java/com/google/devtools/build/lib/sandbox/AbstractContainerizingSandboxedSpawnTest.java b/src/test/java/com/google/devtools/build/lib/sandbox/AbstractContainerizingSandboxedSpawnTest.java
index b8e841e..eb74800 100644
--- a/src/test/java/com/google/devtools/build/lib/sandbox/AbstractContainerizingSandboxedSpawnTest.java
+++ b/src/test/java/com/google/devtools/build/lib/sandbox/AbstractContainerizingSandboxedSpawnTest.java
@@ -15,7 +15,7 @@
package com.google.devtools.build.lib.sandbox;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxOutputs;
@@ -85,12 +85,9 @@
assertThat(outputsDir.getRelative("very/output.link").resolveSymbolicLinks())
.isEqualTo(outputsDir.getRelative("very/output.txt"));
assertThat(outputsDir.getRelative("very/output.dangling").isSymbolicLink()).isTrue();
- try {
- outputsDir.getRelative("very/output.dangling").resolveSymbolicLinks();
- fail("expected IOException");
- } catch (IOException e) {
- // Ignored.
- }
+ assertThrows(
+ IOException.class,
+ () -> outputsDir.getRelative("very/output.dangling").resolveSymbolicLinks());
assertThat(outputsDir.getRelative("very/output.dir").isDirectory(Symlinks.NOFOLLOW)).isTrue();
assertThat(outputsDir.getRelative("very/output.dir/test.txt").isFile(Symlinks.NOFOLLOW))
.isTrue();
diff --git a/src/test/java/com/google/devtools/build/lib/sandbox/SandboxOptionsTest.java b/src/test/java/com/google/devtools/build/lib/sandbox/SandboxOptionsTest.java
index 15e3a59..185392a 100644
--- a/src/test/java/com/google/devtools/build/lib/sandbox/SandboxOptionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/sandbox/SandboxOptionsTest.java
@@ -15,7 +15,7 @@
package com.google.devtools.build.lib.sandbox;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.common.options.OptionsParsingException;
@@ -68,33 +68,31 @@
@Test
public void testParsingAdditionalMounts_TooManyPaths() throws Exception {
String input = "a/bc/def/gh:/1/2/3:x/y/z";
- try {
- pathPair = new SandboxOptions.MountPairConverter().convert(input);
- fail();
- } catch (OptionsParsingException e) {
- assertThat(e)
- .hasMessageThat()
- .isEqualTo(
- "Input must be a single path to mount inside the sandbox or "
- + "a mounting pair in the form of 'source:target'");
- }
+ OptionsParsingException e =
+ assertThrows(
+ OptionsParsingException.class,
+ () -> pathPair = new SandboxOptions.MountPairConverter().convert(input));
+ assertThat(e)
+ .hasMessageThat()
+ .isEqualTo(
+ "Input must be a single path to mount inside the sandbox or "
+ + "a mounting pair in the form of 'source:target'");
}
@Test
public void testParsingAdditionalMounts_EmptyInput() throws Exception {
String input = "";
- try {
- pathPair = new SandboxOptions.MountPairConverter().convert(input);
- fail();
- } catch (OptionsParsingException e) {
- assertThat(
- "Input "
- + input
- + " contains one or more empty paths. "
- + "Input must be a single path to mount inside the sandbox or "
- + "a mounting pair in the form of 'source:target'")
- .isEqualTo(e.getMessage());
- }
+ OptionsParsingException e =
+ assertThrows(
+ OptionsParsingException.class,
+ () -> pathPair = new SandboxOptions.MountPairConverter().convert(input));
+ assertThat(
+ "Input "
+ + input
+ + " contains one or more empty paths. "
+ + "Input must be a single path to mount inside the sandbox or "
+ + "a mounting pair in the form of 'source:target'")
+ .isEqualTo(e.getMessage());
}
private static void assertMountPair(
diff --git a/src/test/java/com/google/devtools/build/lib/sandbox/SandboxfsSandboxedSpawnTest.java b/src/test/java/com/google/devtools/build/lib/sandbox/SandboxfsSandboxedSpawnTest.java
index 20eff2b..8b3593c 100644
--- a/src/test/java/com/google/devtools/build/lib/sandbox/SandboxfsSandboxedSpawnTest.java
+++ b/src/test/java/com/google/devtools/build/lib/sandbox/SandboxfsSandboxedSpawnTest.java
@@ -14,7 +14,7 @@
package com.google.devtools.build.lib.sandbox;
import static com.google.common.truth.Truth.assertThat;
-import static junit.framework.TestCase.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -211,11 +211,10 @@
assertThat(execRoot.getRelative("such/link-to-input-2").resolveSymbolicLinks())
.isEqualTo(input2);
} else {
- try {
- execRoot.getRelative("such/link-to-link.txt").resolveSymbolicLinks();
- fail("Symlink resolution worked, which means the target was mapped when not expected");
- } catch (IOException expected) {
- }
+ assertThrows(
+ "Symlink resolution worked, which means the target was mapped when not expected",
+ IOException.class,
+ () -> execRoot.getRelative("such/link-to-link.txt").resolveSymbolicLinks());
}
// Targets of symlinks must have been mapped inside the sandbox only when requested.