Code example for Socket

Methods: close, getInputStream

0
    } 
 
    public void testReadAfterClose() throws Exception { 
        MockServer server = new MockServer();
        server.enqueue(new byte[]{5, 3}, 0);
        Socket socket = new Socket("localhost", server.port);
        InputStream in = socket.getInputStream();
        assertEquals(5, in.read());
        assertEquals(3, in.read());
        assertEquals(-1, in.read());
        assertEquals(-1, in.read());
        socket.close();
        in.close();
 
        /* 
         * Rather astonishingly, read() doesn't throw even though the stream is 
         * closed. This is consistent with the RI's behavior. 
         */ 
        assertEquals(-1, in.read());
        assertEquals(-1, in.read());