AirplayDevice extends Device { public Socket getConnection(final SocketFactory socketFactory) throws IOException { if (socket == null || socket.isClosed()) { socket = socketFactory.createSocket(getAddress(), port); } return socket; } AirplayDevice(final String id, final String name, final InetAddress address, final int port, final AirplayDeviceFeatures features); @Override String getType(); @Override boolean isVideoSupported(); @Override boolean isAudioSupported(); @Override boolean isImageSupported(); @Override boolean isSlideShowSupported(); @Override void close(); Socket getConnection(final SocketFactory socketFactory); @Override String toString();  }
@Test public void testGetConnection() throws IOException { SocketFactory socketFactory = createMock(SocketFactory.class); Socket socket = createMock(Socket.class); expect(socketFactory.createSocket(isNull(InetAddress.class), anyInt())).andReturn(socket); replay(socketFactory, socket); AirplayDeviceFeatures features = new AirplayDeviceFeatures("0"); AirplayDevice device = new AirplayDevice("id", "name", null, 8080, features); Socket aSocket = device.getConnection(socketFactory); assertNotNull(aSocket); verify(socketFactory, socket); }
@Test public void testGetConnectionSocketOpen() throws IOException { SocketFactory socketFactory = createMock(SocketFactory.class); Socket socket = createMock(Socket.class); expect(socketFactory.createSocket(isNull(InetAddress.class), anyInt())).andReturn(socket); expect(socket.isClosed()).andReturn(false); replay(socketFactory, socket); AirplayDeviceFeatures features = new AirplayDeviceFeatures("0"); AirplayDevice device = new AirplayDevice("id", "name", null, 8080, features); Socket aSocket1 = device.getConnection(socketFactory); Socket aSocket2 = device.getConnection(socketFactory); assertNotNull(aSocket1); assertNotNull(aSocket2); verify(socketFactory, socket); }
@Test public void testGetConnectionSocketClosed() throws IOException { SocketFactory socketFactory = createMock(SocketFactory.class); Socket socket = createMock(Socket.class); expect(socketFactory.createSocket(isNull(InetAddress.class), anyInt())).andReturn(socket).atLeastOnce(); expect(socket.isClosed()).andReturn(true); replay(socketFactory, socket); AirplayDeviceFeatures features = new AirplayDeviceFeatures("0"); AirplayDevice device = new AirplayDevice("id", "name", null, 8080, features); Socket aSocket1 = device.getConnection(socketFactory); Socket aSocket2 = device.getConnection(socketFactory); assertNotNull(aSocket1); assertNotNull(aSocket2); verify(socketFactory, socket); }