2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.harmony.nio.tests.java.nio.channels;
20 import dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestTargetClass;
23 import dalvik.annotation.TestLevel;
24 import dalvik.annotation.AndroidOnly;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.net.BindException;
30 import java.net.ConnectException;
31 import java.net.InetSocketAddress;
32 import java.net.ServerSocket;
33 import java.net.Socket;
34 import java.net.SocketAddress;
35 import java.net.SocketException;
36 import java.nio.ByteBuffer;
37 import java.nio.channels.AlreadyConnectedException;
38 import java.nio.channels.ClosedChannelException;
39 import java.nio.channels.ConnectionPendingException;
40 import java.nio.channels.IllegalBlockingModeException;
41 import java.nio.channels.NoConnectionPendingException;
42 import java.nio.channels.NotYetConnectedException;
43 import java.nio.channels.SelectableChannel;
44 import java.nio.channels.SelectionKey;
45 import java.nio.channels.ServerSocketChannel;
46 import java.nio.channels.SocketChannel;
47 import java.nio.channels.UnresolvedAddressException;
48 import java.nio.channels.UnsupportedAddressTypeException;
49 import java.nio.channels.spi.SelectorProvider;
51 import junit.framework.TestCase;
52 import tests.support.Support_PortManager;
54 @TestTargetClass(SocketChannel.class)
56 * Tests for SocketChannel and its default implementation.
58 public class SocketChannelTest extends TestCase {
60 private static final int CAPACITY_NORMAL = 200;
62 private static final int CAPACITY_HUGE = 512 * 1024;
64 private InetSocketAddress localAddr1;
66 private InetSocketAddress localAddr2;
68 private SocketChannel channel1;
70 private SocketChannel channel2;
72 private ServerSocket server1;
74 private ServerSocket server2;
76 private final static int TIMEOUT = 60000;
78 private final static int EOF = -1;
80 protected void setUp() throws Exception {
82 this.localAddr1 = new InetSocketAddress("127.0.0.1",
83 Support_PortManager.getNextPort());
84 this.localAddr2 = new InetSocketAddress("127.0.0.1",
85 Support_PortManager.getNextPort());
86 this.channel1 = SocketChannel.open();
87 this.channel2 = SocketChannel.open();
88 this.server1 = new ServerSocket(localAddr1.getPort());
91 protected void tearDown() throws Exception {
93 if (null != this.channel1) {
95 this.channel1.close();
96 } catch (Exception e) {
100 if (null != this.channel2) {
102 this.channel2.close();
103 } catch (Exception e) {
107 if (null != this.server1) {
109 this.server1.close();
110 } catch (Exception e) {
114 if (null != this.server2) {
116 this.server2.close();
117 } catch (Exception e) {
123 // -------------------------------------------------------------------
124 // Test for methods in abstract class.
125 // -------------------------------------------------------------------
127 * Test method for 'java.nio.channels.SocketChannel()'
131 level = TestLevel.PARTIAL_COMPLETE,
133 method = "SocketChannel",
134 args = {SelectorProvider.class}
137 level = TestLevel.PARTIAL_COMPLETE,
143 level = TestLevel.PARTIAL_COMPLETE,
149 level = TestLevel.PARTIAL_COMPLETE,
151 clazz = SelectorProvider.class,
152 method = "openSocketChannel",
156 public void testConstructor() throws IOException {
157 SocketChannel channel =
158 SelectorProvider.provider().openSocketChannel();
159 assertNotNull(channel);
160 assertSame(SelectorProvider.provider(), channel.provider());
161 channel = SocketChannel.open();
162 assertNotNull(channel);
163 assertSame(SelectorProvider.provider(), channel.provider());
164 MockSocketChannel chan = new MockSocketChannel(
165 SelectorProvider.provider());
166 assertTrue(chan.isConstructorCalled);
170 * Test method for 'java.nio.channels.SocketChannel.validOps()'
173 level = TestLevel.PARTIAL_COMPLETE,
178 public void testValidOps() {
179 MockSocketChannel testMSChannel = new MockSocketChannel(null);
180 assertEquals(13, this.channel1.validOps());
181 assertEquals(13, testMSChannel.validOps());
185 * Test method for 'java.nio.channels.SocketChannel.open()'
189 level = TestLevel.PARTIAL_COMPLETE,
195 level = TestLevel.PARTIAL_COMPLETE,
197 method = "SocketChannel",
198 args = {SelectorProvider.class}
201 public void testOpen() throws IOException {
202 java.nio.ByteBuffer[] buf = new java.nio.ByteBuffer[1];
203 buf[0] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL);
204 MockSocketChannel testMSChannel = new MockSocketChannel(null);
205 MockSocketChannel testMSChannelnotnull = new MockSocketChannel(
206 SelectorProvider.provider());
207 SocketChannel testSChannel = MockSocketChannel.open();
208 assertTrue(testSChannel.isOpen());
209 assertNull(testMSChannel.provider());
210 assertNotNull(testSChannel.provider());
211 assertEquals(SelectorProvider.provider(), testSChannel.provider());
212 assertNotNull(testMSChannelnotnull.provider());
213 assertEquals(this.channel1.provider(), testMSChannelnotnull.provider());
215 this.channel1.write(buf);
216 fail("Should throw NotYetConnectedException");
217 } catch (NotYetConnectedException e) {
223 level = TestLevel.PARTIAL_COMPLETE,
228 public void testIsOpen() throws Exception {
229 assertTrue(this.channel1.isOpen());
230 this.channel1.close();
231 assertFalse(this.channel1.isOpen());
235 level = TestLevel.COMPLETE,
237 method = "isConnected",
240 public void testIsConnected() throws Exception {
241 assertFalse(this.channel1.isConnected());// not connected
242 this.channel1.configureBlocking(false);
243 assertFalse(this.channel1.connect(localAddr1));
244 assertFalse(this.channel1.isConnected());
245 assertTrue(this.channel1.isConnectionPending());
246 assertTrue(tryFinish());
247 assertTrue(this.channel1.isConnected());
248 this.channel1.close();
249 assertFalse(this.channel1.isConnected());
253 level = TestLevel.COMPLETE,
255 method = "isConnectionPending",
258 public void testIsConnectionPending() throws Exception {
260 ensureServerClosed();
261 this.channel1.configureBlocking(false);
262 assertFalse(this.channel1.isConnectionPending());
265 this.channel1.finishConnect();
266 fail("Should throw NoConnectionPendingException");
267 } catch (NoConnectionPendingException e) {
270 assertFalse(this.channel1.isConnectionPending());
272 assertFalse(this.channel1.connect(localAddr1));
273 assertTrue(this.channel1.isConnectionPending());
274 this.channel1.close();
276 assertFalse(this.channel1.isConnectionPending());
281 level = TestLevel.PARTIAL_COMPLETE,
282 notes = "Verifies default status of SocketChannel.",
287 level = TestLevel.PARTIAL_COMPLETE,
288 notes = "Verifies default status of SocketChannel.",
293 level = TestLevel.PARTIAL_COMPLETE,
294 notes = "Verifies default status of SocketChannel.",
295 method = "isRegistered",
299 level = TestLevel.PARTIAL_COMPLETE,
300 notes = "Verifies default status of SocketChannel.",
301 method = "isBlocking",
305 public void testChannelBasicStatus() {
306 Socket gotSocket = this.channel1.socket();
307 assertFalse(gotSocket.isClosed());
308 assertTrue(this.channel1.isBlocking());
309 assertFalse(this.channel1.isRegistered());
310 assertEquals((SelectionKey.OP_CONNECT | SelectionKey.OP_READ |
311 SelectionKey.OP_WRITE), this.channel1.validOps());
312 assertEquals(SelectorProvider.provider(), this.channel1.provider());
316 * Test method for 'java.nio.channels.SocketChannel.open(SocketAddress)'
319 level = TestLevel.PARTIAL_COMPLETE,
322 args = {java.net.SocketAddress.class}
324 public void testOpenSocketAddress() throws IOException {
325 this.channel1 = SocketChannel.open(localAddr1);
326 assertTrue(this.channel1.isConnected());
328 SecurityManager smngr = System.getSecurityManager();
329 System.setSecurityManager(new MockSecurityManager("blargh"));
331 this.channel1 = SocketChannel.open(localAddr2);
332 fail("Should throw SecurityException");
333 } catch (SecurityException e) {
336 System.setSecurityManager(smngr);
338 SocketAddress newTypeAddress = new SubSocketAddress();
340 this.channel1 = SocketChannel.open(newTypeAddress);
341 fail("Should throw UnexpectedAddressTypeException");
342 } catch (UnsupportedAddressTypeException e) {
346 SocketAddress unresolvedAddress =
347 InetSocketAddress.createUnresolved("127.0.0.1", 8080);
349 this.channel1 = SocketChannel.open(unresolvedAddress);
350 fail("Should throw UnresolvedAddressException");
351 } catch (UnresolvedAddressException e) {
355 SocketChannel channel1IP = null;
357 channel1IP = SocketChannel.open(null);
358 fail("Should throw an IllegalArgumentException");
359 } catch (IllegalArgumentException e) {
362 assertNull(channel1IP);
366 * Test method for 'java.nio.channels.SocketChannel.read(ByteBuffer[])'
369 level = TestLevel.PARTIAL_COMPLETE,
372 args = {java.nio.ByteBuffer[].class}
374 public void testReadByteBufferArray() throws IOException {
375 java.nio.ByteBuffer[] byteBuf = null;
376 MockSocketChannel testMSChannelnull = new MockSocketChannel(null);
377 MockSocketChannel testMSChannel = new MockSocketChannel(
378 SelectorProvider.provider());
379 ServerSocket testServer = new ServerSocket(Support_PortManager
383 this.channel1.read(byteBuf);
384 fail("Should throw NPE");
385 } catch (NullPointerException e) {
388 byteBuf = new java.nio.ByteBuffer[CAPACITY_NORMAL];
390 this.channel1.read(byteBuf);
391 fail("Should throw NotYetConnectedException");
392 } catch (NotYetConnectedException e) {
395 long readNum = CAPACITY_NORMAL;
396 readNum = testMSChannel.read(byteBuf);
397 assertEquals(0, readNum);
398 readNum = CAPACITY_NORMAL;
399 readNum = testMSChannelnull.read(byteBuf);
400 assertEquals(0, readNum);
407 * Test method for 'java.nio.channels.SocketChannel.read(ByteBuffer[])'
410 level = TestLevel.PARTIAL_COMPLETE,
413 args = {java.nio.ByteBuffer[].class}
415 public void testReadByteBufferArray_BufNull() throws IOException {
416 java.nio.ByteBuffer[] byteBuf = null;
417 MockSocketChannel testMSChannelnull = new MockSocketChannel(null);
418 MockSocketChannel testMSChannel = new MockSocketChannel(
419 SelectorProvider.provider());
421 this.channel1.read(byteBuf);
422 fail("Should throw NPE");
423 } catch (NullPointerException e) {
427 testMSChannel.read(byteBuf);
428 fail("Should throw NPE");
429 } catch (NullPointerException e) {
433 testMSChannelnull.read(byteBuf);
434 fail("Should throw NPE");
435 } catch (NullPointerException e) {
441 * Test method for 'java.nio.channels.SocketChannel.write(ByteBuffer[])'
444 level = TestLevel.PARTIAL_COMPLETE,
445 notes = "Doesn't verify AsynchronousCloseException," +
446 "ClosedByInterruptException.",
448 args = {java.nio.ByteBuffer[].class}
450 public void testWriteByteBufferArray() throws IOException {
451 java.nio.ByteBuffer[] byteBuf = null;
452 MockSocketChannel testMSChannelnull = new MockSocketChannel(null);
453 MockSocketChannel testMSChannel = new MockSocketChannel(
454 SelectorProvider.provider());
456 this.channel1.write(byteBuf);
457 fail("Should throw NPE");
458 } catch (NullPointerException e) {
461 byteBuf = new java.nio.ByteBuffer[1];
462 byteBuf[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
464 this.channel1.write(byteBuf);
465 fail("Should throw NotYetConnectedException");
466 } catch (NotYetConnectedException e) {
469 testMSChannel.write(byteBuf);
470 testMSChannelnull.write(byteBuf);
472 this.channel1.close();
474 this.channel1.write(byteBuf);
475 fail("Should throw ClosedChannelException");
476 } catch (ClosedChannelException e) {
482 * Test method for 'java.nio.channels.SocketChannel.write(ByteBuffer[])'
485 level = TestLevel.PARTIAL_COMPLETE,
488 args = {java.nio.ByteBuffer[].class}
490 public void testWriteByteBufferArray_BufNull() throws IOException {
491 java.nio.ByteBuffer[] byteBuf = null;
492 MockSocketChannel testMSChannelnull = new MockSocketChannel(null);
493 MockSocketChannel testMSChannel = new MockSocketChannel(
494 SelectorProvider.provider());
495 this.channel1.connect(localAddr1);
497 this.channel1.write(byteBuf);
498 fail("Should throw NPE");
499 } catch (NullPointerException e) {
502 byteBuf = new java.nio.ByteBuffer[1];
504 this.channel1.write(byteBuf);
505 fail("Should throw NPE");
506 } catch (NullPointerException e) {
512 level = TestLevel.PARTIAL_COMPLETE,
517 @AndroidOnly("Fails on RI. See comment below")
518 public void testSocket_BasicStatusBeforeConnect() throws IOException {
519 assertFalse(this.channel1.isConnected());// not connected
520 Socket s1 = this.channel1.socket();
521 // RI fails here. RI returns 0 while spec says getLocalPort()
522 // shall return -1 for unbound socket
523 assertSocketBeforeConnect(s1);
524 Socket s2 = this.channel1.socket();
530 level = TestLevel.PARTIAL_COMPLETE,
535 public void testSocket_Block_BasicStatusAfterConnect() throws IOException {
536 assertFalse(this.channel1.isConnected());// not connected
537 assertTrue(this.channel1.connect(localAddr1));
539 assertTrue(this.channel1.isConnected());
540 Socket s1 = this.channel1.socket();
542 assertSocketAfterConnect(s1, localAddr1);
543 Socket s2 = this.channel1.socket();
549 level = TestLevel.PARTIAL_COMPLETE,
554 @AndroidOnly("Fails on RI. See comment below")
555 public void testSocket_NonBlock_BasicStatusAfterConnect() throws Exception {
556 assertFalse(this.channel1.isConnected());// not connected
557 this.channel1.configureBlocking(false);
558 assertFalse(this.channel1.connect(localAddr1));
559 assertFalse(this.channel1.isConnected());
560 assertTrue(this.channel1.isConnectionPending());
561 Socket s1 = this.channel1.socket();
562 // status of not connected
563 // RI fails here. RI returns 0 while spec says getLocalPort()
564 // shall return -1 for unbound socket
565 assertSocketBeforeConnect(s1);
566 Socket s2 = this.channel1.socket();
570 assertTrue(tryFinish());
571 assertTrue(this.channel1.isConnected());
572 s1 = this.channel1.socket();
573 assertSocketAfterConnect(s1, localAddr1);
574 s2 = this.channel1.socket();
580 level = TestLevel.PARTIAL_COMPLETE,
585 public void testSocket_Block_ActionsBeforeConnect() throws IOException {
586 assertFalse(this.channel1.isConnected());// not connected
587 Socket s = this.channel1.socket();
588 assertSocketAction_Block_BeforeConnect(s);
592 level = TestLevel.PARTIAL_COMPLETE,
597 public void testSocket_Block_ActionsAfterConnect() throws IOException {
598 assertFalse(this.channel1.isConnected());// not connected
599 assertTrue(this.channel1.connect(localAddr1));
600 assertTrue(this.channel1.isConnected());
601 Socket s = this.channel1.socket();
602 assertSocketAction_Block_AfterConnect(s);
607 level = TestLevel.PARTIAL_COMPLETE,
612 public void testSocket_NonBlock_ActionsAfterConnectBeforeFinish()
614 assertFalse(this.channel1.isConnected());// not connected
615 this.channel1.configureBlocking(false);
616 assertFalse(this.channel1.connect(localAddr1));
617 assertFalse(this.channel1.isConnected());
618 assertTrue(this.channel1.isConnectionPending());
619 Socket s1 = this.channel1.socket();
620 // Action of not connected
621 assertSocketAction_NonBlock_BeforeConnect(s1);
622 Socket s2 = this.channel1.socket();
628 level = TestLevel.PARTIAL_COMPLETE,
633 public void testSocket_NonBlock_ActionsAfterConnectAfterFinish()
635 assertFalse(this.channel1.isConnected());// not connected
636 this.channel1.configureBlocking(false);
637 assertFalse(this.channel1.connect(localAddr1));
638 assertTrue(tryFinish());
639 Socket s1 = this.channel1.socket();
640 assertSocketAction_NonBlock_AfterConnect(s1);
641 Socket s2 = this.channel1.socket();
646 private void assertSocketBeforeConnect(Socket s) throws IOException {
647 assertFalse(s.isBound());
648 assertFalse(s.isClosed());
649 assertFalse(s.isConnected());
650 assertFalse(s.getKeepAlive());
653 fail("Should throw SocketException.");
654 } catch (SocketException e) {
657 assertFalse(s.getOOBInline());
660 fail("Should throw SocketException.");
661 } catch (SocketException e) {
664 assertEquals(-1, s.getSoLinger());
665 assertFalse(s.getTcpNoDelay());
667 assertFalse(s.isInputShutdown());
668 assertFalse(s.isOutputShutdown());
670 assertNull(s.getInetAddress());
671 assertEquals(s.getLocalAddress().getHostAddress(), "0.0.0.0");
672 assertFalse(s.getReuseAddress());
673 assertNull(s.getLocalSocketAddress());
676 assertEquals(0, s.getPort());
677 assertTrue(s.getReceiveBufferSize() >= 8192);
678 assertNull(s.getRemoteSocketAddress());
679 assertTrue(s.getSendBufferSize() >= 8192);
680 assertEquals(0, s.getSoTimeout());
681 assertEquals(0, s.getTrafficClass());
683 // RI fails here. RI returns 0 while spec says unbound socket should
685 assertEquals(-1, s.getLocalPort());
688 private void assertSocketAfterConnect(Socket s, InetSocketAddress address)
690 assertTrue(s.isBound());
691 assertFalse(s.isClosed());
692 assertTrue(s.isConnected());
693 assertFalse(s.getKeepAlive());
695 assertNotNull(s.getInputStream());
696 assertNotNull(s.getOutputStream());
698 assertFalse(s.getOOBInline());
699 assertEquals(-1, s.getSoLinger());
700 assertFalse(s.getTcpNoDelay());
702 assertFalse(s.isInputShutdown());
703 assertFalse(s.isOutputShutdown());
705 assertSame(s.getInetAddress(), address.getAddress());
707 assertEquals(this.localAddr1.getAddress(), s.getLocalAddress());
708 assertEquals(address.getPort(), s.getPort());
709 assertNotNull(s.getLocalSocketAddress());
710 assertTrue(s.getReceiveBufferSize() >= 8192);
712 assertNotSame(address, s.getRemoteSocketAddress());
713 assertEquals(address, s.getRemoteSocketAddress());
714 // assertFalse(s.getReuseAddress());
715 assertTrue(s.getSendBufferSize() >= 8192);
716 assertEquals(0, s.getSoTimeout());
717 assertEquals(0, s.getTrafficClass());
720 private void assertSocketAction_Block_BeforeConnect(Socket s)
722 assertFalse(this.channel1.isConnected());
723 this.server2 = new ServerSocket(localAddr2.getPort());
724 s.connect(localAddr2);
725 assertTrue(this.channel1.isConnected());
726 assertTrue(s.isConnected());
728 assertSocketAfterConnect(s, localAddr2);
732 fail("Should throw AlreadyConnectedException");
733 } catch (AlreadyConnectedException e) {
738 assertTrue(s.isClosed());
739 assertFalse(this.channel1.isOpen());
742 private void assertSocketAction_NonBlock_BeforeConnect(Socket s)
744 assertFalse(this.channel1.isConnected());
745 this.server2 = new ServerSocket(localAddr2.getPort());
747 s.connect(localAddr2);
748 fail("Should throw IllegalBlockingModeException");
749 } catch (IllegalBlockingModeException e1) {
753 if (this.channel1.isConnectionPending()) {
756 fail("Should throw ConnectionPendingException");
757 } catch (ConnectionPendingException e1) {
763 fail("Should throw BindException");
764 } catch (BindException e1) {
769 assertFalse(this.channel1.isConnected());
770 assertFalse(s.isConnected());
773 assertTrue(s.isClosed());
774 assertFalse(this.channel1.isOpen());
777 private void assertSocketAction_Block_AfterConnect(Socket s)
779 assertEquals(localAddr1.getPort(), s.getPort());
780 assertTrue(this.channel1.isConnected());
781 assertTrue(s.isConnected());
783 s.connect(localAddr2);
784 fail("Should throw AlreadyConnectedException");
785 } catch (AlreadyConnectedException e) {
791 fail("Should throw AlreadyConnectedException");
792 } catch (AlreadyConnectedException e) {
797 assertTrue(s.isClosed());
798 assertFalse(this.channel1.isOpen());
801 private void assertSocketAction_NonBlock_AfterConnect(Socket s)
803 assertEquals(localAddr1.getPort(), s.getPort());
804 assertTrue(this.channel1.isConnected());
805 assertTrue(s.isConnected());
807 if (this.channel1.isConnectionPending()) {
809 s.connect(localAddr2);
810 fail("Should throw AlreadyConnectedException");
811 } catch (AlreadyConnectedException e) {
816 s.connect(localAddr2);
817 fail("Should throw IllegalBlockingModeException");
818 } catch (IllegalBlockingModeException e) {
825 fail("Should throw AlreadyConnectedException");
826 } catch (AlreadyConnectedException e) {
831 assertTrue(s.isClosed());
832 assertFalse(this.channel1.isOpen());
835 // -------------------------------------------------------------------
836 // Tests for connect(), finishConnect(),isConnected(),isConnectionPending()
837 // These methods are very close, so we test them together, call them "CFII".
838 // -------------------------------------------------------------------
840 * connect-->finish-->close
844 level = TestLevel.PARTIAL_COMPLETE,
847 args = {java.net.SocketAddress.class}
850 level = TestLevel.PARTIAL_COMPLETE,
852 method = "finishConnect",
856 public void testCFII_Norml_NoServer_Block() throws Exception {
858 ensureServerClosed();
859 assertTrue(this.channel1.isBlocking());
860 statusNotConnected_NotPending();
863 this.channel1.connect(localAddr1);
864 fail("Should throw a ConnectException here.");
865 } catch (ConnectException e) {
868 statusChannelClosed();
870 this.channel1.finishConnect();
871 fail("Should throw a ClosedChannelException here.");
872 } catch (ClosedChannelException e) {
878 * connect-->finish-->close
882 level = TestLevel.PARTIAL_COMPLETE,
885 args = {java.net.SocketAddress.class}
888 level = TestLevel.PARTIAL_COMPLETE,
890 method = "finishConnect",
894 public void testCFII_Norml_NoServer_NonBlock() throws Exception {
895 connectNoServerNonBlock();
897 this.channel1.close();
898 statusChannelClosed();
902 * connect-->finish-->close
906 level = TestLevel.PARTIAL_COMPLETE,
909 args = {java.net.SocketAddress.class}
912 level = TestLevel.PARTIAL_COMPLETE,
914 method = "finishConnect",
918 public void testCFII_Norml_Server_Block() throws Exception {
919 connectServerBlock();
921 this.channel1.close();
922 statusChannelClosed();
927 * connect-->finish-->close
931 level = TestLevel.PARTIAL_COMPLETE,
934 args = {java.net.SocketAddress.class}
937 level = TestLevel.PARTIAL_COMPLETE,
939 method = "finishConnect",
943 public void testCFII_Norml_Server_NonBlock() throws Exception {
944 connectServerNonBlock();
946 this.channel1.close();
947 statusChannelClosed();
951 * connect-->server closed-->finish-->close
955 level = TestLevel.PARTIAL_COMPLETE,
958 args = {java.net.SocketAddress.class}
961 level = TestLevel.PARTIAL_COMPLETE,
963 method = "finishConnect",
967 public void testCFII_ServerClosed_Block() throws Exception {
970 assertTrue(this.channel1.isBlocking());
971 statusNotConnected_NotPending();
973 assertTrue(this.channel1.connect(localAddr1));
974 statusConnected_NotPending();
976 ensureServerClosed();
980 this.channel1.close();
981 statusChannelClosed();
986 * connect-->server closed-->finish-->close
990 level = TestLevel.PARTIAL_COMPLETE,
993 args = {java.net.SocketAddress.class}
996 level = TestLevel.PARTIAL_COMPLETE,
998 method = "finishConnect",
1002 public void testCFII_ServerClosed_NonBlock() throws Exception {
1005 this.channel1.configureBlocking(false);
1006 statusNotConnected_NotPending();
1008 assertFalse(this.channel1.connect(localAddr1));
1009 statusNotConnected_Pending();
1011 ensureServerClosed();
1015 this.channel1.close();
1016 statusChannelClosed();
1020 * connect-->finish-->server closed-->close
1024 level = TestLevel.PARTIAL_COMPLETE,
1027 args = {java.net.SocketAddress.class}
1030 level = TestLevel.PARTIAL_COMPLETE,
1032 method = "finishConnect",
1036 public void testCFII_ServerClosedAfterFinish_Block() throws Exception {
1037 connectServerBlock();
1039 ensureServerClosed();
1040 assertTrue(this.channel1.isOpen());
1041 this.channel1.close();
1042 statusChannelClosed();
1047 * connect-->finish-->server closed-->close
1051 level = TestLevel.PARTIAL_COMPLETE,
1054 args = {java.net.SocketAddress.class}
1057 level = TestLevel.PARTIAL_COMPLETE,
1059 method = "finishConnect",
1063 public void testCFII_ServerClosedAfterFinish_NonBlock() throws Exception {
1064 connectServerNonBlock();
1066 ensureServerClosed();
1067 assertTrue(this.channel1.isOpen());
1068 this.channel1.close();
1069 statusChannelClosed();
1073 * no server-->connect-->server open-->finish-->close
1077 level = TestLevel.PARTIAL_COMPLETE,
1080 args = {java.net.SocketAddress.class}
1083 level = TestLevel.PARTIAL_COMPLETE,
1085 method = "finishConnect",
1089 public void testCFII_ServerStartLater_Block() throws Exception {
1091 ensureServerClosed();
1092 assertTrue(this.channel1.isBlocking());
1093 statusNotConnected_NotPending();
1096 this.channel1.connect(localAddr1);
1097 fail("Should throw a ConnectException here.");
1098 } catch (ConnectException e) {
1101 statusChannelClosed();
1104 this.channel1.finishConnect();
1105 fail("Should throw a ClosedChannelException here.");
1106 } catch (ClosedChannelException e) {
1112 * no server-->connect-->server open-->finish-->close
1116 level = TestLevel.PARTIAL_COMPLETE,
1119 args = {java.net.SocketAddress.class}
1122 level = TestLevel.PARTIAL_COMPLETE,
1124 method = "finishConnect",
1128 public void testCFII_ServerStartLater_NonBlock() throws Exception {
1130 ensureServerClosed();
1131 this.channel1.configureBlocking(false);
1132 statusNotConnected_NotPending();
1134 assertFalse(this.channel1.connect(localAddr1));
1135 statusNotConnected_Pending();
1140 assertFalse(this.channel1.finishConnect());
1141 statusNotConnected_Pending();
1142 this.channel1.close();
1143 } catch (ConnectException e) {
1149 * connect-->finish-->finish-->close
1153 level = TestLevel.PARTIAL_COMPLETE,
1156 args = {java.net.SocketAddress.class}
1159 level = TestLevel.PARTIAL_COMPLETE,
1161 method = "finishConnect",
1165 public void testCFII_FinishTwice_NoServer_NonBlock() throws Exception {
1167 ensureServerClosed();
1168 this.channel1.configureBlocking(false);
1169 statusNotConnected_NotPending();
1171 assertFalse(this.channel1.connect(localAddr1));
1172 statusNotConnected_Pending();
1174 assertFalse(this.channel1.finishConnect());
1175 } catch (ConnectException e) {
1178 statusChannelClosed();
1180 assertFalse(this.channel1.finishConnect());
1181 } catch (ClosedChannelException e) {
1184 statusChannelClosed();
1186 this.channel1.close();
1187 statusChannelClosed();
1191 * connect-->finish-->finish-->close
1195 level = TestLevel.PARTIAL_COMPLETE,
1198 args = {java.net.SocketAddress.class}
1201 level = TestLevel.PARTIAL_COMPLETE,
1203 method = "finishConnect",
1207 public void testCFII_FinishTwice_Server_Block() throws Exception {
1208 connectServerBlock();
1210 this.channel1.close();
1211 statusChannelClosed();
1216 * connect-->finish-->finish-->close
1220 level = TestLevel.PARTIAL_COMPLETE,
1223 args = {java.net.SocketAddress.class}
1226 level = TestLevel.PARTIAL_COMPLETE,
1228 method = "finishConnect",
1232 public void testCFII_FinishTwice_Server_NonBlock() throws Exception {
1233 connectServerNonBlock();
1235 this.channel1.close();
1236 statusChannelClosed();
1240 * connect-->finish-->connect-->close
1244 level = TestLevel.PARTIAL_COMPLETE,
1245 notes = "Verifies case: connect-->finish-->connect-->close. Verifies ClosedChannelException, ConnectException.",
1247 args = {java.net.SocketAddress.class}
1250 level = TestLevel.PARTIAL_COMPLETE,
1251 notes = "Verifies case: connect-->finish-->connect-->close. Verifies ClosedChannelException, ConnectException.",
1252 method = "finishConnect",
1256 public void testCFII_ConnectAfterFinish_NoServer_Block() throws Exception {
1258 ensureServerClosed();
1259 assertTrue(this.channel1.isBlocking());
1260 statusNotConnected_NotPending();
1263 this.channel1.connect(localAddr1);
1264 fail("Should throw a ConnectException here.");
1265 } catch (ConnectException e) {
1268 statusChannelClosed();
1270 this.channel1.finishConnect();
1271 fail("Should throw a ClosedChannelException here.");
1272 } catch (ClosedChannelException e) {
1275 statusChannelClosed();
1277 this.channel1.connect(localAddr1);
1278 fail("Should throw a ClosedChannelException here.");
1279 } catch (ClosedChannelException e) {
1282 statusChannelClosed();
1286 * connect-->finish-->connect-->close
1290 level = TestLevel.PARTIAL_COMPLETE,
1291 notes = "Verifies case: connect-->finish-->connect-->close. Verifies ConnectionPendingException, ConnectException.",
1293 args = {java.net.SocketAddress.class}
1296 level = TestLevel.PARTIAL_COMPLETE,
1297 notes = "Verifies case: connect-->finish-->connect-->close. Verifies ConnectionPendingException, ConnectException.",
1298 method = "finishConnect",
1302 public void testCFII_ConnectAfterFinish_NoServer_NonBlock()
1305 ensureServerClosed();
1306 this.channel1.configureBlocking(false);
1307 statusNotConnected_NotPending();
1309 assertFalse(this.channel1.connect(localAddr1));
1310 statusNotConnected_Pending();
1313 this.channel1.connect(localAddr1);
1314 fail("Should throw a ConnectionPendingException here.");
1315 } catch (ConnectionPendingException e) {
1318 statusNotConnected_Pending();
1320 // connect another addr
1322 this.channel1.connect(localAddr2);
1323 fail("Should throw a ConnectionPendingException here.");
1324 } catch (ConnectionPendingException e) {
1327 statusNotConnected_Pending();
1329 // connect if server closed
1330 ensureServerClosed();
1333 this.channel1.connect(localAddr1);
1334 fail("Should throw a ConnectionPendingException here.");
1335 } catch (ConnectionPendingException e) {
1338 statusNotConnected_Pending();
1340 this.channel1.close();
1342 statusChannelClosed();
1346 * connect-->finish-->connect-->close
1349 level = TestLevel.PARTIAL_COMPLETE,
1350 notes = "Verifies AlreadyConnectedException.",
1352 args = {java.net.SocketAddress.class}
1354 public void testCFII_ConnectAfterFinish_Server_Block() throws Exception {
1355 connectServerBlock();
1357 if (!this.channel1.isConnected()) {
1358 fail("Connection failed," +
1359 "testCFII_ConnectAfterFinish_Server_Block not finished.");
1363 this.channel1.connect(localAddr1);
1364 fail("Should throw an AlreadyConnectedException here.");
1365 } catch (AlreadyConnectedException e) {
1368 statusConnected_NotPending();
1370 // connect another addr
1372 this.channel1.connect(localAddr2);
1373 fail("Should throw an AlreadyConnectedException here.");
1374 } catch (AlreadyConnectedException e) {
1377 statusConnected_NotPending();
1379 // connect if server closed
1380 ensureServerClosed();
1383 this.channel1.connect(localAddr1);
1384 fail("Should throw an AlreadyConnectedException here.");
1385 } catch (AlreadyConnectedException e) {
1388 statusConnected_NotPending();
1390 this.channel1.close();
1391 statusChannelClosed();
1396 * connect-->finish-->connect-->close
1399 level = TestLevel.PARTIAL_COMPLETE,
1400 notes = "Verifies AlreadyConnectedException.",
1402 args = {java.net.SocketAddress.class}
1404 public void testCFII_ConnectAfterFinish_Server_NonBlock() throws Exception {
1405 connectServerNonBlock();
1407 if (!this.channel1.isConnected()) {
1408 fail("Connection failed," +
1409 "testCFII_ConnectAfterFinish_Server_Block not finished.");
1412 this.channel1.connect(localAddr1);
1413 fail("Should throw an AlreadyConnectedException or a ConnectionPendingException here.");
1414 } catch (AlreadyConnectedException e) {
1418 statusConnected_NotPending();
1420 // connect another addr
1422 this.channel1.connect(localAddr2);
1423 fail("Should throw an AlreadyConnectedException here.");
1424 } catch (AlreadyConnectedException e) {
1427 statusConnected_NotPending();
1429 // connect if server closed
1430 ensureServerClosed();
1433 this.channel1.connect(localAddr1);
1434 fail("Should throw an AlreadyConnectedException here.");
1435 } catch (AlreadyConnectedException e) {
1438 statusConnected_NotPending();
1440 this.channel1.close();
1441 statusChannelClosed();
1445 * connect-->connect-->finish-->close
1449 level = TestLevel.PARTIAL_COMPLETE,
1450 notes = "Verifies case: connect-->connect-->finish-->close. Verifies ConnectionPendingException, ConnectException.",
1452 args = {java.net.SocketAddress.class}
1455 level = TestLevel.PARTIAL_COMPLETE,
1456 notes = "Verifies case: connect-->connect-->finish-->close. Verifies ConnectionPendingException, ConnectException.",
1457 method = "finishConnect",
1461 public void testCFII_ConnectTwice_NoServer_NonBlock() throws Exception {
1463 ensureServerClosed();
1464 this.channel1.configureBlocking(false);
1465 statusNotConnected_NotPending();
1467 assertFalse(this.channel1.connect(localAddr1));
1468 statusNotConnected_Pending();
1471 this.channel1.connect(localAddr1);
1472 fail("Should throw a ConnectionPendingException here.");
1473 } catch (ConnectionPendingException e) {
1476 statusNotConnected_Pending();
1478 // connect another addr
1480 this.channel1.connect(localAddr2);
1481 fail("Should throw a ConnectionPendingException here.");
1482 } catch (ConnectionPendingException e) {
1485 statusNotConnected_Pending();
1487 // connect if server closed
1488 ensureServerClosed();
1491 this.channel1.connect(localAddr1);
1492 fail("Should throw a ConnectionPendingException here.");
1493 } catch (ConnectionPendingException e) {
1496 statusNotConnected_Pending();
1497 this.channel1.close();
1499 statusChannelClosed();
1503 * connect-->connect-->finish-->close
1507 level = TestLevel.PARTIAL_COMPLETE,
1508 notes = "Verifies case connect-->connect-->finish-->close. Verifies AlreadyConnectedException.",
1510 args = {java.net.SocketAddress.class}
1513 level = TestLevel.PARTIAL_COMPLETE,
1514 notes = "Verifies case connect-->connect-->finish-->close. Verifies AlreadyConnectedException.",
1515 method = "finishConnect",
1519 public void testCFII_ConnectTwice_Server_Block() throws Exception {
1522 assertTrue(this.channel1.isBlocking());
1523 statusNotConnected_NotPending();
1525 assertTrue(this.channel1.connect(localAddr1));
1526 statusConnected_NotPending();
1529 this.channel1.connect(localAddr1);
1530 fail("Should throw an AlreadyConnectedException here.");
1531 } catch (AlreadyConnectedException e) {
1534 statusConnected_NotPending();
1536 // connect another addr
1538 this.channel1.connect(localAddr2);
1539 fail("Should throw an AlreadyConnectedException here.");
1540 } catch (AlreadyConnectedException e) {
1543 statusConnected_NotPending();
1545 // connect if server closed
1546 ensureServerClosed();
1549 this.channel1.connect(localAddr1);
1550 fail("Should throw an AlreadyConnectedException here.");
1551 } catch (AlreadyConnectedException e) {
1554 statusConnected_NotPending();
1558 this.channel1.close();
1559 statusChannelClosed();
1564 * connect-->connect-->finish-->close
1568 level = TestLevel.PARTIAL_COMPLETE,
1569 notes = "Verifies case connect-->connect-->finish-->close. Verifies ConnectionPendingException.",
1571 args = {java.net.SocketAddress.class}
1574 level = TestLevel.PARTIAL_COMPLETE,
1575 notes = "Verifies case connect-->connect-->finish-->close. Verifies ConnectionPendingException.",
1576 method = "finishConnect",
1580 public void testCFII_ConnectTwice_Server_NonBlock() throws Exception {
1583 this.channel1.configureBlocking(false);
1584 statusNotConnected_NotPending();
1586 assertFalse(this.channel1.connect(localAddr1));
1587 statusNotConnected_Pending();
1590 this.channel1.connect(localAddr1);
1591 fail("Should throw a ConnectionPendingException here.");
1592 } catch (ConnectionPendingException e) {
1595 statusNotConnected_Pending();
1597 // connect another addr
1599 this.channel1.connect(localAddr2);
1600 fail("Should throw a ConnectionPendingException here.");
1601 } catch (ConnectionPendingException e) {
1604 statusNotConnected_Pending();
1606 // connect if server closed
1607 ensureServerClosed();
1610 this.channel1.connect(localAddr1);
1611 fail("Should throw a ConnectionPendingException here.");
1612 } catch (ConnectionPendingException e) {
1615 statusNotConnected_Pending();
1619 this.channel1.close();
1620 statusChannelClosed();
1624 * finish-->connect-->finish-->close
1628 level = TestLevel.PARTIAL_COMPLETE,
1631 args = {java.net.SocketAddress.class}
1634 level = TestLevel.PARTIAL_COMPLETE,
1636 method = "finishConnect",
1640 public void testCFII_FinishFirst_NoServer_Block() throws Exception {
1642 ensureServerClosed();
1643 assertTrue(this.channel1.isBlocking());
1644 statusNotConnected_NotPending();
1647 this.channel1.finishConnect();
1648 fail("Should throw NoConnectionPendingException");
1649 } catch (NoConnectionPendingException e) {
1652 statusNotConnected_NotPending();
1655 this.channel1.connect(localAddr1);
1656 fail("Should throw a ConnectException here.");
1657 } catch (ConnectException e) {
1660 statusChannelClosed();
1662 this.channel1.finishConnect();
1663 fail("Should throw a ClosedChannelException here.");
1664 } catch (ClosedChannelException e) {
1667 statusChannelClosed();
1671 * finish-->connect-->finish-->close
1675 level = TestLevel.PARTIAL_COMPLETE,
1678 args = {java.net.SocketAddress.class}
1681 level = TestLevel.PARTIAL_COMPLETE,
1683 method = "finishConnect",
1687 public void testCFII_FinishFirst_NoServer_NonBlock() throws Exception {
1689 ensureServerClosed();
1690 this.channel1.configureBlocking(false);
1691 statusNotConnected_NotPending();
1694 this.channel1.finishConnect();
1695 fail("Should throw NoConnectionPendingException");
1696 } catch (NoConnectionPendingException e) {
1699 statusNotConnected_NotPending();
1701 assertFalse(this.channel1.connect(localAddr1));
1702 statusNotConnected_Pending();
1703 this.channel1.close();
1705 statusChannelClosed();
1709 * finish-->connect-->finish-->close
1713 level = TestLevel.PARTIAL_COMPLETE,
1716 args = {java.net.SocketAddress.class}
1719 level = TestLevel.PARTIAL_COMPLETE,
1721 method = "finishConnect",
1725 public void testCFII_FinishFirst_Server_Block() throws Exception {
1728 assertTrue(this.channel1.isBlocking());
1729 statusNotConnected_NotPending();
1732 this.channel1.finishConnect();
1733 fail("Should throw NoConnectionPendingException");
1734 } catch (NoConnectionPendingException e) {
1737 statusNotConnected_NotPending();
1739 assertTrue(this.channel1.connect(localAddr1));
1740 statusConnected_NotPending();
1744 this.channel1.close();
1745 statusChannelClosed();
1750 * finish-->connect-->finish-->close
1754 level = TestLevel.PARTIAL_COMPLETE,
1757 args = {java.net.SocketAddress.class}
1760 level = TestLevel.PARTIAL_COMPLETE,
1762 method = "finishConnect",
1766 public void testCFII_FinishFirst_Server_NonBlock() throws Exception {
1769 this.channel1.configureBlocking(false);
1770 statusNotConnected_NotPending();
1773 this.channel1.finishConnect();
1774 fail("Should throw NoConnectionPendingException");
1775 } catch (NoConnectionPendingException e) {
1778 statusNotConnected_NotPending();
1780 assertFalse(this.channel1.connect(localAddr1));
1781 statusNotConnected_Pending();
1785 this.channel1.close();
1786 statusChannelClosed();
1790 level = TestLevel.PARTIAL_COMPLETE,
1793 args = {java.net.SocketAddress.class}
1795 public void testCFII_Null() throws Exception {
1796 statusNotConnected_NotPending();
1798 this.channel1.connect(null);
1799 fail("Should throw an IllegalArgumentException here.");
1800 } catch (IllegalArgumentException e) {
1806 level = TestLevel.PARTIAL_COMPLETE,
1809 args = {java.net.SocketAddress.class}
1811 public void testCFII_UnsupportedType() throws Exception {
1812 statusNotConnected_NotPending();
1813 SocketAddress newTypeAddress = new SubSocketAddress();
1815 this.channel1.connect(newTypeAddress);
1816 fail("Should throw an UnsupportedAddressTypeException here.");
1817 } catch (UnsupportedAddressTypeException e) {
1823 level = TestLevel.PARTIAL_COMPLETE,
1826 args = {java.net.SocketAddress.class}
1828 public void testCFII_Unresolved() throws IOException {
1829 statusNotConnected_NotPending();
1830 InetSocketAddress unresolved = new InetSocketAddress(
1831 "unresolved address", 1080);
1833 this.channel1.connect(unresolved);
1834 fail("Should throw an UnresolvedAddressException here.");
1835 } catch (UnresolvedAddressException e) {
1841 level = TestLevel.PARTIAL_COMPLETE,
1844 args = {java.net.SocketAddress.class}
1846 public void testCFII_EmptyHost() throws Exception {
1847 statusNotConnected_NotPending();
1848 ServerSocket server = new ServerSocket(0);
1849 int port = server.getLocalPort();
1852 this.channel1.connect(new InetSocketAddress("", port));
1853 fail("Should throw ConnectException");
1854 } catch (ConnectException e) {
1861 level = TestLevel.PARTIAL_COMPLETE,
1862 notes = "Verifies ClosedChannelException.",
1864 args = {java.net.SocketAddress.class}
1867 level = TestLevel.PARTIAL_COMPLETE,
1868 notes = "Verifies ClosedChannelException.",
1869 method = "finishConnect",
1873 public void testCFII_CloseFirst() throws Exception {
1874 this.channel1.close();
1875 statusChannelClosed();
1878 this.channel1.connect(localAddr1);
1879 fail("Should throw ClosedChannelException.");
1880 } catch (ClosedChannelException e) {
1883 statusChannelClosed();
1885 this.channel1.finishConnect();
1886 fail("Should throw ClosedChannelException.");
1887 } catch (ClosedChannelException e) {
1890 statusChannelClosed();
1892 this.channel1.configureBlocking(false);
1893 fail("Should throw ClosedChannelException.");
1894 } catch (ClosedChannelException e) {
1897 statusChannelClosed();
1902 level = TestLevel.PARTIAL_COMPLETE,
1905 args = {java.net.SocketAddress.class}
1908 level = TestLevel.PARTIAL_COMPLETE,
1910 method = "finishConnect",
1914 public void testCFII_StatusAfterFinish() throws Exception {
1915 // 1. close server, finish must return false, check the status
1916 ensureServerClosed();
1919 assertTrue(this.channel1.isBlocking());
1921 channel1.connect(localAddr1);
1922 fail("Should throw ConnectException");
1923 } catch (ConnectException e) {
1927 assertFalse(this.channel1.isOpen());
1928 assertTrue(this.channel1.isBlocking());
1929 assertFalse(this.channel1.isConnectionPending());
1931 // 1.2 non block mode
1932 this.channel1 = SocketChannel.open();
1933 this.channel1.configureBlocking(false);
1934 assertFalse(this.channel1.connect(localAddr1));
1936 assertFalse(this.channel1.finishConnect());
1937 fail("Should throw IOException: Connection refused");
1938 } catch (ConnectException e) {
1941 statusChannelClosed();
1943 // 2. start server, finish usually return true, check the status
1947 this.channel1 = SocketChannel.open();
1948 assertTrue(this.channel1.isBlocking());
1949 assertTrue(this.channel1.connect(localAddr1));
1950 assertTrue(this.channel1.finishConnect());
1951 statusConnected_NotPending();
1952 this.channel1.close();
1954 // 2.2 non block mode
1955 this.channel1 = SocketChannel.open();
1956 this.channel1.configureBlocking(false);
1957 assertFalse(this.channel1.connect(localAddr1));
1959 this.channel1.close();
1962 private void ensureServerClosed() throws IOException {
1963 if (null != this.server1) {
1964 this.server1.close();
1965 assertTrue(this.server1.isClosed());
1967 if (null != this.server2) {
1968 this.server2.close();
1969 assertTrue(this.server2.isClosed());
1973 private void ensureServerOpen() throws IOException {
1974 ensureServerClosed();
1975 this.server1 = new ServerSocket(localAddr1.getPort());
1976 this.server2 = new ServerSocket(localAddr2.getPort());
1977 assertTrue(this.server1.isBound());
1978 assertTrue(this.server2.isBound());
1981 private void connectNoServerNonBlock() throws Exception {
1983 ensureServerClosed();
1984 this.channel1.configureBlocking(false);
1985 statusNotConnected_NotPending();
1987 assertFalse(this.channel1.connect(localAddr1));
1988 statusNotConnected_Pending();
1990 assertFalse(this.channel1.finishConnect());
1991 } catch (ConnectException e) {
1994 ensureServerClosed();
1997 private void connectServerNonBlock() throws Exception {
2000 this.channel1.configureBlocking(false);
2001 statusNotConnected_NotPending();
2003 assertFalse(this.channel1.connect(localAddr1));
2004 statusNotConnected_Pending();
2009 private void connectServerBlock() throws Exception {
2012 assertTrue(this.channel1.isBlocking());
2013 statusNotConnected_NotPending();
2015 assertTrue(this.channel1.connect(localAddr1));
2016 statusConnected_NotPending();
2021 private void statusChannelClosed() {
2022 assertFalse(this.channel1.isConnected());
2023 assertFalse(this.channel1.isConnectionPending());
2024 assertFalse(this.channel1.isOpen());
2027 private void statusNotConnected_NotPending() {
2028 assertFalse(this.channel1.isConnected());
2029 assertFalse(this.channel1.isConnectionPending());
2030 assertTrue(this.channel1.isOpen());
2033 private void statusNotConnected_Pending() {
2034 assertFalse(this.channel1.isConnected());
2035 assertTrue(this.channel1.isConnectionPending());
2036 assertTrue(this.channel1.isOpen());
2039 private void statusConnected_NotPending() {
2040 assertTrue(this.channel1.isConnected());
2041 assertFalse(this.channel1.isConnectionPending());
2042 assertTrue(this.channel1.isOpen());
2045 private boolean tryFinish() throws IOException {
2047 * the result of finish will be asserted in multi-thread tests.
2049 boolean connected = false;
2050 assertTrue(this.channel1.isOpen());
2052 connected = this.channel1.finishConnect();
2053 } catch (SocketException e) {
2054 // Finish connection failed, probably due to reset by peer error.
2057 statusConnected_NotPending();
2062 // -------------------------------------------------------------------
2063 // Original tests. Test method for CFII with real data.
2064 // -------------------------------------------------------------------
2068 * 'SocketChannelImpl.connect(SocketAddress)'
2071 level = TestLevel.PARTIAL_COMPLETE,
2074 args = {java.net.SocketAddress.class}
2076 public void testCFII_Data_ConnectWithServer() throws Exception {
2078 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer
2079 .allocate(CAPACITY_NORMAL);
2080 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
2081 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
2082 assertFalse(this.channel1.isRegistered());
2083 assertTrue(this.channel1.isBlocking());
2085 this.channel1.connect(localAddr1);
2087 assertTrue(this.channel1.isBlocking());
2088 assertTrue(this.channel1.isConnected());
2089 assertFalse(this.channel1.isConnectionPending());
2090 assertTrue(this.channel1.isOpen());
2091 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
2092 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBufArr, 0, 1));
2094 this.channel1.configureBlocking(false);
2096 this.channel1.connect(localAddr1);
2097 fail("Should throw AlreadyConnectedException");
2098 } catch (AlreadyConnectedException e) {
2102 assertFalse(this.channel1.isRegistered());
2107 * Test method for 'SocketChannelImpl.connect(SocketAddress)'
2111 level = TestLevel.PARTIAL_COMPLETE,
2114 args = {java.net.SocketAddress.class}
2117 level = TestLevel.PARTIAL_COMPLETE,
2119 method = "finishConnect",
2123 public void testCFII_Data_ConnectWithServer_nonBlocking() throws Exception {
2125 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer
2126 .allocate(CAPACITY_NORMAL);
2127 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
2128 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
2129 assertFalse(this.channel1.isRegistered());
2130 assertTrue(this.channel1.isBlocking());
2131 this.channel1.configureBlocking(false);
2132 this.channel1.connect(localAddr1);
2134 assertFalse(this.channel1.isBlocking());
2135 assertFalse(this.channel1.isConnected());
2136 assertTrue(this.channel1.isConnectionPending());
2137 assertTrue(this.channel1.isOpen());
2138 assertTrue(tryFinish());
2139 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
2140 assertEquals(CAPACITY_NORMAL, this.channel1
2141 .write(writeBufArr, 0, 1));
2143 this.channel1.configureBlocking(false);
2145 this.channel1.connect(localAddr1);
2146 fail("Should throw AlreadyConnectedException");
2147 } catch (AlreadyConnectedException e) {
2151 assertFalse(this.channel1.isRegistered());
2156 * Test method for 'SocketChannelImpl.finishConnect()'
2159 level = TestLevel.PARTIAL_COMPLETE,
2161 method = "finishConnect",
2164 public void testCFII_Data_FinishConnect_nonBlocking() throws IOException {
2167 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer
2168 .allocate(CAPACITY_NORMAL);
2169 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
2170 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
2172 this.channel1.configureBlocking(false);
2174 this.channel1.finishConnect();
2175 fail("Should throw NoConnectionPendingException");
2176 } catch (NoConnectionPendingException e) {
2179 this.channel1.connect(localAddr1);
2180 assertFalse(this.channel1.isBlocking());
2181 assertFalse(this.channel1.isConnected());
2182 assertTrue(this.channel1.isConnectionPending());
2183 assertTrue(this.channel1.isOpen());
2184 this.server1.accept();
2185 assertTrue(tryFinish());
2186 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
2187 assertEquals(CAPACITY_NORMAL, this.channel1
2188 .write(writeBufArr, 0, 1));
2190 this.channel1.connect(localAddr1);
2191 fail("Should throw AlreadyConnectedException");
2192 } catch (AlreadyConnectedException e) {
2196 assertFalse(this.channel1.isRegistered());
2202 level = TestLevel.PARTIAL_COMPLETE,
2205 args = {java.net.SocketAddress.class}
2208 level = TestLevel.PARTIAL_COMPLETE,
2210 method = "finishConnect",
2214 level = TestLevel.PARTIAL_COMPLETE,
2215 notes = "Verifies IOException",
2217 args = {SocketAddress.class}
2220 public void testCFII_Data_FinishConnect_AddrSetServerStartLater()
2221 throws IOException {
2222 ensureServerClosed();
2223 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
2224 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
2225 this.channel1.configureBlocking(false);
2227 SocketChannel.open(localAddr1);
2228 fail("Should throw ConnectException");
2229 } catch (ConnectException e) {
2232 assertTrue(this.channel1.isOpen());
2233 assertFalse(this.channel1.isBlocking());
2234 assertFalse(this.channel1.isConnectionPending());
2235 this.channel1.configureBlocking(true);
2237 this.channel1.finishConnect();
2238 fail("Should throw NoConnectionPendingException");
2239 } catch (NoConnectionPendingException e) {
2243 this.channel1.connect(localAddr2);
2244 fail("Should throw ConnectException");
2245 } catch (ConnectException e) {
2249 assertTrue(this.channel1.isBlocking());
2251 this.channel1.finishConnect();
2252 fail("Should throw ClosedChannelException");
2253 } catch (ClosedChannelException e) {
2256 assertFalse(this.channel1.isConnected());
2257 // finish after finish OK
2258 assertFalse(this.channel1.isConnectionPending());
2259 this.channel1 = SocketChannel.open();
2260 this.channel1.configureBlocking(false);
2261 this.channel1.connect(localAddr1);
2262 assertFalse(this.channel1.isConnected());
2265 assertFalse(this.channel1.isBlocking());
2266 assertFalse(this.channel1.isConnected());
2267 assertTrue(this.channel1.isConnectionPending());
2268 assertTrue(this.channel1.isOpen());
2270 this.channel1.connect(localAddr1);
2271 fail("Should throw ConnectionPendingException");
2272 } catch (ConnectionPendingException e) {
2275 this.channel1.configureBlocking(true);
2277 this.channel1.connect(localAddr1);
2278 fail("Should throw ConnectionPendingException");
2279 } catch (ConnectionPendingException e) {
2286 level = TestLevel.PARTIAL_COMPLETE,
2288 method = "finishConnect",
2291 public void testCFII_Data_FinishConnect_ServerStartLater()
2292 throws IOException {
2293 ensureServerClosed();
2294 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
2295 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
2296 this.channel1.configureBlocking(true);
2298 this.channel1.finishConnect();
2299 fail("Should throw NoConnectionPendingException");
2300 } catch (NoConnectionPendingException e) {
2304 this.channel1.connect(localAddr1);
2305 fail("Should throw ConnectException");
2306 } catch (ConnectException e) {
2311 this.channel1.finishConnect();
2312 fail("Should throw ClosedChannelException");
2313 } catch (ClosedChannelException e) {
2316 assertFalse(this.channel1.isConnected());
2317 // finish after finish OK
2318 assertFalse(this.channel1.isConnectionPending());
2319 this.channel1 = SocketChannel.open();
2320 this.channel1.configureBlocking(false);
2321 this.channel1.connect(localAddr1);
2322 assertFalse(this.channel1.isConnected());
2325 assertFalse(this.channel1.isBlocking());
2326 assertFalse(this.channel1.isConnected());
2327 assertTrue(this.channel1.isConnectionPending());
2328 assertTrue(this.channel1.isOpen());
2330 this.channel1.connect(localAddr1);
2331 fail("Should throw ConnectionPendingException");
2332 } catch (ConnectionPendingException e) {
2335 this.channel1.configureBlocking(true);
2337 this.channel1.connect(localAddr1);
2338 fail("Should throw ConnectionPendingException");
2339 } catch (ConnectionPendingException e) {
2346 level = TestLevel.PARTIAL_COMPLETE,
2348 method = "finishConnect",
2351 public void testCFII_Data_FinishConnect_Blocking() throws IOException {
2353 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer
2354 .allocate(CAPACITY_NORMAL);
2355 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
2356 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
2357 this.channel1.configureBlocking(true);
2359 this.channel1.finishConnect();
2360 fail("Should throw NoConnectionPendingException");
2361 } catch (NoConnectionPendingException e) {
2365 this.channel1.connect(localAddr1);
2367 assertTrue(this.channel1.isConnected());
2368 assertFalse(this.channel1.isConnectionPending());
2369 assertTrue(this.channel1.isOpen());
2370 assertTrue(tryFinish());
2371 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
2372 assertEquals(CAPACITY_NORMAL, this.channel1
2373 .write(writeBufArr, 0, 1));
2376 this.channel1.connect(localAddr1);
2377 fail("Should throw AlreadyConnectedException");
2378 } catch (AlreadyConnectedException e) {
2382 assertFalse(this.channel1.isRegistered());
2387 * Regression test for Harmony-1947.
2390 level = TestLevel.PARTIAL_COMPLETE,
2391 notes = "Doesn't verify exceptions.",
2392 method = "finishConnect",
2395 public void test_finishConnect() throws Exception {
2396 SocketAddress address = new InetSocketAddress("localhost", 2046);
2398 ServerSocketChannel theServerChannel = ServerSocketChannel.open();
2399 ServerSocket serversocket = theServerChannel.socket();
2400 serversocket.setReuseAddress(true);
2402 serversocket.bind(address);
2404 boolean doneNonBlockingConnect = false;
2405 // Loop so that we make sure we're definitely testing finishConnect()
2406 while (!doneNonBlockingConnect) {
2407 channel1 = SocketChannel.open();
2409 // Set the SocketChannel to non-blocking so that connect(..) does
2411 channel1.configureBlocking(false);
2412 boolean connected = channel1.connect(address);
2414 // Now set the SocketChannel back to blocking so that
2415 // finishConnect() blocks.
2416 channel1.configureBlocking(true);
2417 doneNonBlockingConnect = channel1.finishConnect();
2419 fail("Non blocking connect was connected too fast." +
2420 "Could not test finishConnect().");
2422 if (doneNonBlockingConnect) {
2425 fail("finishConnect() did not finish the connection.");
2429 if (!serversocket.isClosed()) {
2430 serversocket.close();
2434 // -------------------------------------------------------------------
2435 // End of original tests. Test method for CFII with real data.
2436 // -------------------------------------------------------------------
2439 * @tests java.nio.channels.SocketChannel#read(ByteBuffer)
2442 level = TestLevel.PARTIAL_COMPLETE,
2445 args = {java.nio.ByteBuffer[].class}
2447 public void test_readLByteBuffer_Blocking() throws IOException {
2448 // initialize write content
2449 byte[] writeContent = new byte[CAPACITY_NORMAL];
2450 for (int i = 0; i < writeContent.length; i++) {
2451 writeContent[i] = (byte) i;
2453 // establish connection
2454 channel1.connect(localAddr1);
2455 Socket acceptedSocket = server1.accept();
2457 // use OutputStream.write to send CAPACITY_NORMAL bytes data
2458 OutputStream out = acceptedSocket.getOutputStream();
2459 out.write(writeContent);
2460 // use close to guarantee all data is sent
2461 acceptedSocket.close();
2463 ByteBuffer readContent = ByteBuffer.allocate(CAPACITY_NORMAL + 1);
2466 long startTime = System.currentTimeMillis();
2467 // use SocketChannel.read to read data
2468 while (totalCount <= CAPACITY_NORMAL) {
2469 count = channel1.read(readContent);
2473 totalCount += count;
2474 // if the channel could not finish reading in TIMEOUT ms, the
2475 // test fails. It is used to guarantee the test never hangs even
2476 // if there are bugs of SocketChannel implementation. For
2477 // blocking read, it possibly returns 0 in some cases.
2478 assertTimeout(startTime, TIMEOUT);
2481 // assert read content
2482 assertEquals(CAPACITY_NORMAL, totalCount);
2483 assertEquals(CAPACITY_NORMAL, readContent.position());
2485 for (int i = 0; i < CAPACITY_NORMAL; i++) {
2486 assertEquals(writeContent[i], readContent.get());
2491 * @tests java.nio.channels.SocketChannel#read(ByteBuffer)
2494 level = TestLevel.PARTIAL_COMPLETE,
2497 args = {java.nio.ByteBuffer[].class}
2499 public void test_readLByteBuffer_Nonblocking() throws IOException {
2500 // initialize write content
2501 byte[] writeContent = new byte[CAPACITY_NORMAL];
2502 for (int i = 0; i < writeContent.length; i++) {
2503 writeContent[i] = (byte) i;
2506 // establish connection
2507 channel1.connect(localAddr1);
2508 Socket acceptedSocket = server1.accept();
2509 // use OutputStream.write to write CAPACITY_NORMAL bytes data.
2510 OutputStream out = acceptedSocket.getOutputStream();
2511 out.write(writeContent);
2512 // use close to guarantee all data is sent
2513 acceptedSocket.close();
2515 channel1.configureBlocking(false);
2516 ByteBuffer readContent = ByteBuffer.allocate(CAPACITY_NORMAL + 1);
2519 long startTime = System.currentTimeMillis();
2520 // use SocketChannel.read to read data
2521 while (totalCount <= CAPACITY_NORMAL) {
2522 count = channel1.read(readContent);
2526 totalCount += count;
2527 // if the channel could not finish reading in TIMEOUT ms, the
2528 // test fails. It is used to guarantee the test never hangs even
2529 // if there are bugs of SocketChannel implementation.
2530 assertTimeout(startTime, TIMEOUT);
2533 // assert read content
2534 assertEquals(CAPACITY_NORMAL, totalCount);
2535 assertEquals(CAPACITY_NORMAL, readContent.position());
2537 for (int i = 0; i < CAPACITY_NORMAL; i++) {
2538 assertEquals(writeContent[i], readContent.get());
2543 * @tests java.nio.channels.SocketChannel#write(ByteBuffer)
2546 level = TestLevel.PARTIAL_COMPLETE,
2549 args = {java.nio.ByteBuffer.class}
2551 public void test_writeLjava_nio_ByteBuffer_Blocking() throws IOException {
2552 // initialize write content
2553 ByteBuffer writeContent = ByteBuffer.allocate(CAPACITY_NORMAL);
2554 for (int i = 0; i < CAPACITY_NORMAL; i++) {
2555 writeContent.put((byte) i);
2557 writeContent.flip();
2558 // establish connection
2559 channel1.connect(localAddr1);
2560 Socket acceptedSocket = server1.accept();
2562 // use SocketChannel.write(ByteBuffer) to write CAPACITY_NORMAL bytes
2564 int writtenCount = channel1.write(writeContent);
2565 // assert written count and ByteBuffer position
2566 assertEquals(CAPACITY_NORMAL, writtenCount);
2567 assertEquals(CAPACITY_NORMAL, writeContent.position());
2568 // use close to guarantee all data is sent
2571 InputStream in = acceptedSocket.getInputStream();
2574 byte[] readContent = new byte[CAPACITY_NORMAL + 1];
2575 // if the channel could not finish reading in TIMEOUT ms, the test
2576 // fails. It is used to guarantee the test never hangs even if there
2577 // are bugs of SocketChannel implementation.
2578 acceptedSocket.setSoTimeout(TIMEOUT);
2580 // use InputStream.read to read data.
2581 while (totalCount <= CAPACITY_NORMAL) {
2582 count = in.read(readContent, totalCount, readContent.length
2587 totalCount += count;
2590 // assert read content
2591 assertEquals(CAPACITY_NORMAL, totalCount);
2592 writeContent.flip();
2593 for (int i = 0; i < CAPACITY_NORMAL; i++) {
2594 assertEquals(writeContent.get(), readContent[i]);
2599 * @tests java.nio.channels.SocketChannel#write(ByteBuffer)
2602 level = TestLevel.PARTIAL_COMPLETE,
2605 args = {java.nio.ByteBuffer.class}
2607 public void test_writeLjava_nio_ByteBuffer_NonBlocking() throws Exception {
2608 // initialize write content
2609 ByteBuffer writeContent = ByteBuffer.allocate(CAPACITY_NORMAL);
2610 for (int i = 0; i < CAPACITY_NORMAL; i++) {
2611 writeContent.put((byte) i);
2613 writeContent.flip();
2615 // establish connection
2616 channel1.connect(localAddr1);
2617 Socket acceptedSocket = server1.accept();
2619 channel1.configureBlocking(false);
2620 int writtenTotalCount = 0;
2621 int writtenCount = 0;
2622 long startTime = System.currentTimeMillis();
2623 // use SocketChannel.write(ByteBuffer) to write CAPACITY_NORMAL bytes
2624 while (writtenTotalCount < CAPACITY_NORMAL) {
2625 writtenCount = channel1.write(writeContent);
2626 writtenTotalCount += writtenCount;
2627 // if the channel could not finish writing in TIMEOUT ms, the
2628 // test fails. It is used to guarantee the test never hangs even
2629 // if there are bugs of SocketChannel implementation.
2630 assertTimeout(startTime, TIMEOUT);
2632 // assert written count and ByteBuffer position
2633 assertEquals(CAPACITY_NORMAL, writtenTotalCount);
2634 assertEquals(CAPACITY_NORMAL, writeContent.position());
2635 // use close to guarantee all data is sent
2638 InputStream in = acceptedSocket.getInputStream();
2639 byte[] readContent = new byte[CAPACITY_NORMAL + 1];
2642 // if the channel could not finish reading in TIMEOUT ms, the test
2643 // fails. It is used to guarantee the test never hangs even if there
2644 // are bugs of SocketChannel implementation.
2645 acceptedSocket.setSoTimeout(TIMEOUT);
2646 // use InputStream.read to read data.
2647 while (totalCount <= CAPACITY_NORMAL) {
2648 count = in.read(readContent, totalCount, readContent.length
2653 totalCount += count;
2655 // assert read content
2656 assertEquals(CAPACITY_NORMAL, totalCount);
2657 writeContent.flip();
2658 for (int i = 0; i < CAPACITY_NORMAL; i++) {
2659 assertEquals(writeContent.get(), readContent[i]);
2664 * @tests java.nio.channels.SocketChannel#read(ByteBuffer)
2667 level = TestLevel.PARTIAL_COMPLETE,
2670 args = {java.nio.ByteBuffer.class}
2672 public void test_writeLjava_nio_ByteBuffer_Nonblocking_HugeData() throws IOException {
2673 // initialize write content
2674 ByteBuffer writeContent = ByteBuffer.allocate(CAPACITY_HUGE);
2675 for (int i = 0; i < CAPACITY_HUGE; i++) {
2676 writeContent.put((byte) i);
2678 writeContent.flip();
2680 // establish connection
2681 channel1.connect(localAddr1);
2682 Socket acceptedSocket = server1.accept();
2684 channel1.configureBlocking(false);
2685 int writtenTotalCount = 0;
2686 int writtenCount = 1;
2687 long startTime = System.currentTimeMillis();
2688 // use SocketChannel.write(ByteBuffer) to try to write CAPACITY_HUGE bytes
2689 while (writtenTotalCount < CAPACITY_HUGE && writtenCount > 0) {
2690 writtenCount = channel1.write(writeContent);
2691 if (writtenCount == 0 && writtenTotalCount < CAPACITY_HUGE) {
2692 assertEquals(0, channel1.write(writeContent));
2695 writtenTotalCount += writtenCount;
2696 // if the channel could not finish writing in TIMEOUT ms, the
2697 // test fails. It is used to guarantee the test never hangs even
2698 // if there are bugs of SocketChannel implementation.
2699 assertTimeout(startTime, TIMEOUT);
2704 * Fails if the difference between current time and start time is greater
2707 private void assertTimeout(long startTime, long timeout) {
2708 long currentTime = System.currentTimeMillis();
2709 if ((currentTime - startTime) > timeout) {
2714 // -------------------------------------------------
2715 // Test for read/write but no real data expressed
2716 // -------------------------------------------------
2718 level = TestLevel.PARTIAL_COMPLETE,
2719 notes = "Verifies ClosedChannelException",
2721 args = {java.nio.ByteBuffer.class}
2723 public void testReadByteBuffer() throws Exception {
2724 assertTrue(this.server1.isBound());
2725 java.nio.ByteBuffer readBuf = java.nio.ByteBuffer
2726 .allocate(CAPACITY_NORMAL);
2727 assertFalse(this.channel1.isRegistered());
2728 assertTrue(this.channel1.isBlocking());
2729 assertFalse(this.channel1.isConnected());
2730 assertFalse(this.channel1.isConnectionPending());
2731 assertTrue(this.channel1.isOpen());
2732 // note: blocking-mode will make the read process endless!
2733 this.channel1.configureBlocking(false);
2735 channel1.read(readBuf);
2736 fail("Should throw NotYetConnectedException");
2737 } catch (NotYetConnectedException e) {
2740 this.channel1.connect(localAddr1);
2741 assertFalse(this.channel1.isBlocking());
2742 assertTrue(this.channel1.isConnectionPending());
2743 assertFalse(this.channel1.isConnected());
2744 assertTrue(tryFinish());
2745 assertEquals(0, this.channel1.read(readBuf));
2747 this.channel1.close();
2749 channel1.read(readBuf);
2750 fail("Should throw ClosedChannelException");
2751 } catch (ClosedChannelException e) {
2757 level = TestLevel.PARTIAL_COMPLETE,
2758 notes = "Verifies ClosedChannelException",
2760 args = {java.nio.ByteBuffer.class}
2762 public void testReadByteBuffer_Direct() throws Exception {
2763 assertTrue(this.server1.isBound());
2764 java.nio.ByteBuffer readBuf = java.nio.ByteBuffer
2765 .allocateDirect(CAPACITY_NORMAL);
2766 assertFalse(this.channel1.isRegistered());
2767 assertTrue(this.channel1.isBlocking());
2768 assertFalse(this.channel1.isConnected());
2769 assertFalse(this.channel1.isConnectionPending());
2770 assertTrue(this.channel1.isOpen());
2771 // note: blocking-mode will make the read process endless!
2772 this.channel1.configureBlocking(false);
2774 channel1.read(readBuf);
2775 fail("Should throw NotYetConnectedException");
2776 } catch (NotYetConnectedException e) {
2779 this.channel1.connect(localAddr1);
2780 assertFalse(this.channel1.isBlocking());
2781 assertTrue(this.channel1.isConnectionPending());
2782 assertFalse(this.channel1.isConnected());
2783 assertTrue(tryFinish());
2784 assertEquals(0, this.channel1.read(readBuf));
2786 this.channel1.close();
2788 channel1.read(readBuf);
2789 fail("Should throw ClosedChannelException");
2790 } catch (ClosedChannelException e) {
2796 level = TestLevel.PARTIAL_COMPLETE,
2799 args = {java.nio.ByteBuffer.class}
2801 public void testReadByteBuffer_BufNull() throws Exception {
2802 assertTrue(this.server1.isBound());
2803 java.nio.ByteBuffer readBuf = java.nio.ByteBuffer.allocate(0);
2804 // note: blocking-mode will make the read process endless!
2805 this.channel1.configureBlocking(false);
2807 channel1.read((java.nio.ByteBuffer) null);
2808 fail("Should throw NPE");
2809 } catch (NullPointerException e) {
2812 this.channel1.connect(localAddr1);
2813 assertTrue(tryFinish());
2815 this.channel1.read((java.nio.ByteBuffer) null);
2816 fail("Should throw NPE");
2817 } catch (NullPointerException e) {
2820 assertEquals(0, this.channel1.read(readBuf));
2824 * SocketChannelImpl.read(ByteBuffer[], int, int)'
2827 level = TestLevel.PARTIAL_COMPLETE,
2830 args = {java.nio.ByteBuffer[].class, int.class, int.class}
2832 public void testReadByteBufferArrayIntInt() throws Exception {
2833 assertTrue(this.server1.isBound());
2834 java.nio.ByteBuffer[] readBuf = new java.nio.ByteBuffer[2];
2835 readBuf[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
2836 readBuf[1] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
2837 assertFalse(this.channel1.isRegistered());
2838 assertTrue(this.channel1.isBlocking());
2839 assertFalse(this.channel1.isConnected());
2840 assertFalse(this.channel1.isConnectionPending());
2841 assertTrue(this.channel1.isOpen());
2842 // note: blocking-mode will make the read process endless!
2843 this.channel1.configureBlocking(false);
2845 channel1.read(readBuf, 0, 1);
2846 fail("Should throw NotYetConnectedException");
2847 } catch (NotYetConnectedException e) {
2850 this.channel1.connect(localAddr1);
2851 assertFalse(this.channel1.isBlocking());
2852 assertTrue(this.channel1.isConnectionPending());
2853 assertFalse(this.channel1.isConnected());
2854 assertTrue(tryFinish());
2855 assertEquals(0, this.channel1.read(readBuf, 0, 1));
2856 assertEquals(0, this.channel1.read(readBuf, 0, 2));
2858 this.channel1.close();
2860 channel1.read(readBuf, 0, 1);
2861 fail("Should throw ClosedChannelException");
2862 } catch (ClosedChannelException e) {
2868 * SocketChannelImpl.read(ByteBuffer[], int, int)'
2871 level = TestLevel.PARTIAL_COMPLETE,
2874 args = {java.nio.ByteBuffer[].class, int.class, int.class}
2876 public void testReadByteBufferArrayIntInt_Direct() throws Exception {
2877 assertTrue(this.server1.isBound());
2878 java.nio.ByteBuffer[] readBuf = new java.nio.ByteBuffer[2];
2879 readBuf[0] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL);
2880 readBuf[1] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL);
2881 assertFalse(this.channel1.isRegistered());
2882 assertTrue(this.channel1.isBlocking());
2883 assertFalse(this.channel1.isConnected());
2884 assertFalse(this.channel1.isConnectionPending());
2885 assertTrue(this.channel1.isOpen());
2886 // note: blocking-mode will make the read process endless!
2887 this.channel1.configureBlocking(false);
2889 channel1.read(readBuf, 0, 1);
2890 fail("Should throw NotYetConnectedException");
2891 } catch (NotYetConnectedException e) {
2894 this.channel1.connect(localAddr1);
2895 assertFalse(this.channel1.isBlocking());
2896 assertTrue(this.channel1.isConnectionPending());
2897 assertFalse(this.channel1.isConnected());
2898 assertTrue(tryFinish());
2899 assertEquals(0, this.channel1.read(readBuf, 0, 1));
2900 assertEquals(0, this.channel1.read(readBuf, 0, 2));
2902 this.channel1.close();
2904 channel1.read(readBuf, 0, 1);
2905 fail("Should throw ClosedChannelException");
2906 } catch (ClosedChannelException e) {
2912 level = TestLevel.PARTIAL_COMPLETE,
2915 args = {java.nio.ByteBuffer[].class, int.class, int.class}
2917 public void testReadByteBufferArrayIntInt_BufNull() throws Exception {
2918 assertTrue(this.server1.isBound());
2919 java.nio.ByteBuffer[] readBuf = new java.nio.ByteBuffer[2];
2920 readBuf[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
2921 assertFalse(this.channel1.isRegistered());
2922 assertTrue(this.channel1.isBlocking());
2923 assertFalse(this.channel1.isConnected());
2924 assertFalse(this.channel1.isConnectionPending());
2925 assertTrue(this.channel1.isOpen());
2926 // note: blocking-mode will make the read process endless!
2927 this.channel1.configureBlocking(false);
2929 channel1.read(null, 0, 0);
2930 fail("Should throw NPE");
2931 } catch (NullPointerException e) {
2934 this.channel1.connect(localAddr1);
2935 assertTrue(tryFinish());
2938 channel1.read(null, 0, 0);
2939 fail("Should throw NPE");
2940 } catch (NullPointerException e) {
2944 channel1.read(readBuf, 0, 2);
2945 fail("Should throw NPE");
2946 } catch (NullPointerException e) {
2950 assertEquals(0, this.channel1.read(readBuf, 0, 1));
2952 this.channel1.close();
2954 channel1.read(null, 0, 1);
2955 fail("Should throw NPE");
2956 } catch (NullPointerException e) {
2962 level = TestLevel.PARTIAL_COMPLETE,
2965 args = {java.nio.ByteBuffer.class}
2967 public void testWriteByteBuffer() throws IOException {
2968 assertTrue(this.server1.isBound());
2969 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer
2970 .allocate(CAPACITY_NORMAL);
2971 assertFalse(this.channel1.isRegistered());
2972 assertTrue(this.channel1.isBlocking());
2973 assertFalse(this.channel1.isConnected());
2974 assertFalse(this.channel1.isConnectionPending());
2975 assertTrue(this.channel1.isOpen());
2977 channel1.write(writeBuf);
2978 fail("Should throw NotYetConnectedException");
2979 } catch (NotYetConnectedException e) {
2982 this.channel1.connect(localAddr1);
2983 assertTrue(this.channel1.isBlocking());
2984 assertTrue(this.channel1.isConnected());
2985 assertFalse(this.channel1.isConnectionPending());
2986 assertTrue(this.channel1.isOpen());
2987 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
2989 this.channel1.close();
2991 channel1.write(writeBuf);
2992 fail("Should throw ClosedChannelException");
2993 } catch (ClosedChannelException e) {
2999 level = TestLevel.PARTIAL_COMPLETE,
3000 notes = "Doesn't verify AsynchronousCloseException, ClosedByInterruptException.",
3002 args = {java.nio.ByteBuffer.class}
3004 public void testWriteByteBuffer_Direct() throws IOException {
3005 assertTrue(this.server1.isBound());
3006 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer
3007 .allocateDirect(CAPACITY_NORMAL);
3008 assertFalse(this.channel1.isRegistered());
3009 assertTrue(this.channel1.isBlocking());
3010 assertFalse(this.channel1.isConnected());
3011 assertFalse(this.channel1.isConnectionPending());
3012 assertTrue(this.channel1.isOpen());
3014 channel1.write(writeBuf);
3015 fail("Should throw NotYetConnectedException");
3016 } catch (NotYetConnectedException e) {
3019 this.channel1.connect(localAddr1);
3020 assertTrue(this.channel1.isBlocking());
3021 assertTrue(this.channel1.isConnected());
3022 assertFalse(this.channel1.isConnectionPending());
3023 assertTrue(this.channel1.isOpen());
3024 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
3026 this.channel1.close();
3028 channel1.write(writeBuf);
3029 fail("Should throw ClosedChannelException");
3030 } catch (ClosedChannelException e) {
3036 level = TestLevel.PARTIAL_COMPLETE,
3039 args = {java.nio.ByteBuffer.class}
3041 public void testWriteByteBuffer_BufNull() throws IOException {
3042 assertTrue(this.server1.isBound());
3043 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer.allocate(0);
3044 this.channel1.connect(localAddr1);
3045 assertEquals(this.channel1.write(writeBuf), 0);
3047 this.channel1.write((java.nio.ByteBuffer) null);
3048 fail("Should throw NPE");
3049 } catch (NullPointerException e) {
3055 * SocketChannelImpl.write(ByteBuffer[], int, int)'
3058 level = TestLevel.PARTIAL_COMPLETE,
3059 notes = "Doesn't verify AsynchronousCloseException," +
3060 "ClosedByInterruptException.",
3062 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3064 public void testWriteByteBufferArrayIntInt() throws IOException {
3065 java.nio.ByteBuffer[] writeBuf = new java.nio.ByteBuffer[2];
3066 writeBuf[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
3067 writeBuf[1] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
3068 assertFalse(this.channel1.isRegistered());
3069 assertTrue(this.channel1.isBlocking());
3070 assertFalse(this.channel1.isConnected());
3071 assertFalse(this.channel1.isConnectionPending());
3072 assertTrue(this.channel1.isOpen());
3074 channel1.write(writeBuf, 0, 1);
3075 fail("Should throw NotYetConnectedException");
3076 } catch (NotYetConnectedException e) {
3079 this.channel1.connect(localAddr1);
3080 assertTrue(this.channel1.isBlocking());
3081 assertTrue(this.channel1.isConnected());
3082 assertFalse(this.channel1.isConnectionPending());
3083 assertTrue(this.channel1.isOpen());
3084 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf, 0, 1));
3085 // still writes the same size as above
3086 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf, 0, 2));
3089 assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2));
3090 this.channel1.close();
3092 channel1.write(writeBuf, 0, 1);
3093 fail("Should throw ClosedChannelException");
3094 } catch (ClosedChannelException e) {
3100 * SocketChannelImpl.write(ByteBuffer[], int, int)'
3103 level = TestLevel.PARTIAL_COMPLETE,
3106 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3108 public void testWriteByteBufferArrayIntInt_Direct() throws IOException {
3109 java.nio.ByteBuffer[] writeBuf = new java.nio.ByteBuffer[2];
3110 writeBuf[0] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL);
3111 writeBuf[1] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL);
3112 assertFalse(this.channel1.isRegistered());
3113 assertTrue(this.channel1.isBlocking());
3114 assertFalse(this.channel1.isConnected());
3115 assertFalse(this.channel1.isConnectionPending());
3116 assertTrue(this.channel1.isOpen());
3118 channel1.write(writeBuf, 0, 1);
3119 fail("Should throw NotYetConnectedException");
3120 } catch (NotYetConnectedException e) {
3123 this.channel1.connect(localAddr1);
3124 assertTrue(this.channel1.isBlocking());
3125 assertTrue(this.channel1.isConnected());
3126 assertFalse(this.channel1.isConnectionPending());
3127 assertTrue(this.channel1.isOpen());
3128 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf, 0, 1));
3129 // still writes the same size as above
3130 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf, 0, 2));
3133 assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2));
3134 this.channel1.close();
3136 channel1.write(writeBuf, 0 ,1);
3137 fail("Should throw ClosedChannelException");
3138 } catch (ClosedChannelException e) {
3144 level = TestLevel.PARTIAL_COMPLETE,
3147 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3149 public void testWriteByteBufferArrayIntInt_BufNull() throws IOException {
3150 java.nio.ByteBuffer[] writeBuf = new java.nio.ByteBuffer[0];
3152 this.channel1.connect(localAddr1);
3154 this.channel1.write(null, 0, 1);
3155 fail("Should throw NPE");
3156 } catch (NullPointerException e) {
3159 assertEquals(0, this.channel1.write(writeBuf, 0, 0));
3161 this.channel1.write(writeBuf, 0, 1);
3162 fail("Should throw IndexOutOfBoundsException");
3163 } catch (IndexOutOfBoundsException e) {
3166 writeBuf = new java.nio.ByteBuffer[1];
3168 this.channel1.write(writeBuf, 0, 1);
3169 fail("Should throw NPE");
3170 } catch (NullPointerException e) {
3173 writeBuf[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
3175 this.channel1.write(writeBuf, 0, 2);
3176 fail("Should throw IndexOutOfBoundsException");
3177 } catch (IndexOutOfBoundsException e) {
3180 this.server1.close();
3182 channel1.read(null, 0, 1);
3183 fail("Should throw NPE");
3184 } catch (NullPointerException e) {
3190 level = TestLevel.PARTIAL_COMPLETE,
3191 notes = "Verifies IndexOutOfBoundsException.",
3193 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3195 public void testWriteByteBufferArrayIntInt_SizeError() throws IOException {
3196 java.nio.ByteBuffer[] writeBuf =
3197 {java.nio.ByteBuffer.allocate(CAPACITY_NORMAL)};
3199 this.channel1.connect(localAddr1);
3201 this.channel1.write(writeBuf, -1, 1);
3202 fail("Should throw IndexOutOfBoundsException");
3203 } catch (IndexOutOfBoundsException e) {
3206 assertEquals(0, this.channel1.write(writeBuf, 0, 0));
3208 this.channel1.write(writeBuf, 0, -1);
3209 fail("Should throw IndexOutOfBoundsException");
3210 } catch (IndexOutOfBoundsException e) {
3214 this.channel1.write(writeBuf, 1, 1);
3215 fail("Should throw IndexOutOfBoundsException");
3216 } catch (IndexOutOfBoundsException e) {
3220 this.channel1.write(writeBuf, 0, 2);
3221 fail("Should throw IndexOutOfBoundsException");
3222 } catch (IndexOutOfBoundsException e) {
3228 level = TestLevel.PARTIAL_COMPLETE,
3231 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3233 public void testReadByteBufferArrayIntInt_SizeError() throws IOException {
3234 java.nio.ByteBuffer[] readBuf = new java.nio.ByteBuffer[0];
3236 this.channel1.connect(localAddr1);
3238 this.channel1.read(null, -1, 1);
3239 fail("Should throw IndexOutOfBoundsException");
3240 } catch (IndexOutOfBoundsException e) {
3243 assertEquals(0, this.channel1.read(readBuf, 0, 0));
3245 this.channel1.read(readBuf, 0, -1);
3246 fail("Should throw IndexOutOfBoundsException");
3247 } catch (IndexOutOfBoundsException e) {
3250 readBuf = new java.nio.ByteBuffer[1];
3252 this.channel1.read(readBuf, 2, 1);
3253 fail("Should throw IndexOutOfBoundsException");
3254 } catch (IndexOutOfBoundsException e) {
3258 this.channel1.read(readBuf, 2, -1);
3259 fail("Should throw IndexOutOfBoundsException");
3260 } catch (IndexOutOfBoundsException e) {
3263 this.server1.close();
3265 assertEquals(CAPACITY_NORMAL, this.channel1.read(null, -1, -1));
3266 fail("Should throw IndexOutOfBoundsException");
3267 } catch (IndexOutOfBoundsException e) {
3273 * ==========================================================================
3274 * Tests for read/write real data
3275 * ==========================================================================
3279 * @tests java.nio.channels.SocketChannel#read(ByteBuffer[],int,int)
3282 level = TestLevel.PARTIAL_COMPLETE,
3285 args = {java.nio.ByteBuffer[].class}
3287 public void test_read$LByteBuffer_blocking() throws Exception {
3288 assert_read$LByteBuffer(true);
3292 * @tests java.nio.channels.SocketChannel#read(ByteBuffer[],int,int)
3295 level = TestLevel.PARTIAL_COMPLETE,
3298 args = {java.nio.ByteBuffer[].class}
3300 public void test_read$LByteBuffer_nonblocking() throws Exception {
3301 assert_read$LByteBuffer(false);
3304 private void assert_read$LByteBuffer(boolean isBlocking) throws IOException {
3305 // initialize write content
3306 byte[] writeContent = new byte[CAPACITY_NORMAL * 2];
3307 for (int i = 0; i < CAPACITY_NORMAL * 2; i++) {
3308 writeContent[i] = (byte) i;
3310 ByteBuffer[] readContents = new ByteBuffer[2];
3311 readContents[0] = ByteBuffer.allocate(CAPACITY_NORMAL);
3312 readContents[1] = ByteBuffer.allocate(CAPACITY_NORMAL + 1);
3313 // establish connection
3314 channel1.connect(localAddr1);
3315 Socket acceptedSocket = server1.accept();
3316 // use OutputStream.write to send CAPACITY_NORMAL * 2 bytes data
3317 OutputStream out = acceptedSocket.getOutputStream();
3318 out.write(writeContent);
3319 // use close to guarantee all data is sent
3320 acceptedSocket.close();
3321 // configure block/nonblock mode
3322 channel1.configureBlocking(isBlocking);
3323 long startTime = System.currentTimeMillis();
3327 while (totalRead <= CAPACITY_NORMAL * 2) {
3328 countRead = channel1.read(readContents);
3329 if (0 == countRead && !readContents[1].hasRemaining()) {
3330 // read returns 0 because readContents is full
3333 if (EOF == countRead) {
3336 totalRead += countRead;
3337 // if the channel could not finish reading in TIMEOUT ms, the
3338 // test fails. It is used to guarantee the test never hangs even
3339 // if there are bugs of SocketChannel implementation. For
3340 // blocking read, it possibly returns 0 in some cases.
3341 assertTimeout(startTime, TIMEOUT);
3344 // assert total bytes read and the position of ByteBuffers
3345 assertEquals(CAPACITY_NORMAL * 2, totalRead);
3346 assertEquals(CAPACITY_NORMAL, readContents[0].position());
3347 assertEquals(CAPACITY_NORMAL, readContents[1].position());
3348 // assert read content
3349 readContents[0].flip();
3350 readContents[1].flip();
3351 for (int i = 0; i < CAPACITY_NORMAL; i++) {
3352 assertEquals(writeContent[i], readContents[0].get());
3354 for (int i = CAPACITY_NORMAL; i < CAPACITY_NORMAL * 2; i++) {
3355 assertEquals(writeContent[i], readContents[1].get());
3360 * @tests java.nio.channels.SocketChannel#read(ByteBuffer[],int,int)
3363 level = TestLevel.PARTIAL_COMPLETE,
3366 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3368 public void test_read$LByteBufferII_blocking() throws Exception {
3369 assert_read$LByteBufferII(true);
3373 * @tests java.nio.channels.SocketChannel#read(ByteBuffer[],int,int)
3376 level = TestLevel.PARTIAL_COMPLETE,
3379 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3381 public void test_read$LByteBufferII_nonblocking() throws Exception {
3382 assert_read$LByteBufferII(false);
3385 private void assert_read$LByteBufferII(boolean isBlocking) throws IOException {
3386 // initialize write content
3387 byte[] writeContent = new byte[CAPACITY_NORMAL * 2];
3388 for (int i = 0; i < CAPACITY_NORMAL * 2; i++) {
3389 writeContent[i] = (byte) i;
3391 ByteBuffer[] readContents = new ByteBuffer[2];
3392 readContents[0] = ByteBuffer.allocate(CAPACITY_NORMAL);
3393 readContents[1] = ByteBuffer.allocate(CAPACITY_NORMAL + 1);
3394 // establish connection
3395 channel1.connect(localAddr1);
3396 Socket acceptedSocket = server1.accept();
3397 // use OutputStream.write to send CAPACITY_NORMAL * 2 bytes data
3398 OutputStream out = acceptedSocket.getOutputStream();
3399 out.write(writeContent);
3400 // use close to guarantee all data is sent
3401 acceptedSocket.close();
3402 // configure block/nonblock mode
3403 channel1.configureBlocking(isBlocking);
3404 long startTime = System.currentTimeMillis();
3408 while (totalRead <= CAPACITY_NORMAL * 2) {
3409 countRead = channel1.read(readContents, 0, 2);
3410 if (0 == countRead && !readContents[1].hasRemaining()) {
3411 // read returns 0 because readContents is full
3414 if (EOF == countRead) {
3417 totalRead += countRead;
3418 // if the channel could not finish reading in TIMEOUT ms, the
3419 // test fails. It is used to guarantee the test never hangs even
3420 // if there are bugs of SocketChannel implementation. For
3421 // blocking read, it possibly returns 0 in some cases.
3422 assertTimeout(startTime, TIMEOUT);
3425 // assert total bytes read and the position of ByteBuffers
3426 assertEquals(CAPACITY_NORMAL * 2, totalRead);
3427 assertEquals(CAPACITY_NORMAL, readContents[0].position());
3428 assertEquals(CAPACITY_NORMAL, readContents[1].position());
3429 // assert read content
3430 readContents[0].flip();
3431 readContents[1].flip();
3432 for (int i = 0; i < CAPACITY_NORMAL; i++) {
3433 assertEquals(writeContent[i], readContents[0].get());
3435 for (int i = CAPACITY_NORMAL; i < CAPACITY_NORMAL * 2; i++) {
3436 assertEquals(writeContent[i], readContents[1].get());
3441 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[],int,int)
3444 level = TestLevel.PARTIAL_COMPLETE,
3447 args = {java.nio.ByteBuffer[].class}
3449 public void test_write$LByteBuffer_blocking() throws Exception {
3450 assert_write$LByteBuffer(true);
3454 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[],int,int)
3457 level = TestLevel.PARTIAL_COMPLETE,
3460 args = {java.nio.ByteBuffer[].class}
3462 public void test_write$LByteBuffer_nonblocking() throws Exception {
3463 assert_write$LByteBuffer(false);
3466 private void assert_write$LByteBuffer(boolean isBlocking)
3467 throws IOException {
3468 // initialize write contents
3469 ByteBuffer writeContents[] = new ByteBuffer[2];
3470 writeContents[0] = ByteBuffer.allocate(CAPACITY_NORMAL);
3471 writeContents[1] = ByteBuffer.allocate(CAPACITY_NORMAL);
3472 for (int i = 0; i < CAPACITY_NORMAL; i++) {
3473 writeContents[0].put((byte) i);
3475 for (int i = CAPACITY_NORMAL; i < CAPACITY_NORMAL * 2; i++) {
3476 writeContents[1].put((byte) i);
3478 writeContents[0].flip();
3479 writeContents[1].flip();
3480 // establish connection
3481 channel1.connect(localAddr1);
3482 Socket acceptedSocket = server1.accept();
3483 // set blocking/nonblocking mode
3484 channel1.configureBlocking(isBlocking);
3486 assertEquals(2 * CAPACITY_NORMAL, channel1.write(writeContents));
3488 // assert written count and ByteBuffer position
3489 assertEquals(CAPACITY_NORMAL, writeContents[0].position());
3490 assertEquals(CAPACITY_NORMAL, writeContents[1].position());
3491 // use close to guarantee all data is sent
3493 InputStream in = acceptedSocket.getInputStream();
3494 byte[] readContent = new byte[CAPACITY_NORMAL * 2 + 1];
3497 // if the channel could not finish reading in TIMEOUT ms, the test
3498 // fails. It is used to guarantee the test never hangs even if there
3499 // are bugs of SocketChannel implementation.
3500 acceptedSocket.setSoTimeout(TIMEOUT);
3501 // use InputStream.read to read data.
3502 while (totalCount <= CAPACITY_NORMAL) {
3503 count = in.read(readContent, totalCount, readContent.length
3508 totalCount += count;
3510 // assert read content
3511 assertEquals(CAPACITY_NORMAL * 2, totalCount);
3512 writeContents[0].flip();
3513 writeContents[1].flip();
3514 for (int i = 0; i < CAPACITY_NORMAL; i++) {
3515 assertEquals(writeContents[0].get(), readContent[i]);
3517 for (int i = CAPACITY_NORMAL; i < CAPACITY_NORMAL * 2; i++) {
3518 assertEquals(writeContents[1].get(), readContent[i]);
3523 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[],int,int)
3526 level = TestLevel.PARTIAL_COMPLETE,
3529 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3531 public void test_write$LByteBufferII_blocking() throws Exception {
3532 assert_write$LByteBufferII(true);
3536 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[],int,int)
3539 level = TestLevel.PARTIAL_COMPLETE,
3542 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3544 public void test_write$LByteBufferII_nonblocking() throws Exception {
3545 assert_write$LByteBufferII(false);
3548 private void assert_write$LByteBufferII(boolean isBlocking)
3549 throws IOException {
3550 // initialize write contents
3551 ByteBuffer writeContents[] = new ByteBuffer[2];
3552 writeContents[0] = ByteBuffer.allocate(CAPACITY_NORMAL);
3553 writeContents[1] = ByteBuffer.allocate(CAPACITY_NORMAL);
3554 for (int i = 0; i < CAPACITY_NORMAL; i++) {
3555 writeContents[0].put((byte) i);
3557 for (int i = CAPACITY_NORMAL; i < CAPACITY_NORMAL * 2; i++) {
3558 writeContents[1].put((byte) i);
3560 writeContents[0].flip();
3561 writeContents[1].flip();
3562 // establish connection
3563 channel1.connect(localAddr1);
3564 Socket acceptedSocket = server1.accept();
3565 // set blocking/nonblocking mode
3566 channel1.configureBlocking(isBlocking);
3568 assertEquals(CAPACITY_NORMAL, channel1.write(writeContents, 0, 1));
3569 assertEquals(CAPACITY_NORMAL, channel1.write(writeContents, 1, 1));
3571 // assert written count and ByteBuffer position
3572 assertEquals(CAPACITY_NORMAL, writeContents[0].position());
3573 assertEquals(CAPACITY_NORMAL, writeContents[1].position());
3574 // use close to guarantee all data is sent
3576 InputStream in = acceptedSocket.getInputStream();
3577 byte[] readContent = new byte[CAPACITY_NORMAL * 2 + 1];
3580 // if the channel could not finish reading in TIMEOUT ms, the test
3581 // fails. It is used to guarantee the test never hangs even if there
3582 // are bugs of SocketChannel implementation.
3583 acceptedSocket.setSoTimeout(TIMEOUT);
3584 // use InputStream.read to read data.
3585 while (totalCount <= CAPACITY_NORMAL) {
3586 count = in.read(readContent, totalCount, readContent.length
3591 totalCount += count;
3593 // assert read content
3594 assertEquals(CAPACITY_NORMAL * 2, totalCount);
3595 writeContents[0].flip();
3596 writeContents[1].flip();
3597 for (int i = 0; i < CAPACITY_NORMAL; i++) {
3598 assertEquals(writeContents[0].get(), readContent[i]);
3600 for (int i = CAPACITY_NORMAL; i < CAPACITY_NORMAL * 2; i++) {
3601 assertEquals(writeContents[1].get(), readContent[i]);
3607 level = TestLevel.PARTIAL_COMPLETE,
3613 level = TestLevel.PARTIAL_COMPLETE,
3615 clazz = SelectableChannel.class,
3616 method = "configureBlocking",
3617 args = {boolean.class}
3620 public void testSocket_configureblocking() throws IOException {
3621 byte[] serverWBuf = new byte[CAPACITY_NORMAL];
3622 for (int i = 0; i < serverWBuf.length; i++) {
3623 serverWBuf[i] = (byte) i;
3625 java.nio.ByteBuffer buf = java.nio.ByteBuffer
3626 .allocate(CAPACITY_NORMAL + 1);
3627 channel1.connect(localAddr1);
3629 Socket sock = this.channel1.socket();
3630 channel1.configureBlocking(false);
3631 assertFalse(channel1.isBlocking());
3632 OutputStream channelSocketOut = sock.getOutputStream();
3634 // write operation is not allowed in non-blocking mode
3635 channelSocketOut.write(buf.array());
3636 fail("Non-Blocking mode should cause IllegalBlockingModeException");
3637 } catch (IllegalBlockingModeException e) {
3640 channel1.configureBlocking(true);
3641 assertTrue(channel1.isBlocking());
3642 // write operation is allowed in blocking mode
3643 channelSocketOut.write(buf.array());
3647 * @tests SocketChannel#read(ByteBuffer[], int, int) when remote server
3651 level = TestLevel.PARTIAL_COMPLETE,
3654 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3656 public void test_socketChannel_read_ByteBufferII_remoteClosed()
3658 // regression 1 for HARMONY-549
3659 ServerSocketChannel ssc = ServerSocketChannel.open();
3660 ssc.socket().bind(localAddr2);
3661 SocketChannel sc = SocketChannel.open();
3662 sc.connect(localAddr2);
3663 ssc.accept().close();
3664 ByteBuffer[] buf = {ByteBuffer.allocate(10)};
3665 assertEquals(-1, sc.read(buf, 0, 1));
3671 * @tests SocketChannel#write(ByteBuffer[], int, int)
3674 level = TestLevel.PARTIAL_COMPLETE,
3677 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3679 public void test_socketChannel_write_ByteBufferII() throws Exception {
3680 // regression 2 for HARMONY-549
3681 ServerSocketChannel ssc = ServerSocketChannel.open();
3682 ssc.socket().bind(localAddr2);
3683 SocketChannel sc = SocketChannel.open();
3684 sc.connect(localAddr2);
3685 SocketChannel sock = ssc.accept();
3686 ByteBuffer[] buf = {ByteBuffer.allocate(10), null};
3688 sc.write(buf, 0, 2);
3689 fail("should throw NPE");
3690 } catch (NullPointerException e) {
3695 ByteBuffer target = ByteBuffer.allocate(10);
3696 assertEquals(-1, sock.read(target));
3700 * @tests SocketChannel#read(ByteBuffer[], int, int) with a null ByteBuffer
3703 level = TestLevel.PARTIAL_COMPLETE,
3706 args = {java.nio.ByteBuffer[].class, int.class, int.class}
3708 public void test_socketChannel_read_ByteBufferII_bufNULL() throws Exception {
3709 // regression 3 for HARMONY-549
3710 ServerSocketChannel ssc = ServerSocketChannel.open();
3711 ssc.socket().bind(localAddr2);
3712 SocketChannel sc = SocketChannel.open();
3713 sc.connect(localAddr2);
3715 ByteBuffer[] buf = new ByteBuffer[2];
3716 buf[0] = ByteBuffer.allocate(1);
3717 // let buf[1] be null
3720 fail("should throw NullPointerException");
3721 } catch (NullPointerException e) {
3729 * @tests SocketChannel#write(ByteBuffer) after close
3732 level = TestLevel.PARTIAL_COMPLETE,
3735 args = {java.nio.ByteBuffer.class}
3737 public void test_socketChannel_write_close() throws Exception {
3738 // regression 4 for HARMONY-549
3739 ServerSocketChannel ssc = ServerSocketChannel.open();
3740 ssc.socket().bind(localAddr2);
3741 SocketChannel sc = SocketChannel.open();
3742 sc.connect(localAddr2);
3743 SocketChannel sock = ssc.accept();
3744 ByteBuffer buf = null;
3749 fail("should throw NPE");
3750 } catch (NullPointerException e) {
3757 * @tests SocketChannel#write(ByteBuffer) if position is not zero
3760 level = TestLevel.PARTIAL_COMPLETE,
3763 args = {java.nio.ByteBuffer.class}
3765 public void test_socketChannel_write_ByteBuffer_posNotZero()
3767 // regression 5 for HARMONY-549
3768 final String testStr = "Hello World";
3769 ByteBuffer readBuf = ByteBuffer.allocate(11);
3770 ByteBuffer buf = ByteBuffer.wrap(testStr.getBytes());
3771 ServerSocketChannel ssc = ServerSocketChannel.open();
3772 ssc.socket().bind(localAddr2);
3773 SocketChannel sc = SocketChannel.open();
3774 sc.connect(localAddr2);
3776 ssc.accept().write(buf);
3777 assertEquals(9, sc.read(readBuf));
3780 byte[] read = new byte[9];
3781 byte[] write = new byte[11];
3784 for (int i = 0; i < 9; i++) {
3785 assertEquals(read[i], write[i + 2]);
3790 * @tests SocketChannelImpl#read(ByteBuffer[])
3793 level = TestLevel.PARTIAL_COMPLETE,
3794 notes = "Doesn't verify exceptions.",
3796 args = {java.nio.ByteBuffer[].class}
3798 public void test_read$LByteBuffer_Blocking() throws IOException {
3799 // regression test for Harmony-728
3800 byte[] data = new byte[CAPACITY_NORMAL];
3801 for (int i = 0; i < CAPACITY_NORMAL; i++) {
3804 ByteBuffer[] buf = new ByteBuffer[2];
3805 buf[0] = ByteBuffer.allocate(CAPACITY_NORMAL);
3806 buf[1] = ByteBuffer.allocate(CAPACITY_NORMAL);
3807 channel1.connect(localAddr1);
3808 Socket socket = null;
3810 socket = server1.accept();
3811 OutputStream out = socket.getOutputStream();
3813 // should not block here
3816 if (null != socket) {
3825 * @tests SocketChannelImpl#socket().getOutputStream().read
3828 level = TestLevel.PARTIAL_COMPLETE,
3829 notes = "Verifies IllegalBlockingModeException, NullPointerException.",
3833 public void test_socket_getOutputStream_nonBlocking_read_Exception()
3834 throws IOException {
3835 channel1.connect(this.localAddr1);
3836 InputStream is = channel1.socket().getInputStream();
3837 channel1.configureBlocking(false);
3840 fail("should throw IllegalBlockingModeException");
3841 } catch (IllegalBlockingModeException e) {
3845 is.read(null, 1, 1);
3846 fail("should throw NullPointerException");
3847 } catch (NullPointerException e) {
3851 is.read(null, -1, 1);
3852 fail("should throw IndexOutOfBoundsException");
3853 } catch (IndexOutOfBoundsException e) {
3860 fail("should throw NullPointerException");
3861 } catch (NullPointerException e) {
3866 fail("should throw IllegalBlockingModeException");
3867 } catch (IllegalBlockingModeException e) {
3871 is.read(null, 1, 1);
3872 fail("should throw NullPointerException");
3873 } catch (NullPointerException e) {
3877 is.read(null, -1, 1);
3878 fail("should throw IndexOutOfBoundsException");
3879 } catch (IndexOutOfBoundsException e) {
3885 * @tests SocketChannelImpl#socket().getOutputStream().read
3888 level = TestLevel.PARTIAL_COMPLETE,
3889 notes = "Verifies NullPointerException, ClosedChannelException, IndexOutOfBoundsException.",
3893 public void test_socket_getOutputStream_blocking_read_Exception()
3894 throws IOException {
3895 channel1.connect(this.localAddr1);
3896 InputStream is = channel1.socket().getInputStream();
3899 fail("should throw NullPointerException");
3900 } catch (NullPointerException e) {
3904 is.read(null, 1, 1);
3905 fail("should throw NullPointerException");
3906 } catch (NullPointerException e) {
3910 is.read(null, -1, 1);
3911 fail("should throw IndexOutOfBoundsException");
3912 } catch (IndexOutOfBoundsException e) {
3919 fail("should throw NullPointerException");
3920 } catch (NullPointerException e) {
3925 fail("should throw ClosedChannelException");
3926 } catch (ClosedChannelException e) {
3930 is.read(null, 1, 1);
3931 fail("should throw NullPointerException");
3932 } catch (NullPointerException e) {
3936 is.read(null, -1, 1);
3937 fail("should throw IndexOutOfBoundsException");
3938 } catch (IndexOutOfBoundsException e) {
3944 * @tests SocketChannelImpl#socket().getOutputStream().write
3947 level = TestLevel.PARTIAL_COMPLETE,
3948 notes = "Verifies NullPointerException, IllegalBlockingModeException.",
3952 public void test_socket_getOutputStream_nonBlocking_write_Exception()
3953 throws IOException {
3954 channel1.connect(this.localAddr1);
3955 OutputStream os = channel1.socket().getOutputStream();
3956 channel1.configureBlocking(false);
3959 fail("should throw NullPointerException");
3960 } catch (NullPointerException e) {
3965 fail("should throw IllegalBlockingModeException");
3966 } catch (IllegalBlockingModeException e) {
3971 fail("should throw IllegalBlockingModeException");
3972 } catch (IllegalBlockingModeException e) {
3976 os.write(null, 1, 1);
3977 fail("should throw NullPointerException");
3978 } catch (NullPointerException e) {
3982 os.write(null, -1, 1);
3983 fail("should throw IndexOutOfBoundsException");
3984 } catch (IndexOutOfBoundsException e) {
3991 fail("should throw NullPointerException");
3992 } catch (NullPointerException e) {
3997 fail("should throw IllegalBlockingModeException");
3998 } catch (IllegalBlockingModeException e) {
4002 os.write(null, 1, 1);
4003 fail("should throw NullPointerException");
4004 } catch (NullPointerException e) {
4008 os.write(null, -1, 1);
4009 fail("should throw IndexOutOfBoundsException");
4010 } catch (IndexOutOfBoundsException e) {
4016 * @tests SocketChannelImpl#socket().getOutputStream().write
4019 level = TestLevel.PARTIAL_COMPLETE,
4020 notes = "Verifies NullPointerException, IndexOutOfBoundsException, ClosedChannelException.",
4024 public void test_socket_getOutputStream_blocking_write_Exception()
4025 throws IOException {
4026 channel1.connect(this.localAddr1);
4027 OutputStream os = channel1.socket().getOutputStream();
4030 fail("should throw NullPointerException");
4031 } catch (NullPointerException e) {
4035 os.write(null, 1, 1);
4036 fail("should throw NullPointerException");
4037 } catch (NullPointerException e) {
4041 os.write(null, -1, 1);
4042 fail("should throw IndexOutOfBoundsException");
4043 } catch (IndexOutOfBoundsException e) {
4050 fail("should throw NullPointerException");
4051 } catch (NullPointerException e) {
4056 fail("should throw ClosedChannelException");
4057 } catch (ClosedChannelException e) {
4061 os.write(null, 1, 1);
4062 fail("should throw NullPointerException");
4063 } catch (NullPointerException e) {
4067 os.write(null, -1, 1);
4068 fail("should throw IndexOutOfBoundsException");
4069 } catch (IndexOutOfBoundsException e) {
4075 * @tests SocketChannelImpl#socket().getOutputStream().write(int)
4078 level = TestLevel.PARTIAL_COMPLETE,
4083 public void test_socket_getOutputStream_write_oneByte()
4084 throws IOException {
4086 // Regression test for Harmony-3475
4090 channel1.connect(this.localAddr1);
4092 OutputStream os = channel1.socket().getOutputStream();
4094 Socket acceptedSocket = server1.accept();
4096 InputStream in = acceptedSocket.getInputStream();
4101 int lastByte = in.read();
4102 if (lastByte == -1) {
4103 fail("Server received nothing. Expected 1 byte.");
4104 } else if (lastByte != MAGIC) {
4105 fail("Server received wrong single byte: " + lastByte
4106 + ", expected: " + MAGIC);
4109 lastByte = in.read();
4110 if (lastByte != -1) {
4111 fail("Server received too long sequence. Expected 1 byte.");
4115 class MockSocketChannel extends SocketChannel {
4117 private boolean isWriteCalled = false;
4119 private boolean isReadCalled = false;
4121 private boolean isConstructorCalled = false;
4123 public MockSocketChannel(SelectorProvider provider) {
4125 isConstructorCalled = true;
4128 public Socket socket() {
4132 public boolean isConnected() {
4136 public boolean isConnectionPending() {
4140 public boolean connect(SocketAddress address) throws IOException {
4144 public boolean finishConnect() throws IOException {
4148 public int read(ByteBuffer target) throws IOException {
4152 public long read(ByteBuffer[] targets, int offset, int length)
4153 throws IOException {
4154 // Verify that calling read(ByteBuffer[]) leads to the method
4155 // read(ByteBuffer[], int, int) being called with a 0 for the
4156 // second parameter and targets.length as the third parameter.
4157 if (0 == offset && length == targets.length) {
4158 isReadCalled = true;
4163 public int write(ByteBuffer source) throws IOException {
4167 public long write(ByteBuffer[] sources, int offset, int length)
4168 throws IOException {
4169 // Verify that calling write(ByteBuffer[]) leads to the method
4170 // write(ByteBuffer[], int, int) being called with a 0 for the
4171 // second parameter and sources.length as the third parameter.
4172 if (0 == offset && length == sources.length) {
4173 isWriteCalled = true;
4178 protected void implCloseSelectableChannel() throws IOException {
4182 protected void implConfigureBlocking(boolean blockingMode)
4183 throws IOException {
4188 class SubSocketAddress extends SocketAddress {
4189 private static final long serialVersionUID = 1L;
4192 public SubSocketAddress() {