From 5c1561bf944f0e344ccd3a95884cdbbfe3219985 Mon Sep 17 00:00:00 2001 From: Art Date: Fri, 17 Mar 2017 19:46:14 +0300 Subject: [PATCH 1/7] #58 .gitignore changes --- .gitignore | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index f9917b3..db430f3 100644 --- a/.gitignore +++ b/.gitignore @@ -6,13 +6,15 @@ ################ /.idea/workspace.xml /.idea/tasks.xml +/.idea/libraries +jadb.iml +/.idea/compiler.xml +/.idea/uiDesigner.xml ############# # Maven # ############# -.idea/workspace.xml -.idea/libraries out/ target/ From ebbceb99d54aaa5b0f9f52a912800d3f50b3bffb Mon Sep 17 00:00:00 2001 From: Art Date: Fri, 17 Mar 2017 19:48:13 +0300 Subject: [PATCH 2/7] #58 skeleton implemented --- .idea/compiler.xml | 32 --------- .idea/misc.xml | 17 ----- jadb.iml | 17 ----- pom.xml | 7 ++ .../ConnectionToRemoteDeviceException.java | 7 ++ .../jadb/HostConnectToRemoteTcpDevice.java | 65 +++++++++++++++++++ src/se/vidstige/jadb/JadbConnection.java | 12 ++++ .../jadb/entities/TcpAddressEntity.java | 42 ++++++++++++ .../HostConnectToRemoteTcpDeviceTest.java | 20 ++++++ .../test/integration/RealDeviceTestCases.java | 7 ++ 10 files changed, 160 insertions(+), 66 deletions(-) delete mode 100644 .idea/compiler.xml delete mode 100644 .idea/misc.xml delete mode 100644 jadb.iml create mode 100644 src/se/vidstige/jadb/ConnectionToRemoteDeviceException.java create mode 100644 src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java create mode 100644 src/se/vidstige/jadb/entities/TcpAddressEntity.java create mode 100644 test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index f329bae..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index b6da4f3..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/jadb.iml b/jadb.iml deleted file mode 100644 index c419de5..0000000 --- a/jadb.iml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1490413..31fb6ce 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,13 @@ 4.10 test + + + junit + junit + 4.10 + test + diff --git a/src/se/vidstige/jadb/ConnectionToRemoteDeviceException.java b/src/se/vidstige/jadb/ConnectionToRemoteDeviceException.java new file mode 100644 index 0000000..f4719bb --- /dev/null +++ b/src/se/vidstige/jadb/ConnectionToRemoteDeviceException.java @@ -0,0 +1,7 @@ +package se.vidstige.jadb; + +public class ConnectionToRemoteDeviceException extends Exception { + public ConnectionToRemoteDeviceException(String message) { + super(message); + } +} diff --git a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java new file mode 100644 index 0000000..6b6b1a5 --- /dev/null +++ b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java @@ -0,0 +1,65 @@ +package se.vidstige.jadb; + +import se.vidstige.jadb.entities.TcpAddressEntity; + +import java.io.IOException; + +public class HostConnectToRemoteTcpDevice { + private final Transport transport; + + public HostConnectToRemoteTcpDevice(Transport transport) { + this.transport = transport; + } + + public TcpAddressEntity connect(TcpAddressEntity tcpAddressEntity) + throws IOException, JadbException, ConnectionToRemoteDeviceException { + transport.send(String.format("host:connect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort())); + verifyProtocolLevel(); + verifyCommandLevel(); + + return tcpAddressEntity; + } + + private void verifyProtocolLevel() throws IOException, JadbException { + transport.verifyResponse(); + } + + private void verifyCommandLevel() throws IOException, ConnectionToRemoteDeviceException { + String status = transport.readString(); + new ResponseValidator(status).validate(); + } + + final static class ResponseValidator { + private final static String SUCCESSFULLY_CONNECTED = "connected to"; + private final static String ALREADY_CONNECTED = "already connected to"; + + private final String response; + + public ResponseValidator(String response) { + this.response = response; + } + + public void validate() throws ConnectionToRemoteDeviceException { + if(!checkIfConnectedSuccessfully() && !checkIfAlreadyConnected()) { + throw new ConnectionToRemoteDeviceException(extractError()); + } + } + + private boolean checkIfConnectedSuccessfully() { + return response.contains(SUCCESSFULLY_CONNECTED); + } + + private boolean checkIfAlreadyConnected() { + return response.equals(ALREADY_CONNECTED); + } + + private String extractError() { + int lastColon = response.lastIndexOf(":"); + if(lastColon != -1) { + return response.substring(lastColon, response.length()); + } else { + return response; + } + } + } +} diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 7a76ec1..2956b72 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -1,5 +1,7 @@ package se.vidstige.jadb; +import se.vidstige.jadb.entities.TcpAddressEntity; + import java.io.IOException; import java.net.Socket; import java.util.ArrayList; @@ -34,6 +36,16 @@ public class JadbConnection implements ITransportFactory { return version; } + public TcpAddressEntity connectToTcpDevice(TcpAddressEntity tcpAddressEntity) + throws IOException, JadbException, ConnectionToRemoteDeviceException { + Transport transport = createTransport(); + try { + return new HostConnectToRemoteTcpDevice(transport).connect(tcpAddressEntity); + } finally { + transport.close(); + } + } + public List getDevices() throws IOException, JadbException { Transport devices = createTransport(); devices.send("host:devices"); diff --git a/src/se/vidstige/jadb/entities/TcpAddressEntity.java b/src/se/vidstige/jadb/entities/TcpAddressEntity.java new file mode 100644 index 0000000..3979ce2 --- /dev/null +++ b/src/se/vidstige/jadb/entities/TcpAddressEntity.java @@ -0,0 +1,42 @@ +package se.vidstige.jadb.entities; + + +/** + * Tcp address + */ +public final class TcpAddressEntity { + private final String host; + private final Integer port; + + public TcpAddressEntity(String host, Integer port) { + this.host = host; + this.port = port; + } + + public String getHost() { + return host; + } + + public Integer getPort() { + return port; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TcpAddressEntity)) return false; + + TcpAddressEntity that = (TcpAddressEntity) o; + + if (!host.equals(that.host)) return false; + return port.equals(that.port); + } + + @Override + public int hashCode() { + int result = host.hashCode(); + result = 31 * result + port.hashCode(); + return result; + } +} + diff --git a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java new file mode 100644 index 0000000..62e93e8 --- /dev/null +++ b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java @@ -0,0 +1,20 @@ +package se.vidstige.jadb; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class HostConnectToRemoteTcpDeviceTest { + @Test + public void testNormalConnection() { + //mock() + + //HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(); + } + + + @Test + public void parserTest() { + + } +} \ No newline at end of file diff --git a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java index be6120c..f3bdcff 100644 --- a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java +++ b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java @@ -7,6 +7,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import se.vidstige.jadb.*; +import se.vidstige.jadb.entities.TcpAddressEntity; import java.io.ByteArrayOutputStream; import java.io.File; @@ -117,4 +118,10 @@ public class RealDeviceTestCases { if (outputStream != null) outputStream.close(); } } + + + @Test + public void testConnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException { + TcpAddressEntity tcpAddressEntity = jadb.connectToTcpDevice(new TcpAddressEntity("127.0.0.1", 10001)); + } } From f6e7da4f1f49cddb6a97b7d4eb21b8234138ffae Mon Sep 17 00:00:00 2001 From: Art Date: Mon, 20 Mar 2017 13:18:56 +0300 Subject: [PATCH 3/7] #58 connect command implemented and it`s test --- .gitignore | 6 +- pom.xml | 15 ++--- .../jadb/HostConnectToRemoteTcpDevice.java | 43 ++++++++----- src/se/vidstige/jadb/JadbConnection.java | 10 +++ .../HostConnectToRemoteTcpDeviceTest.java | 63 +++++++++++++++++-- .../test/integration/RealDeviceTestCases.java | 8 +++ 6 files changed, 112 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index db430f3..f972353 100644 --- a/.gitignore +++ b/.gitignore @@ -4,12 +4,8 @@ ################ # IntelliJ # ################ -/.idea/workspace.xml -/.idea/tasks.xml -/.idea/libraries +/.idea/* jadb.iml -/.idea/compiler.xml -/.idea/uiDesigner.xml ############# diff --git a/pom.xml b/pom.xml index 31fb6ce..30d7a96 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,7 @@ UTF-8 1.7 1.7 + 2.7.17 @@ -40,11 +41,11 @@ test + - junit - junit - 4.10 - test + org.mockito + mockito-core + ${mockito-core.version} @@ -121,7 +122,7 @@ **/*.java - + se.vidstige.jadb.test.integration.* **/data/* @@ -138,8 +139,8 @@ **/*.java - - se.vidstige.jadb.test.unit.* + + se.vidstige.jadb.test.integration.* **/data/* **/fakes/* diff --git a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java index 6b6b1a5..fd740f5 100644 --- a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java +++ b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java @@ -6,54 +6,65 @@ import java.io.IOException; public class HostConnectToRemoteTcpDevice { private final Transport transport; + private final ResponseValidator responseValidator; public HostConnectToRemoteTcpDevice(Transport transport) { this.transport = transport; + this.responseValidator = new ResponseValidatorImp(); + } + + //Visible for testing + HostConnectToRemoteTcpDevice(Transport transport, ResponseValidator responseValidator) { + this.transport = transport; + this.responseValidator = responseValidator; } public TcpAddressEntity connect(TcpAddressEntity tcpAddressEntity) throws IOException, JadbException, ConnectionToRemoteDeviceException { transport.send(String.format("host:connect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort())); + verifyTransportLevel(); verifyProtocolLevel(); - verifyCommandLevel(); return tcpAddressEntity; } - private void verifyProtocolLevel() throws IOException, JadbException { + private void verifyTransportLevel() throws IOException, JadbException { transport.verifyResponse(); } - private void verifyCommandLevel() throws IOException, ConnectionToRemoteDeviceException { + private void verifyProtocolLevel() throws IOException, ConnectionToRemoteDeviceException { String status = transport.readString(); - new ResponseValidator(status).validate(); + responseValidator.validate(status); } - final static class ResponseValidator { + //@VisibleForTesting + interface ResponseValidator { + void validate(String response) throws ConnectionToRemoteDeviceException; + } + + final static class ResponseValidatorImp implements ResponseValidator { private final static String SUCCESSFULLY_CONNECTED = "connected to"; private final static String ALREADY_CONNECTED = "already connected to"; - private final String response; - public ResponseValidator(String response) { - this.response = response; + public ResponseValidatorImp() { } - public void validate() throws ConnectionToRemoteDeviceException { - if(!checkIfConnectedSuccessfully() && !checkIfAlreadyConnected()) { - throw new ConnectionToRemoteDeviceException(extractError()); + public void validate(String response) throws ConnectionToRemoteDeviceException { + if(!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) { + throw new ConnectionToRemoteDeviceException(extractError(response)); } } - private boolean checkIfConnectedSuccessfully() { - return response.contains(SUCCESSFULLY_CONNECTED); + private boolean checkIfConnectedSuccessfully(String response) { + return response.startsWith(SUCCESSFULLY_CONNECTED); } - private boolean checkIfAlreadyConnected() { - return response.equals(ALREADY_CONNECTED); + private boolean checkIfAlreadyConnected(String response) { + return response.startsWith(ALREADY_CONNECTED); } - private String extractError() { + private String extractError(String response) { int lastColon = response.lastIndexOf(":"); if(lastColon != -1) { return response.substring(lastColon, response.length()); diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 2956b72..229058c 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -46,6 +46,16 @@ public class JadbConnection implements ITransportFactory { } } + public TcpAddressEntity connectFromTcpDevice(TcpAddressEntity tcpAddressEntity) + throws IOException, JadbException, ConnectionToRemoteDeviceException { + Transport transport = createTransport(); + try { + return new HostConnectToRemoteTcpDevice(transport).connect(tcpAddressEntity); + } finally { + transport.close(); + } + } + public List getDevices() throws IOException, JadbException { Transport devices = createTransport(); devices.send("host:devices"); diff --git a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java index 62e93e8..9ef25be 100644 --- a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java +++ b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java @@ -1,20 +1,73 @@ package se.vidstige.jadb; import org.junit.Test; +import se.vidstige.jadb.entities.TcpAddressEntity; + +import java.io.IOException; import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class HostConnectToRemoteTcpDeviceTest { - @Test - public void testNormalConnection() { - //mock() - //HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(); + @Test + public void testNormalConnection() throws ConnectionToRemoteDeviceException, IOException, JadbException { + //Prepare + Transport transport = mock(Transport.class); + when(transport.readString()).thenReturn("connected to host:1"); + + TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + + //Do + HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport); + TcpAddressEntity resultTcpAddressEntity = hostConnectToRemoteTcpDevice.connect(tcpAddressEntity); + + //Validate + assertEquals(resultTcpAddressEntity, tcpAddressEntity); } + @Test(expected = JadbException.class) + public void testTransportLevelException() throws ConnectionToRemoteDeviceException, IOException, JadbException { + //Prepare + Transport transport = mock(Transport.class); + TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + doThrow(new JadbException("Fake exception")).when(transport).verifyResponse(); + + //Do + HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport); + hostConnectToRemoteTcpDevice.connect(tcpAddressEntity); + } + + @Test(expected = ConnectionToRemoteDeviceException.class) + public void testProtocolException() throws ConnectionToRemoteDeviceException, IOException, JadbException { + //Prepare + Transport transport = mock(Transport.class); + when(transport.readString()).thenReturn("connected to host:1"); + HostConnectToRemoteTcpDevice.ResponseValidator responseValidator = mock(HostConnectToRemoteTcpDevice.ResponseValidator.class); + doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString()); + + TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + + //Do + HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport, responseValidator); + hostConnectToRemoteTcpDevice.connect(tcpAddressEntity); + } @Test - public void parserTest() { + public void testProtocolResponseValidatorSuccessfullyConnected() throws ConnectionToRemoteDeviceException, IOException, JadbException { + new HostConnectToRemoteTcpDevice.ResponseValidatorImp().validate("connected to host:1"); + } + @Test + public void testProtocolResponseValidatorAlreadyConnected() throws ConnectionToRemoteDeviceException, IOException, JadbException { + new HostConnectToRemoteTcpDevice.ResponseValidatorImp().validate("already connected to host:1"); + } + + @Test(expected = ConnectionToRemoteDeviceException.class) + public void testProtocolResponseValidatorErrorInValidate() throws ConnectionToRemoteDeviceException, IOException, JadbException { + new HostConnectToRemoteTcpDevice.ResponseValidatorImp().validate("some error occurred"); } } \ No newline at end of file diff --git a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java index f3bdcff..23847fc 100644 --- a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java +++ b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java @@ -16,6 +16,10 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + public class RealDeviceTestCases { private JadbConnection jadb; @@ -123,5 +127,9 @@ public class RealDeviceTestCases { @Test public void testConnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException { TcpAddressEntity tcpAddressEntity = jadb.connectToTcpDevice(new TcpAddressEntity("127.0.0.1", 10001)); + List devices = jadb.getDevices(); + + assertNotNull(devices); + assertFalse(devices.isEmpty()); } } From 0a12f784caa29af346f41b89d7f871a03f67d12a Mon Sep 17 00:00:00 2001 From: Art Date: Mon, 20 Mar 2017 13:48:24 +0300 Subject: [PATCH 4/7] #58 implemented disconnect command --- .../jadb/HostConnectToRemoteTcpDevice.java | 8 +- .../HostDisconnectFromRemoteTcpDevice.java | 76 +++++++++++++++++++ src/se/vidstige/jadb/JadbConnection.java | 4 +- .../HostConnectToRemoteTcpDeviceTest.java | 3 +- ...HostDisconnectFromRemoteTcpDeviceTest.java | 74 ++++++++++++++++++ .../test/integration/RealDeviceTestCases.java | 30 +++++++- 6 files changed, 186 insertions(+), 9 deletions(-) create mode 100644 src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java create mode 100644 test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java diff --git a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java index fd740f5..68271d4 100644 --- a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java +++ b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java @@ -4,11 +4,11 @@ import se.vidstige.jadb.entities.TcpAddressEntity; import java.io.IOException; -public class HostConnectToRemoteTcpDevice { +class HostConnectToRemoteTcpDevice { private final Transport transport; private final ResponseValidator responseValidator; - public HostConnectToRemoteTcpDevice(Transport transport) { + HostConnectToRemoteTcpDevice(Transport transport) { this.transport = transport; this.responseValidator = new ResponseValidatorImp(); } @@ -19,7 +19,7 @@ public class HostConnectToRemoteTcpDevice { this.responseValidator = responseValidator; } - public TcpAddressEntity connect(TcpAddressEntity tcpAddressEntity) + TcpAddressEntity connect(TcpAddressEntity tcpAddressEntity) throws IOException, JadbException, ConnectionToRemoteDeviceException { transport.send(String.format("host:connect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort())); verifyTransportLevel(); @@ -47,7 +47,7 @@ public class HostConnectToRemoteTcpDevice { private final static String ALREADY_CONNECTED = "already connected to"; - public ResponseValidatorImp() { + ResponseValidatorImp() { } public void validate(String response) throws ConnectionToRemoteDeviceException { diff --git a/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java b/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java new file mode 100644 index 0000000..2130e31 --- /dev/null +++ b/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java @@ -0,0 +1,76 @@ +package se.vidstige.jadb; + +import se.vidstige.jadb.entities.TcpAddressEntity; + +import java.io.IOException; + +public class HostDisconnectFromRemoteTcpDevice { + private final Transport transport; + private final ResponseValidator responseValidator; + + public HostDisconnectFromRemoteTcpDevice(Transport transport) { + this.transport = transport; + this.responseValidator = new ResponseValidatorImp(); + } + + //Visible for testing + HostDisconnectFromRemoteTcpDevice(Transport transport, ResponseValidator responseValidator) { + this.transport = transport; + this.responseValidator = responseValidator; + } + + public TcpAddressEntity disconnect(TcpAddressEntity tcpAddressEntity) + throws IOException, JadbException, ConnectionToRemoteDeviceException { + transport.send(String.format("host:disconnect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort())); + verifyTransportLevel(); + verifyProtocolLevel(); + + return tcpAddressEntity; + } + + private void verifyTransportLevel() throws IOException, JadbException { + transport.verifyResponse(); + } + + private void verifyProtocolLevel() throws IOException, ConnectionToRemoteDeviceException { + String status = transport.readString(); + responseValidator.validate(status); + } + + //@VisibleForTesting + interface ResponseValidator { + void validate(String response) throws ConnectionToRemoteDeviceException; + } + + final static class ResponseValidatorImp implements ResponseValidator { + private final static String SUCCESSFULLY_DISCONNECTED = "disconnected"; + private final static String ALREADY_DISCONNECTED = "error: no such device"; + + + public ResponseValidatorImp() { + } + + public void validate(String response) throws ConnectionToRemoteDeviceException { + if(!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) { + throw new ConnectionToRemoteDeviceException(extractError(response)); + } + } + + private boolean checkIfConnectedSuccessfully(String response) { + return response.startsWith(SUCCESSFULLY_DISCONNECTED); + } + + private boolean checkIfAlreadyConnected(String response) { + return response.startsWith(ALREADY_DISCONNECTED); + } + + private String extractError(String response) { + int lastColon = response.lastIndexOf(":"); + if(lastColon != -1) { + return response.substring(lastColon, response.length()); + } else { + return response; + } + } + } +} diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 229058c..7a3126e 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -46,11 +46,11 @@ public class JadbConnection implements ITransportFactory { } } - public TcpAddressEntity connectFromTcpDevice(TcpAddressEntity tcpAddressEntity) + public TcpAddressEntity disconnectFromTcpDevice(TcpAddressEntity tcpAddressEntity) throws IOException, JadbException, ConnectionToRemoteDeviceException { Transport transport = createTransport(); try { - return new HostConnectToRemoteTcpDevice(transport).connect(tcpAddressEntity); + return new HostDisconnectFromRemoteTcpDevice(transport).disconnect(tcpAddressEntity); } finally { transport.close(); } diff --git a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java index 9ef25be..896200a 100644 --- a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java +++ b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java @@ -33,9 +33,10 @@ public class HostConnectToRemoteTcpDeviceTest { public void testTransportLevelException() throws ConnectionToRemoteDeviceException, IOException, JadbException { //Prepare Transport transport = mock(Transport.class); - TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); doThrow(new JadbException("Fake exception")).when(transport).verifyResponse(); + TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + //Do HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport); hostConnectToRemoteTcpDevice.connect(tcpAddressEntity); diff --git a/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java b/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java new file mode 100644 index 0000000..92b825b --- /dev/null +++ b/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java @@ -0,0 +1,74 @@ +package se.vidstige.jadb; + +import org.junit.Test; +import se.vidstige.jadb.entities.TcpAddressEntity; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class HostDisconnectFromRemoteTcpDeviceTest { + + @Test + public void testNormalConnection() throws ConnectionToRemoteDeviceException, IOException, JadbException { + //Prepare + Transport transport = mock(Transport.class); + when(transport.readString()).thenReturn("disconnected host:1"); + + TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + + //Do + HostDisconnectFromRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostDisconnectFromRemoteTcpDevice(transport); + TcpAddressEntity resultTcpAddressEntity = hostConnectToRemoteTcpDevice.disconnect(tcpAddressEntity); + + //Validate + assertEquals(resultTcpAddressEntity, tcpAddressEntity); + } + + @Test(expected = JadbException.class) + public void testTransportLevelException() throws ConnectionToRemoteDeviceException, IOException, JadbException { + //Prepare + Transport transport = mock(Transport.class); + doThrow(new JadbException("Fake exception")).when(transport).verifyResponse(); + + TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + + //Do + HostDisconnectFromRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostDisconnectFromRemoteTcpDevice(transport); + hostConnectToRemoteTcpDevice.disconnect(tcpAddressEntity); + } + + @Test(expected = ConnectionToRemoteDeviceException.class) + public void testProtocolException() throws ConnectionToRemoteDeviceException, IOException, JadbException { + //Prepare + Transport transport = mock(Transport.class); + when(transport.readString()).thenReturn("any string"); + HostConnectToRemoteTcpDevice.ResponseValidator responseValidator = mock(HostConnectToRemoteTcpDevice.ResponseValidator.class); + doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString()); + + TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + + //Do + HostDisconnectFromRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostDisconnectFromRemoteTcpDevice(transport); + hostConnectToRemoteTcpDevice.disconnect(tcpAddressEntity); + } + + @Test + public void testProtocolResponseValidatorSuccessfullyConnected() throws ConnectionToRemoteDeviceException, IOException, JadbException { + new HostDisconnectFromRemoteTcpDevice.ResponseValidatorImp().validate("disconnected 127.0.0.1:10001"); + } + + @Test + public void testProtocolResponseValidatorAlreadyConnected() throws ConnectionToRemoteDeviceException, IOException, JadbException { + new HostDisconnectFromRemoteTcpDevice.ResponseValidatorImp().validate("error: no such device '127.0.0.1:10001'"); + } + + @Test(expected = ConnectionToRemoteDeviceException.class) + public void testProtocolResponseValidatorErrorInValidate() throws ConnectionToRemoteDeviceException, IOException, JadbException { + new HostDisconnectFromRemoteTcpDevice.ResponseValidatorImp().validate("some error occurred"); + } +} \ No newline at end of file diff --git a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java index 23847fc..ebe0a44 100644 --- a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java +++ b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java @@ -123,13 +123,39 @@ public class RealDeviceTestCases { } } - + /** + * This test requires emulator running on non-standard tcp port - this may be achieve by executing such command: + * ${ANDROID_HOME}/emulator -verbose -avd ${NAME} -ports 10000,10001 + * + * @throws IOException + * @throws JadbException + * @throws ConnectionToRemoteDeviceException + */ @Test public void testConnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException { - TcpAddressEntity tcpAddressEntity = jadb.connectToTcpDevice(new TcpAddressEntity("127.0.0.1", 10001)); + jadb.connectToTcpDevice(new TcpAddressEntity("127.0.0.1", 10001)); List devices = jadb.getDevices(); assertNotNull(devices); assertFalse(devices.isEmpty()); } + + /** + * @see #testConnectionToTcpDevice() + * + * @throws IOException + * @throws JadbException + * @throws ConnectionToRemoteDeviceException + */ + @Test + public void testDisconnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException { + testConnectionToTcpDevice(); + + jadb.disconnectFromTcpDevice(new TcpAddressEntity("127.0.0.1", 10001)); + jadb.getDevices(); + + List devices = jadb.getDevices(); + assertNotNull(devices); + assertTrue(devices.isEmpty()); + } } From 64544b5bff859964c1dad819049effca87eb26a0 Mon Sep 17 00:00:00 2001 From: Art Date: Mon, 20 Mar 2017 14:48:09 +0300 Subject: [PATCH 5/7] #58 fixes in spaces and newline at the end of new files. Fixed typo in test --- .../jadb/HostConnectToRemoteTcpDevice.java | 8 ++++---- .../jadb/HostDisconnectFromRemoteTcpDevice.java | 14 +++++++------- .../HostDisconnectFromRemoteTcpDeviceTest.java | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java index 68271d4..d350a0e 100644 --- a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java +++ b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java @@ -51,22 +51,22 @@ class HostConnectToRemoteTcpDevice { } public void validate(String response) throws ConnectionToRemoteDeviceException { - if(!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) { + if (!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) { throw new ConnectionToRemoteDeviceException(extractError(response)); } } - private boolean checkIfConnectedSuccessfully(String response) { + private boolean checkIfConnectedSuccessfully(String response) { return response.startsWith(SUCCESSFULLY_CONNECTED); } - private boolean checkIfAlreadyConnected(String response) { + private boolean checkIfAlreadyConnected(String response) { return response.startsWith(ALREADY_CONNECTED); } private String extractError(String response) { int lastColon = response.lastIndexOf(":"); - if(lastColon != -1) { + if (lastColon != -1) { return response.substring(lastColon, response.length()); } else { return response; diff --git a/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java b/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java index 2130e31..69ca88f 100644 --- a/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java +++ b/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java @@ -8,7 +8,7 @@ public class HostDisconnectFromRemoteTcpDevice { private final Transport transport; private final ResponseValidator responseValidator; - public HostDisconnectFromRemoteTcpDevice(Transport transport) { + HostDisconnectFromRemoteTcpDevice(Transport transport) { this.transport = transport; this.responseValidator = new ResponseValidatorImp(); } @@ -19,7 +19,7 @@ public class HostDisconnectFromRemoteTcpDevice { this.responseValidator = responseValidator; } - public TcpAddressEntity disconnect(TcpAddressEntity tcpAddressEntity) + TcpAddressEntity disconnect(TcpAddressEntity tcpAddressEntity) throws IOException, JadbException, ConnectionToRemoteDeviceException { transport.send(String.format("host:disconnect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort())); verifyTransportLevel(); @@ -47,26 +47,26 @@ public class HostDisconnectFromRemoteTcpDevice { private final static String ALREADY_DISCONNECTED = "error: no such device"; - public ResponseValidatorImp() { + ResponseValidatorImp() { } public void validate(String response) throws ConnectionToRemoteDeviceException { - if(!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) { + if (!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) { throw new ConnectionToRemoteDeviceException(extractError(response)); } } - private boolean checkIfConnectedSuccessfully(String response) { + private boolean checkIfConnectedSuccessfully(String response) { return response.startsWith(SUCCESSFULLY_DISCONNECTED); } - private boolean checkIfAlreadyConnected(String response) { + private boolean checkIfAlreadyConnected(String response) { return response.startsWith(ALREADY_DISCONNECTED); } private String extractError(String response) { int lastColon = response.lastIndexOf(":"); - if(lastColon != -1) { + if (lastColon != -1) { return response.substring(lastColon, response.length()); } else { return response; diff --git a/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java b/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java index 92b825b..93adebc 100644 --- a/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java +++ b/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java @@ -47,19 +47,19 @@ public class HostDisconnectFromRemoteTcpDeviceTest { //Prepare Transport transport = mock(Transport.class); when(transport.readString()).thenReturn("any string"); - HostConnectToRemoteTcpDevice.ResponseValidator responseValidator = mock(HostConnectToRemoteTcpDevice.ResponseValidator.class); + HostDisconnectFromRemoteTcpDevice.ResponseValidator responseValidator = mock(HostDisconnectFromRemoteTcpDevice.ResponseValidator.class); doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString()); TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); //Do - HostDisconnectFromRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostDisconnectFromRemoteTcpDevice(transport); + HostDisconnectFromRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostDisconnectFromRemoteTcpDevice(transport, responseValidator); hostConnectToRemoteTcpDevice.disconnect(tcpAddressEntity); } @Test public void testProtocolResponseValidatorSuccessfullyConnected() throws ConnectionToRemoteDeviceException, IOException, JadbException { - new HostDisconnectFromRemoteTcpDevice.ResponseValidatorImp().validate("disconnected 127.0.0.1:10001"); + new HostDisconnectFromRemoteTcpDevice.ResponseValidatorImp().validate("disconnected 127.0.0.1:10001"); } @Test @@ -71,4 +71,4 @@ public class HostDisconnectFromRemoteTcpDeviceTest { public void testProtocolResponseValidatorErrorInValidate() throws ConnectionToRemoteDeviceException, IOException, JadbException { new HostDisconnectFromRemoteTcpDevice.ResponseValidatorImp().validate("some error occurred"); } -} \ No newline at end of file +} From 444266388956cfd7e41c9bca39fba4268b129406 Mon Sep 17 00:00:00 2001 From: Art Date: Mon, 20 Mar 2017 15:17:38 +0300 Subject: [PATCH 6/7] #58 migration to InetSocketAddress --- .../jadb/HostConnectToRemoteTcpDevice.java | 9 ++-- .../HostDisconnectFromRemoteTcpDevice.java | 9 ++-- src/se/vidstige/jadb/JadbConnection.java | 8 ++-- .../jadb/entities/TcpAddressEntity.java | 42 ------------------- .../HostConnectToRemoteTcpDeviceTest.java | 12 +++--- ...HostDisconnectFromRemoteTcpDeviceTest.java | 16 +++---- .../test/integration/RealDeviceTestCases.java | 6 +-- 7 files changed, 29 insertions(+), 73 deletions(-) delete mode 100644 src/se/vidstige/jadb/entities/TcpAddressEntity.java diff --git a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java index d350a0e..2b1f300 100644 --- a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java +++ b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java @@ -1,8 +1,7 @@ package se.vidstige.jadb; -import se.vidstige.jadb.entities.TcpAddressEntity; - import java.io.IOException; +import java.net.InetSocketAddress; class HostConnectToRemoteTcpDevice { private final Transport transport; @@ -19,13 +18,13 @@ class HostConnectToRemoteTcpDevice { this.responseValidator = responseValidator; } - TcpAddressEntity connect(TcpAddressEntity tcpAddressEntity) + InetSocketAddress connect(InetSocketAddress inetSocketAddress) throws IOException, JadbException, ConnectionToRemoteDeviceException { - transport.send(String.format("host:connect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort())); + transport.send(String.format("host:connect:%s:%d", inetSocketAddress.getHostString(), inetSocketAddress.getPort())); verifyTransportLevel(); verifyProtocolLevel(); - return tcpAddressEntity; + return inetSocketAddress; } private void verifyTransportLevel() throws IOException, JadbException { diff --git a/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java b/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java index 69ca88f..1c9aead 100644 --- a/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java +++ b/src/se/vidstige/jadb/HostDisconnectFromRemoteTcpDevice.java @@ -1,8 +1,7 @@ package se.vidstige.jadb; -import se.vidstige.jadb.entities.TcpAddressEntity; - import java.io.IOException; +import java.net.InetSocketAddress; public class HostDisconnectFromRemoteTcpDevice { private final Transport transport; @@ -19,13 +18,13 @@ public class HostDisconnectFromRemoteTcpDevice { this.responseValidator = responseValidator; } - TcpAddressEntity disconnect(TcpAddressEntity tcpAddressEntity) + InetSocketAddress disconnect(InetSocketAddress inetSocketAddress) throws IOException, JadbException, ConnectionToRemoteDeviceException { - transport.send(String.format("host:disconnect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort())); + transport.send(String.format("host:disconnect:%s:%d", inetSocketAddress.getHostString(), inetSocketAddress.getPort())); verifyTransportLevel(); verifyProtocolLevel(); - return tcpAddressEntity; + return inetSocketAddress; } private void verifyTransportLevel() throws IOException, JadbException { diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 7a3126e..2eee46d 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -1,8 +1,8 @@ package se.vidstige.jadb; -import se.vidstige.jadb.entities.TcpAddressEntity; import java.io.IOException; +import java.net.InetSocketAddress; import java.net.Socket; import java.util.ArrayList; import java.util.List; @@ -36,17 +36,17 @@ public class JadbConnection implements ITransportFactory { return version; } - public TcpAddressEntity connectToTcpDevice(TcpAddressEntity tcpAddressEntity) + public InetSocketAddress connectToTcpDevice(InetSocketAddress inetSocketAddress) throws IOException, JadbException, ConnectionToRemoteDeviceException { Transport transport = createTransport(); try { - return new HostConnectToRemoteTcpDevice(transport).connect(tcpAddressEntity); + return new HostConnectToRemoteTcpDevice(transport).connect(inetSocketAddress); } finally { transport.close(); } } - public TcpAddressEntity disconnectFromTcpDevice(TcpAddressEntity tcpAddressEntity) + public InetSocketAddress disconnectFromTcpDevice(InetSocketAddress tcpAddressEntity) throws IOException, JadbException, ConnectionToRemoteDeviceException { Transport transport = createTransport(); try { diff --git a/src/se/vidstige/jadb/entities/TcpAddressEntity.java b/src/se/vidstige/jadb/entities/TcpAddressEntity.java deleted file mode 100644 index 3979ce2..0000000 --- a/src/se/vidstige/jadb/entities/TcpAddressEntity.java +++ /dev/null @@ -1,42 +0,0 @@ -package se.vidstige.jadb.entities; - - -/** - * Tcp address - */ -public final class TcpAddressEntity { - private final String host; - private final Integer port; - - public TcpAddressEntity(String host, Integer port) { - this.host = host; - this.port = port; - } - - public String getHost() { - return host; - } - - public Integer getPort() { - return port; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof TcpAddressEntity)) return false; - - TcpAddressEntity that = (TcpAddressEntity) o; - - if (!host.equals(that.host)) return false; - return port.equals(that.port); - } - - @Override - public int hashCode() { - int result = host.hashCode(); - result = 31 * result + port.hashCode(); - return result; - } -} - diff --git a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java index 896200a..04d85a6 100644 --- a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java +++ b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java @@ -1,9 +1,9 @@ package se.vidstige.jadb; import org.junit.Test; -import se.vidstige.jadb.entities.TcpAddressEntity; import java.io.IOException; +import java.net.InetSocketAddress; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.anyString; @@ -19,14 +19,14 @@ public class HostConnectToRemoteTcpDeviceTest { Transport transport = mock(Transport.class); when(transport.readString()).thenReturn("connected to host:1"); - TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + InetSocketAddress inetSocketAddress = new InetSocketAddress("host", 1); //Do HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport); - TcpAddressEntity resultTcpAddressEntity = hostConnectToRemoteTcpDevice.connect(tcpAddressEntity); + InetSocketAddress resultTcpAddressEntity = hostConnectToRemoteTcpDevice.connect(inetSocketAddress); //Validate - assertEquals(resultTcpAddressEntity, tcpAddressEntity); + assertEquals(resultTcpAddressEntity, inetSocketAddress); } @Test(expected = JadbException.class) @@ -35,7 +35,7 @@ public class HostConnectToRemoteTcpDeviceTest { Transport transport = mock(Transport.class); doThrow(new JadbException("Fake exception")).when(transport).verifyResponse(); - TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + InetSocketAddress tcpAddressEntity = new InetSocketAddress("host", 1); //Do HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport); @@ -50,7 +50,7 @@ public class HostConnectToRemoteTcpDeviceTest { HostConnectToRemoteTcpDevice.ResponseValidator responseValidator = mock(HostConnectToRemoteTcpDevice.ResponseValidator.class); doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString()); - TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + InetSocketAddress tcpAddressEntity = new InetSocketAddress("host", 1); //Do HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport, responseValidator); diff --git a/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java b/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java index 93adebc..86c87d1 100644 --- a/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java +++ b/test/se/vidstige/jadb/HostDisconnectFromRemoteTcpDeviceTest.java @@ -1,9 +1,9 @@ package se.vidstige.jadb; import org.junit.Test; -import se.vidstige.jadb.entities.TcpAddressEntity; import java.io.IOException; +import java.net.InetSocketAddress; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.anyString; @@ -19,14 +19,14 @@ public class HostDisconnectFromRemoteTcpDeviceTest { Transport transport = mock(Transport.class); when(transport.readString()).thenReturn("disconnected host:1"); - TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + InetSocketAddress inetSocketAddress = new InetSocketAddress("host", 1); //Do HostDisconnectFromRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostDisconnectFromRemoteTcpDevice(transport); - TcpAddressEntity resultTcpAddressEntity = hostConnectToRemoteTcpDevice.disconnect(tcpAddressEntity); + InetSocketAddress resultInetSocketAddress = hostConnectToRemoteTcpDevice.disconnect(inetSocketAddress); //Validate - assertEquals(resultTcpAddressEntity, tcpAddressEntity); + assertEquals(inetSocketAddress, resultInetSocketAddress); } @Test(expected = JadbException.class) @@ -35,11 +35,11 @@ public class HostDisconnectFromRemoteTcpDeviceTest { Transport transport = mock(Transport.class); doThrow(new JadbException("Fake exception")).when(transport).verifyResponse(); - TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + InetSocketAddress inetSocketAddress = new InetSocketAddress("host", 1); //Do HostDisconnectFromRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostDisconnectFromRemoteTcpDevice(transport); - hostConnectToRemoteTcpDevice.disconnect(tcpAddressEntity); + hostConnectToRemoteTcpDevice.disconnect(inetSocketAddress); } @Test(expected = ConnectionToRemoteDeviceException.class) @@ -50,11 +50,11 @@ public class HostDisconnectFromRemoteTcpDeviceTest { HostDisconnectFromRemoteTcpDevice.ResponseValidator responseValidator = mock(HostDisconnectFromRemoteTcpDevice.ResponseValidator.class); doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString()); - TcpAddressEntity tcpAddressEntity = new TcpAddressEntity("host", 1); + InetSocketAddress inetSocketAddress = new InetSocketAddress("host", 1); //Do HostDisconnectFromRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostDisconnectFromRemoteTcpDevice(transport, responseValidator); - hostConnectToRemoteTcpDevice.disconnect(tcpAddressEntity); + hostConnectToRemoteTcpDevice.disconnect(inetSocketAddress); } @Test diff --git a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java index ebe0a44..9bb1339 100644 --- a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java +++ b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java @@ -7,13 +7,13 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import se.vidstige.jadb.*; -import se.vidstige.jadb.entities.TcpAddressEntity; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.InetSocketAddress; import java.util.List; import static org.junit.Assert.assertFalse; @@ -133,7 +133,7 @@ public class RealDeviceTestCases { */ @Test public void testConnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException { - jadb.connectToTcpDevice(new TcpAddressEntity("127.0.0.1", 10001)); + jadb.connectToTcpDevice(new InetSocketAddress("127.0.0.1", 10001)); List devices = jadb.getDevices(); assertNotNull(devices); @@ -151,7 +151,7 @@ public class RealDeviceTestCases { public void testDisconnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException { testConnectionToTcpDevice(); - jadb.disconnectFromTcpDevice(new TcpAddressEntity("127.0.0.1", 10001)); + jadb.disconnectFromTcpDevice(new InetSocketAddress("127.0.0.1", 10001)); jadb.getDevices(); List devices = jadb.getDevices(); From b4fa17abffeb41e4c6059866e67fdaa8573ac529 Mon Sep 17 00:00:00 2001 From: Art Date: Mon, 20 Mar 2017 16:03:34 +0300 Subject: [PATCH 7/7] #58 fixes in tests --- .../HostConnectToRemoteTcpDeviceTest.java | 31 ++++++++++++++----- .../jadb/test/fakes/FakeAdbServer.java | 2 +- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java index 04d85a6..b3d5113 100644 --- a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java +++ b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java @@ -1,14 +1,18 @@ package se.vidstige.jadb; import org.junit.Test; +import org.mockito.ArgumentCaptor; import java.io.IOException; import java.net.InetSocketAddress; import static org.junit.Assert.*; +import static org.mockito.ArgumentCaptor.forClass; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class HostConnectToRemoteTcpDeviceTest { @@ -17,9 +21,9 @@ public class HostConnectToRemoteTcpDeviceTest { public void testNormalConnection() throws ConnectionToRemoteDeviceException, IOException, JadbException { //Prepare Transport transport = mock(Transport.class); - when(transport.readString()).thenReturn("connected to host:1"); + when(transport.readString()).thenReturn("connected to somehost:1"); - InetSocketAddress inetSocketAddress = new InetSocketAddress("host", 1); + InetSocketAddress inetSocketAddress = new InetSocketAddress("somehost", 1); //Do HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport); @@ -27,6 +31,10 @@ public class HostConnectToRemoteTcpDeviceTest { //Validate assertEquals(resultTcpAddressEntity, inetSocketAddress); + + ArgumentCaptor argument = forClass(String.class); + verify(transport, times(1)).send(argument.capture()); + assertEquals("host:connect:somehost:1", argument.getValue()); } @Test(expected = JadbException.class) @@ -35,36 +43,45 @@ public class HostConnectToRemoteTcpDeviceTest { Transport transport = mock(Transport.class); doThrow(new JadbException("Fake exception")).when(transport).verifyResponse(); - InetSocketAddress tcpAddressEntity = new InetSocketAddress("host", 1); + InetSocketAddress tcpAddressEntity = new InetSocketAddress("somehost", 1); //Do HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport); hostConnectToRemoteTcpDevice.connect(tcpAddressEntity); + + //Validate + verify(transport, times(1)).send(anyString()); + verify(transport, times(1)).verifyResponse(); } @Test(expected = ConnectionToRemoteDeviceException.class) public void testProtocolException() throws ConnectionToRemoteDeviceException, IOException, JadbException { //Prepare Transport transport = mock(Transport.class); - when(transport.readString()).thenReturn("connected to host:1"); + when(transport.readString()).thenReturn("connected to somehost:1"); HostConnectToRemoteTcpDevice.ResponseValidator responseValidator = mock(HostConnectToRemoteTcpDevice.ResponseValidator.class); doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString()); - InetSocketAddress tcpAddressEntity = new InetSocketAddress("host", 1); + InetSocketAddress tcpAddressEntity = new InetSocketAddress("somehost", 1); //Do HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice(transport, responseValidator); hostConnectToRemoteTcpDevice.connect(tcpAddressEntity); + + //Validate + verify(transport, times(1)).send(anyString()); + verify(transport, times(1)).verifyResponse(); + verify(responseValidator, times(1)).validate(anyString()); } @Test public void testProtocolResponseValidatorSuccessfullyConnected() throws ConnectionToRemoteDeviceException, IOException, JadbException { - new HostConnectToRemoteTcpDevice.ResponseValidatorImp().validate("connected to host:1"); + new HostConnectToRemoteTcpDevice.ResponseValidatorImp().validate("connected to somehost:1"); } @Test public void testProtocolResponseValidatorAlreadyConnected() throws ConnectionToRemoteDeviceException, IOException, JadbException { - new HostConnectToRemoteTcpDevice.ResponseValidatorImp().validate("already connected to host:1"); + new HostConnectToRemoteTcpDevice.ResponseValidatorImp().validate("already connected to somehost:1"); } @Test(expected = ConnectionToRemoteDeviceException.class) diff --git a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java index a82a0dd..05b7547 100644 --- a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java +++ b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java @@ -20,7 +20,7 @@ import java.util.List; */ public class FakeAdbServer implements AdbResponder { private final AdbServer server; - private List devices = new ArrayList(); + private final List devices = new ArrayList(); public FakeAdbServer(int port) { server = new AdbServer(this, port);