mirror of
https://github.com/revanced/jadb.git
synced 2024-06-10 07:07:49 +02:00
#58 migration to InetSocketAddress
This commit is contained in:
parent
64544b5bff
commit
4442663889
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<JadbDevice> 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<JadbDevice> devices = jadb.getDevices();
|
||||
|
|
Loading…
Reference in New Issue
Block a user