mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestServerSocketChannel.java
@Test
    public void testTrySendMessage() throws IOException
    {
        // mock the socket that will be used to send the message
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        when(socket.getOutputStream()).thenReturn(outStream);
        SocketAddress socketAddress = mock(SocketAddress.class);
        when(socketAddress.toString()).thenReturn(REMOTE_SOCKET_ADDR);
        when(socket.getRemoteSocketAddress()).thenReturn(socketAddress);
        
        // activate method expects a connected socket to be passed in
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(ServerSocketChannel.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        
        TerraHarvestMessage message = createBaseMessage();
        
        // replay
        boolean result = m_SUT.trySendMessage(message);
        assertThat(result, is(true));
        
        // verify mock socket's output stream was asked to send message in binary form with delimiter at beginning
        byte[] binaryMessage = message.toByteArray();
        // first byte sent will be the varint (https://developers.google.com/protocol-buffers/docs/encoding#varints)
        // representing the length of the message following
        byte[] fullBinaryMessage = new byte[binaryMessage.length + 1]; // leave room for delimiter
        fullBinaryMessage[0] = (byte)binaryMessage.length;
        System.arraycopy(binaryMessage, 0, fullBinaryMessage, 1, binaryMessage.length);
        
        assertThat(outStream.toByteArray(), is(fullBinaryMessage));
        
        // verify logging occurs
        verify(m_Logging, times(1)).debug(anyString(), anyVararg());
    }
/**
     * Verify the message is sent using the given socket in binary form.
     * 
     * Verify method returns false if socket throws exception
     */
Verify the message is sent using the given socket in binary form.

Verify method returns false if socket throws exception
Verify the message is sent using the given socket in binary form . Verify method returns false if socket throws exception
@Test
    public void testTrySendMessage() throws IOException
    {
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        when(socket.getOutputStream()).thenReturn(outStream);
        SocketAddress socketAddress = mock(SocketAddress.class);
        when(socketAddress.toString()).thenReturn(REMOTE_SOCKET_ADDR);
        when(socket.getRemoteSocketAddress()).thenReturn(socketAddress);
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(ServerSocketChannel.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        TerraHarvestMessage message = createBaseMessage();
        boolean result = m_SUT.trySendMessage(message);
        assertThat(result, is(true));
        byte[] binaryMessage = message.toByteArray();
        byte[] fullBinaryMessage = new byte[binaryMessage.length + 1]; 
        fullBinaryMessage[0] = (byte)binaryMessage.length;
        System.arraycopy(binaryMessage, 0, fullBinaryMessage, 1, binaryMessage.length);
        assertThat(outStream.toByteArray(), is(fullBinaryMessage));
        verify(m_Logging, times(1)).debug(anyString(), anyVararg());
    }
@ Test public void testTrySendMessage ( ) throws IOException { Socket socket = mock ( Socket . class ) ; ByteArrayOutputStream outStream = new ByteArrayOutputStream ( ) ; when ( socket . getOutputStream ( ) ) . thenReturn ( outStream ) ; SocketAddress socketAddress = mock ( SocketAddress . class ) ; when ( socketAddress . toString ( ) ) . thenReturn ( REMOTE_SOCKET_ADDR ) ; when ( socket . getRemoteSocketAddress ( ) ) . thenReturn ( socketAddress ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; props . put ( ServerSocketChannel . SOCKET_PROP_KEY , socket ) ; m_SUT . activate ( props ) ; TerraHarvestMessage message = createBaseMessage ( ) ; boolean result = m_SUT . trySendMessage ( message ) ; assertThat ( result , is ( true ) ) ; byte [ ] binaryMessage = message . toByteArray ( ) ; byte [ ] fullBinaryMessage = new byte [ binaryMessage . length + 1 ] ; fullBinaryMessage [ 0 ] = ( byte ) binaryMessage . length ; System . arraycopy ( binaryMessage , 0 , fullBinaryMessage , 1 , binaryMessage . length ) ; assertThat ( outStream . toByteArray ( ) , is ( fullBinaryMessage ) ) ; verify ( m_Logging , times ( 1 ) ) . debug ( anyString ( ) , anyVararg ( ) ) ; }
Verify the message is sent using the given socket in binary form.
Verify the message is sent using the given socket in binary form .
// mock the socket that will be used to send the message // activate method expects a connected socket to be passed in // replay // verify mock socket's output stream was asked to send message in binary form with delimiter at beginning // first byte sent will be the varint (https://developers.google.com/protocol-buffers/docs/encoding#varints) // representing the length of the message following // leave room for delimiter // verify logging occurs