Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 85546 invoked from network); 23 Sep 2009 12:57:05 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 23 Sep 2009 12:57:05 -0000 Received: (qmail 1268 invoked by uid 500); 23 Sep 2009 12:57:05 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 1208 invoked by uid 500); 23 Sep 2009 12:57:05 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 1199 invoked by uid 99); 23 Sep 2009 12:57:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Sep 2009 12:57:05 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Sep 2009 12:57:03 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 2B3862388907; Wed, 23 Sep 2009 12:56:43 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r818086 - /harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SocketChannelTest.java Date: Wed, 23 Sep 2009 12:56:43 -0000 To: commits@harmony.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090923125643.2B3862388907@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tellison Date: Wed Sep 23 12:56:42 2009 New Revision: 818086 URL: http://svn.apache.org/viewvc?rev=818086&view=rev Log: More tests to explore the NIO writev gathering functionality. Modified: harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SocketChannelTest.java Modified: harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SocketChannelTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SocketChannelTest.java?rev=818086&r1=818085&r2=818086&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SocketChannelTest.java (original) +++ harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SocketChannelTest.java Wed Sep 23 12:56:42 2009 @@ -28,6 +28,7 @@ import java.net.Socket; import java.net.SocketAddress; import java.net.SocketException; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.channels.AlreadyConnectedException; import java.nio.channels.ClosedChannelException; @@ -2683,7 +2684,208 @@ sc.close(); sock.close(); } - + + /** + * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) + */ + public void test_write$LByteBuffer2() throws IOException { + // Set-up + ServerSocketChannel server = ServerSocketChannel.open(); + server.socket().bind(null); + SocketChannel client = SocketChannel.open(); + client.connect(server.socket().getLocalSocketAddress()); + SocketChannel worker = server.accept(); + + // Test overlapping buffers + byte[] data = "Hello world!".getBytes("UTF-8"); + ByteBuffer[] buffers = new ByteBuffer[3]; + buffers[0] = ByteBuffer.wrap(data, 0, 6); + buffers[1] = ByteBuffer.wrap(data, 6, data.length - 6); + buffers[2] = ByteBuffer.wrap(data); + + // Write them out, read what we wrote and check it + client.write(buffers); + ByteBuffer readBuffer = ByteBuffer.allocate(1024); + worker.read(readBuffer); + readBuffer.flip(); + Buffer expected = ByteBuffer.allocate(1024).put(data).put(data).flip(); + assertEquals(expected, readBuffer); + + // Tidy-up + worker.close(); + client.close(); + server.close(); + } + + /** + * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) + */ + public void test_write$LByteBuffer_buffers() throws IOException { + // Set-up + ServerSocketChannel server = ServerSocketChannel.open(); + server.socket().bind(null); + SocketChannel client = SocketChannel.open(); + client.connect(server.socket().getLocalSocketAddress()); + SocketChannel worker = server.accept(); + + // A variety of buffer types to write + byte[] data = "Hello world!".getBytes("UTF-8"); + ByteBuffer[] buffers = new ByteBuffer[3]; + buffers[0] = ByteBuffer.wrap(data, 0, 2); + assertFalse(buffers[0].isDirect()); + assertTrue(buffers[0].hasArray()); + + buffers[1] = ByteBuffer.wrap(data, 2, 4).asReadOnlyBuffer(); + assertFalse(buffers[1].isDirect()); + assertFalse(buffers[1].hasArray()); + + buffers[2] = ByteBuffer.allocateDirect(42); + buffers[2].put(data, 6, data.length - 6); + buffers[2].flip(); + assertTrue(buffers[2].isDirect()); + assertFalse(buffers[2].hasArray()); + + // Write them out, read what we wrote and check it + client.write(buffers); + ByteBuffer readBuffer = ByteBuffer.allocate(1024); + worker.read(readBuffer); + readBuffer.flip(); + assertEquals(ByteBuffer.wrap(data), readBuffer); + + // Tidy-up + worker.close(); + client.close(); + server.close(); + } + + /** + * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) + */ + public void test_write$LByteBuffer_writes() throws IOException { + // Set-up + ServerSocketChannel server = ServerSocketChannel.open(); + server.socket().bind(null); + SocketChannel client = SocketChannel.open(); + client.connect(server.socket().getLocalSocketAddress()); + SocketChannel worker = server.accept(); + + // Data to write + byte[] data = "Hello world!".getBytes("UTF-8"); + ByteBuffer[] buffers = new ByteBuffer[3]; + buffers[0] = ByteBuffer.wrap(data, 0, 6); + buffers[1] = ByteBuffer.wrap("world!".getBytes("UTF-8")); + buffers[2] = buffers[0]; + assertTrue(buffers[0].hasArray()); + + // Test a sequence of write calls + client.write(buffers, 0, 0); // write nothing + client.write(buffers, 1, 0); // write nothing + client.write(buffers, 0, 1); // write "Hello " + assertEquals("Failed to drain buffer 0", 0, buffers[0].remaining()); + assertEquals("Shouldn't touch buffer 1", buffers[1].limit(), buffers[1] + .remaining()); + client.write(buffers, 0, 2); // writes "world!" + assertEquals("Failed to drain buffer 1", 0, buffers[1].remaining()); + client.write(buffers, 0, 3); // write nothing + + // Read what we wrote and check it + ByteBuffer readBuffer = ByteBuffer.allocate(1024); + worker.read(readBuffer); + readBuffer.flip(); + assertEquals(ByteBuffer.wrap(data), readBuffer); + + // Tidy-up + worker.close(); + client.close(); + server.close(); + } + + /** + * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) + */ + public void test_write$LByteBuffer_invalid() throws IOException { + // Set-up + ServerSocketChannel server = ServerSocketChannel.open(); + server.socket().bind(null); + + SocketChannel client = SocketChannel.open(); + client.connect(server.socket().getLocalSocketAddress()); + + SocketChannel worker = server.accept(); + + // Do some stuff + try { + client.write((ByteBuffer[]) null); + fail("Should throw a NPE"); + } catch (NullPointerException e) { + // expected + } + try { + client.write((ByteBuffer[]) null, 0, 0); + fail("Should throw a NPE"); + } catch (NullPointerException e) { + // expected + } + try { + client.write((ByteBuffer[]) null, 1, 0); + fail("Should throw a NPE"); + } catch (NullPointerException e) { + // expected + } + try { + client.write((ByteBuffer[]) null, 0, 1); + fail("Should throw a NPE"); + } catch (NullPointerException e) { + // expected + } + try { + client.write((ByteBuffer[]) null, 1, 1); + fail("Should throw a NPE"); + } catch (NullPointerException e) { + // expected + } + + ByteBuffer[] buffers = new ByteBuffer[2]; + buffers[0] = ByteBuffer.wrap("Hello ".getBytes("UTF-8")); + buffers[1] = ByteBuffer.wrap("world!".getBytes("UTF-8")); + + try { + client.write((ByteBuffer[]) null, -1, 0); + fail("Should throw a IOBE"); + } catch (IndexOutOfBoundsException e) { + // expected + } + try { + client.write((ByteBuffer[]) null, 0, -1); + fail("Should throw a IOBE"); + } catch (IndexOutOfBoundsException e) { + // expected + } + try { + client.write(buffers, 0, 42); + fail("Should throw a IOBE"); + } catch (IndexOutOfBoundsException e) { + // expected + } + try { + client.write(buffers, 42, 0); + fail("Should throw a IOBE"); + } catch (IndexOutOfBoundsException e) { + // expected + } + try { + client.write(buffers, 1, 2); + fail("Should throw a IOBE"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + // Tidy-up + worker.close(); + client.close(); + server.close(); + } + public void testSocket_configureblocking() throws IOException { byte[] serverWBuf = new byte[CAPACITY_NORMAL]; for (int i = 0; i < serverWBuf.length; i++) {