SocketControlPoint implements ControlPoint { @Override public void execute(final AirplayDevice device, final AirplayCommand command) { runDeviceCommand(device, command); } SocketControlPoint(final SocketFactory socketFactory); @Override void execute(final AirplayDevice device, final AirplayCommand command);  }
@Test public void testExecuteOKResponse() throws IOException { AirplayDevice device = createMock(AirplayDevice.class); AirplayCommand command = createMock(AirplayCommand.class); Socket socket = createMock(Socket.class); SocketFactory socketFactory = createMock(SocketFactory.class); OutputStream socketOutputStream = createMock(OutputStream.class); InputStream socketInputStream = new ByteArrayInputStream("HTTP/1.1 200 OK\n\n".getBytes()); String request = "Request"; Capture<byte[]> captureRequest = newCapture(); Capture<Map<String, String>> captureContentParameters = newCapture(); expect(device.getConnection(isA(SocketFactory.class))).andReturn(socket).atLeastOnce(); expect(socket.getOutputStream()).andReturn(socketOutputStream).atLeastOnce(); expect(socket.getInputStream()).andReturn(socketInputStream).atLeastOnce(); expect(command.getRequest()).andReturn(request).atLeastOnce(); socketOutputStream.write(capture(captureRequest), eq(0), eq(request.length())); expectLastCall().atLeastOnce(); socketOutputStream.flush(); expectLastCall().atLeastOnce(); command.success(capture(captureContentParameters)); expectLastCall().atLeastOnce(); replay(device, command, socket, socketFactory, socketOutputStream); SocketControlPoint controlPoint = new SocketControlPoint(socketFactory); controlPoint.execute(device, command); assertEquals(request, new String(captureRequest.getValue(), 0, request.length())); assertEquals(0, captureContentParameters.getValue().size()); verify(device, command, socket, socketFactory, socketOutputStream); }
@Test public void testExecuteOKResponseWithParameters() throws IOException { AirplayDevice device = createMock(AirplayDevice.class); AirplayCommand command = createMock(AirplayCommand.class); Socket socket = createMock(Socket.class); SocketFactory socketFactory = createMock(SocketFactory.class); OutputStream socketOutputStream = createMock(OutputStream.class); String content = "duration: 83.124794\nposition: 14.467000\n"; String httpHeader = "HTTP/1.1 200 OK\nContent-Type: text/parameters\nContent-Length: " + content.length() + "\n\n"; String response = httpHeader + content; InputStream socketInputStream = new ByteArrayInputStream(response.getBytes()); String request = "Request"; Capture<byte[]> captureRequest = newCapture(); Capture<Map<String, String>> captureContentParameters = newCapture(); expect(device.getConnection(isA(SocketFactory.class))).andReturn(socket).atLeastOnce(); expect(socket.getOutputStream()).andReturn(socketOutputStream).atLeastOnce(); expect(socket.getInputStream()).andReturn(socketInputStream).atLeastOnce(); expect(command.getRequest()).andReturn(request).atLeastOnce(); socketOutputStream.write(capture(captureRequest), eq(0), eq(request.length())); expectLastCall().atLeastOnce(); socketOutputStream.flush(); expectLastCall().atLeastOnce(); command.success(capture(captureContentParameters)); expectLastCall().atLeastOnce(); replay(device, command, socket, socketFactory, socketOutputStream); SocketControlPoint controlPoint = new SocketControlPoint(socketFactory); controlPoint.execute(device, command); assertEquals(request, new String(captureRequest.getValue(), 0, request.length())); assertEquals(2, captureContentParameters.getValue().size()); verify(device, command, socket, socketFactory, socketOutputStream); }
@Test public void testExecuteOKResponseWithNoParameters() throws IOException { AirplayDevice device = createMock(AirplayDevice.class); AirplayCommand command = createMock(AirplayCommand.class); Socket socket = createMock(Socket.class); SocketFactory socketFactory = createMock(SocketFactory.class); OutputStream socketOutputStream = createMock(OutputStream.class); String content = "some content\n"; String httpHeader = "HTTP/1.1 200 OK\nContent-Type: text/something\nContent-Length: 20000\n\n"; String response = httpHeader + content; InputStream socketInputStream = new ByteArrayInputStream(response.getBytes()); String request = "Request"; Capture<byte[]> captureRequest = newCapture(); Capture<Map<String, String>> captureContentParameters = newCapture(); expect(device.getConnection(isA(SocketFactory.class))).andReturn(socket).atLeastOnce(); expect(socket.getOutputStream()).andReturn(socketOutputStream).atLeastOnce(); expect(socket.getInputStream()).andReturn(socketInputStream).atLeastOnce(); expect(command.getRequest()).andReturn(request).atLeastOnce(); socketOutputStream.write(capture(captureRequest), eq(0), eq(request.length())); expectLastCall().atLeastOnce(); socketOutputStream.flush(); expectLastCall().atLeastOnce(); command.success(capture(captureContentParameters)); expectLastCall().atLeastOnce(); replay(device, command, socket, socketFactory, socketOutputStream); SocketControlPoint controlPoint = new SocketControlPoint(socketFactory); controlPoint.execute(device, command); assertEquals(request, new String(captureRequest.getValue(), 0, request.length())); assertEquals(0, captureContentParameters.getValue().size()); verify(device, command, socket, socketFactory, socketOutputStream); }
@Test public void testExecuteKOResponse() throws IOException { AirplayDevice device = createMock(AirplayDevice.class); AirplayCommand command = createMock(AirplayCommand.class); Socket socket = createMock(Socket.class); SocketFactory socketFactory = createMock(SocketFactory.class); OutputStream socketOutputStream = createMock(OutputStream.class); InputStream socketInputStream = new ByteArrayInputStream("HTTP/1.1 404 NOT FOUND\n\n".getBytes()); String request = "Request"; Capture<byte[]> captureRequest = newCapture(); expect(device.getConnection(isA(SocketFactory.class))).andReturn(socket).atLeastOnce(); expect(socket.getOutputStream()).andReturn(socketOutputStream).atLeastOnce(); expect(socket.getInputStream()).andReturn(socketInputStream).atLeastOnce(); expect(command.getRequest()).andReturn(request).atLeastOnce(); socketOutputStream.write(capture(captureRequest), eq(0), eq(request.length())); expectLastCall().atLeastOnce(); socketOutputStream.flush(); expectLastCall().atLeastOnce(); replay(device, command, socket, socketFactory, socketOutputStream); SocketControlPoint controlPoint = new SocketControlPoint(socketFactory); controlPoint.execute(device, command); assertEquals(request, new String(captureRequest.getValue(), 0, request.length())); verify(device, command, socket, socketFactory, socketOutputStream); }
@Test public void testExecuteIOException() throws IOException { AirplayDevice device = createMock(AirplayDevice.class); CommandFailureHandler failureHandler = createMock(CommandFailureHandler.class); Socket socket = createMock(Socket.class); SocketFactory socketFactory = createMock(SocketFactory.class); OutputStream socketOutputStream = createMock(OutputStream.class); expect(device.getConnection(isA(SocketFactory.class))).andThrow(new IOException("IOException message")); failureHandler.handle(eq("IOException message")); expectLastCall(); replay(device, failureHandler, socket, socketFactory, socketOutputStream); SocketControlPoint controlPoint = new SocketControlPoint(socketFactory); AirplayCommand command = new AirplayCommandTest(PLAY, failureHandler); controlPoint.execute(device, command); verify(device, failureHandler, socket, socketFactory, socketOutputStream); }