mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test
    public void testRunWithCloseInputException() throws IOException, InterruptedException
    {
        int sourceId = 100;
        
        // setup the message to be received
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId, 1, Namespace.Base, 100, baseMessage);

        // simulate received message from socket
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeDelimitedTo(output);
        
        InputStream inStream = spy(new ByteArrayInputStream(output.toByteArray()));
        when(socket.getInputStream()).thenReturn(inStream);
        doThrow(IOException.class).when(inStream).close();
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        // mock the channel that will be sent in map
        AbstractSocketChannel channel = mock(AbstractSocketChannel.class);
        props.put(SocketMessageListener.CHANNEL_PROP_KEY, channel);
        m_SUT.activate(props);
        
        Thread thread = new Thread(m_SUT);
        thread.start();
        
        // wait to give thread time to start and try to read message
        Thread.sleep(1000);
        
        //SocketMessageListener exits out because input stream passed to message listener is a set size. 
        //therefore deactivate does not need to be called. (i.e. the message read in is the only message
        //on the stream).
        thread.join(100);
        
        verify(m_Logging).error(Mockito.any(IOException.class), eq("Unable to close input stream")); 
    }
/**
     * Verify that exception is logged appropriately when input stream does not close correctly.
     */
Verify that exception is logged appropriately when input stream does not close correctly.
Verify that exception is logged appropriately when input stream does not close correctly .
@Test
    public void testRunWithCloseInputException() throws IOException, InterruptedException
    {
        int sourceId = 100;
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId, 1, Namespace.Base, 100, baseMessage);
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeDelimitedTo(output);
        InputStream inStream = spy(new ByteArrayInputStream(output.toByteArray()));
        when(socket.getInputStream()).thenReturn(inStream);
        doThrow(IOException.class).when(inStream).close();
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        AbstractSocketChannel channel = mock(AbstractSocketChannel.class);
        props.put(SocketMessageListener.CHANNEL_PROP_KEY, channel);
        m_SUT.activate(props);
        Thread thread = new Thread(m_SUT);
        thread.start();
        Thread.sleep(1000);
        thread.join(100);
        verify(m_Logging).error(Mockito.any(IOException.class), eq("Unable to close input stream")); 
    }
@ Test public void testRunWithCloseInputException ( ) throws IOException , InterruptedException { int sourceId = 100 ; BaseNamespace baseMessage = BaseNamespace . newBuilder ( ) . setType ( BaseMessageType . RequestControllerInfo ) . build ( ) ; final TerraHarvestMessage message = TerraHarvestMessageHelper . createTerraHarvestMessage ( sourceId , 1 , Namespace . Base , 100 , baseMessage ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; Socket socket = mock ( Socket . class ) ; ByteArrayOutputStream output = new ByteArrayOutputStream ( ) ; message . writeDelimitedTo ( output ) ; InputStream inStream = spy ( new ByteArrayInputStream ( output . toByteArray ( ) ) ) ; when ( socket . getInputStream ( ) ) . thenReturn ( inStream ) ; doThrow ( IOException . class ) . when ( inStream ) . close ( ) ; props . put ( SocketMessageListener . SOCKET_PROP_KEY , socket ) ; AbstractSocketChannel channel = mock ( AbstractSocketChannel . class ) ; props . put ( SocketMessageListener . CHANNEL_PROP_KEY , channel ) ; m_SUT . activate ( props ) ; Thread thread = new Thread ( m_SUT ) ; thread . start ( ) ; Thread . sleep ( 1000 ) ; thread . join ( 100 ) ; verify ( m_Logging ) . error ( Mockito . any ( IOException . class ) , eq ( "Unable to close input stream" ) ) ; }
Verify that exception is logged appropriately when input stream does not close correctly.
Verify that exception is logged appropriately when input stream does not close correctly .
// setup the message to be received // simulate received message from socket // mock the channel that will be sent in map // wait to give thread time to start and try to read message //SocketMessageListener exits out because input stream passed to message listener is a set size.  //therefore deactivate does not need to be called. (i.e. the message read in is the only message //on the stream).