From f6e7da4f1f49cddb6a97b7d4eb21b8234138ffae Mon Sep 17 00:00:00 2001 From: Art Date: Mon, 20 Mar 2017 13:18:56 +0300 Subject: [PATCH] #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()); } }