diff --git a/test/se/vidstige/jadb/test/MockedTestCases.java b/test/se/vidstige/jadb/test/MockedTestCases.java index b54ebd6..3318c15 100644 --- a/test/se/vidstige/jadb/test/MockedTestCases.java +++ b/test/se/vidstige/jadb/test/MockedTestCases.java @@ -7,12 +7,13 @@ import org.junit.Test; import se.vidstige.jadb.JadbConnection; import se.vidstige.jadb.JadbDevice; import se.vidstige.jadb.test.fakes.AdbServer; +import se.vidstige.jadb.test.fakes.SocketServer; import java.util.List; public class MockedTestCases { - private AdbServer server; + private SocketServer server; private JadbConnection connection; @Before diff --git a/test/se/vidstige/jadb/test/fakes/AdbServer.java b/test/se/vidstige/jadb/test/fakes/AdbServer.java index 28288b4..697d08d 100644 --- a/test/se/vidstige/jadb/test/fakes/AdbServer.java +++ b/test/se/vidstige/jadb/test/fakes/AdbServer.java @@ -1,71 +1,31 @@ package se.vidstige.jadb.test.fakes; -import java.io.IOException; -import java.net.ServerSocket; import java.net.Socket; -// >set ANDROID_ADB_SERVER_PORT=15037 -public class AdbServer implements Runnable { +/** + * Created by vidstige on 2014-03-20 + */ +public class AdbServer extends SocketServer { - private static final int DEFAULT_PORT = 15037; - private final int port; - private ServerSocket socket; - private Thread thread; - private final Object lockObject = new Object(); - - public static void main(String[] args) - { - AdbServer server = new AdbServer(); - server.run(); - } + public static final int DEFAULT_PORT = 15037; public AdbServer() { this(DEFAULT_PORT); } - public AdbServer(int port) - { - this.port = port; + + public AdbServer(int port) { + super(port); } - - 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) { - } - } + @Override + protected Runnable createResponder(Socket socket) { + return new AdbResponder(socket); + } - public void stop() throws IOException, InterruptedException { - socket.close(); - thread.join(); + public static void main(String[] args) + { + SocketServer server = new AdbServer(); + server.run(); } } diff --git a/test/se/vidstige/jadb/test/fakes/SocketServer.java b/test/se/vidstige/jadb/test/fakes/SocketServer.java new file mode 100644 index 0000000..8633c11 --- /dev/null +++ b/test/se/vidstige/jadb/test/fakes/SocketServer.java @@ -0,0 +1,62 @@ +package se.vidstige.jadb.test.fakes; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +// >set ANDROID_ADB_SERVER_PORT=15037 +public abstract class SocketServer implements Runnable { + + private final int port; + private ServerSocket socket; + private Thread thread; + private final Object lockObject = new Object(); + + public SocketServer(int port) + { + this.port = port; + } + + 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(createResponder(c), "AdbClientWorker"); + clientThread.setDaemon(true); + clientThread.start(); + } + } catch (IOException e) { + } + } + + protected abstract Runnable createResponder(Socket socket); + + public void stop() throws IOException, InterruptedException { + socket.close(); + thread.join(); + } +}