### Question:
ClientReceiver extends StreamReceiver { public static ClientReceiver connect(Connector connector, Address address) throws CommunicationException { return new ClientReceiver(new SocketWrapper(connector.connect(address))); } private  ClientReceiver(SocketWrapper socketWrapper); static ClientReceiver connect(Connector connector, Address address); @Override void shutdown();  }### Answer:
@Test public void testReceive() throws Exception { final SocketAcceptorThread socketAcceptor = SocketAcceptorThread.create(); final Connector connector = new Connector(socketAcceptor.getHostName(), socketAcceptor.getPort(), ConnectionType.AGENT); final Address address = new StubAddress(); final Receiver clientReceiver = ClientReceiver.connect(connector, address); socketAcceptor.join(); final Socket acceptedSocket = socketAcceptor.getAcceptedSocket(); assertConnection( acceptedSocket.getInputStream(), ConnectionType.AGENT, address); final OutputStream socketOutput = acceptedSocket.getOutputStream(); final SimpleMessage message1 = new SimpleMessage(); final ObjectOutputStream objectStream1 = new ObjectOutputStream(socketOutput); objectStream1.writeObject(message1); objectStream1.flush(); final SimpleMessage message2 = new SimpleMessage(); final ObjectOutputStream objectStream2 = new ObjectOutputStream(socketOutput); objectStream2.writeObject(message2); objectStream2.flush(); final Message receivedMessage1 = clientReceiver.waitForMessage(); final Message receivedMessage2 = clientReceiver.waitForMessage(); assertEquals(message1, receivedMessage1); assertEquals(message2, receivedMessage2); socketAcceptor.close(); try { ClientReceiver.connect(connector, address); fail("Expected CommunicationException"); } catch (CommunicationException e) { } }
@Test public void testCloseCommunicationMessage() throws Exception { final SocketAcceptorThread socketAcceptor = SocketAcceptorThread.create(); final Connector connector = new Connector(socketAcceptor.getHostName(), socketAcceptor.getPort(), ConnectionType.AGENT); final Address address = new StubAddress(); final Receiver clientReceiver = ClientReceiver.connect(connector, address); socketAcceptor.join(); final Socket acceptedSocket = socketAcceptor.getAcceptedSocket(); assertConnection( acceptedSocket.getInputStream(), ConnectionType.AGENT, address); final OutputStream socketOutput = acceptedSocket.getOutputStream(); final SimpleMessage message1 = new SimpleMessage(); final ObjectOutputStream objectStream1 = new ObjectOutputStream(socketOutput); objectStream1.writeObject(message1); objectStream1.flush(); final Message receivedMessage = clientReceiver.waitForMessage(); assertNotNull(receivedMessage); final Message closeCommunicationMessage = new CloseCommunicationMessage(); final ObjectOutputStream objectStream2 = new ObjectOutputStream(socketOutput); objectStream2.writeObject(closeCommunicationMessage); objectStream2.flush(); assertNull(clientReceiver.waitForMessage()); socketAcceptor.close(); }