Code example for DatagramChannel

Methods: close, configureBlocking, connect, isConnected, isOpen, receive

0
        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()' 
     * 
     * @throws IOException 
     */ 
    public void testDisconnect_NonBlockWithServerServerClosed() 
            throws IOException { 
        this.channel1.configureBlocking(false);
        assertFalse(this.channel1.isBlocking());
        connectLocalServer(); 
        // disconnect after server close 
        this.datagramSocket1.close();
        assertTrue(this.channel1.isOpen());
        assertTrue(this.channel1.isConnected());
        disconnectAfterConnected(); 
    } 
 
    // ------------------------------------------------------------------- 
    // Test for receive(): Behavior Without Server. 
    // ------------------------------------------------------------------- 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_UnconnectedNull() throws Exception { 
        assertFalse(this.channel1.isConnected());
        try { 
            this.channel1.receive(null);
            fail("Should throw a NPE here."); //$NON-NLS-1$
        } catch (NullPointerException e) {
            // OK. 
        } 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_UnconnectedReadonly() throws Exception { 
        assertFalse(this.channel1.isConnected());
        ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL)
                .asReadOnlyBuffer();
        assertTrue(dst.isReadOnly());
        try { 
            this.channel1.receive(dst);
            fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$
        } catch (IllegalArgumentException e) {
            // OK. 
        } 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_UnconnectedBufEmpty() throws Exception { 
        this.channel1.configureBlocking(false);
        assertFalse(this.channel1.isConnected());
        ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        assertNull(this.channel1.receive(dst));
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_UnconnectedBufZero() throws Exception { 
        assertFalse(this.channel1.isConnected());
        ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ZERO);
        assertNull(this.channel1.receive(dst));
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_UnconnectedBufNotEmpty() throws Exception { 
        assertFalse(this.channel1.isConnected());
        ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        // buf is not empty 
        dst.put((byte) 88);
        assertEquals(dst.position() + CAPACITY_NORMAL - 1, dst.limit());
        assertNull(this.channel1.receive(dst));
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_UnconnectedBufFull() throws Exception { 
        assertFalse(this.channel1.isConnected());
        ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ONE);
        // buf is full 
        dst.put((byte) 88);
        assertEquals(dst.position(), dst.limit());
        assertNull(this.channel1.receive(dst));
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_UnconnectedClose() throws Exception { 
        assertFalse(this.channel1.isConnected());
        ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
        try { 
            assertNull(this.channel1.receive(dst));
            fail("Should throw a ClosedChannelException here."); //$NON-NLS-1$
        } catch (ClosedChannelException e) {
            // OK. 
        } 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_UnconnectedCloseNull() throws Exception { 
        assertFalse(this.channel1.isConnected());
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
        // checking buffer before checking open 
        try { 
            this.channel1.receive(null);
            fail("Should throw a NPE here."); //$NON-NLS-1$
        } catch (NullPointerException e) {
            // OK. 
        } 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_UnconnectedCloseReadonly() throws Exception { 
        assertFalse(this.channel1.isConnected());
        ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL)
                .asReadOnlyBuffer();
        assertTrue(dst.isReadOnly());
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
        try { 
            this.channel1.receive(dst);
            fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$
        } catch (IllegalArgumentException e) {
            // OK. 
        } 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_NonBlockNoServerBufEmpty() throws Exception { 
        this.channel1.configureBlocking(false);
        receiveNonBlockNoServer(CAPACITY_NORMAL);
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_BlockNoServerNull() throws Exception { 
        assertTrue(this.channel1.isBlocking());
        receiveNoServerNull(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_NonBlockNoServerNull() throws Exception { 
        this.channel1.configureBlocking(false);
        receiveNoServerNull(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_BlockNoServerReadonly() throws Exception { 
        assertTrue(this.channel1.isBlocking());
        receiveNoServerReadonly(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_NonBlockNoServerReadonly() throws Exception { 
        this.channel1.configureBlocking(false);
        receiveNoServerReadonly(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_NonBlockNoServerBufZero() throws Exception { 
        this.channel1.configureBlocking(false);
        receiveNonBlockNoServer(CAPACITY_ZERO);
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_NonBlockNoServerBufNotEmpty() throws Exception { 
        this.channel1.configureBlocking(false);
        connectWithoutServer(); 
        ByteBuffer dst = allocateNonEmptyBuf();
        assertNull(this.channel1.receive(dst));
    } 
 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_NonBlockNoServerBufFull() throws Exception { 
        this.channel1.configureBlocking(false);
        connectWithoutServer(); 
        ByteBuffer dst = allocateFullBuf();
        assertNull(this.channel1.receive(dst));
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_BlockNoServerChannelClose() throws Exception { 
        assertTrue(this.channel1.isBlocking());
        receiveNoServerChannelClose(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_NonBlockNoServerChannelClose() throws Exception { 
        this.channel1.configureBlocking(false);
        receiveNoServerChannelClose(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_BlockNoServerCloseNull() throws Exception { 
        assertTrue(this.channel1.isBlocking());
        receiveNoServerChannelCloseNull(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_NonBlockNoServerCloseNull() throws Exception { 
        this.channel1.configureBlocking(false);
        receiveNoServerChannelCloseNull(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_NonBlockNoServerCloseReadonly() throws Exception { 
        this.channel1.configureBlocking(false);
        receiveNoServerChannelCloseReadonly(); 
    } 
 
    /** 
     * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 
     * 
     * @throws Exception 
     */ 
    public void testReceive_BlockNoServerCloseReadonly() throws Exception { 
        assertTrue(this.channel1.isBlocking());
        receiveNoServerChannelCloseReadonly(); 
    } 
 
    private void receiveNoServerNull() throws IOException { 
        connectWithoutServer(); 
        try { 
            this.channel1.receive(null);
            fail("Should throw a NPE here."); //$NON-NLS-1$
        } catch (NullPointerException e) {
            // OK. 
        } 
    } 
 
    private void receiveNoServerReadonly() throws IOException { 
        connectWithoutServer(); 
        ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL)
                .asReadOnlyBuffer();
        assertTrue(dst.isReadOnly());
        try { 
            this.channel1.receive(dst);
            fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$
        } catch (IllegalArgumentException e) {
            // OK. 
        } 
    } 
 
    private void receiveNonBlockNoServer(int size) throws IOException {
        connectWithoutServer(); 
        ByteBuffer dst = ByteBuffer.allocateDirect(size);
        assertNull(this.channel1.receive(dst));
    } 
 
    private void receiveNoServerChannelClose() throws IOException { 
        connectWithoutServer(); 
        ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
        try { 
            assertNull(this.channel1.receive(dst));
            fail("Should throw a ClosedChannelException here."); //$NON-NLS-1$
        } catch (ClosedChannelException e) {
            // OK. 
        } 
    }