mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test
    public void testRunWithMessageSizeError() throws IOException, InterruptedException
    {
        // setup the message to be received
        byte[] tooBig = new byte[1025]; // a byte array that is just 1 byte too big
        for (int i = 0; i < tooBig.length; i++)
        {
            tooBig[i] = 0x01;
        }
        
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                setData(ByteString.copyFrom(tooBig)).
                build();
        
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(100, 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);
        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();
                
        // should exit out immediately without a call to deactivate since input stream is closed
        thread.join(1000);
        
        assertThat(thread.isAlive(), is(false));
        
        // ensure input stream and socket gets closed
        verify(inStream).close();
        verify(socket).close();
        
        verify(m_Logging).error(eq("Error reading message: %s"), contains("Message over Max Size"));
    }
/**
     * Test the message max size limit.
     * @throws IOException Thrown on failure to establish socket.
     * @throws InterruptedException Thrown if thread is interrupted during sleep to initialize socket.
     */
Test the message max size limit.
@throws IOException Thrown on failure to establish socket.
@throws InterruptedException Thrown if thread is interrupted during sleep to initialize socket.
Test the message max size limit . @throws IOException Thrown on failure to establish socket . @throws InterruptedException Thrown if thread is interrupted during sleep to initialize socket .
@Test
    public void testRunWithMessageSizeError() throws IOException, InterruptedException
    {
        byte[] tooBig = new byte[1025]; 
        for (int i = 0; i < tooBig.length; i++)
        {
            tooBig[i] = 0x01;
        }
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                setData(ByteString.copyFrom(tooBig)).
                build();
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(100, 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);
        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.join(1000);
        assertThat(thread.isAlive(), is(false));
        verify(inStream).close();
        verify(socket).close();
        verify(m_Logging).error(eq("Error reading message: %s"), contains("Message over Max Size"));
    }
@ Test public void testRunWithMessageSizeError ( ) throws IOException , InterruptedException { byte [ ] tooBig = new byte [ 1025 ] ; for ( int i = 0 ; i < tooBig . length ; i ++ ) { tooBig [ i ] = 0x01 ; } BaseNamespace baseMessage = BaseNamespace . newBuilder ( ) . setType ( BaseMessageType . RequestControllerInfo ) . setData ( ByteString . copyFrom ( tooBig ) ) . build ( ) ; final TerraHarvestMessage message = TerraHarvestMessageHelper . createTerraHarvestMessage ( 100 , 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 ) ; 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 . join ( 1000 ) ; assertThat ( thread . isAlive ( ) , is ( false ) ) ; verify ( inStream ) . close ( ) ; verify ( socket ) . close ( ) ; verify ( m_Logging ) . error ( eq ( "Error reading message: %s" ) , contains ( "Message over Max Size" ) ) ; }
Test the message max size limit.
Test the message max size limit .
// setup the message to be received // a byte array that is just 1 byte too big // simulate received message from socket // mock the channel that will be sent in map // should exit out immediately without a call to deactivate since input stream is closed // ensure input stream and socket gets closed