diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/shell')
6 files changed, 38 insertions, 35 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/shell/CommandLargeInputsTest.java b/src/test/java/com/google/devtools/build/lib/shell/CommandLargeInputsTest.java index 06d4f9a52c..8ffacd1822 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/CommandLargeInputsTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/CommandLargeInputsTest.java @@ -121,11 +121,11 @@ public class CommandLargeInputsTest { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); command.execute(Command.NO_INPUT, Command.NO_OBSERVER, out, err); - StringBuffer expectedOut = new StringBuffer(); - StringBuffer expectedErr = new StringBuffer(); + StringBuilder expectedOut = new StringBuilder(); + StringBuilder expectedErr = new StringBuilder(); for (int i = 0; i < 1000; i++) { - expectedOut.append("OUT").append(Integer.toString(i)).append("\n"); - expectedErr.append("ERR").append(Integer.toString(i)).append("\n"); + expectedOut.append("OUT").append(i).append("\n"); + expectedErr.append("ERR").append(i).append("\n"); } assertEquals(expectedOut.toString(), out.toString("UTF-8")); assertEquals(expectedErr.toString(), err.toString("UTF-8")); diff --git a/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java b/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java index 694972ed59..ef45a6313d 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java @@ -13,6 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.shell; +import static com.google.common.truth.Truth.assertThat; import static com.google.devtools.build.lib.shell.TestUtil.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -100,7 +101,7 @@ public class CommandTest { final Command command = new Command(commandArgs, env, workingDir); assertArrayEquals(commandArgs, command.getCommandLineElements()); for (final String key : env.keySet()) { - assertEquals(env.get(key), command.getEnvironmentVariables().get(key)); + assertThat(command.getEnvironmentVariables()).containsEntry(key, env.get(key)); } assertEquals(workingDir, command.getWorkingDirectory()); } @@ -112,8 +113,8 @@ public class CommandTest { final Command command = new Command(new String[] {"ls"}); final CommandResult result = command.execute(); assertTrue(result.getTerminationStatus().success()); - assertTrue(result.getStderr().length == 0); - assertTrue(result.getStdout().length > 0); + assertEquals(0, result.getStderr().length); + assertThat(result.getStdout().length).isGreaterThan(0); } @Test @@ -149,8 +150,8 @@ public class CommandTest { "print 'a'x100000" }); final CommandResult result = command.execute(); assertTrue(result.getTerminationStatus().success()); - assertTrue(result.getStderr().length == 0); - assertTrue(result.getStdout().length > 0); + assertEquals(0, result.getStderr().length); + assertThat(result.getStdout().length).isGreaterThan(0); } @Test @@ -199,8 +200,8 @@ public class CommandTest { CommandResult result = command.execute(emptyInput, Command.NO_OBSERVER, out, err); assertTrue(result.getTerminationStatus().success()); - assertEquals("", out.toString("UTF-8")); - assertEquals("", err.toString("UTF-8")); + assertThat(out.toString("UTF-8")).isEmpty(); + assertThat(err.toString("UTF-8")).isEmpty(); } @Test @@ -208,8 +209,8 @@ public class CommandTest { final Command command = new Command(new String[]{"/bin/cat"}); CommandResult result = command.execute(); assertTrue(result.getTerminationStatus().success()); - assertEquals("", new String(result.getStdout(), "UTF-8")); - assertEquals("", new String(result.getStderr(), "UTF-8")); + assertThat(new String(result.getStdout(), "UTF-8")).isEmpty(); + assertThat(new String(result.getStderr(), "UTF-8")).isEmpty(); } @Test @@ -355,7 +356,7 @@ public class CommandTest { new Command(args).execute(); fail("Should have exited with status " + exit); } catch (BadExitStatusException e) { - assertEquals("Process exited with status " + exit, e.getMessage()); + assertThat(e).hasMessage("Process exited with status " + exit); checkCommandElements(e, "/bin/sh", "-c", "exit " + exit); TerminationStatus status = e.getResult().getTerminationStatus(); assertFalse(status.success()); @@ -373,7 +374,7 @@ public class CommandTest { new Command(args).execute(); fail("Should have exited with status " + expected); } catch (BadExitStatusException e) { - assertEquals("Process exited with status " + expected, e.getMessage()); + assertThat(e).hasMessage("Process exited with status " + expected); checkCommandElements(e, "/bin/sh", "-c", "exit " + exit); TerminationStatus status = e.getResult().getTerminationStatus(); assertFalse(status.success()); @@ -398,7 +399,7 @@ public class CommandTest { new Command(args).execute(); fail("Expected signal " + signal); } catch (AbnormalTerminationException e) { - assertEquals("Process terminated by signal " + signal, e.getMessage()); + assertThat(e).hasMessage("Process terminated by signal " + signal); checkCommandElements(e, killmyself, "" + signal); TerminationStatus status = e.getResult().getTerminationStatus(); assertFalse(status.success()); @@ -535,8 +536,8 @@ public class CommandTest { final CommandResult result = resultContainer[0]; assertTrue(result.getTerminationStatus().success()); - assertTrue(result.getStderr().length == 0); - assertTrue(result.getStdout().length == 0); + assertEquals(0, result.getStderr().length); + assertEquals(0, result.getStdout().length); } @Test @@ -554,7 +555,7 @@ public class CommandTest { } catch (AbnormalTerminationException e) { // Good. checkCommandElements(e, "/bin/echo", "foo"); - assertEquals("java.io.IOException", e.getMessage()); + assertThat(e).hasMessage("java.io.IOException"); } } @@ -681,7 +682,7 @@ public class CommandTest { private static void checkSuccess(final CommandResult result, final String expectedOutput) { assertTrue(result.getTerminationStatus().success()); - assertTrue(result.getStderr().length == 0); + assertEquals(0, result.getStderr().length); assertEquals(expectedOutput, new String(result.getStdout())); } } diff --git a/src/test/java/com/google/devtools/build/lib/shell/ConsumersTest.java b/src/test/java/com/google/devtools/build/lib/shell/ConsumersTest.java index d03012dac1..e521949315 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/ConsumersTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/ConsumersTest.java @@ -13,7 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.shell; -import static org.junit.Assert.assertEquals; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; @@ -65,7 +65,7 @@ public class ConsumersTest { outErr.waitForCompletion(); fail(); } catch (IOException e) { - assertEquals(SECRET_MESSAGE, e.getMessage()); + assertThat(e).hasMessage(SECRET_MESSAGE); } } @@ -118,7 +118,7 @@ public class ConsumersTest { } catch (IOException e) { fail(); } catch (Error e) { - assertEquals(SECRET_MESSAGE, e.getMessage()); + assertThat(e).hasMessage(SECRET_MESSAGE); } } @@ -143,7 +143,7 @@ public class ConsumersTest { outErr.waitForCompletion(); fail(); } catch (RuntimeException e) { - assertEquals(SECRET_MESSAGE, e.getMessage()); + assertThat(e).hasMessage(SECRET_MESSAGE); } } diff --git a/src/test/java/com/google/devtools/build/lib/shell/LoadTest.java b/src/test/java/com/google/devtools/build/lib/shell/LoadTest.java index b49ddd9564..545120eae4 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/LoadTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/LoadTest.java @@ -52,12 +52,12 @@ public class LoadTest { tempFile.deleteOnExit(); // write some random numbers to the file - final PrintWriter out = new PrintWriter(new FileWriter(tempFile)); - final Random r = new Random(); - for (int i = 0; i < 100; i++) { - out.println(String.valueOf(r.nextDouble())); + try (final PrintWriter out = new PrintWriter(new FileWriter(tempFile))) { + final Random r = new Random(); + for (int i = 0; i < 100; i++) { + out.println(String.valueOf(r.nextDouble())); + } } - out.close(); } @After diff --git a/src/test/java/com/google/devtools/build/lib/shell/ShellUtilsTest.java b/src/test/java/com/google/devtools/build/lib/shell/ShellUtilsTest.java index 1034561b35..cfda80773b 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/ShellUtilsTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/ShellUtilsTest.java @@ -13,6 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.shell; +import static com.google.common.truth.Truth.assertThat; import static com.google.devtools.build.lib.shell.ShellUtils.prettyPrintArgv; import static com.google.devtools.build.lib.shell.ShellUtils.shellEscape; import static com.google.devtools.build.lib.shell.ShellUtils.tokenize; @@ -55,7 +56,7 @@ public class ShellUtilsTest { private void assertTokenize(String copts, String... expectedTokens) throws Exception { - List<String> actualTokens = new ArrayList<String>(); + List<String> actualTokens = new ArrayList<>(); tokenize(actualTokens, copts); assertEquals(Arrays.asList(expectedTokens), actualTokens); } @@ -126,7 +127,7 @@ public class ShellUtilsTest { tokenize(new ArrayList<String>(), copts); fail(); } catch (ShellUtils.TokenizationException e) { - assertEquals(expectedError, e.getMessage()); + assertThat(e).hasMessage(expectedError); } } @@ -163,14 +164,14 @@ public class ShellUtilsTest { // because String.split() ignores trailing empty strings. ArrayList<String> words = Lists.newArrayList(); int index; - while ((index = stdout.indexOf("\n")) >= 0) { + while ((index = stdout.indexOf('\n')) >= 0) { words.add(stdout.substring(0, index)); stdout = stdout.substring(index + 1); } assertEquals(in, words); // Assert that tokenize is dual to pretty-print: - List<String> out = new ArrayList<String>(); + List<String> out = new ArrayList<>(); try { tokenize(out, shellCommand); } finally { diff --git a/src/test/java/com/google/devtools/build/lib/shell/ToTruncatedStringTest.java b/src/test/java/com/google/devtools/build/lib/shell/ToTruncatedStringTest.java index 90a423496a..3a7b7d6e8d 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/ToTruncatedStringTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/ToTruncatedStringTest.java @@ -13,6 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.shell; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import org.junit.Before; @@ -44,12 +45,12 @@ public class ToTruncatedStringTest { @Test public void testTruncatingNullYieldsEmptyString() { - assertEquals("", LogUtil.toTruncatedString(null)); + assertThat(LogUtil.toTruncatedString(null)).isEmpty(); } @Test public void testTruncatingEmptyArrayYieldsEmptyString() { - assertEquals("", LogUtil.toTruncatedString(new byte[0])); + assertThat(LogUtil.toTruncatedString(new byte[0])).isEmpty(); } @Test |