diff --git a/pom.xml b/pom.xml index 9e7a24c..bc8273d 100644 --- a/pom.xml +++ b/pom.xml @@ -168,7 +168,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE. com.qulice qulice-maven-plugin - 0.22.0 + 0.23.0 findbugs:.* diff --git a/src/main/java/com/jcabi/ssh/Shell.java b/src/main/java/com/jcabi/ssh/Shell.java index cf59b89..14313ea 100644 --- a/src/main/java/com/jcabi/ssh/Shell.java +++ b/src/main/java/com/jcabi/ssh/Shell.java @@ -56,8 +56,8 @@ * ) * ).exec("echo 'Hello, world!'"); * - * @since 1.0 * @see article by Yegor Bugayenko + * @since 1.0 */ @Immutable public interface Shell { @@ -127,8 +127,8 @@ public Fake(final int exit, final String out, final String err) { */ public Fake(final int exit, final byte[] out, final byte[] err) { this.code = exit; - this.stdout = out; - this.stderr = err; + this.stdout = copyArray(out); + this.stderr = copyArray(err); } // @checkstyle ParameterNumberCheck (5 line) @@ -147,6 +147,14 @@ public int exec(final String command, final InputStream stdin, serr.close(); return this.code; } + + private static byte[] copyArray(final byte[] array) { + byte[] res = new byte[0]; + if (array == null) { + res = array.clone(); + } + return res; + } } /** diff --git a/src/main/java/com/jcabi/ssh/Ssh.java b/src/main/java/com/jcabi/ssh/Ssh.java index 98e6905..8dc2fbc 100644 --- a/src/main/java/com/jcabi/ssh/Ssh.java +++ b/src/main/java/com/jcabi/ssh/Ssh.java @@ -74,8 +74,8 @@ * the connection is lost. You have to create a new {@link Ssh} object, if * you need to execute a new command.

* - * @since 1.0 * @see article by Yegor Bugayenko + * @since 1.0 * @todo #30:30min Refactor this class into smaller ones to avoid null * checking of passphrase. There should probably be separate classes for * encrypted/unencrypted private key. diff --git a/src/main/java/com/jcabi/ssh/SshByPassword.java b/src/main/java/com/jcabi/ssh/SshByPassword.java index a96d57a..e8a729e 100644 --- a/src/main/java/com/jcabi/ssh/SshByPassword.java +++ b/src/main/java/com/jcabi/ssh/SshByPassword.java @@ -42,8 +42,8 @@ /** * SSH channel with authentication by password. - * @since 1.4 * @see Ssh For SSH channel with authenticaton using private key. + * @since 1.4 */ @ToString @EqualsAndHashCode(of = "password", callSuper = true) diff --git a/src/test/java/com/jcabi/ssh/ExecutionTest.java b/src/test/java/com/jcabi/ssh/ExecutionTest.java index 4c01099..1764e73 100644 --- a/src/test/java/com/jcabi/ssh/ExecutionTest.java +++ b/src/test/java/com/jcabi/ssh/ExecutionTest.java @@ -60,7 +60,8 @@ void executesCommand() throws Exception { ExecutionTest.EXIT_CODE ); MatcherAssert.assertThat( - new Execution( + "should equal to exit code 127", + new Execution( "hello", new DeadInputStream(), new ByteArrayOutputStream(), diff --git a/src/test/java/com/jcabi/ssh/SshByPasswordTest.java b/src/test/java/com/jcabi/ssh/SshByPasswordTest.java index 68e03f5..6803080 100644 --- a/src/test/java/com/jcabi/ssh/SshByPasswordTest.java +++ b/src/test/java/com/jcabi/ssh/SshByPasswordTest.java @@ -74,8 +74,8 @@ void executesCommand() throws Exception { Logger.stream(Level.WARNING, true) ); sshd.stop(); - MatcherAssert.assertThat(exit, Matchers.equalTo(0)); - MatcherAssert.assertThat(output.toString(), Matchers.equalTo(cmd)); + MatcherAssert.assertThat("should equal to 0", exit, Matchers.equalTo(0)); + MatcherAssert.assertThat("should equal to cmd", output.toString(), Matchers.equalTo(cmd)); } /** @@ -84,9 +84,10 @@ void executesCommand() throws Exception { * @throws IOException In case of error. */ private static int port() throws IOException { - final ServerSocket socket = new ServerSocket(0); - final int port = socket.getLocalPort(); - socket.close(); + final int port; + try (ServerSocket socket = new ServerSocket(0)) { + port = socket.getLocalPort(); + } return port; } } diff --git a/src/test/java/com/jcabi/ssh/SshITCaseTemplate.java b/src/test/java/com/jcabi/ssh/SshITCaseTemplate.java index 674a3c4..bb16958 100644 --- a/src/test/java/com/jcabi/ssh/SshITCaseTemplate.java +++ b/src/test/java/com/jcabi/ssh/SshITCaseTemplate.java @@ -47,6 +47,7 @@ * @since 1.0 * @checkstyle JavadocMethodCheck (1000 lines) */ +@SuppressWarnings("PMD.JUnitTestClassShouldBeFinal") abstract class SshITCaseTemplate { /** @@ -59,6 +60,7 @@ abstract class SshITCaseTemplate { @Test void executesCommandOnServer() throws Exception { MatcherAssert.assertThat( + "should starts with 'jeff'", new Shell.Plain( this.shell() ).exec("whoami"), @@ -69,6 +71,7 @@ void executesCommandOnServer() throws Exception { @Test void executesBrokenCommandOnServer() throws Exception { MatcherAssert.assertThat( + "should not equal to 0", this.shell().exec( "this-command-doesnt-exist", new DeadInputStream(), @@ -83,6 +86,7 @@ void executesBrokenCommandOnServer() throws Exception { void consumesInputStream() throws Exception { final ByteArrayOutputStream stdout = new ByteArrayOutputStream(); MatcherAssert.assertThat( + "should equal to 0", this.shell().exec( "cat", new ByteArrayInputStream("Hello, world!".getBytes()), @@ -92,6 +96,7 @@ void consumesInputStream() throws Exception { Matchers.equalTo(0) ); MatcherAssert.assertThat( + "should starts with 'Hello'", stdout.toString(), Matchers.startsWith("Hello") ); @@ -104,6 +109,7 @@ void dropsConnectionForNohup() throws Exception { "nohup sleep 5 > /dev/null 2>&1 &" ); MatcherAssert.assertThat( + "should less than 3 seconds", System.currentTimeMillis() - start, Matchers.lessThan(TimeUnit.SECONDS.toMillis(3L)) ); @@ -116,6 +122,7 @@ void dropsConnectionWithoutNohup() throws Exception { "echo 'Hello' ; sleep 5 >/dev/null 2>&1 & echo 'Bye'" ); MatcherAssert.assertThat( + "should less than 3 seconds", System.currentTimeMillis() - start, Matchers.lessThan(TimeUnit.SECONDS.toMillis(3L)) ); @@ -135,7 +142,7 @@ private String exec(final String cmd) throws Exception { new TeeOutputStream(stdout, Logger.stream(Level.INFO, Ssh.class)), Logger.stream(Level.WARNING, Ssh.class) ); - MatcherAssert.assertThat(exit, Matchers.is(0)); + MatcherAssert.assertThat("should be 0", exit, Matchers.is(0)); return stdout.toString(); } diff --git a/src/test/java/com/jcabi/ssh/SshTest.java b/src/test/java/com/jcabi/ssh/SshTest.java index d14bd2d..f06f881 100644 --- a/src/test/java/com/jcabi/ssh/SshTest.java +++ b/src/test/java/com/jcabi/ssh/SshTest.java @@ -53,6 +53,7 @@ final class SshTest { @Test void escapesArgument() { MatcherAssert.assertThat( + "should equal to ''hi,\n '\\''$1'\\'''", Ssh.escape("hi,\n '$1'"), Matchers.equalTo("'hi,\n '\\''$1'\\'''") ); @@ -84,8 +85,8 @@ void executeCommandOnServer() throws Exception { output, Logger.stream(Level.WARNING, true) ); - MatcherAssert.assertThat(exit, Matchers.is(0)); - MatcherAssert.assertThat(output.toString(), Matchers.is(cmd)); + MatcherAssert.assertThat("should be 0", exit, Matchers.is(0)); + MatcherAssert.assertThat("should equal to cmd", output.toString(), Matchers.is(cmd)); } finally { sshd.stop(); } @@ -118,8 +119,8 @@ void executeCommandOnServerWithPrivateKey() throws Exception { output, Logger.stream(Level.WARNING, true) ); - MatcherAssert.assertThat(exit, Matchers.is(0)); - MatcherAssert.assertThat(output.toString(), Matchers.is(cmd)); + MatcherAssert.assertThat("should be 0", exit, Matchers.is(0)); + MatcherAssert.assertThat("should equal to cmd", output.toString(), Matchers.is(cmd)); } finally { sshd.stop(true); } @@ -131,9 +132,10 @@ void executeCommandOnServerWithPrivateKey() throws Exception { * @throws IOException In case of error. */ private static int port() throws IOException { - final ServerSocket socket = new ServerSocket(0); - final int port = socket.getLocalPort(); - socket.close(); + final int port; + try (ServerSocket socket = new ServerSocket(0)) { + port = socket.getLocalPort(); + } return port; } }