Search in sources :

Example 21 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class AbstractSelectorTest method test_register_LSelectorI_error.

/**
     * @tests AbstractSelector#register(Selector,int)
     */
public void test_register_LSelectorI_error() throws IOException {
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.configureBlocking(false);
    acceptSelector.close();
    assertFalse(acceptSelector.isOpen());
    try {
        ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    assertFalse(ssc.isRegistered());
    acceptSelector = Selector.open();
    ssc.configureBlocking(true);
    try {
        ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    assertFalse(ssc.isRegistered());
    ssc.configureBlocking(false);
    SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
    assertNotNull(acceptKey);
    assertTrue(acceptSelector.keys().contains(acceptKey));
    assertTrue(ssc.isRegistered());
}
Also used : SelectionKey(java.nio.channels.SelectionKey) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 22 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class SocketChannelTest method test_socket_getOutputStream_nonBlocking_read_Exception.

public void test_socket_getOutputStream_nonBlocking_read_Exception() throws IOException {
    byte[] buf = new byte[1];
    channel1.connect(this.localAddr1);
    InputStream is = channel1.socket().getInputStream();
    channel1.configureBlocking(false);
    try {
        is.read();
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
    try {
        is.read(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        is.read(buf, -1, 1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        is.read(buf, 0, -1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        is.read(buf, 0, 2);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        is.read(buf, 2, 0);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        is.read(null, 0, 0);
        fail();
    } catch (NullPointerException expected) {
    }
    is.close();
    try {
        is.read();
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
    try {
        is.read(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        is.read(buf, -1, 1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        is.read(buf, 0, -1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        is.read(buf, 0, 2);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        is.read(buf, 2, 0);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        is.read(null, 0, 0);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : InputStream(java.io.InputStream) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 23 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class ServerSocketChannelTest method test_socket_accept_Blocking_NotBound.

/**
     * @tests ServerSocket#socket().accept()
     */
public void test_socket_accept_Blocking_NotBound() throws IOException {
    // regression test for Harmony-748
    ServerSocket gotSocket = serverChannel.socket();
    serverChannel.configureBlocking(true);
    try {
        gotSocket.accept();
        fail("Should throw an IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    serverChannel.close();
    try {
        gotSocket.accept();
        fail("Should throw an IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
}
Also used : ServerSocket(java.net.ServerSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 24 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class SocketChannelTest method testSocket_configureblocking.

public void testSocket_configureblocking() throws IOException {
    byte[] serverWBuf = new byte[CAPACITY_NORMAL];
    for (int i = 0; i < serverWBuf.length; i++) {
        serverWBuf[i] = (byte) i;
    }
    java.nio.ByteBuffer buf = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL + 1);
    channel1.connect(localAddr1);
    server1.accept();
    Socket sock = this.channel1.socket();
    channel1.configureBlocking(false);
    assertFalse(channel1.isBlocking());
    OutputStream channelSocketOut = sock.getOutputStream();
    try {
        // write operation is not allowed in non-blocking mode
        channelSocketOut.write(buf.array());
        fail("Non-Blocking mode should cause IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // correct
    }
    channel1.configureBlocking(true);
    assertTrue(channel1.isBlocking());
    // write operation is allowed in blocking mode
    channelSocketOut.write(buf.array());
}
Also used : OutputStream(java.io.OutputStream) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) ByteBuffer(java.nio.ByteBuffer) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 25 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class SocketChannelTest method test_socket_getOutputStream_nonBlocking_write_Exception.

public void test_socket_getOutputStream_nonBlocking_write_Exception() throws IOException {
    byte[] buf = new byte[1];
    channel1.connect(this.localAddr1);
    OutputStream os = channel1.socket().getOutputStream();
    channel1.configureBlocking(false);
    try {
        os.write(1);
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
    try {
        os.write(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        os.write(buf, -1, 1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        os.write(buf, 0, -1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        os.write(buf, 0, 2);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        os.write(buf, 2, 0);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        os.write(null, 0, 0);
        fail();
    } catch (NullPointerException expected) {
    }
    os.close();
    try {
        os.write(1);
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
    try {
        os.write(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        os.write(buf, -1, 1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        os.write(buf, 0, -1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        os.write(buf, 0, 2);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        os.write(buf, 2, 0);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        os.write(null, 0, 0);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : OutputStream(java.io.OutputStream) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Aggregations

IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)36 ServerSocket (java.net.ServerSocket)14 SocketChannel (java.nio.channels.SocketChannel)10 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 Socket (java.net.Socket)7 ClosedChannelException (java.nio.channels.ClosedChannelException)7 SelectionKey (java.nio.channels.SelectionKey)7 Selector (java.nio.channels.Selector)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)6 BindException (java.net.BindException)5 InetSocketAddress (java.net.InetSocketAddress)5 SocketTimeoutException (java.net.SocketTimeoutException)5 IllegalSelectorException (java.nio.channels.IllegalSelectorException)5 InputStream (java.io.InputStream)4 DatagramSocket (java.net.DatagramSocket)4 SocketException (java.net.SocketException)4 UnknownHostException (java.net.UnknownHostException)4 DatagramChannel (java.nio.channels.DatagramChannel)4 SocketAddress (java.net.SocketAddress)3