#58 connect command implemented and it`s test

This commit is contained in:
Art 2017-03-20 13:18:56 +03:00
parent ebbceb99d5
commit f6e7da4f1f
6 changed files with 112 additions and 33 deletions

6
.gitignore vendored
View File

@ -4,12 +4,8 @@
################
# IntelliJ #
################
/.idea/workspace.xml
/.idea/tasks.xml
/.idea/libraries
/.idea/*
jadb.iml
/.idea/compiler.xml
/.idea/uiDesigner.xml
#############

15
pom.xml
View File

@ -30,6 +30,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<mockito-core.version>2.7.17</mockito-core.version>
</properties>
<dependencies>
@ -40,11 +41,11 @@
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
</dependency>
</dependencies>
@ -121,7 +122,7 @@
<include>**/*.java</include>
</includes>
<excludes>
<!--Integration tests -->
<!--Exclude integration tests -->
<exclude>se.vidstige.jadb.test.integration.*</exclude>
<!--Mocks and data-->
<exclude>**/data/*</exclude>
@ -138,8 +139,8 @@
<include>**/*.java</include>
</includes>
<excludes>
<!--Integration tests -->
<exclude>se.vidstige.jadb.test.unit.*</exclude>
<!--Include only integration tests -->
<include>se.vidstige.jadb.test.integration.*</include>
<!--Mocks and data-->
<exclude>**/data/*</exclude>
<exclude>**/fakes/*</exclude>

View File

@ -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());

View File

@ -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<JadbDevice> getDevices() throws IOException, JadbException {
Transport devices = createTransport();
devices.send("host:devices");

View File

@ -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");
}
}

View File

@ -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<JadbDevice> devices = jadb.getDevices();
assertNotNull(devices);
assertFalse(devices.isEmpty());
}
}