Permalink
Browse files

#65 Shell.Fake

  • Loading branch information...
yegor256 committed Sep 5, 2017
1 parent 89d61e3 commit dd6c6b28f2deba6f97e0822303ef34460787d9d2
Showing with 72 additions and 2 deletions.
  1. +3 −2 src/main/java/com/jcabi/ssh/SSH.java
  2. +69 −0 src/main/java/com/jcabi/ssh/Shell.java
@@ -53,7 +53,8 @@
*
* <p>This class implements {@link Shell} interface. In order to use
* it, just make an instance and call
* {@link #exec(String,InputStream,OutputStream,OutputStream)} exec()}:
* {@link #exec(String, java.io.InputStream,
* java.io.OutputStream, java.io.OutputStream)} exec()}:
*
* <pre> String hello = new Shell.Plain(
* new SSH(
@@ -91,7 +92,7 @@
/**
* Private SSH key pass phrase.
*/
private transient String passphrase;
private final transient String passphrase;
/**
* Constructor.
@@ -77,6 +77,75 @@
int exec(String command, InputStream stdin,
OutputStream stdout, OutputStream stderr) throws IOException;
/**
* Fake shell for unit testing.
*
* <p>It doesn't do anything, but imitates the behavior of a real
* shell, returning the code and the output provided in the ctor.</p>
*
* @since 1.6
*/
@Immutable
@ToString
@EqualsAndHashCode(of = "code")
final class Fake implements Shell {
/**
* Exit code.
*/
private final int code;
/**
* Stdout to return.
*/
private final byte[] stdout;
/**
* Stderr to return.
*/
private final byte[] stderr;
/**
* Ctor.
*/
public Fake() {
this(0, "", "");
}
/**
* Ctor.
* @param exit Exit code to return
* @param out Stdout to return
* @param err Stderr to return
*/
public Fake(final int exit, final String out, final String err) {
this(exit, out.getBytes(), err.getBytes());
}
/**
* Ctor.
* @param exit Exit code to return
* @param out Stdout to return
* @param err Stderr to return
*/
public Fake(final int exit, final byte[] out, final byte[] err) {
this.code = exit;
this.stdout = out;
this.stderr = err;
}
// @checkstyle ParameterNumberCheck (5 line)
@Override
public int exec(final String command, final InputStream stdin,
final OutputStream sout, final OutputStream serr)
throws IOException {
while (true) {
// @checkstyle MagicNumber (1 line)
if (stdin.read(new byte[2048]) < 0) {
break;
}
}
sout.write(this.stdout);
sout.close();
serr.write(this.stderr);
serr.close();
return this.code;
}
}
/**
* Safe run (throws if exit code is not zero).
*/

0 comments on commit dd6c6b2

Please sign in to comment.