harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
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 GMT
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++) {



Mime
View raw message