Skip to content

Commit

Permalink
#12 new ctor in SSH
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Dec 14, 2014
1 parent df54ae8 commit 0d9b204
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/jcabi/ssh/SSH.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.Validate;

Expand Down Expand Up @@ -96,6 +98,21 @@ public final class SSH implements Shell {
*/
private final transient String key;

/**
* Constructor.
* @param adr IP address
* @param prt Port of server
* @param user Login
* @param priv Private SSH key
* @throws IOException If fails
* @checkstyle ParameterNumberCheck (6 lines)
* @since 1.4
*/
public SSH(final String adr, final int prt,
final String user, final URL priv) throws IOException {
this(adr, prt, user, IOUtils.toString(priv));
}

/**
* Constructor.
* @param adr IP address
Expand Down
39 changes: 37 additions & 2 deletions src/test/java/com/jcabi/ssh/SSHDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
package com.jcabi.ssh;

import org.apache.commons.lang3.SystemUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -52,16 +55,48 @@ public final class SSHDTest {
@Rule
public final transient TemporaryFolder temp = new TemporaryFolder();

/**
* Check that it's not Windows.
*/
@Before
public void notWindows() {
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
}

/**
* SSH can execute command on a real SSH server.
* @throws Exception In case of error.
*/
@Test
public void executeCommandOnServer() throws Exception {
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
final SSHD sshd = new SSHD(this.temp.newFolder());
try {
new Shell.Empty(sshd.connect()).exec("echo one");
MatcherAssert.assertThat(
new Shell.Plain(sshd.connect()).exec("echo one"),
Matchers.startsWith("one")
);
} finally {
sshd.close();
}
}

/**
* SSH can execute command on a real SSH server.
* @throws Exception In case of error.
*/
@Test
public void executeCommandOnServerWithManualConfig() throws Exception {
final SSHD sshd = new SSHD(this.temp.newFolder());
try {
MatcherAssert.assertThat(
new Shell.Plain(
new SSH(
sshd.host(), sshd.port(), sshd.login(),
this.getClass().getResource("id_rsa")
)
).exec("echo 'how are you'"),
Matchers.startsWith("how are")
);
} finally {
sshd.close();
}
Expand Down

0 comments on commit 0d9b204

Please sign in to comment.