mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test
    public void testRunWithSocketException() throws InterruptedException, IOException
    {
        Socket socket = mock(Socket.class);
        InputStream inStream = mock(InputStream.class);
        when(socket.getInputStream()).thenReturn(inStream);
        when(inStream.read()).thenThrow(new IOException("socket exception"));
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        
        Thread thread = new Thread(m_SUT);
        thread.start();
        
        // wait to give thread time to start and try to use socket
        Thread.sleep(1000);
        
        thread.join(100);
        // verify thread has stopped because of the socket exception
        assertThat(thread.isAlive(), is(false));
        
        // ensure input stream and socket gets closed
        verify(inStream).close();
        verify(socket).close();        
    }
/**
     * Verify if the socket throws an exception, the thread will stop.
     */
Verify if the socket throws an exception, the thread will stop.
Verify if the socket throws an exception the thread will stop .
@Test
    public void testRunWithSocketException() throws InterruptedException, IOException
    {
        Socket socket = mock(Socket.class);
        InputStream inStream = mock(InputStream.class);
        when(socket.getInputStream()).thenReturn(inStream);
        when(inStream.read()).thenThrow(new IOException("socket exception"));
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        Thread thread = new Thread(m_SUT);
        thread.start();
        Thread.sleep(1000);
        thread.join(100);
        assertThat(thread.isAlive(), is(false));
        verify(inStream).close();
        verify(socket).close();        
    }
@ Test public void testRunWithSocketException ( ) throws InterruptedException , IOException { Socket socket = mock ( Socket . class ) ; InputStream inStream = mock ( InputStream . class ) ; when ( socket . getInputStream ( ) ) . thenReturn ( inStream ) ; when ( inStream . read ( ) ) . thenThrow ( new IOException ( "socket exception" ) ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; props . put ( SocketMessageListener . SOCKET_PROP_KEY , socket ) ; m_SUT . activate ( props ) ; Thread thread = new Thread ( m_SUT ) ; thread . start ( ) ; Thread . sleep ( 1000 ) ; thread . join ( 100 ) ; assertThat ( thread . isAlive ( ) , is ( false ) ) ; verify ( inStream ) . close ( ) ; verify ( socket ) . close ( ) ; }
Verify if the socket throws an exception, the thread will stop.
Verify if the socket throws an exception the thread will stop .
// wait to give thread time to start and try to use socket // verify thread has stopped because of the socket exception // ensure input stream and socket gets closed