From a58587f79465442f223f5693d33f87ba2d4a7d21 Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Wed, 26 Mar 2014 22:12:29 +0100 Subject: [PATCH] Adding test case for pulling file. --- .../jadb/server/AdbDeviceResponder.java | 2 ++ .../jadb/server/AdbProtocolHandler.java | 8 +++++ .../vidstige/jadb/test/MockedTestCases.java | 12 ++++++++ .../jadb/test/fakes/FakeAdbServer.java | 29 +++++++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/src/se/vidstige/jadb/server/AdbDeviceResponder.java b/src/se/vidstige/jadb/server/AdbDeviceResponder.java index 4e9a280..02f46ef 100644 --- a/src/se/vidstige/jadb/server/AdbDeviceResponder.java +++ b/src/se/vidstige/jadb/server/AdbDeviceResponder.java @@ -4,6 +4,7 @@ import se.vidstige.jadb.JadbException; import se.vidstige.jadb.RemoteFile; import java.io.ByteArrayOutputStream; +import java.io.IOException; /** * Created by vidstige on 20/03/14. @@ -13,4 +14,5 @@ public interface AdbDeviceResponder { String getType(); void filePushed(RemoteFile path, int mode, ByteArrayOutputStream buffer) throws JadbException; + void filePulled(RemoteFile path, ByteArrayOutputStream buffer) throws JadbException, IOException; } diff --git a/src/se/vidstige/jadb/server/AdbProtocolHandler.java b/src/se/vidstige/jadb/server/AdbProtocolHandler.java index 96522ea..cf89344 100644 --- a/src/se/vidstige/jadb/server/AdbProtocolHandler.java +++ b/src/se/vidstige/jadb/server/AdbProtocolHandler.java @@ -136,6 +136,14 @@ public class AdbProtocolHandler implements Runnable { selected.filePushed(new RemoteFile(path), mode, buffer); transport.sendStatus("OKAY", 0); // 0 = ignored } + else if ("RECV".equals(id)) { + String remotePath = readString(input, length); + SyncTransport transport = new SyncTransport(output, input); + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + selected.filePulled(new RemoteFile(remotePath), buffer); + transport.sendStream(new ByteArrayInputStream(buffer.toByteArray())); + transport.sendStatus("DONE", 0); // ignored + } else throw new JadbException("Unknown sync id " + id); } diff --git a/test/se/vidstige/jadb/test/MockedTestCases.java b/test/se/vidstige/jadb/test/MockedTestCases.java index 58d44db..1863754 100644 --- a/test/se/vidstige/jadb/test/MockedTestCases.java +++ b/test/se/vidstige/jadb/test/MockedTestCases.java @@ -11,6 +11,8 @@ import se.vidstige.jadb.RemoteFile; import se.vidstige.jadb.test.fakes.FakeAdbServer; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.nio.charset.Charset; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -66,6 +68,16 @@ public class MockedTestCases { device.push(fileContents, parseDate("1981-08-25 13:37"), 0666, new RemoteFile("/remote/path/abc.txt")); } + @Test + public void testPullFile() throws Exception { + server.add("serial-123"); + server.expectPull("serial-123", new RemoteFile("/remote/path/abc.txt")).withContent("foobar"); + JadbDevice device = connection.getDevices().get(0); + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + device.pull(new RemoteFile("/remote/path/abc.txt"), buffer); + Assert.assertArrayEquals("foobar".getBytes(Charset.forName("utf-8")), buffer.toByteArray()); + } + private long parseDate(String date) throws ParseException { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return dateFormat.parse(date).getTime(); diff --git a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java index ab20ac4..5c0969a 100644 --- a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java +++ b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java @@ -70,6 +70,10 @@ public class FakeAdbServer implements AdbResponder { return findBySerial(serial).expectPush(path); } + public ExpectationBuilder expectPull(String serial, RemoteFile path) { + return findBySerial(serial).expectPull(path); + } + @Override public List getDevices() { return new ArrayList(devices); @@ -107,6 +111,20 @@ public class FakeAdbServer implements AdbResponder { new JadbException("Unexpected push to device " + serial + " at " + path); } + @Override + public void filePulled(RemoteFile path, ByteArrayOutputStream buffer) throws JadbException, IOException { + for (FileExpectation fe : expectations) { + if (fe.matches(path)) + { + expectations.remove(fe); + fe.throwIfFail(); + fe.returnFile(buffer); + return; + } + } + new JadbException("Unexpected push to device " + serial + " at " + path); + } + public void verifyExpectations() { org.junit.Assert.assertEquals(0, expectations.size()); } @@ -149,6 +167,10 @@ public class FakeAdbServer implements AdbResponder { public void verifyContent(byte[] content) { org.junit.Assert.assertArrayEquals(this.content, content); } + + public void returnFile(ByteArrayOutputStream buffer) throws IOException { + buffer.write(content); + } } public ExpectationBuilder expectPush(RemoteFile path) { @@ -156,5 +178,12 @@ public class FakeAdbServer implements AdbResponder { expectations.add(expectation); return expectation; } + + public ExpectationBuilder expectPull(RemoteFile path) { + FileExpectation expectation = new FileExpectation(path); + expectations.add(expectation); + return expectation; + } + } }