/external/apache-harmony/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SocketChannelTest.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 1568 lines · 1099 code · 192 blank · 277 comment · 33 complexity · a862ba1a08c89b79c36c5a4e32a3fa6d MD5 · raw file

  1. /*
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. */
  17. package org.apache.harmony.nio.tests.java.nio.channels;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.net.BindException;
  22. import java.net.ConnectException;
  23. import java.net.InetAddress;
  24. import java.net.InetSocketAddress;
  25. import java.net.ServerSocket;
  26. import java.net.Socket;
  27. import java.net.SocketAddress;
  28. import java.net.SocketException;
  29. import java.nio.Buffer;
  30. import java.nio.ByteBuffer;
  31. import java.nio.channels.AlreadyConnectedException;
  32. import java.nio.channels.ClosedChannelException;
  33. import java.nio.channels.ConnectionPendingException;
  34. import java.nio.channels.IllegalBlockingModeException;
  35. import java.nio.channels.NoConnectionPendingException;
  36. import java.nio.channels.NotYetConnectedException;
  37. import java.nio.channels.ServerSocketChannel;
  38. import java.nio.channels.SocketChannel;
  39. import java.nio.channels.UnresolvedAddressException;
  40. import java.nio.channels.UnsupportedAddressTypeException;
  41. import java.nio.channels.spi.SelectorProvider;
  42. import junit.framework.TestCase;
  43. import tests.support.Support_PortManager;
  44. /**
  45. * Tests for SocketChannel and its default implementation.
  46. */
  47. public class SocketChannelTest extends TestCase {
  48. private static final int CAPACITY_NORMAL = 200;
  49. private InetSocketAddress localAddr1;
  50. private InetSocketAddress localAddr2;
  51. private SocketChannel channel1;
  52. private SocketChannel channel2;
  53. private ServerSocket server1;
  54. private ServerSocket server2;
  55. private final static int TIMEOUT = 60000;
  56. private final static int EOF = -1;
  57. protected void setUp() throws Exception {
  58. super.setUp();
  59. this.localAddr1 = new InetSocketAddress("127.0.0.1",
  60. Support_PortManager.getNextPort());
  61. this.localAddr2 = new InetSocketAddress("127.0.0.1",
  62. Support_PortManager.getNextPort());
  63. this.channel1 = SocketChannel.open();
  64. this.channel2 = SocketChannel.open();
  65. this.server1 = new ServerSocket(localAddr1.getPort());
  66. }
  67. protected void tearDown() throws Exception {
  68. super.tearDown();
  69. if (null != this.channel1) {
  70. try {
  71. this.channel1.close();
  72. } catch (Exception e) {
  73. //ignore
  74. }
  75. }
  76. if (null != this.channel2) {
  77. try {
  78. this.channel2.close();
  79. } catch (Exception e) {
  80. //ignore
  81. }
  82. }
  83. if (null != this.server1) {
  84. try {
  85. this.server1.close();
  86. } catch (Exception e) {
  87. //ignore
  88. }
  89. }
  90. if (null != this.server2) {
  91. try {
  92. this.server2.close();
  93. } catch (Exception e) {
  94. //ignore
  95. }
  96. }
  97. }
  98. // -------------------------------------------------------------------
  99. // Test for methods in abstract class.
  100. // -------------------------------------------------------------------
  101. /*
  102. * Test method for 'java.nio.channels.SocketChannel.validOps()'
  103. */
  104. public void testValidOps() {
  105. MockSocketChannel testMSChannel = new MockSocketChannel(null);
  106. assertEquals(13, this.channel1.validOps());
  107. assertEquals(13, testMSChannel.validOps());
  108. }
  109. /*
  110. * Test method for 'java.nio.channels.SocketChannel.open()'
  111. */
  112. public void testOpen() throws IOException {
  113. java.nio.ByteBuffer[] buf = new java.nio.ByteBuffer[1];
  114. buf[0] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  115. MockSocketChannel testMSChannel = new MockSocketChannel(null);
  116. MockSocketChannel testMSChannelnotnull = new MockSocketChannel(
  117. SelectorProvider.provider());
  118. assertNull(testMSChannel.provider());
  119. assertNotNull(testMSChannelnotnull.provider());
  120. assertNotNull(this.channel1);
  121. assertEquals(this.channel1.provider(), testMSChannelnotnull.provider());
  122. try {
  123. this.channel1.write(buf);
  124. fail("Should throw NotYetConnectedException");
  125. } catch (NotYetConnectedException e) {
  126. // correct
  127. }
  128. }
  129. /*
  130. * Test method for 'java.nio.channels.SocketChannel.open(SocketAddress)'
  131. */
  132. public void testOpenSocketAddress_Null() throws IOException {
  133. SocketChannel channel1IP = null;
  134. try {
  135. channel1IP = SocketChannel.open(null);
  136. fail("Should throw an IllegalArgumentException");
  137. } catch (IllegalArgumentException e) {
  138. // correct
  139. }
  140. assertNull(channel1IP);
  141. }
  142. /*
  143. * Test method for 'java.nio.channels.SocketChannel.read(ByteBuffer[])'
  144. */
  145. public void testReadByteBufferArray() throws IOException {
  146. java.nio.ByteBuffer[] byteBuf = null;
  147. MockSocketChannel testMSChannelnull = new MockSocketChannel(null);
  148. MockSocketChannel testMSChannel = new MockSocketChannel(
  149. SelectorProvider.provider());
  150. ServerSocket testServer = new ServerSocket(Support_PortManager
  151. .getNextPort());
  152. try {
  153. try {
  154. this.channel1.read(byteBuf);
  155. fail("Should throw NPE");
  156. } catch (NullPointerException e) {
  157. // correct
  158. }
  159. byteBuf = new java.nio.ByteBuffer[CAPACITY_NORMAL];
  160. try {
  161. this.channel1.read(byteBuf);
  162. fail("Should throw NotYetConnectedException");
  163. } catch (NotYetConnectedException e) {
  164. // correct
  165. }
  166. long readNum = CAPACITY_NORMAL;
  167. readNum = testMSChannel.read(byteBuf);
  168. assertEquals(0, readNum);
  169. readNum = CAPACITY_NORMAL;
  170. readNum = testMSChannelnull.read(byteBuf);
  171. assertEquals(0, readNum);
  172. } finally {
  173. testServer.close();
  174. }
  175. }
  176. /*
  177. * Test method for 'java.nio.channels.SocketChannel.read(ByteBuffer[])'
  178. */
  179. public void testReadByteBufferArray_BufNull() throws IOException {
  180. java.nio.ByteBuffer[] byteBuf = null;
  181. MockSocketChannel testMSChannelnull = new MockSocketChannel(null);
  182. MockSocketChannel testMSChannel = new MockSocketChannel(
  183. SelectorProvider.provider());
  184. try {
  185. this.channel1.read(byteBuf);
  186. fail("Should throw NPE");
  187. } catch (NullPointerException e) {
  188. // correct
  189. }
  190. try {
  191. testMSChannel.read(byteBuf);
  192. fail("Should throw NPE");
  193. } catch (NullPointerException e) {
  194. // correct
  195. }
  196. try {
  197. testMSChannelnull.read(byteBuf);
  198. fail("Should throw NPE");
  199. } catch (NullPointerException e) {
  200. // correct
  201. }
  202. }
  203. /*
  204. * Test method for 'java.nio.channels.SocketChannel.write(ByteBuffer[])'
  205. */
  206. public void testWriteByteBufferArray() throws IOException {
  207. java.nio.ByteBuffer[] byteBuf = null;
  208. MockSocketChannel testMSChannelnull = new MockSocketChannel(null);
  209. MockSocketChannel testMSChannel = new MockSocketChannel(
  210. SelectorProvider.provider());
  211. try {
  212. this.channel1.write(byteBuf);
  213. fail("Should throw NPE");
  214. } catch (NullPointerException e) {
  215. // correct
  216. }
  217. byteBuf = new java.nio.ByteBuffer[CAPACITY_NORMAL];
  218. try {
  219. this.channel1.write(byteBuf);
  220. fail("Should throw NotYetConnectedException");
  221. } catch (NotYetConnectedException e) {
  222. // correct
  223. }
  224. testMSChannel.write(byteBuf);
  225. testMSChannelnull.write(byteBuf);
  226. }
  227. /*
  228. * Test method for 'java.nio.channels.SocketChannel.write(ByteBuffer[])'
  229. */
  230. public void testWriteByteBufferArray_BufNull() throws IOException {
  231. java.nio.ByteBuffer[] byteBuf = null;
  232. MockSocketChannel testMSChannelnull = new MockSocketChannel(null);
  233. MockSocketChannel testMSChannel = new MockSocketChannel(
  234. SelectorProvider.provider());
  235. try {
  236. this.channel1.write(byteBuf);
  237. fail("Should throw NPE");
  238. } catch (NullPointerException e) {
  239. // correct
  240. }
  241. try {
  242. testMSChannel.write(byteBuf);
  243. fail("Should throw NPE");
  244. } catch (NullPointerException e) {
  245. // correct
  246. }
  247. try {
  248. testMSChannelnull.write(byteBuf);
  249. fail("Should throw NPE");
  250. } catch (NullPointerException e) {
  251. // correct
  252. }
  253. }
  254. public void testSocket_BasicStatusBeforeConnect() throws IOException {
  255. assertFalse(this.channel1.isConnected());// not connected
  256. Socket s1 = this.channel1.socket();
  257. assertSocketBeforeConnect(s1);
  258. Socket s2 = this.channel1.socket();
  259. // same
  260. assertSame(s1, s2);
  261. }
  262. public void testSocket_Block_BasicStatusAfterConnect() throws IOException {
  263. assertFalse(this.channel1.isConnected());// not connected
  264. assertTrue(this.channel1.connect(localAddr1));
  265. assertTrue(this.channel1.isConnected());
  266. Socket s1 = this.channel1.socket();
  267. assertSocketAfterConnect(s1, localAddr1);
  268. Socket s2 = this.channel1.socket();
  269. // same
  270. assertSame(s1, s2);
  271. }
  272. public void testSocket_NonBlock_BasicStatusAfterConnect() throws Exception {
  273. assertFalse(this.channel1.isConnected());// not connected
  274. this.channel1.configureBlocking(false);
  275. boolean connected = channel1.connect(localAddr1);
  276. Socket s1 = null;
  277. Socket s2 = null;
  278. if (!connected) {
  279. assertFalse(this.channel1.isConnected());
  280. assertTrue(this.channel1.isConnectionPending());
  281. s1 = this.channel1.socket();
  282. // status of not connected
  283. assertSocketBeforeConnect(s1);
  284. s2 = this.channel1.socket();
  285. // same
  286. assertSame(s1, s2);
  287. }
  288. if (tryFinish()) {
  289. assertTrue(this.channel1.isConnected());
  290. s1 = this.channel1.socket();
  291. assertSocketAfterConnect(s1, localAddr1);
  292. s2 = this.channel1.socket();
  293. // same
  294. assertSame(s1, s2);
  295. }
  296. }
  297. public void testSocket_Block_ActionsBeforeConnect() throws IOException {
  298. assertFalse(this.channel1.isConnected());// not connected
  299. Socket s = this.channel1.socket();
  300. assertSocketAction_Block_BeforeConnect(s);
  301. }
  302. public void testSocket_Block_ActionsAfterConnect() throws IOException {
  303. assertFalse(this.channel1.isConnected());// not connected
  304. assertTrue(this.channel1.connect(localAddr1));
  305. assertTrue(this.channel1.isConnected());
  306. Socket s = this.channel1.socket();
  307. assertSocketAction_Block_AfterConnect(s);
  308. }
  309. public void testSocket_NonBlock_ActionsAfterConnectBeforeFinish()
  310. throws IOException {
  311. assertFalse(this.channel1.isConnected());// not connected
  312. this.channel1.configureBlocking(false);
  313. boolean connected = channel1.connect(localAddr1);
  314. if (!connected) {
  315. assertFalse(this.channel1.isConnected());
  316. assertTrue(this.channel1.isConnectionPending());
  317. Socket s1 = this.channel1.socket();
  318. // Action of not connected
  319. assertSocketAction_NonBlock_BeforeConnect(s1);
  320. Socket s2 = this.channel1.socket();
  321. // same
  322. assertSame(s1, s2);
  323. }
  324. }
  325. public void testSocket_NonBlock_ActionsAfterConnectAfterFinish()
  326. throws Exception {
  327. assertFalse(this.channel1.isConnected());// not connected
  328. this.channel1.configureBlocking(false);
  329. channel1.connect(localAddr1);
  330. if (tryFinish()) {
  331. Socket s1 = this.channel1.socket();
  332. assertSocketAction_NonBlock_AfterConnect(s1);
  333. Socket s2 = this.channel1.socket();
  334. // same
  335. assertSame(s1, s2);
  336. }
  337. }
  338. public void testSocket_getInetAddress() throws Exception {
  339. Socket socket = channel1.socket();
  340. assertNull(socket.getInetAddress());
  341. channel1.connect(localAddr1);
  342. assertNotNull(socket.getInetAddress());
  343. assertEquals(localAddr1.getAddress(), socket.getInetAddress());
  344. }
  345. public void testSocket_getRemoteSocketAddress() throws Exception {
  346. Socket socket = channel1.socket();
  347. assertNull(socket.getRemoteSocketAddress());
  348. channel1.connect(localAddr1);
  349. assertNotNull(socket.getRemoteSocketAddress());
  350. assertEquals(localAddr1, socket.getRemoteSocketAddress());
  351. }
  352. public void testSocket_getPort() throws Exception {
  353. Socket socket = channel1.socket();
  354. assertEquals(0, socket.getPort());
  355. channel1.connect(localAddr1);
  356. assertEquals(localAddr1.getPort(), socket.getPort());
  357. }
  358. public void testSocket_getLocalAddress() throws Exception {
  359. Socket socket = channel1.socket();
  360. assertNotNull(socket.getLocalAddress());
  361. channel1.connect(localAddr1);
  362. assertNotNull(socket.getLocalAddress());
  363. }
  364. public void testSocket_getLocalSocketAddress() throws Exception {
  365. Socket socket = channel1.socket();
  366. assertNull(socket.getLocalSocketAddress());
  367. channel1.connect(localAddr1);
  368. assertNotNull(socket.getLocalSocketAddress());
  369. }
  370. public void testSocket_getLocalPort() throws Exception {
  371. Socket socket = channel1.socket();
  372. assertEquals(-1, socket.getLocalPort());
  373. channel1.connect(localAddr1);
  374. assertTrue(-1 != socket.getLocalPort());
  375. assertTrue(0 != socket.getLocalPort());
  376. }
  377. public void testSocket_bind() throws Exception {
  378. Socket socket = channel1.socket();
  379. socket.bind(new InetSocketAddress("127.0.0.1", 0));
  380. assertEquals("127.0.0.1", socket.getLocalAddress().getHostAddress());
  381. assertTrue(socket.getLocalPort() != -1);
  382. }
  383. private void assertSocketBeforeConnect(Socket s) throws IOException {
  384. assertFalse(s.isBound());
  385. assertFalse(s.isClosed());
  386. assertFalse(s.isConnected());
  387. assertFalse(s.getKeepAlive());
  388. try {
  389. s.getInputStream();
  390. fail("Should throw SocketException.");
  391. } catch (SocketException e) {
  392. // OK.
  393. }
  394. assertFalse(s.getOOBInline());
  395. try {
  396. s.getOutputStream();
  397. fail("Should throw SocketException.");
  398. } catch (SocketException e) {
  399. // OK.
  400. }
  401. assertEquals(-1, s.getSoLinger());
  402. assertFalse(s.getTcpNoDelay());
  403. assertFalse(s.isInputShutdown());
  404. assertFalse(s.isOutputShutdown());
  405. assertNull(s.getInetAddress());
  406. assertEquals(s.getLocalAddress().getHostAddress(), "0.0.0.0");
  407. // RI fails here. RI returns 0 while spec says unbound socket should
  408. // return -1.
  409. assertEquals(-1, s.getLocalPort());
  410. assertFalse(s.getReuseAddress());
  411. assertNull(s.getLocalSocketAddress());
  412. // not connected
  413. assertEquals(0, s.getPort());
  414. assertTrue(s.getReceiveBufferSize() >= 8192);
  415. assertNull(s.getRemoteSocketAddress());
  416. assertTrue(s.getSendBufferSize() >= 8192);
  417. assertEquals(0, s.getSoTimeout());
  418. assertEquals(0, s.getTrafficClass());
  419. }
  420. private void assertSocketAfterConnect(Socket s, InetSocketAddress address)
  421. throws IOException {
  422. assertTrue(s.isBound());
  423. assertFalse(s.isClosed());
  424. assertTrue(s.isConnected());
  425. assertFalse(s.getKeepAlive());
  426. assertNotNull(s.getInputStream());
  427. assertNotNull(s.getOutputStream());
  428. assertFalse(s.getOOBInline());
  429. assertEquals(-1, s.getSoLinger());
  430. assertFalse(s.getTcpNoDelay());
  431. assertFalse(s.isInputShutdown());
  432. assertFalse(s.isOutputShutdown());
  433. assertSame(s.getInetAddress(), address.getAddress());
  434. assertEquals(s.getLocalAddress(), this.localAddr1.getAddress());
  435. assertEquals(s.getPort(), address.getPort());
  436. assertNotNull(s.getLocalSocketAddress());
  437. assertTrue(s.getReceiveBufferSize() >= 8192);
  438. assertEquals(s.getRemoteSocketAddress(), (SocketAddress) address);
  439. // assertFalse(s.getReuseAddress());
  440. assertTrue(s.getSendBufferSize() >= 8192);
  441. assertEquals(0, s.getSoTimeout());
  442. assertEquals(0, s.getTrafficClass());
  443. }
  444. private void assertSocketAction_Block_BeforeConnect(Socket s)
  445. throws IOException {
  446. assertFalse(this.channel1.isConnected());
  447. this.server2 = new ServerSocket(localAddr2.getPort());
  448. s.connect(localAddr2);
  449. assertTrue(this.channel1.isConnected());
  450. assertTrue(s.isConnected());
  451. assertSocketAfterConnect(s, localAddr2);
  452. try {
  453. s.bind(localAddr2);
  454. fail("Should throw AlreadyConnectedException");
  455. } catch (AlreadyConnectedException e) {
  456. // OK.
  457. }
  458. s.close();
  459. assertTrue(s.isClosed());
  460. assertFalse(this.channel1.isOpen());
  461. }
  462. private void assertSocketAction_NonBlock_BeforeConnect(Socket s)
  463. throws IOException {
  464. assertFalse(this.channel1.isConnected());
  465. this.server2 = new ServerSocket(localAddr2.getPort());
  466. try {
  467. s.connect(localAddr2);
  468. fail("Should throw IllegalBlockingModeException");
  469. } catch (IllegalBlockingModeException e1) {
  470. // OK.
  471. }
  472. if (this.channel1.isConnectionPending()) {
  473. try {
  474. s.bind(localAddr2);
  475. fail("Should throw ConnectionPendingException");
  476. } catch (ConnectionPendingException e1) {
  477. // OK.
  478. }
  479. } else {
  480. try {
  481. s.bind(localAddr2);
  482. fail("Should throw BindException");
  483. } catch (BindException e1) {
  484. // OK.
  485. }
  486. }
  487. assertFalse(this.channel1.isConnected());
  488. assertFalse(s.isConnected());
  489. s.close();
  490. assertTrue(s.isClosed());
  491. assertFalse(this.channel1.isOpen());
  492. }
  493. private void assertSocketAction_Block_AfterConnect(Socket s)
  494. throws IOException {
  495. assertEquals(s.getPort(), localAddr1.getPort());
  496. assertTrue(this.channel1.isConnected());
  497. assertTrue(s.isConnected());
  498. try {
  499. s.connect(localAddr2);
  500. fail("Should throw AlreadyConnectedException");
  501. } catch (AlreadyConnectedException e) {
  502. // OK.
  503. }
  504. try {
  505. s.bind(localAddr2);
  506. fail("Should throw AlreadyConnectedException");
  507. } catch (AlreadyConnectedException e) {
  508. // OK.
  509. }
  510. s.close();
  511. assertTrue(s.isClosed());
  512. assertFalse(this.channel1.isOpen());
  513. }
  514. private void assertSocketAction_NonBlock_AfterConnect(Socket s)
  515. throws IOException {
  516. assertEquals(s.getPort(), localAddr1.getPort());
  517. assertTrue(this.channel1.isConnected());
  518. assertTrue(s.isConnected());
  519. if (this.channel1.isConnectionPending()) {
  520. try {
  521. s.connect(localAddr2);
  522. fail("Should throw AlreadyConnectedException");
  523. } catch (AlreadyConnectedException e) {
  524. // OK.
  525. }
  526. } else {
  527. try {
  528. s.connect(localAddr2);
  529. fail("Should throw IllegalBlockingModeException");
  530. } catch (IllegalBlockingModeException e) {
  531. // OK.
  532. }
  533. }
  534. try {
  535. s.bind(localAddr2);
  536. fail("Should throw AlreadyConnectedException");
  537. } catch (AlreadyConnectedException e) {
  538. // OK.
  539. }
  540. s.close();
  541. assertTrue(s.isClosed());
  542. assertFalse(this.channel1.isOpen());
  543. }
  544. // -------------------------------------------------------------------
  545. // Tests for connect(), finishConnect(),isConnected(),isConnectionPending()
  546. // These methods are very close, so we test them together, call them "CFII".
  547. // -------------------------------------------------------------------
  548. /**
  549. * connect-->finish-->close
  550. */
  551. public void testCFII_Norml_NoServer_Block() throws Exception {
  552. // ensure
  553. ensureServerClosed();
  554. assertTrue(this.channel1.isBlocking());
  555. statusNotConnected_NotPending();
  556. // connect
  557. try {
  558. this.channel1.connect(localAddr1);
  559. fail("Should throw a ConnectException here.");
  560. } catch (ConnectException e) {
  561. // OK.
  562. }
  563. statusChannelClosed();
  564. try {
  565. this.channel1.finishConnect();
  566. fail("Should throw a ClosedChannelException here.");
  567. } catch (ClosedChannelException e) {
  568. // OK.
  569. }
  570. }
  571. /**
  572. * connect-->finish-->close
  573. */
  574. public void testCFII_Norml_NoServer_NonBlock() throws Exception {
  575. connectNoServerNonBlock();
  576. this.channel1.close();
  577. statusChannelClosed();
  578. }
  579. /**
  580. * connect-->finish-->close
  581. */
  582. public void testCFII_Norml_Server_Block() throws Exception {
  583. connectServerBlock();
  584. this.channel1.close();
  585. statusChannelClosed();
  586. }
  587. /**
  588. * connect-->finish-->close
  589. */
  590. public void testCFII_Norml_Server_NonBlock() throws Exception {
  591. connectServerNonBlock();
  592. this.channel1.close();
  593. statusChannelClosed();
  594. }
  595. /**
  596. * connect-->server closed-->finish-->close
  597. */
  598. public void testCFII_ServerClosed_Block() throws Exception {
  599. // ensure
  600. ensureServerOpen();
  601. assertTrue(this.channel1.isBlocking());
  602. statusNotConnected_NotPending();
  603. // connect
  604. assertTrue(this.channel1.connect(localAddr1));
  605. statusConnected_NotPending();
  606. ensureServerClosed();
  607. tryFinish();
  608. this.channel1.close();
  609. statusChannelClosed();
  610. }
  611. /**
  612. * connect-->server closed-->finish-->close
  613. */
  614. public void testCFII_ServerClosed_NonBlock() throws Exception {
  615. // ensure
  616. ensureServerOpen();
  617. this.channel1.configureBlocking(false);
  618. statusNotConnected_NotPending();
  619. // connect
  620. boolean connected = channel1.connect(localAddr1);
  621. if (!connected) {
  622. statusNotConnected_Pending();
  623. }
  624. ensureServerClosed();
  625. tryFinish();
  626. this.channel1.close();
  627. statusChannelClosed();
  628. }
  629. /**
  630. * connect-->finish-->server closed-->close
  631. */
  632. public void testCFII_ServerClosedAfterFinish_Block() throws Exception {
  633. connectServerBlock();
  634. ensureServerClosed();
  635. assertTrue(this.channel1.isOpen());
  636. this.channel1.close();
  637. statusChannelClosed();
  638. }
  639. /**
  640. * connect-->finish-->server closed-->close
  641. */
  642. public void testCFII_ServerClosedAfterFinish_NonBlock() throws Exception {
  643. connectServerNonBlock();
  644. ensureServerClosed();
  645. assertTrue(this.channel1.isOpen());
  646. this.channel1.close();
  647. statusChannelClosed();
  648. }
  649. /**
  650. * no server-->connect-->server open-->finish-->close
  651. */
  652. public void testCFII_ServerStartLater_Block() throws Exception {
  653. // ensure
  654. ensureServerClosed();
  655. assertTrue(this.channel1.isBlocking());
  656. statusNotConnected_NotPending();
  657. // connect
  658. try {
  659. this.channel1.connect(localAddr1);
  660. fail("Should throw a ConnectException here.");
  661. } catch (ConnectException e) {
  662. // OK.
  663. }
  664. statusChannelClosed();
  665. ensureServerOpen();
  666. try {
  667. this.channel1.finishConnect();
  668. fail("Should throw a ClosedChannelException here.");
  669. } catch (ClosedChannelException e) {
  670. // OK.
  671. }
  672. }
  673. /**
  674. * no server-->connect-->server open-->finish-->close
  675. */
  676. public void testCFII_ServerStartLater_NonBlock() throws Exception {
  677. // ensure
  678. ensureServerClosed();
  679. this.channel1.configureBlocking(false);
  680. statusNotConnected_NotPending();
  681. // connect
  682. assertFalse(this.channel1.connect(localAddr1));
  683. statusNotConnected_Pending();
  684. ensureServerOpen();
  685. try {
  686. assertFalse(this.channel1.finishConnect());
  687. statusNotConnected_Pending();
  688. this.channel1.close();
  689. } catch (ConnectException e) {
  690. // FIXME: assertEquals(e.getMessage(), "Connection refused");
  691. }
  692. }
  693. /**
  694. * connect-->finish-->finish-->close
  695. */
  696. public void testCFII_FinishTwice_NoServer_NonBlock() throws Exception {
  697. // ensure
  698. ensureServerClosed();
  699. this.channel1.configureBlocking(false);
  700. statusNotConnected_NotPending();
  701. // connect
  702. assertFalse(this.channel1.connect(localAddr1));
  703. statusNotConnected_Pending();
  704. try {
  705. assertFalse(this.channel1.finishConnect());
  706. statusNotConnected_Pending();
  707. assertFalse(this.channel1.finishConnect());
  708. statusNotConnected_Pending();
  709. this.channel1.close();
  710. } catch (ConnectException e) {
  711. // FIXME: assertEquals(e.getMessage(), "Connection refused");
  712. }
  713. statusChannelClosed();
  714. }
  715. /**
  716. * connect-->finish-->finish-->close
  717. */
  718. public void testCFII_FinishTwice_Server_Block() throws Exception {
  719. connectServerBlock();
  720. tryFinish();
  721. this.channel1.close();
  722. statusChannelClosed();
  723. }
  724. /**
  725. * connect-->finish-->finish-->close
  726. */
  727. public void testCFII_FinishTwice_Server_NonBlock() throws Exception {
  728. connectServerNonBlock();
  729. tryFinish();
  730. this.channel1.close();
  731. statusChannelClosed();
  732. }
  733. /**
  734. * connect-->finish-->connect-->close
  735. */
  736. public void testCFII_ConnectAfterFinish_NoServer_Block() throws Exception {
  737. // ensure
  738. ensureServerClosed();
  739. assertTrue(this.channel1.isBlocking());
  740. statusNotConnected_NotPending();
  741. // connect
  742. try {
  743. this.channel1.connect(localAddr1);
  744. fail("Should throw a ConnectException here.");
  745. } catch (ConnectException e) {
  746. // OK.
  747. }
  748. statusChannelClosed();
  749. try {
  750. this.channel1.finishConnect();
  751. fail("Should throw a ClosedChannelException here.");
  752. } catch (ClosedChannelException e) {
  753. // OK.
  754. }
  755. statusChannelClosed();
  756. try {
  757. this.channel1.connect(localAddr1);
  758. fail("Should throw a ClosedChannelException here.");
  759. } catch (ClosedChannelException e) {
  760. // OK.
  761. }
  762. statusChannelClosed();
  763. }
  764. /**
  765. * connect-->finish-->connect-->close
  766. */
  767. public void testCFII_ConnectAfterFinish_NoServer_NonBlock()
  768. throws Exception {
  769. // ensure
  770. ensureServerClosed();
  771. this.channel1.configureBlocking(false);
  772. statusNotConnected_NotPending();
  773. // connect
  774. assertFalse(this.channel1.connect(localAddr1));
  775. statusNotConnected_Pending();
  776. try {
  777. assertFalse(this.channel1.finishConnect());
  778. statusNotConnected_Pending();
  779. } catch (ConnectException e) {
  780. // FIXME: assertEquals(e.getMessage(), "Connection refused");
  781. }
  782. if (this.channel1.isOpen()) {
  783. try {
  784. this.channel1.connect(localAddr1);
  785. fail("Should throw a ConnectionPendingException here.");
  786. } catch (ConnectionPendingException e) {
  787. // OK.
  788. }
  789. statusNotConnected_Pending();
  790. // connect another addr
  791. try {
  792. this.channel1.connect(localAddr2);
  793. fail("Should throw a ConnectionPendingException here.");
  794. } catch (ConnectionPendingException e) {
  795. // OK.
  796. }
  797. statusNotConnected_Pending();
  798. // connect if server closed
  799. ensureServerClosed();
  800. try {
  801. this.channel1.connect(localAddr1);
  802. fail("Should throw a ConnectionPendingException here.");
  803. } catch (ConnectionPendingException e) {
  804. // OK.
  805. }
  806. statusNotConnected_Pending();
  807. this.channel1.close();
  808. }
  809. statusChannelClosed();
  810. }
  811. /**
  812. * connect-->finish-->connect-->close
  813. */
  814. public void testCFII_ConnectAfterFinish_Server_Block() throws Exception {
  815. connectServerBlock();
  816. if (!this.channel1.isConnected()) {
  817. System.err
  818. .println("Connection fail, testCFII_ConnectAfterFinish_Server_Block is not finished.");
  819. return;
  820. }
  821. try {
  822. this.channel1.connect(localAddr1);
  823. fail("Should throw an AlreadyConnectedException here.");
  824. } catch (AlreadyConnectedException e) {
  825. // OK.
  826. }
  827. statusConnected_NotPending();
  828. // connect another addr
  829. try {
  830. this.channel1.connect(localAddr2);
  831. fail("Should throw an AlreadyConnectedException here.");
  832. } catch (AlreadyConnectedException e) {
  833. // OK.
  834. }
  835. statusConnected_NotPending();
  836. // connect if server closed
  837. ensureServerClosed();
  838. try {
  839. this.channel1.connect(localAddr1);
  840. fail("Should throw an AlreadyConnectedException here.");
  841. } catch (AlreadyConnectedException e) {
  842. // OK.
  843. }
  844. statusConnected_NotPending();
  845. this.channel1.close();
  846. statusChannelClosed();
  847. }
  848. /**
  849. * connect-->finish-->connect-->close
  850. */
  851. public void testCFII_ConnectAfterFinish_Server_NonBlock() throws Exception {
  852. connectServerNonBlock();
  853. if (!this.channel1.isConnected()) {
  854. System.err
  855. .println("Connection fail, testCFII_ConnectAfterFinish_Server_Block is not finished.");
  856. return;
  857. }
  858. try {
  859. this.channel1.connect(localAddr1);
  860. fail("Should throw an AlreadyConnectedException or a ConnectionPendingException here.");
  861. } catch (AlreadyConnectedException e) {
  862. // OK.
  863. }
  864. statusConnected_NotPending();
  865. // connect another addr
  866. try {
  867. this.channel1.connect(localAddr2);
  868. fail("Should throw an AlreadyConnectedException here.");
  869. } catch (AlreadyConnectedException e) {
  870. // OK.
  871. }
  872. statusConnected_NotPending();
  873. // connect if server closed
  874. ensureServerClosed();
  875. try {
  876. this.channel1.connect(localAddr1);
  877. fail("Should throw an AlreadyConnectedException here.");
  878. } catch (AlreadyConnectedException e) {
  879. // OK.
  880. }
  881. statusConnected_NotPending();
  882. this.channel1.close();
  883. statusChannelClosed();
  884. }
  885. /**
  886. * connect-->connect-->finish-->close
  887. */
  888. public void testCFII_ConnectTwice_NoServer_NonBlock() throws Exception {
  889. // ensure
  890. ensureServerClosed();
  891. this.channel1.configureBlocking(false);
  892. statusNotConnected_NotPending();
  893. // connect
  894. assertFalse(this.channel1.connect(localAddr1));
  895. statusNotConnected_Pending();
  896. try {
  897. this.channel1.connect(localAddr1);
  898. fail("Should throw a ConnectionPendingException here.");
  899. } catch (ConnectionPendingException e) {
  900. // OK.
  901. }
  902. statusNotConnected_Pending();
  903. // connect another addr
  904. try {
  905. this.channel1.connect(localAddr2);
  906. fail("Should throw a ConnectionPendingException here.");
  907. } catch (ConnectionPendingException e) {
  908. // OK.
  909. }
  910. statusNotConnected_Pending();
  911. // connect if server closed
  912. ensureServerClosed();
  913. try {
  914. this.channel1.connect(localAddr1);
  915. fail("Should throw a ConnectionPendingException here.");
  916. } catch (ConnectionPendingException e) {
  917. // OK.
  918. }
  919. statusNotConnected_Pending();
  920. try {
  921. assertFalse(this.channel1.finishConnect());
  922. statusNotConnected_Pending();
  923. this.channel1.close();
  924. } catch (ConnectException e) {
  925. // FIXME: assertEquals(e.getMessage(), "Connection refused");
  926. }
  927. statusChannelClosed();
  928. }
  929. /**
  930. * connect-->connect-->finish-->close
  931. */
  932. public void testCFII_ConnectTwice_Server_Block() throws Exception {
  933. // ensure
  934. ensureServerOpen();
  935. assertTrue(this.channel1.isBlocking());
  936. statusNotConnected_NotPending();
  937. // connect
  938. assertTrue(this.channel1.connect(localAddr1));
  939. statusConnected_NotPending();
  940. try {
  941. this.channel1.connect(localAddr1);
  942. fail("Should throw an AlreadyConnectedException here.");
  943. } catch (AlreadyConnectedException e) {
  944. // OK.
  945. }
  946. statusConnected_NotPending();
  947. // connect another addr
  948. try {
  949. this.channel1.connect(localAddr2);
  950. fail("Should throw an AlreadyConnectedException here.");
  951. } catch (AlreadyConnectedException e) {
  952. // OK.
  953. }
  954. statusConnected_NotPending();
  955. // connect if server closed
  956. ensureServerClosed();
  957. try {
  958. this.channel1.connect(localAddr1);
  959. fail("Should throw an AlreadyConnectedException here.");
  960. } catch (AlreadyConnectedException e) {
  961. // OK.
  962. }
  963. statusConnected_NotPending();
  964. tryFinish();
  965. this.channel1.close();
  966. statusChannelClosed();
  967. }
  968. /**
  969. * connect-->connect-->finish-->close
  970. */
  971. public void testCFII_ConnectTwice_Server_NonBlock() throws Exception {
  972. // ensure
  973. ensureServerOpen();
  974. this.channel1.configureBlocking(false);
  975. statusNotConnected_NotPending();
  976. // connect
  977. boolean connected = channel1.connect(localAddr1);
  978. if (!connected) {
  979. statusNotConnected_Pending();
  980. try {
  981. this.channel1.connect(localAddr1);
  982. fail("Should throw a ConnectionPendingException here.");
  983. } catch (ConnectionPendingException e) {
  984. // OK.
  985. }
  986. statusNotConnected_Pending();
  987. // connect another addr
  988. try {
  989. this.channel1.connect(localAddr2);
  990. fail("Should throw a ConnectionPendingException here.");
  991. } catch (ConnectionPendingException e) {
  992. // OK.
  993. }
  994. statusNotConnected_Pending();
  995. // connect if server closed
  996. ensureServerClosed();
  997. try {
  998. this.channel1.connect(localAddr1);
  999. fail("Should throw a ConnectionPendingException here.");
  1000. } catch (ConnectionPendingException e) {
  1001. // OK.
  1002. }
  1003. statusNotConnected_Pending();
  1004. }
  1005. tryFinish();
  1006. this.channel1.close();
  1007. statusChannelClosed();
  1008. }
  1009. /**
  1010. * finish-->connect-->finish-->close
  1011. */
  1012. public void testCFII_FinishFirst_NoServer_Block() throws Exception {
  1013. // ensure
  1014. ensureServerClosed();
  1015. assertTrue(this.channel1.isBlocking());
  1016. statusNotConnected_NotPending();
  1017. // finish
  1018. try {
  1019. this.channel1.finishConnect();
  1020. fail("Should throw NoConnectionPendingException");
  1021. } catch (NoConnectionPendingException e) {
  1022. // OK.
  1023. }
  1024. statusNotConnected_NotPending();
  1025. // connect
  1026. try {
  1027. this.channel1.connect(localAddr1);
  1028. fail("Should throw a ConnectException here.");
  1029. } catch (ConnectException e) {
  1030. // OK.
  1031. }
  1032. statusChannelClosed();
  1033. try {
  1034. this.channel1.finishConnect();
  1035. fail("Should throw a ClosedChannelException here.");
  1036. } catch (ClosedChannelException e) {
  1037. // OK.
  1038. }
  1039. statusChannelClosed();
  1040. }
  1041. /**
  1042. * finish-->connect-->finish-->close
  1043. */
  1044. public void testCFII_FinishFirst_NoServer_NonBlock() throws Exception {
  1045. // ensure
  1046. ensureServerClosed();
  1047. this.channel1.configureBlocking(false);
  1048. statusNotConnected_NotPending();
  1049. // finish
  1050. try {
  1051. this.channel1.finishConnect();
  1052. fail("Should throw NoConnectionPendingException");
  1053. } catch (NoConnectionPendingException e) {
  1054. // OK.
  1055. }
  1056. statusNotConnected_NotPending();
  1057. // connect
  1058. assertFalse(this.channel1.connect(localAddr1));
  1059. statusNotConnected_Pending();
  1060. try {
  1061. assertFalse(this.channel1.finishConnect());
  1062. statusNotConnected_Pending();
  1063. this.channel1.close();
  1064. } catch (ConnectException e) {
  1065. // FIXME: assertEquals(e.getMessage(), "Connection refused");
  1066. }
  1067. statusChannelClosed();
  1068. }
  1069. /**
  1070. * finish-->connect-->finish-->close
  1071. */
  1072. public void testCFII_FinishFirst_Server_Block() throws Exception {
  1073. // ensure
  1074. ensureServerOpen();
  1075. assertTrue(this.channel1.isBlocking());
  1076. statusNotConnected_NotPending();
  1077. // finish
  1078. try {
  1079. this.channel1.finishConnect();
  1080. fail("Should throw NoConnectionPendingException");
  1081. } catch (NoConnectionPendingException e) {
  1082. // OK.
  1083. }
  1084. statusNotConnected_NotPending();
  1085. // connect
  1086. assertTrue(this.channel1.connect(localAddr1));
  1087. statusConnected_NotPending();
  1088. tryFinish();
  1089. this.channel1.close();
  1090. statusChannelClosed();
  1091. }
  1092. /**
  1093. * finish-->connect-->finish-->close
  1094. */
  1095. public void testCFII_FinishFirst_Server_NonBlock() throws Exception {
  1096. // ensure
  1097. ensureServerOpen();
  1098. this.channel1.configureBlocking(false);
  1099. statusNotConnected_NotPending();
  1100. // finish
  1101. try {
  1102. this.channel1.finishConnect();
  1103. fail("Should throw NoConnectionPendingException");
  1104. } catch (NoConnectionPendingException e) {
  1105. // OK.
  1106. }
  1107. statusNotConnected_NotPending();
  1108. // connect
  1109. boolean connected = channel1.connect(localAddr1);
  1110. if (!connected) {
  1111. statusNotConnected_Pending();
  1112. }
  1113. tryFinish();
  1114. this.channel1.close();
  1115. statusChannelClosed();
  1116. }
  1117. public void testCFII_Null() throws Exception {
  1118. statusNotConnected_NotPending();
  1119. try {
  1120. this.channel1.connect(null);
  1121. fail("Should throw an IllegalArgumentException here.");
  1122. } catch (IllegalArgumentException e) {
  1123. // OK.
  1124. }
  1125. }
  1126. public void testCFII_UnsupportedType() throws Exception {
  1127. class SubSocketAddress extends SocketAddress {
  1128. private static final long serialVersionUID = 1L;
  1129. //empty
  1130. public SubSocketAddress() {
  1131. super();
  1132. }
  1133. }
  1134. statusNotConnected_NotPending();
  1135. SocketAddress newTypeAddress = new SubSocketAddress();
  1136. try {
  1137. this.channel1.connect(newTypeAddress);
  1138. fail("Should throw an UnsupportedAddressTypeException here.");
  1139. } catch (UnsupportedAddressTypeException e) {
  1140. // OK.
  1141. }
  1142. }
  1143. public void testCFII_Unresolved() throws IOException {
  1144. statusNotConnected_NotPending();
  1145. InetSocketAddress unresolved = new InetSocketAddress(
  1146. "unresolved address", 1080);
  1147. try {
  1148. this.channel1.connect(unresolved);
  1149. fail("Should throw an UnresolvedAddressException here.");
  1150. } catch (UnresolvedAddressException e) {
  1151. // OK.
  1152. }
  1153. }
  1154. public void testCFII_EmptyHost() throws Exception {
  1155. statusNotConnected_NotPending();
  1156. ServerSocket server = new ServerSocket(0);
  1157. int port = server.getLocalPort();
  1158. server.close();
  1159. try {
  1160. this.channel1.connect(new InetSocketAddress("", port));
  1161. fail("Should throw ConnectException");
  1162. } catch (ConnectException e) {
  1163. // correct
  1164. }
  1165. }
  1166. public void testCFII_CloseFirst() throws Exception {
  1167. this.channel1.close();
  1168. statusChannelClosed();
  1169. ensureServerOpen();
  1170. try {
  1171. this.channel1.connect(localAddr1);
  1172. fail("Should throw ClosedChannelException.");
  1173. } catch (ClosedChannelException e) {
  1174. // OK.
  1175. }
  1176. statusChannelClosed();
  1177. try {
  1178. this.channel1.finishConnect();
  1179. fail("Should throw ClosedChannelException.");
  1180. } catch (ClosedChannelException e) {
  1181. // OK.
  1182. }
  1183. statusChannelClosed();
  1184. try {
  1185. this.channel1.configureBlocking(false);
  1186. fail("Should throw ClosedChannelException.");
  1187. } catch (ClosedChannelException e) {
  1188. // OK.
  1189. }
  1190. statusChannelClosed();
  1191. }
  1192. public void testCFII_StatusAfterFinish() throws Exception {
  1193. // 1. close server, finish must return false, check the status
  1194. ensureServerClosed();
  1195. // 1.1 block mode
  1196. assertTrue(this.channel1.isBlocking());
  1197. try {
  1198. channel1.connect(localAddr1);
  1199. fail("Should throw ConnectException");
  1200. } catch (ConnectException e) {
  1201. // OK.
  1202. }
  1203. assertFalse(this.channel1.isOpen());
  1204. assertFalse(this.channel1.isOpen());
  1205. assertTrue(this.channel1.isBlocking());
  1206. assertFalse(this.channel1.isConnectionPending());
  1207. // 1.2 non block mode
  1208. this.channel1 = SocketChannel.open();
  1209. this.channel1.configureBlocking(false);
  1210. assertFalse(this.channel1.connect(localAddr1));
  1211. try {
  1212. assertFalse(this.channel1.finishConnect());
  1213. statusNotConnected_Pending();
  1214. this.channel1.close();
  1215. } catch (ConnectException e) {
  1216. System.out.println(e.getMessage());
  1217. }
  1218. // 2. start server, finish usually return true, check the status
  1219. ensureServerOpen();
  1220. // 2.1 block mode
  1221. this.channel1 = SocketChannel.open();
  1222. assertTrue(this.channel1.isBlocking());
  1223. assertTrue(this.channel1.connect(localAddr1));
  1224. assertTrue(this.channel1.finishConnect());
  1225. statusConnected_NotPending();
  1226. this.channel1.close();
  1227. // 2.2 non block mode
  1228. this.channel1 = SocketChannel.open();
  1229. this.channel1.configureBlocking(false);
  1230. assertFalse(this.channel1.connect(localAddr1));
  1231. tryFinish();
  1232. this.channel1.close();
  1233. }
  1234. private void ensureServerClosed() throws IOException {
  1235. if (null != this.server1) {
  1236. this.server1.close();
  1237. assertTrue(this.server1.isClosed());
  1238. }
  1239. if (null != this.server2) {
  1240. this.server2.close();
  1241. assertTrue(this.server2.isClosed());
  1242. }
  1243. }
  1244. private void ensureServerOpen() throws IOException {
  1245. ensureServerClosed();
  1246. this.server1 = new ServerSocket(localAddr1.getPort());
  1247. this.server2 = new ServerSocket(localAddr2.getPort());
  1248. assertTrue(this.server1.isBound());
  1249. assertTrue(this.server2.isBound());
  1250. }
  1251. private void connectNoServerNonBlock() throws Exception {
  1252. // ensure
  1253. ensureServerClosed();
  1254. this.channel1.configureBlocking(false);
  1255. statusNotConnected_NotPending();
  1256. // connect
  1257. assertFalse(this.channel1.connect(localAddr1));
  1258. statusNotConnected_Pending();
  1259. try {
  1260. assertFalse(this.channel1.finishConnect());
  1261. statusNotConnected_Pending();
  1262. } catch (ConnectException e) {
  1263. // FIXME: assertEquals(e.getMessage(), "Connection refused");
  1264. }
  1265. }
  1266. private void connectServerNonBlock() throws Exception {
  1267. // ensure
  1268. ensureServerOpen();
  1269. this.channel1.configureBlocking(false);
  1270. statusNotConnected_NotPending();
  1271. // connect
  1272. boolean connected = channel1.connect(localAddr1);
  1273. if (!connected) {
  1274. statusNotConnected_Pending();
  1275. }
  1276. tryFinish();
  1277. }
  1278. private void connectServerBlock() throws Exception {
  1279. // ensure
  1280. ensureServerOpen();
  1281. assertTrue(this.channel1.isBlocking());
  1282. statusNotConnected_NotPending();
  1283. // connect
  1284. assertTrue(this.channel1.connect(localAddr1));
  1285. statusConnected_NotPending();
  1286. tryFinish();
  1287. }
  1288. private void statusChannelClosed() {
  1289. assertFalse(this.channel1.isConnected());
  1290. assertFalse(this.channel1.isConnectionPending());
  1291. assertFalse(this.channel1.isOpen());
  1292. }
  1293. private void statusNotConnected_NotPending() {
  1294. assertFalse(this.channel1.isConnected());
  1295. assertFalse(this.channel1.isConnectionPending());
  1296. assertTrue(this.channel1.isOpen());
  1297. }
  1298. private void statusNotConnected_Pending() {
  1299. assertFalse(this.channel1.isConnected());
  1300. assertTrue(this.channel1.isConnectionPending());
  1301. assertTrue(this.channel1.isOpen());
  1302. }
  1303. private void statusConnected_NotPending() {
  1304. assertTrue(this.channel1.isConnected());
  1305. assertFalse(this.channel1.isConnectionPending());
  1306. assertTrue(this.channel1.isOpen());
  1307. }
  1308. private boolean tryFinish() throws IOException {
  1309. /*
  1310. * the result of finish will be asserted in multi-thread tests.
  1311. */
  1312. boolean connected = false;
  1313. assertTrue(this.channel1.isOpen());
  1314. try {
  1315. connected = this.channel1.finishConnect();
  1316. } catch (SocketException e) {
  1317. // Finish connection failed, probably due to reset by peer error.
  1318. }
  1319. if (connected) {
  1320. statusConnected_NotPending();
  1321. }
  1322. return connected;
  1323. }
  1324. // -------------------------------------------------------------------
  1325. // Original tests. Test method for CFII with real data.
  1326. // -------------------------------------------------------------------
  1327. /**
  1328. *
  1329. * 'SocketChannelImpl.connect(SocketAddress)'
  1330. */
  1331. public void testCFII_Data_ConnectWithServer() throws Exception {
  1332. ensureServerOpen();
  1333. java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer
  1334. .allocate(CAPACITY_NORMAL);
  1335. java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
  1336. writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
  1337. assertFalse(this.channel1.isRegistered());
  1338. assertTrue(this.channel1.isBlocking());
  1339. this.channel1.connect(localAddr1);
  1340. assertTrue(this.channel1.isBlocking());
  1341. assertTrue(this.channel1.isConnected());
  1342. assertFalse(this.channel1.isConnectionPending());
  1343. assertTrue(this.channel1.isOpen());
  1344. assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
  1345. assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBufArr, 0, 1));
  1346. this.channel1.configureBlocking(false);
  1347. try {
  1348. this.channel1.connect(localAddr1);
  1349. fail("Should throw AlreadyConnectedException");
  1350. } catch (AlreadyConnectedException e) {
  1351. // correct
  1352. }
  1353. assertFalse(this.channel1.isRegistered());
  1354. tryFinish();
  1355. }
  1356. /*
  1357. * Test method for 'SocketChannelImpl.connect(SocketAddress)'
  1358. */
  1359. public void testCFII_Data_ConnectWithServer_nonBlocking() throws Exception {
  1360. ensureServerOpen();
  1361. java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer
  1362. .allocate(CAPACITY_NORMAL);
  1363. java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
  1364. writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
  1365. assertFalse(this.channel1.isRegistered());
  1366. assertTrue(this.channel1.isBlocking());
  1367. this.channel1.configureBlocking(false);
  1368. this.channel1.connect(localAddr1);
  1369. assertFalse(this.channel1.isBlocking());
  1370. boolean connected = channel1.isConnected();
  1371. if (!connected) {
  1372. assertTrue(this.channel1.isConnectionPending());
  1373. assertTrue(this.channel1.isOpen());
  1374. }
  1375. if (tryFinish()) {
  1376. assertEqual