#58 fixes in tests

This commit is contained in:
Art 2017-03-20 16:03:34 +03:00
parent 4442663889
commit b4fa17abff
2 changed files with 25 additions and 8 deletions

View File

@ -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<String> 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)

View File

@ -20,7 +20,7 @@ import java.util.List;
*/
public class FakeAdbServer implements AdbResponder {
private final AdbServer server;
private List<DeviceResponder> devices = new ArrayList<DeviceResponder>();
private final List<DeviceResponder> devices = new ArrayList<DeviceResponder>();
public FakeAdbServer(int port) {
server = new AdbServer(this, port);