use of java.nio.channels.NotYetConnectedException in project robovm by robovm.
the class DatagramChannelTest method testReadByteBuffer_bufNull.
public void testReadByteBuffer_bufNull() throws IOException {
ByteBuffer readBuf = ByteBuffer.allocateDirect(0);
InetSocketAddress ipAddr = localAddr1;
try {
this.channel1.read(readBuf);
fail("should throw NotYetConnectedException");
} catch (NotYetConnectedException e) {
// correct
}
this.channel1.connect(ipAddr);
assertTrue(this.channel1.isConnected());
try {
channel1.read((ByteBuffer) null);
fail("should throw NPE");
} catch (NullPointerException e) {
// correct
}
this.channel1.configureBlocking(false);
// note : blocking-mode will make the read process endless!
assertEquals(0, this.channel1.read(readBuf));
datagramSocket1.close();
}
use of java.nio.channels.NotYetConnectedException in project robovm by robovm.
the class DatagramChannelTest method test_read_LByteBuffer_readOnlyBuf.
/**
* @tests DatagramChannel#read(ByteBuffer)
*/
public void test_read_LByteBuffer_readOnlyBuf() throws Exception {
// regression test for Harmony-754
ByteBuffer c = ByteBuffer.allocate(1);
DatagramChannel channel = DatagramChannel.open();
try {
channel.read(c.asReadOnlyBuffer());
fail("Should throw NotYetConnectedException");
} catch (NotYetConnectedException e) {
} catch (IllegalArgumentException e) {
// expected
}
channel.connect(localAddr1);
try {
channel.read(c.asReadOnlyBuffer());
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
}
use of java.nio.channels.NotYetConnectedException in project robovm by robovm.
the class DatagramChannelTest method testReadByteBufferArrayIntInt.
/*
* Test method for 'DatagramChannelImpl.read(ByteBuffer[], int, int)'
*/
public void testReadByteBufferArrayIntInt() throws IOException {
ByteBuffer[] readBuf = new ByteBuffer[2];
readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
readBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
InetSocketAddress ipAddr = localAddr1;
try {
this.channel1.read(readBuf, 0, 2);
fail("should throw NotYetConnectedException");
} catch (NotYetConnectedException e) {
// correct
}
this.channel1.connect(ipAddr);
assertTrue(this.channel1.isConnected());
this.channel1.configureBlocking(false);
// note : blocking-mode will make the read process endless!
assertEquals(0, this.channel1.read(readBuf, 0, 1));
assertEquals(0, this.channel1.read(readBuf, 0, 2));
datagramSocket1.close();
}
use of java.nio.channels.NotYetConnectedException in project robovm by robovm.
the class DatagramChannelTest method testWriteByteBufferArrayIntInt_Block.
/*
* Test method for 'DatagramChannelImpl.write(ByteBuffer[], int, int)'
*/
public void testWriteByteBufferArrayIntInt_Block() throws IOException {
ByteBuffer[] writeBuf = new ByteBuffer[2];
writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
InetSocketAddress ipAddr = localAddr1;
try {
this.channel1.write(writeBuf, 0, 2);
fail("Should throw NotYetConnectedException.");
} catch (NotYetConnectedException e) {
// correct
}
this.channel1.connect(ipAddr);
assertTrue(this.channel1.isConnected());
assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2));
// cannot be buffered again!
assertEquals(0, this.channel1.write(writeBuf, 0, 1));
}
use of java.nio.channels.NotYetConnectedException in project jdk8u_jdk by JetBrains.
the class Receive method doTest.
void doTest(SocketAddress peerAddress) {
SctpChannel channel = null;
ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
MessageInfo info;
try {
channel = SctpChannel.open();
ReceiveNotificationHandler handler = new ReceiveNotificationHandler(channel);
/* TEST 1: Verify NotYetConnectedException thrown */
try {
channel.receive(buffer, null, handler);
fail("should have thrown NotYetConnectedException");
} catch (NotYetConnectedException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
channel.connect(peerAddress);
/* TEST 2: receive small message */
do {
debug("Test 2: invoking receive");
info = channel.receive(buffer, null, handler);
if (info == null) {
fail("unexpected null from receive");
return;
}
} while (!info.isComplete());
buffer.flip();
check(handler.receivedCommUp(), "SCTP_COMM_UP not received");
check(info != null, "info is null");
check(info.address() != null, "address is null");
check(info.association() != null, "association is null");
check(info.isComplete(), "message is not complete");
check(info.isUnordered() != true, "message should not be unordered");
check(info.streamNumber() >= 0, "invalid stream number");
check(info.payloadProtocolID() == PPID, "PPID incorrect");
check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").length, "bytes received not equal to message length");
check(info.bytes() == buffer.remaining(), "bytes != remaining");
check(Util.compare(buffer, Util.SMALL_MESSAGE), "received message not the same as sent message");
buffer.clear();
/* TEST 3: receive large message */
do {
debug("Test 3: invoking receive");
info = channel.receive(buffer, null, handler);
if (info == null) {
fail("unexpected null from receive");
return;
}
} while (!info.isComplete());
buffer.flip();
check(info != null, "info is null");
check(info.address() != null, "address is null");
check(info.association() != null, "association is null");
check(info.isComplete(), "message is not complete");
check(info.isUnordered() != true, "message should not be unordered");
check(info.streamNumber() >= 0, "invalid stream number");
check(info.bytes() == Util.LARGE_MESSAGE.getBytes("ISO-8859-1").length, "bytes received not equal to message length");
check(info.bytes() == buffer.remaining(), "bytes != remaining");
check(Util.compare(buffer, Util.LARGE_MESSAGE), "received message not the same as sent message");
buffer.clear();
/* TEST 4: EOF */
// buffer position 0
buffer.clear();
info = channel.receive(buffer, null, handler);
check(info != null, "info is null");
check(info.bytes() == -1, "should have received EOF");
check(buffer.position() == 0, "buffer position should be unchanged");
/* TEST 5: ClosedChannelException */
channel.close();
try {
channel.receive(buffer, null, null);
fail("should have thrown ClosedChannelException");
} catch (ClosedChannelException cce) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
handler = null;
/* TEST 6: handler returns RETURN after handling a notification */
ReceiveNotificationHandler handler2 = new ReceiveNotificationHandler(null);
/* HandlerResult.RETURN */
channel = SctpChannel.open(peerAddress, 0, 0);
info = channel.receive(buffer, null, handler2);
check(info == null, "channel should return null");
check(handler2.receivedCommUp(), "SCTP_COMM_UP not received");
check(buffer.position() == 0, "buffer position should be unchanged");
/* TEST 7: Non blocking channel return null if no data */
channel.configureBlocking(false);
info = channel.receive(buffer, null, null);
check(info == null, "non-blocking channel should return null");
check(buffer.position() == 0, "buffer position should be unchanged");
} catch (IOException ioe) {
unexpected(ioe);
} finally {
clientFinishedLatch.countDown();
try {
serverFinishedLatch.await(10L, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
unexpected(ie);
}
if (channel != null) {
try {
channel.close();
} catch (IOException e) {
unexpected(e);
}
}
}
}
Aggregations