Code example for DatagramChannel

Methods: configureBlocking, connect, disconnect, isConnected

0
     * 
     * @throws IOException 
     */ 
    public void testConnect_NonBlockWithServer() throws IOException { 
        // Non blocking mode 
        this.channel1.configureBlocking(false);
        connectLocalServer(); 
        datagramSocket1.close();
        disconnectAfterConnected(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 
     * 
     * @throws IOException 
     */ 
    public void testConnect_Null() throws IOException { 
        assertFalse(this.channel1.isConnected());
        try { 
            this.channel1.connect(null);
            fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$
        } catch (IllegalArgumentException e) {
            // OK. 
        } 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 
     * 
     * @throws IOException 
     */ 
    public void testConnect_UnsupportedType() throws IOException { 
        assertFalse(this.channel1.isConnected());
        class SubSocketAddress extends SocketAddress {
            private static final long serialVersionUID = 1L;
 
            public SubSocketAddress() { 
                super(); 
            } 
        } 
        SocketAddress newTypeAddress = new SubSocketAddress();
        try { 
            this.channel1.connect(newTypeAddress);
            fail("Should throw an UnsupportedAddressTypeException here.");
        } catch (UnsupportedAddressTypeException e) {
            // OK. 
        } 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 
     * 
     * @throws IOException 
     */ 
    public void testConnect_Unresolved() throws IOException { 
        assertFalse(this.channel1.isConnected());
        InetSocketAddress unresolved = new InetSocketAddress(
                "unresolved address", 1080); 
        try { 
            this.channel1.connect(unresolved);
            fail("Should throw an UnresolvedAddressException here."); //$NON-NLS-1$
        } catch (UnresolvedAddressException e) {
            // OK. 
        } 
    } 
 
    public void testConnect_EmptyHost() throws Exception { 
        assertFalse(this.channel1.isConnected());
 
        assertEquals(this.channel1, this.channel1
                .connect(new InetSocketAddress("", 1081))); //$NON-NLS-1$
 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 
     * 
     * @throws IOException 
     * 
     */ 
    public void testConnect_ClosedChannelException() throws IOException { 
        assertFalse(this.channel1.isConnected());
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
        try { 
            this.channel1.connect(localAddr1);
            fail("Should throw ClosedChannelException."); //$NON-NLS-1$
        } catch (ClosedChannelException e) {
            // OK. 
        } 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 
     * 
     * @throws IOException 
     * 
     */ 
    public void testConnect_IllegalStateException() throws IOException { 
        assertFalse(this.channel1.isConnected());
        this.channel1.connect(localAddr1);
        assertTrue(this.channel1.isConnected());
        // connect after connected. 
        try { 
            this.channel1.connect(localAddr1);
            fail("Should throw IllegalStateException."); //$NON-NLS-1$
        } catch (IllegalStateException e) {
            // OK. 
        } 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 
     * 
     * @throws IOException 
     * 
     */ 
    public void testConnect_CheckOpenBeforeStatus() throws IOException { 
        assertFalse(this.channel1.isConnected());
        this.channel1.connect(localAddr1);
        assertTrue(this.channel1.isConnected());
        // connect after connected. 
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
        // checking open is before checking status. 
        try { 
            this.channel1.connect(localAddr1);
            fail("Should throw ClosedChannelException."); //$NON-NLS-1$
        } catch (ClosedChannelException e) {
            // 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();