mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test
    public void testEndOfSocketFileStream() throws InterruptedException, IOException
    {
        // simulate EOF from socket
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        InputStream inStream = mock(InputStream.class);
        when(socket.getInputStream()).thenReturn(inStream);
        // when a socket is closed by the client, the input stream will return -1 signaling an EOF
        when(inStream.read()).thenReturn(-1); 
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        
        Thread thread = new Thread(m_SUT);
        thread.start();
        
        // thread should stop by itself since the input stream is at the end of file
        thread.join(300);
        
        assertThat(thread.isAlive(), is(false));
        
        // ensure input stream and socket gets closed
        verify(inStream).close();
        verify(socket).close();
    }
/**
     * Verify the thread will stop if it reaches the end of the input stream
     */
Verify the thread will stop if it reaches the end of the input stream
Verify the thread will stop if it reaches the end of the input stream
@Test
    public void testEndOfSocketFileStream() throws InterruptedException, IOException
    {
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        InputStream inStream = mock(InputStream.class);
        when(socket.getInputStream()).thenReturn(inStream);
        when(inStream.read()).thenReturn(-1); 
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        Thread thread = new Thread(m_SUT);
        thread.start();
        thread.join(300);
        assertThat(thread.isAlive(), is(false));
        verify(inStream).close();
        verify(socket).close();
    }
@ Test public void testEndOfSocketFileStream ( ) throws InterruptedException , IOException { Map < String , Object > props = new HashMap < String , Object > ( ) ; Socket socket = mock ( Socket . class ) ; InputStream inStream = mock ( InputStream . class ) ; when ( socket . getInputStream ( ) ) . thenReturn ( inStream ) ; when ( inStream . read ( ) ) . thenReturn ( - 1 ) ; props . put ( SocketMessageListener . SOCKET_PROP_KEY , socket ) ; m_SUT . activate ( props ) ; Thread thread = new Thread ( m_SUT ) ; thread . start ( ) ; thread . join ( 300 ) ; assertThat ( thread . isAlive ( ) , is ( false ) ) ; verify ( inStream ) . close ( ) ; verify ( socket ) . close ( ) ; }
Verify the thread will stop if it reaches the end of the input stream
Verify the thread will stop if it reaches the end of the input stream
// simulate EOF from socket // when a socket is closed by the client, the input stream will return -1 signaling an EOF // thread should stop by itself since the input stream is at the end of file // ensure input stream and socket gets closed