Search in sources :

Example 11 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project j2objc by google.

the class ServerSocketChannelTest method test_socket_accept_Nonblocking_NotBound.

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

Example 12 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project j2objc by google.

the class ServerSocketChannelTest method test_socket_accept_Nonblocking_Bound.

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

Example 13 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project j2objc by google.

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 14 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project j2objc by google.

the class IllegalBlockingModeExceptionTest method test_Constructor.

/**
 * @tests {@link java.nio.channels.IllegalBlockingModeException#IllegalBlockingModeException()}
 */
public void test_Constructor() {
    IllegalBlockingModeException e = new IllegalBlockingModeException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 15 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project j2objc by google.

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 ClosedSelectorException");
    } catch (ClosedSelectorException 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) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

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