Code example for DatagramChannel

Methods: connect, disconnect, isBlocking, isConnected, isOpen

0
            // OK. 
        } 
    } 
 
    private void disconnectAfterConnected() throws IOException { 
        assertTrue(this.channel1.isConnected());
        this.channel1.disconnect();
        assertFalse(this.channel1.isConnected());
    } 
 
    private void disconnectAfterClosed() throws IOException { 
        assertFalse(this.channel1.isOpen());
        assertFalse(this.channel1.isConnected());
        this.channel1.disconnect();
        assertFalse(this.channel1.isConnected());
    } 
 
    private void connectLocalServer() throws IOException { 
        assertFalse(this.channel1.isConnected());
        assertTrue(this.datagramSocket1.isBound());
        assertSame(this.channel1, this.channel1.connect(localAddr1));
        assertTrue(this.channel1.isConnected());
    } 
 
    private void connectWithoutServer() throws IOException { 
        assertFalse(this.channel1.isConnected());
        this.datagramSocket1.close();
        assertTrue(this.datagramSocket1.isClosed());
        assertSame(this.channel1, this.channel1.connect(localAddr1));
        assertTrue(this.channel1.isConnected());
    } 
 
    // ------------------------------------------------------------------- 
    // Test for disconnect() 
    // ------------------------------------------------------------------- 
 
    /** 
     * Test method for 'DatagramChannelImpl.disconnect()' 
     * 
     * @throws IOException 
     */ 
    public void testDisconnect_BeforeConnect() throws IOException { 
        assertFalse(this.channel1.isConnected());
        assertEquals(this.channel1, this.channel1.disconnect());
        assertFalse(this.channel1.isConnected());
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.disconnect()' 
     * 
     * @throws IOException 
     */ 
    public void testDisconnect_UnconnectedClosed() throws IOException { 
        assertFalse(this.channel1.isConnected());
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
        assertEquals(this.channel1, this.channel1.disconnect());
        assertFalse(this.channel1.isConnected());
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.disconnect()' 
     * 
     * @throws IOException 
     */ 
    public void testDisconnect_BlockWithServerChannelClosed() 
            throws IOException { 
        assertTrue(this.channel1.isBlocking());
        connectLocalServer(); 
        // disconnect after channel close 
        this.channel1.close();
        disconnectAfterClosed(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.disconnect()' 
     * 
     * @throws IOException 
     */ 
    public void testDisconnect_NonBlockWithServerChannelClosed() 
            throws IOException { 
        this.channel1.configureBlocking(false);
        connectLocalServer(); 
        // disconnect after channel close 
        this.channel1.close();
        disconnectAfterClosed(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.disconnect()' 
     * 
     * @throws IOException 
     */ 
    public void testDisconnect_BlockWithServerServerClosed() throws IOException { 
        assertTrue(this.channel1.isBlocking());
        connectLocalServer(); 
        // disconnect after server close 
        this.datagramSocket1.close();
        assertTrue(this.channel1.isOpen());
        assertTrue(this.channel1.isConnected());
        disconnectAfterConnected(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.disconnect()'