From 88faded23d8b06e26d9f005632114ee742ccb081 Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Thu, 20 Mar 2014 10:31:13 +0100 Subject: [PATCH] Adding truly mocked test case. --- src/se/vidstige/jadb/JadbConnection.java | 4 ++ src/se/vidstige/jadb/Transport.java | 5 +++ .../vidstige/jadb/test/MockedTestCases.java | 40 +++++++++++-------- .../vidstige/jadb/test/fakes/AdbServer.java | 5 ++- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 9779eaf..5d7f1f0 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -60,4 +60,8 @@ public class JadbConnection { public JadbDevice getAnyDevice() { return JadbDevice.createAny(main); } + + public void close() throws IOException { + main.close(); + } } diff --git a/src/se/vidstige/jadb/Transport.java b/src/se/vidstige/jadb/Transport.java index 7897847..05cafd9 100644 --- a/src/se/vidstige/jadb/Transport.java +++ b/src/se/vidstige/jadb/Transport.java @@ -57,4 +57,9 @@ class Transport { verifyResponse(); return new SyncTransport(outputStream, inputStream); } + + public void close() throws IOException { + inputStream.close(); + outputStream.close(); + } } diff --git a/test/se/vidstige/jadb/test/MockedTestCases.java b/test/se/vidstige/jadb/test/MockedTestCases.java index a6f0433..05c6b05 100644 --- a/test/se/vidstige/jadb/test/MockedTestCases.java +++ b/test/se/vidstige/jadb/test/MockedTestCases.java @@ -2,28 +2,34 @@ package se.vidstige.jadb.test; import java.util.List; -import org.junit.Test; +import org.junit.*; import se.vidstige.jadb.JadbDevice; import se.vidstige.jadb.JadbConnection; import se.vidstige.jadb.JadbException; +import se.vidstige.jadb.test.fakes.AdbServer; public class MockedTestCases { - @Test - public void testGetState() throws Exception { - JadbConnection jadb = new JadbConnection(); - List devices = jadb.getDevices(); - JadbDevice device = devices.get(0); - device.getState(); - } - - @Test(expected=JadbException.class) - public void invalidShellCommand() throws Exception - { - JadbConnection jadb = new JadbConnection("localhost", 5037); - List devices = jadb.getDevices(); - JadbDevice device = devices.get(0); - device.executeShell("cmd", "foo", "bar"); - } + private AdbServer server; + private JadbConnection connection; + + @Before + public void setUp() throws Exception{ + server = new AdbServer(); + server.start(); + connection = new JadbConnection("localhost", 15037); + } + + @After + public void tearDown() throws Exception { + connection.close(); + server.stop(); + } + + @Test + public void testListDevices() throws Exception { + List devices = connection.getDevices(); + Assert.assertEquals("X", devices.get(0).getSerial()); + } } diff --git a/test/se/vidstige/jadb/test/fakes/AdbServer.java b/test/se/vidstige/jadb/test/fakes/AdbServer.java index c36350d..568285e 100644 --- a/test/se/vidstige/jadb/test/fakes/AdbServer.java +++ b/test/se/vidstige/jadb/test/fakes/AdbServer.java @@ -56,7 +56,10 @@ public class AdbServer implements Runnable { clientThread.start(); } } catch (IOException e) { - e.printStackTrace(); } } + + public void stop() throws IOException { + socket.close(); + } }