Adding basic fake AdbServer

This commit is contained in:
Samuel Carlsson 2013-07-26 16:33:27 +02:00
parent 4a6a908fba
commit e538d58ddc
4 changed files with 116 additions and 3 deletions

View File

@ -21,7 +21,7 @@ public class AndroidDeviceTestCases {
@Test(expected=JadbException.class) @Test(expected=JadbException.class)
public void invalidShellCommand() throws Exception public void invalidShellCommand() throws Exception
{ {
JadbConnection jadb = new JadbConnection(); JadbConnection jadb = new JadbConnection("localhost", 5037);
List<AndroidDevice> devices = jadb.getDevices(); List<AndroidDevice> devices = jadb.getDevices();
AndroidDevice device = devices.get(0); AndroidDevice device = devices.get(0);
device.executeShell("cmd", "foo", "bar"); device.executeShell("cmd", "foo", "bar");

View File

@ -7,6 +7,7 @@ import org.junit.Test;
import se.vidstige.jadb.AndroidDevice; import se.vidstige.jadb.AndroidDevice;
import se.vidstige.jadb.JadbConnection; import se.vidstige.jadb.JadbConnection;
import se.vidstige.jadb.test.fakes.AdbServer;
public class JadbTestCases { public class JadbTestCases {
@ -19,7 +20,8 @@ public class JadbTestCases {
@Test @Test
public void testGetDevices() throws Exception public void testGetDevices() throws Exception
{ {
JadbConnection jadb = new JadbConnection(); JadbConnection jadb = new JadbConnection("localhost", 15037);
//JadbConnection jadb = new JadbConnection();
List<AndroidDevice> actual = jadb.getDevices(); List<AndroidDevice> actual = jadb.getDevices();
Assert.assertEquals("emulator-5554", actual.get(0).getSerial()); Assert.assertEquals("emulator-5554", actual.get(0).getSerial());
} }

View File

@ -0,0 +1,55 @@
package se.vidstige.jadb.test.fakes;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.nio.charset.Charset;
public class AdbResponder implements Runnable {
private Socket socket;
public AdbResponder(Socket socket) {
this.socket = socket;
}
@Override
public void run()
{
try {
System.out.println("Serving client");
DataInput input = new DataInputStream(socket.getInputStream());
OutputStreamWriter output = new OutputStreamWriter(socket.getOutputStream());
byte[] buffer = new byte[4];
input.readFully(buffer);
String encodedLength = new String(buffer, Charset.forName("utf-8"));
int length = Integer.parseInt(encodedLength, 16);
buffer = new byte[length];
input.readFully(buffer);
String command = new String(buffer, Charset.forName("utf-8"));
System.out.println("Command: " + command);
if ("host.devices".equals(command)) {
output.write("OKAY");
send(output, "X\tdevice\nY\tdevice");
}
output.write("FAIL");
} catch (IOException e) {
}
}
private String getCommandLength(String command) {
return String.format("%04x", Integer.valueOf(command.length()));
}
public void send(OutputStreamWriter writer, String response) throws IOException {
writer.write(getCommandLength(response));
writer.write(response);
writer.flush();
}
}

View File

@ -1,5 +1,61 @@
package se.vidstige.jadb.test.fakes; package se.vidstige.jadb.test.fakes;
public class AdbServer { import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
public class AdbServer implements Runnable {
private int port = 15037;
private ServerSocket socket;
private Thread thread;
private Object lockObject = new Object();
public static void main(String[] args)
{
AdbServer server = new AdbServer();
server.run();
}
public void start() throws InterruptedException
{
thread = new Thread(this, "Fake Adb Server");
thread.setDaemon(true);
thread.start();
synchronized (lockObject) {
lockObject.wait();
}
}
public int getPort() {
return port;
}
@Override
public void run() {
try {
System.out.println("Starting on port " + port);
socket = new ServerSocket(port);
socket.setReuseAddress(true);
synchronized (lockObject) {
lockObject.notify();
}
while (true)
{
Socket c = socket.accept();
Thread clientThread = new Thread(new AdbResponder(c), "AdbClientWorker");
clientThread.setDaemon(true);
clientThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} }