mirror of
https://github.com/revanced/jadb.git
synced 2025-02-11 01:26:47 +01:00
#58 connect command implemented and it`s test
This commit is contained in:
parent
ebbceb99d5
commit
f6e7da4f1f
6
.gitignore
vendored
6
.gitignore
vendored
@ -4,12 +4,8 @@
|
|||||||
################
|
################
|
||||||
# IntelliJ #
|
# IntelliJ #
|
||||||
################
|
################
|
||||||
/.idea/workspace.xml
|
/.idea/*
|
||||||
/.idea/tasks.xml
|
|
||||||
/.idea/libraries
|
|
||||||
jadb.iml
|
jadb.iml
|
||||||
/.idea/compiler.xml
|
|
||||||
/.idea/uiDesigner.xml
|
|
||||||
|
|
||||||
|
|
||||||
#############
|
#############
|
||||||
|
15
pom.xml
15
pom.xml
@ -30,6 +30,7 @@
|
|||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<maven.compiler.target>1.7</maven.compiler.target>
|
<maven.compiler.target>1.7</maven.compiler.target>
|
||||||
<maven.compiler.source>1.7</maven.compiler.source>
|
<maven.compiler.source>1.7</maven.compiler.source>
|
||||||
|
<mockito-core.version>2.7.17</mockito-core.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -40,11 +41,11 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
<version>4.10</version>
|
<version>${mockito-core.version}</version>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@ -121,7 +122,7 @@
|
|||||||
<include>**/*.java</include>
|
<include>**/*.java</include>
|
||||||
</includes>
|
</includes>
|
||||||
<excludes>
|
<excludes>
|
||||||
<!--Integration tests -->
|
<!--Exclude integration tests -->
|
||||||
<exclude>se.vidstige.jadb.test.integration.*</exclude>
|
<exclude>se.vidstige.jadb.test.integration.*</exclude>
|
||||||
<!--Mocks and data-->
|
<!--Mocks and data-->
|
||||||
<exclude>**/data/*</exclude>
|
<exclude>**/data/*</exclude>
|
||||||
@ -138,8 +139,8 @@
|
|||||||
<include>**/*.java</include>
|
<include>**/*.java</include>
|
||||||
</includes>
|
</includes>
|
||||||
<excludes>
|
<excludes>
|
||||||
<!--Integration tests -->
|
<!--Include only integration tests -->
|
||||||
<exclude>se.vidstige.jadb.test.unit.*</exclude>
|
<include>se.vidstige.jadb.test.integration.*</include>
|
||||||
<!--Mocks and data-->
|
<!--Mocks and data-->
|
||||||
<exclude>**/data/*</exclude>
|
<exclude>**/data/*</exclude>
|
||||||
<exclude>**/fakes/*</exclude>
|
<exclude>**/fakes/*</exclude>
|
||||||
|
@ -6,54 +6,65 @@ import java.io.IOException;
|
|||||||
|
|
||||||
public class HostConnectToRemoteTcpDevice {
|
public class HostConnectToRemoteTcpDevice {
|
||||||
private final Transport transport;
|
private final Transport transport;
|
||||||
|
private final ResponseValidator responseValidator;
|
||||||
|
|
||||||
public HostConnectToRemoteTcpDevice(Transport transport) {
|
public HostConnectToRemoteTcpDevice(Transport transport) {
|
||||||
this.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)
|
public TcpAddressEntity connect(TcpAddressEntity tcpAddressEntity)
|
||||||
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
||||||
transport.send(String.format("host:connect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort()));
|
transport.send(String.format("host:connect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort()));
|
||||||
|
verifyTransportLevel();
|
||||||
verifyProtocolLevel();
|
verifyProtocolLevel();
|
||||||
verifyCommandLevel();
|
|
||||||
|
|
||||||
return tcpAddressEntity;
|
return tcpAddressEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyProtocolLevel() throws IOException, JadbException {
|
private void verifyTransportLevel() throws IOException, JadbException {
|
||||||
transport.verifyResponse();
|
transport.verifyResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyCommandLevel() throws IOException, ConnectionToRemoteDeviceException {
|
private void verifyProtocolLevel() throws IOException, ConnectionToRemoteDeviceException {
|
||||||
String status = transport.readString();
|
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 SUCCESSFULLY_CONNECTED = "connected to";
|
||||||
private final static String ALREADY_CONNECTED = "already connected to";
|
private final static String ALREADY_CONNECTED = "already connected to";
|
||||||
|
|
||||||
private final String response;
|
|
||||||
|
|
||||||
public ResponseValidator(String response) {
|
public ResponseValidatorImp() {
|
||||||
this.response = response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void validate() throws ConnectionToRemoteDeviceException {
|
public void validate(String response) throws ConnectionToRemoteDeviceException {
|
||||||
if(!checkIfConnectedSuccessfully() && !checkIfAlreadyConnected()) {
|
if(!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) {
|
||||||
throw new ConnectionToRemoteDeviceException(extractError());
|
throw new ConnectionToRemoteDeviceException(extractError(response));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkIfConnectedSuccessfully() {
|
private boolean checkIfConnectedSuccessfully(String response) {
|
||||||
return response.contains(SUCCESSFULLY_CONNECTED);
|
return response.startsWith(SUCCESSFULLY_CONNECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkIfAlreadyConnected() {
|
private boolean checkIfAlreadyConnected(String response) {
|
||||||
return response.equals(ALREADY_CONNECTED);
|
return response.startsWith(ALREADY_CONNECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String extractError() {
|
private String extractError(String response) {
|
||||||
int lastColon = response.lastIndexOf(":");
|
int lastColon = response.lastIndexOf(":");
|
||||||
if(lastColon != -1) {
|
if(lastColon != -1) {
|
||||||
return response.substring(lastColon, response.length());
|
return response.substring(lastColon, response.length());
|
||||||
|
@ -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 {
|
public List<JadbDevice> getDevices() throws IOException, JadbException {
|
||||||
Transport devices = createTransport();
|
Transport devices = createTransport();
|
||||||
devices.send("host:devices");
|
devices.send("host:devices");
|
||||||
|
@ -1,20 +1,73 @@
|
|||||||
package se.vidstige.jadb;
|
package se.vidstige.jadb;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import se.vidstige.jadb.entities.TcpAddressEntity;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
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 {
|
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
|
@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");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,6 +16,10 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
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 {
|
public class RealDeviceTestCases {
|
||||||
|
|
||||||
private JadbConnection jadb;
|
private JadbConnection jadb;
|
||||||
@ -123,5 +127,9 @@ public class RealDeviceTestCases {
|
|||||||
@Test
|
@Test
|
||||||
public void testConnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
public void testConnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
||||||
TcpAddressEntity tcpAddressEntity = jadb.connectToTcpDevice(new TcpAddressEntity("127.0.0.1", 10001));
|
TcpAddressEntity tcpAddressEntity = jadb.connectToTcpDevice(new TcpAddressEntity("127.0.0.1", 10001));
|
||||||
|
List<JadbDevice> devices = jadb.getDevices();
|
||||||
|
|
||||||
|
assertNotNull(devices);
|
||||||
|
assertFalse(devices.isEmpty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user