mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestServerSocketChannel.java
@SuppressWarnings({ "rawtypes", "unchecked" })
    // this timeout is less then time the thread below blocks to prove it is blocking on a separate thread
    @Test(timeout = 2000)
    public void testActivate() throws InterruptedException
    {
        // mock the input
        Socket socket = mock(Socket.class);
        SocketAddress socketAddress = mock(SocketAddress.class);
        when(socketAddress.toString()).thenReturn(REMOTE_SOCKET_ADDR);
        when(socket.getRemoteSocketAddress()).thenReturn(socketAddress);

        // mock the message listener and its run method to block, need to block to verify run method is called on 
        // separate thread
        SocketMessageListener socketMessageListener = mock(SocketMessageListener.class);
        when(m_ListenerInstance.getInstance()).thenReturn(socketMessageListener);
        doAnswer(new Answer<Object>()
        {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable
            {
                Thread.sleep(200000); // simulate a blocking call
                return null;
            }
        }).when(socketMessageListener).run();

        when(m_RemoteSettings.isPreventSleepModeEnabled()).thenReturn(false);

        // activate the component
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(ServerSocketChannel.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);

        // verify wake lock is activated
        verify(m_WakeLock, never()).activate();

        // all new channels start out as unknown
        assertThat(m_SUT.getStatus(), is(ChannelStatus.Unknown));
        
        // verify message listener is created and given the socket and channel instance
        ArgumentCaptor<Dictionary> dictionaryCaptor = ArgumentCaptor.forClass(Dictionary.class);
        verify(m_ListenerFactory).newInstance(dictionaryCaptor.capture());
        assertThat((Socket)dictionaryCaptor.getValue().get(ServerSocketChannel.SOCKET_PROP_KEY), is(socket));
        assertThat((ServerSocketChannel)dictionaryCaptor.getValue().get(SocketMessageListener.CHANNEL_PROP_KEY), 
                is(m_SUT));
        
        // verify queued message sender is created and given the socket and channel instance
        verify(m_SenderFactory).newInstance(dictionaryCaptor.capture());
        assertThat((ServerSocketChannel)dictionaryCaptor.getValue().get(QueuedMessageSender.CHANNEL_PROP_KEY), 
                is(m_SUT));
        
        Thread.sleep(500); // allow thread to run
        
        // verify listener was told to run and on a separate thread, know on a separate thread because this test
        // didn't timeout waiting on the run method to exit
        verify(socketMessageListener).run();
    }
/**
     * Verify channel status is initialized and socket message listener is created at startup.
     */
Verify channel status is initialized and socket message listener is created at startup.
Verify channel status is initialized and socket message listener is created at startup .
@SuppressWarnings({ "rawtypes", "unchecked" })
    @Test(timeout = 2000)
    public void testActivate() throws InterruptedException
    {
        Socket socket = mock(Socket.class);
        SocketAddress socketAddress = mock(SocketAddress.class);
        when(socketAddress.toString()).thenReturn(REMOTE_SOCKET_ADDR);
        when(socket.getRemoteSocketAddress()).thenReturn(socketAddress);
        SocketMessageListener socketMessageListener = mock(SocketMessageListener.class);
        when(m_ListenerInstance.getInstance()).thenReturn(socketMessageListener);
        doAnswer(new Answer<Object>()
        {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable
            {
                Thread.sleep(200000); 
                return null;
            }
        }).when(socketMessageListener).run();
        when(m_RemoteSettings.isPreventSleepModeEnabled()).thenReturn(false);
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(ServerSocketChannel.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        verify(m_WakeLock, never()).activate();
        assertThat(m_SUT.getStatus(), is(ChannelStatus.Unknown));
        ArgumentCaptor<Dictionary> dictionaryCaptor = ArgumentCaptor.forClass(Dictionary.class);
        verify(m_ListenerFactory).newInstance(dictionaryCaptor.capture());
        assertThat((Socket)dictionaryCaptor.getValue().get(ServerSocketChannel.SOCKET_PROP_KEY), is(socket));
        assertThat((ServerSocketChannel)dictionaryCaptor.getValue().get(SocketMessageListener.CHANNEL_PROP_KEY), 
                is(m_SUT));
        verify(m_SenderFactory).newInstance(dictionaryCaptor.capture());
        assertThat((ServerSocketChannel)dictionaryCaptor.getValue().get(QueuedMessageSender.CHANNEL_PROP_KEY), 
                is(m_SUT));
        Thread.sleep(500); 
        verify(socketMessageListener).run();
    }
@ SuppressWarnings ( { "rawtypes" , "unchecked" } ) @ Test ( timeout = 2000 ) public void testActivate ( ) throws InterruptedException { Socket socket = mock ( Socket . class ) ; SocketAddress socketAddress = mock ( SocketAddress . class ) ; when ( socketAddress . toString ( ) ) . thenReturn ( REMOTE_SOCKET_ADDR ) ; when ( socket . getRemoteSocketAddress ( ) ) . thenReturn ( socketAddress ) ; SocketMessageListener socketMessageListener = mock ( SocketMessageListener . class ) ; when ( m_ListenerInstance . getInstance ( ) ) . thenReturn ( socketMessageListener ) ; doAnswer ( new Answer < Object > ( ) { @ Override public Object answer ( InvocationOnMock invocation ) throws Throwable { Thread . sleep ( 200000 ) ; return null ; } } ) . when ( socketMessageListener ) . run ( ) ; when ( m_RemoteSettings . isPreventSleepModeEnabled ( ) ) . thenReturn ( false ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; props . put ( ServerSocketChannel . SOCKET_PROP_KEY , socket ) ; m_SUT . activate ( props ) ; verify ( m_WakeLock , never ( ) ) . activate ( ) ; assertThat ( m_SUT . getStatus ( ) , is ( ChannelStatus . Unknown ) ) ; ArgumentCaptor < Dictionary > dictionaryCaptor = ArgumentCaptor . forClass ( Dictionary . class ) ; verify ( m_ListenerFactory ) . newInstance ( dictionaryCaptor . capture ( ) ) ; assertThat ( ( Socket ) dictionaryCaptor . getValue ( ) . get ( ServerSocketChannel . SOCKET_PROP_KEY ) , is ( socket ) ) ; assertThat ( ( ServerSocketChannel ) dictionaryCaptor . getValue ( ) . get ( SocketMessageListener . CHANNEL_PROP_KEY ) , is ( m_SUT ) ) ; verify ( m_SenderFactory ) . newInstance ( dictionaryCaptor . capture ( ) ) ; assertThat ( ( ServerSocketChannel ) dictionaryCaptor . getValue ( ) . get ( QueuedMessageSender . CHANNEL_PROP_KEY ) , is ( m_SUT ) ) ; Thread . sleep ( 500 ) ; verify ( socketMessageListener ) . run ( ) ; }
Verify channel status is initialized and socket message listener is created at startup.
Verify channel status is initialized and socket message listener is created at startup .
// this timeout is less then time the thread below blocks to prove it is blocking on a separate thread // mock the input // mock the message listener and its run method to block, need to block to verify run method is called on  // separate thread // simulate a blocking call // activate the component // verify wake lock is activated // all new channels start out as unknown // verify message listener is created and given the socket and channel instance // verify queued message sender is created and given the socket and channel instance // allow thread to run // verify listener was told to run and on a separate thread, know on a separate thread because this test // didn't timeout waiting on the run method to exit