Merge pull request #40 from vidstige/shell-stdout

Shell stdout
This commit is contained in:
Samuel Carlsson 2016-10-01 17:07:53 +02:00 committed by GitHub
commit 46045ce241
5 changed files with 33 additions and 21 deletions

View File

@ -4,6 +4,8 @@ import se.vidstige.jadb.JadbException;
import se.vidstige.jadb.RemoteFile;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
/**
@ -16,5 +18,5 @@ public interface AdbDeviceResponder {
void filePushed(RemoteFile path, int mode, ByteArrayOutputStream buffer) throws JadbException;
void filePulled(RemoteFile path, ByteArrayOutputStream buffer) throws JadbException, IOException;
void shell(String command) throws IOException;
void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException;
}

View File

@ -81,8 +81,11 @@ class AdbProtocolHandler implements Runnable {
sync.send("FAIL", e.getMessage());
}
} else if (command.startsWith("shell:")) {
shell(command.substring("shell:".length()));
String shellCommand = command.substring("shell:".length());
output.writeBytes("OKAY");
shell(shellCommand, output, input);
output.close();
return;
} else {
throw new ProtocolException("Unknown command: " + command);
}
@ -94,8 +97,8 @@ class AdbProtocolHandler implements Runnable {
}
}
private void shell(String command) throws IOException {
selected.shell(command);
private void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException {
selected.shell(command, stdout, stdin);
}
private int readInt(DataInput input) throws IOException {

View File

@ -22,7 +22,7 @@ public abstract class SocketServer implements Runnable {
thread = new Thread(this, "Fake Adb Server");
thread.setDaemon(true);
thread.start();
serverReady();
waitForServer();
}
public int getPort() {
@ -35,7 +35,7 @@ public abstract class SocketServer implements Runnable {
socket = new ServerSocket(port);
socket.setReuseAddress(true);
waitForServer();
serverReady();
while (true) {
Socket c = socket.accept();
@ -47,7 +47,14 @@ public abstract class SocketServer implements Runnable {
}
}
private void serverReady() throws InterruptedException {
private void serverReady() {
synchronized (lockObject) {
isStarted = true;
lockObject.notify();
}
}
private void waitForServer() throws InterruptedException {
synchronized (lockObject) {
if (!isStarted) {
lockObject.wait();
@ -55,13 +62,6 @@ public abstract class SocketServer implements Runnable {
}
}
private void waitForServer() {
synchronized (lockObject) {
lockObject.notify();
isStarted = true;
}
}
protected abstract Runnable createResponder(Socket socket);
public void stop() throws IOException, InterruptedException {

View File

@ -7,6 +7,8 @@ import se.vidstige.jadb.server.AdbResponder;
import se.vidstige.jadb.server.AdbServer;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ProtocolException;
import java.nio.charset.Charset;
@ -76,8 +78,8 @@ public class FakeAdbServer implements AdbResponder {
return findBySerial(serial).expectPull(path);
}
public void expectShell(String serial, String commands) {
findBySerial(serial).expectShell(commands);
public DeviceResponder.ShellExpectation expectShell(String serial, String commands) {
return findBySerial(serial).expectShell(commands);
}
@Override
@ -131,11 +133,11 @@ public class FakeAdbServer implements AdbResponder {
}
@Override
public void shell(String command) throws IOException {
public void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException {
for (ShellExpectation se : shellExpectations) {
if (se.matches(command)) {
shellExpectations.remove(se);
se.throwIfFail();
se.writeOutputTo(stdout);
return;
}
}
@ -192,6 +194,7 @@ public class FakeAdbServer implements AdbResponder {
public class ShellExpectation {
private final String command;
private byte[] stdout;
public ShellExpectation(String command) {
this.command = command;
@ -201,8 +204,12 @@ public class FakeAdbServer implements AdbResponder {
return command.equals(this.command);
}
public void throwIfFail() {
public void returns(String stdout) {
this.stdout = stdout.getBytes(Charset.forName("utf-8"));
}
public void writeOutputTo(DataOutputStream stdout) throws IOException {
stdout.write(this.stdout);
}
}
@ -220,7 +227,7 @@ public class FakeAdbServer implements AdbResponder {
public ShellExpectation expectShell(String command) {
ShellExpectation expectation = new ShellExpectation(command);
shellExpectations.add(new ShellExpectation(command));
shellExpectations.add(expectation);
return expectation;
}
}

View File

@ -80,7 +80,7 @@ public class MockedTestCases {
@Test
public void testExecuteShell() throws Exception {
server.add("serial-123");
server.expectShell("serial-123", "ls -l");
server.expectShell("serial-123", "ls -l").returns("total 0");
JadbDevice device = connection.getDevices().get(0);
device.executeShell("ls", "-l");
}