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

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 1457 lines · 963 code · 140 blank · 354 comment · 16 complexity · da59aed10c3c9a0d2bb6e5157cc80a61 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.net.DatagramPacket;
  20. import java.net.DatagramSocket;
  21. import java.net.InetAddress;
  22. import java.net.InetSocketAddress;
  23. import java.net.SocketAddress;
  24. import java.net.SocketException;
  25. import java.nio.ByteBuffer;
  26. import java.nio.channels.AsynchronousCloseException;
  27. import java.nio.channels.ClosedChannelException;
  28. import java.nio.channels.DatagramChannel;
  29. import java.nio.channels.IllegalBlockingModeException;
  30. import java.nio.channels.NotYetConnectedException;
  31. import java.nio.channels.UnresolvedAddressException;
  32. import java.nio.channels.UnsupportedAddressTypeException;
  33. import java.nio.channels.spi.SelectorProvider;
  34. import java.security.Permission;
  35. import junit.framework.TestCase;
  36. import tests.support.Support_PortManager;
  37. /**
  38. * Test for DatagramChannel
  39. *
  40. */
  41. public class DatagramChannelTest extends TestCase {
  42. private static final int CAPACITY_NORMAL = 200;
  43. private static final int CAPACITY_1KB = 1024;
  44. private static final int CAPACITY_64KB = 65536;
  45. private static final int CAPACITY_ZERO = 0;
  46. private static final int CAPACITY_ONE = 1;
  47. private static final int TIME_UNIT = 500;
  48. private InetSocketAddress localAddr1;
  49. private InetSocketAddress localAddr2;
  50. private DatagramChannel channel1;
  51. private DatagramChannel channel2;
  52. private DatagramSocket datagramSocket1;
  53. private DatagramSocket datagramSocket2;
  54. // The port to be used in test cases.
  55. private int testPort;
  56. protected void setUp() throws Exception {
  57. super.setUp();
  58. this.channel1 = DatagramChannel.open();
  59. this.channel2 = DatagramChannel.open();
  60. int[] ports = Support_PortManager.getNextPortsForUDP(5);
  61. this.localAddr1 = new InetSocketAddress("127.0.0.1", ports[0]);
  62. this.localAddr2 = new InetSocketAddress("127.0.0.1", ports[1]);
  63. this.datagramSocket1 = new DatagramSocket(ports[2]);
  64. this.datagramSocket2 = new DatagramSocket(ports[3]);
  65. testPort = ports[4];
  66. }
  67. protected void tearDown() throws Exception {
  68. if (null != this.channel1) {
  69. try {
  70. this.channel1.close();
  71. } catch (Exception e) {
  72. //ignore
  73. }
  74. }
  75. if (null != this.channel2) {
  76. try {
  77. this.channel2.close();
  78. } catch (Exception e) {
  79. //ignore
  80. }
  81. }
  82. if (null != this.datagramSocket1) {
  83. try {
  84. this.datagramSocket1.close();
  85. } catch (Exception e) {
  86. //ignore
  87. }
  88. }
  89. if (null != this.datagramSocket2) {
  90. try {
  91. this.datagramSocket2.close();
  92. } catch (Exception e) {
  93. //ignore
  94. }
  95. }
  96. localAddr1 = null;
  97. localAddr2 = null;
  98. super.tearDown();
  99. }
  100. // -------------------------------------------------------------------
  101. // Test for methods in abstract class.
  102. // -------------------------------------------------------------------
  103. /*
  104. * Test method for 'java.nio.channels.DatagramChannel.validOps()'
  105. */
  106. public void testValidOps() {
  107. MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider
  108. .provider());
  109. MockDatagramChannel testMocknull = new MockDatagramChannel(null);
  110. int val = this.channel1.validOps();
  111. assertEquals(5, val);
  112. assertEquals(val, testMock.validOps());
  113. assertEquals(val, testMocknull.validOps());
  114. }
  115. /*
  116. * Test method for 'java.nio.channels.DatagramChannel.open()'
  117. */
  118. public void testOpen() {
  119. MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider
  120. .provider());
  121. MockDatagramChannel testMocknull = new MockDatagramChannel(null);
  122. assertNull(testMocknull.provider());
  123. assertNotNull(testMock.provider());
  124. assertEquals(this.channel1.provider(), testMock.provider());
  125. assertEquals(5, testMock.validOps());
  126. }
  127. /*
  128. * Test method for 'java.nio.channels.DatagramChannel.read(ByteBuffer)'
  129. */
  130. public void testReadByteBufferArray() throws IOException {
  131. final int testNum = 0;
  132. long readres = testNum;
  133. MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider
  134. .provider());
  135. MockDatagramChannel testMocknull = new MockDatagramChannel(null);
  136. int bufSize = 10;
  137. ByteBuffer[] readBuf = null;
  138. try {
  139. this.channel1.read(readBuf);
  140. fail("Should throw NPE");
  141. } catch (NullPointerException e) {
  142. // correct
  143. }
  144. try {
  145. readres = testMock.read(readBuf);
  146. fail("Should throw NPE");
  147. } catch (NullPointerException e) {
  148. // correct
  149. }
  150. readBuf = new ByteBuffer[bufSize];
  151. try {
  152. readres = this.channel1.read(readBuf);
  153. fail("Should throw NotYetConnectedException");
  154. } catch (NotYetConnectedException e) {
  155. // correct
  156. }
  157. readres = testMock.read(readBuf);
  158. assertEquals(testNum, readres);
  159. readres = testMocknull.read(readBuf);
  160. assertEquals(testNum, readres);
  161. }
  162. /*
  163. * Test method for 'java.nio.channels.DatagramChannel.read(ByteBuffer)'
  164. */
  165. public void testReadByteBufferArray_BufNull() throws IOException {
  166. MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider
  167. .provider());
  168. MockDatagramChannel testMocknull = new MockDatagramChannel(null);
  169. ByteBuffer[] readBuf = null;
  170. try {
  171. this.channel1.read(readBuf);
  172. fail("Should throw NPE");
  173. } catch (NullPointerException e) {
  174. // correct
  175. }
  176. try {
  177. testMock.read(readBuf);
  178. fail("Should throw NPE");
  179. } catch (NullPointerException e) {
  180. // correct
  181. }
  182. try {
  183. testMocknull.read(readBuf);
  184. fail("Should throw NPE");
  185. } catch (NullPointerException e) {
  186. // correct
  187. }
  188. }
  189. /*
  190. * Test method for 'java.nio.channels.DatagramChannel.write(ByteBuffer)'
  191. */
  192. public void testWriteByteBuffer() throws IOException {
  193. MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider
  194. .provider());
  195. MockDatagramChannel testMocknull = new MockDatagramChannel(null);
  196. int bufSize = 10;
  197. ByteBuffer[] readBuf = null;
  198. try {
  199. this.channel1.write(readBuf);
  200. fail("Should throw NPE");
  201. } catch (NullPointerException e) {
  202. // correct
  203. }
  204. try {
  205. testMock.write(readBuf);
  206. fail("Should throw NPE");
  207. } catch (NullPointerException e) {
  208. // correct
  209. }
  210. readBuf = new ByteBuffer[bufSize];
  211. try {
  212. this.channel1.write(readBuf);
  213. fail("Should throw NotYetConnectedException");
  214. } catch (NotYetConnectedException e) {
  215. // correct
  216. }
  217. long writeres = 0;
  218. writeres = testMock.write(readBuf);
  219. assertEquals(0, writeres);
  220. writeres = testMocknull.write(readBuf);
  221. assertEquals(0, writeres);
  222. }
  223. /*
  224. * Test method for 'java.nio.channels.DatagramChannel.write(ByteBuffer)'
  225. */
  226. public void testWriteByteBuffer_Bufnull() throws IOException {
  227. MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider
  228. .provider());
  229. MockDatagramChannel testMocknull = new MockDatagramChannel(null);
  230. ByteBuffer[] readBuf = null;
  231. try {
  232. this.channel1.write(readBuf);
  233. fail("Should throw NPE");
  234. } catch (NullPointerException e) {
  235. // correct
  236. }
  237. try {
  238. testMock.write(readBuf);
  239. fail("Should throw NPE");
  240. } catch (NullPointerException e) {
  241. // correct
  242. }
  243. try {
  244. testMocknull.write(readBuf);
  245. fail("Should throw NPE");
  246. } catch (NullPointerException e) {
  247. // correct
  248. }
  249. }
  250. // -------------------------------------------------------------------
  251. // Test for socket()
  252. // -------------------------------------------------------------------
  253. /**
  254. * Test method for 'DatagramChannelImpl.socket()'
  255. *
  256. * @throws SocketException
  257. */
  258. public void testSocket_BasicStatusBeforeConnect() throws SocketException {
  259. assertFalse(this.channel1.isConnected());// not connected
  260. DatagramSocket s1 = this.channel1.socket();
  261. assertSocketBeforeConnect(s1);
  262. DatagramSocket s2 = this.channel1.socket();
  263. // same
  264. assertSame(s1, s2);
  265. }
  266. /**
  267. * Test method for 'DatagramChannelImpl.socket()'
  268. *
  269. * @throws IOException
  270. */
  271. public void testSocket_Block_BasicStatusAfterConnect() throws IOException {
  272. this.channel1.connect(localAddr1);
  273. DatagramSocket s1 = this.channel1.socket();
  274. assertSocketAfterConnect(s1);
  275. DatagramSocket s2 = this.channel1.socket();
  276. // same
  277. assertSame(s1, s2);
  278. }
  279. public void testSocket_NonBlock_BasicStatusAfterConnect()
  280. throws IOException {
  281. this.channel1.connect(localAddr1);
  282. this.channel1.configureBlocking(false);
  283. DatagramSocket s1 = this.channel1.socket();
  284. assertSocketAfterConnect(s1);
  285. DatagramSocket s2 = this.channel1.socket();
  286. // same
  287. assertSame(s1, s2);
  288. }
  289. /**
  290. * Test method for 'DatagramChannelImpl.socket()'
  291. *
  292. * @throws IOException
  293. */
  294. public void testSocket_ActionsBeforeConnect() throws IOException {
  295. assertFalse(this.channel1.isConnected());// not connected
  296. DatagramSocket s = this.channel1.socket();
  297. assertSocketActionBeforeConnect(s);
  298. }
  299. /**
  300. * Test method for 'DatagramChannelImpl.socket()'
  301. *
  302. * @throws IOException
  303. */
  304. public void testSocket_Block_ActionsAfterConnect() throws IOException {
  305. assertFalse(this.channel1.isConnected());// not connected
  306. this.channel1.connect(localAddr1);
  307. DatagramSocket s = this.channel1.socket();
  308. assertSocketActionAfterConnect(s);
  309. }
  310. public void testSocket_NonBlock_ActionsAfterConnect() throws IOException {
  311. this.channel1.connect(localAddr1);
  312. this.channel1.configureBlocking(false);
  313. DatagramSocket s = this.channel1.socket();
  314. assertSocketActionAfterConnect(s);
  315. }
  316. private void assertSocketBeforeConnect(DatagramSocket s)
  317. throws SocketException {
  318. assertFalse(s.isBound());
  319. assertFalse(s.isClosed());
  320. assertFalse(s.isConnected());
  321. assertFalse(s.getBroadcast());
  322. assertFalse(s.getReuseAddress());
  323. assertNull(s.getInetAddress());
  324. assertTrue(s.getLocalAddress().isAnyLocalAddress());
  325. assertEquals(s.getLocalPort(), 0);
  326. assertNull(s.getLocalSocketAddress());
  327. assertEquals(s.getPort(), -1);
  328. assertTrue(s.getReceiveBufferSize() >= 8192);
  329. assertNull(s.getRemoteSocketAddress());
  330. assertFalse(s.getReuseAddress());
  331. assertTrue(s.getSendBufferSize() >= 8192);
  332. assertEquals(s.getSoTimeout(), 0);
  333. assertEquals(s.getTrafficClass(), 0);
  334. }
  335. private void assertSocketAfterConnect(DatagramSocket s)
  336. throws SocketException {
  337. assertTrue(s.isBound());
  338. assertFalse(s.isClosed());
  339. assertTrue(s.isConnected());
  340. assertFalse(s.getBroadcast());
  341. assertFalse(s.getReuseAddress());
  342. assertSame(s.getInetAddress(), localAddr1.getAddress());
  343. assertEquals(s.getLocalAddress(), localAddr1.getAddress());
  344. assertNotNull(s.getLocalSocketAddress());
  345. assertEquals(s.getPort(), localAddr1.getPort());
  346. assertTrue(s.getReceiveBufferSize() >= 8192);
  347. // not same , but equals
  348. assertNotSame(s.getRemoteSocketAddress(), (SocketAddress) localAddr1);
  349. assertEquals(s.getRemoteSocketAddress(), (SocketAddress) localAddr1);
  350. assertFalse(s.getReuseAddress());
  351. assertTrue(s.getSendBufferSize() >= 8192);
  352. assertEquals(s.getSoTimeout(), 0);
  353. assertEquals(s.getTrafficClass(), 0);
  354. }
  355. private void assertSocketActionBeforeConnect(DatagramSocket s)
  356. throws IOException {
  357. s.connect(localAddr2);
  358. assertFalse(this.channel1.isConnected());
  359. assertFalse(s.isConnected());
  360. s.disconnect();
  361. assertFalse(this.channel1.isConnected());
  362. assertFalse(s.isConnected());
  363. s.close();
  364. assertTrue(s.isClosed());
  365. assertFalse(this.channel1.isOpen());
  366. }
  367. private void assertSocketActionAfterConnect(DatagramSocket s)
  368. throws IOException {
  369. assertEquals(s.getPort(), localAddr1.getPort());
  370. s.connect(localAddr2);
  371. assertTrue(this.channel1.isConnected());
  372. assertTrue(s.isConnected());
  373. // not changed
  374. assertEquals(s.getPort(), localAddr1.getPort());
  375. s.disconnect();
  376. assertFalse(this.channel1.isConnected());
  377. assertFalse(s.isConnected());
  378. s.close();
  379. assertTrue(s.isClosed());
  380. assertFalse(this.channel1.isOpen());
  381. }
  382. // -------------------------------------------------------------------
  383. // Test for configureBlocking()
  384. // -------------------------------------------------------------------
  385. public void testConfigureBlocking_Read() throws Exception {
  386. assertTrue(this.channel1.isBlocking());
  387. ByteBuffer buf = ByteBuffer.allocate(CAPACITY_1KB);
  388. new Thread() {
  389. public void run() {
  390. try {
  391. sleep(TIME_UNIT * 5);
  392. channel1.configureBlocking(false);
  393. assertFalse(channel1.isBlocking());
  394. datagramSocket1.close();
  395. } catch (Exception e) {
  396. // do nothing
  397. }
  398. }
  399. }.start();
  400. SocketAddress addr = channel1.receive(buf);
  401. assertNull(addr);
  402. }
  403. // -------------------------------------------------------------------
  404. // Test for isConnected()
  405. // -------------------------------------------------------------------
  406. /**
  407. * Test method for 'DatagramChannelImpl.isConnected()'
  408. *
  409. * @throws IOException
  410. */
  411. public void testIsConnected_WithServer() throws IOException {
  412. connectLocalServer();
  413. disconnectAfterConnected();
  414. this.datagramSocket1.close();
  415. this.channel1.close();
  416. assertFalse(this.channel1.isConnected());
  417. }
  418. // -------------------------------------------------------------------
  419. // Test for connect()
  420. // -------------------------------------------------------------------
  421. /**
  422. * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
  423. */
  424. public void testConnect_BlockWithServer() throws IOException {
  425. // blocking mode
  426. assertTrue(this.channel1.isBlocking());
  427. connectLocalServer();
  428. datagramSocket1.close();
  429. disconnectAfterConnected();
  430. }
  431. /**
  432. * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
  433. */
  434. public void testConnect_BlockNoServer() throws IOException {
  435. connectWithoutServer();
  436. disconnectAfterConnected();
  437. }
  438. /**
  439. * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
  440. *
  441. * @throws IOException
  442. */
  443. public void testConnect_NonBlockWithServer() throws IOException {
  444. // Non blocking mode
  445. this.channel1.configureBlocking(false);
  446. connectLocalServer();
  447. datagramSocket1.close();
  448. disconnectAfterConnected();
  449. }
  450. /**
  451. * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
  452. *
  453. * @throws IOException
  454. */
  455. public void testConnect_Null() throws IOException {
  456. assertFalse(this.channel1.isConnected());
  457. try {
  458. this.channel1.connect(null);
  459. fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$
  460. } catch (IllegalArgumentException e) {
  461. // OK.
  462. }
  463. }
  464. /**
  465. * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
  466. *
  467. * @throws IOException
  468. */
  469. public void testConnect_UnsupportedType() throws IOException {
  470. assertFalse(this.channel1.isConnected());
  471. class SubSocketAddress extends SocketAddress {
  472. private static final long serialVersionUID = 1L;
  473. public SubSocketAddress() {
  474. super();
  475. }
  476. }
  477. SocketAddress newTypeAddress = new SubSocketAddress();
  478. try {
  479. this.channel1.connect(newTypeAddress);
  480. fail("Should throw an UnsupportedAddressTypeException here.");
  481. } catch (UnsupportedAddressTypeException e) {
  482. // OK.
  483. }
  484. }
  485. /**
  486. * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
  487. *
  488. * @throws IOException
  489. */
  490. public void testConnect_Unresolved() throws IOException {
  491. assertFalse(this.channel1.isConnected());
  492. InetSocketAddress unresolved = new InetSocketAddress(
  493. "unresolved address", 1080);
  494. try {
  495. this.channel1.connect(unresolved);
  496. fail("Should throw an UnresolvedAddressException here."); //$NON-NLS-1$
  497. } catch (UnresolvedAddressException e) {
  498. // OK.
  499. }
  500. }
  501. public void testConnect_EmptyHost() throws Exception {
  502. assertFalse(this.channel1.isConnected());
  503. assertEquals(this.channel1, this.channel1
  504. .connect(new InetSocketAddress("", 1081))); //$NON-NLS-1$
  505. }
  506. /**
  507. * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
  508. *
  509. * @throws IOException
  510. *
  511. */
  512. public void testConnect_ClosedChannelException() throws IOException {
  513. assertFalse(this.channel1.isConnected());
  514. this.channel1.close();
  515. assertFalse(this.channel1.isOpen());
  516. try {
  517. this.channel1.connect(localAddr1);
  518. fail("Should throw ClosedChannelException."); //$NON-NLS-1$
  519. } catch (ClosedChannelException e) {
  520. // OK.
  521. }
  522. }
  523. /**
  524. * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
  525. *
  526. * @throws IOException
  527. *
  528. */
  529. public void testConnect_IllegalStateException() throws IOException {
  530. assertFalse(this.channel1.isConnected());
  531. this.channel1.connect(localAddr1);
  532. assertTrue(this.channel1.isConnected());
  533. // connect after connected.
  534. try {
  535. this.channel1.connect(localAddr1);
  536. fail("Should throw IllegalStateException."); //$NON-NLS-1$
  537. } catch (IllegalStateException e) {
  538. // OK.
  539. }
  540. }
  541. /**
  542. * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
  543. *
  544. * @throws IOException
  545. *
  546. */
  547. public void testConnect_CheckOpenBeforeStatus() throws IOException {
  548. assertFalse(this.channel1.isConnected());
  549. this.channel1.connect(localAddr1);
  550. assertTrue(this.channel1.isConnected());
  551. // connect after connected.
  552. this.channel1.close();
  553. assertFalse(this.channel1.isOpen());
  554. // checking open is before checking status.
  555. try {
  556. this.channel1.connect(localAddr1);
  557. fail("Should throw ClosedChannelException."); //$NON-NLS-1$
  558. } catch (ClosedChannelException e) {
  559. // OK.
  560. }
  561. }
  562. private void disconnectAfterConnected() throws IOException {
  563. assertTrue(this.channel1.isConnected());
  564. this.channel1.disconnect();
  565. assertFalse(this.channel1.isConnected());
  566. }
  567. private void disconnectAfterClosed() throws IOException {
  568. assertFalse(this.channel1.isOpen());
  569. assertFalse(this.channel1.isConnected());
  570. this.channel1.disconnect();
  571. assertFalse(this.channel1.isConnected());
  572. }
  573. private void connectLocalServer() throws IOException {
  574. assertFalse(this.channel1.isConnected());
  575. assertTrue(this.datagramSocket1.isBound());
  576. assertSame(this.channel1, this.channel1.connect(localAddr1));
  577. assertTrue(this.channel1.isConnected());
  578. }
  579. private void connectWithoutServer() throws IOException {
  580. assertFalse(this.channel1.isConnected());
  581. this.datagramSocket1.close();
  582. assertTrue(this.datagramSocket1.isClosed());
  583. assertSame(this.channel1, this.channel1.connect(localAddr1));
  584. assertTrue(this.channel1.isConnected());
  585. }
  586. // -------------------------------------------------------------------
  587. // Test for disconnect()
  588. // -------------------------------------------------------------------
  589. /**
  590. * Test method for 'DatagramChannelImpl.disconnect()'
  591. *
  592. * @throws IOException
  593. */
  594. public void testDisconnect_BeforeConnect() throws IOException {
  595. assertFalse(this.channel1.isConnected());
  596. assertEquals(this.channel1, this.channel1.disconnect());
  597. assertFalse(this.channel1.isConnected());
  598. }
  599. /**
  600. * Test method for 'DatagramChannelImpl.disconnect()'
  601. *
  602. * @throws IOException
  603. */
  604. public void testDisconnect_UnconnectedClosed() throws IOException {
  605. assertFalse(this.channel1.isConnected());
  606. this.channel1.close();
  607. assertFalse(this.channel1.isOpen());
  608. assertEquals(this.channel1, this.channel1.disconnect());
  609. assertFalse(this.channel1.isConnected());
  610. }
  611. /**
  612. * Test method for 'DatagramChannelImpl.disconnect()'
  613. *
  614. * @throws IOException
  615. */
  616. public void testDisconnect_BlockWithServerChannelClosed()
  617. throws IOException {
  618. assertTrue(this.channel1.isBlocking());
  619. connectLocalServer();
  620. // disconnect after channel close
  621. this.channel1.close();
  622. disconnectAfterClosed();
  623. }
  624. /**
  625. * Test method for 'DatagramChannelImpl.disconnect()'
  626. *
  627. * @throws IOException
  628. */
  629. public void testDisconnect_NonBlockWithServerChannelClosed()
  630. throws IOException {
  631. this.channel1.configureBlocking(false);
  632. connectLocalServer();
  633. // disconnect after channel close
  634. this.channel1.close();
  635. disconnectAfterClosed();
  636. }
  637. /**
  638. * Test method for 'DatagramChannelImpl.disconnect()'
  639. *
  640. * @throws IOException
  641. */
  642. public void testDisconnect_BlockWithServerServerClosed() throws IOException {
  643. assertTrue(this.channel1.isBlocking());
  644. connectLocalServer();
  645. // disconnect after server close
  646. this.datagramSocket1.close();
  647. assertTrue(this.channel1.isOpen());
  648. assertTrue(this.channel1.isConnected());
  649. disconnectAfterConnected();
  650. }
  651. /**
  652. * Test method for 'DatagramChannelImpl.disconnect()'
  653. *
  654. * @throws IOException
  655. */
  656. public void testDisconnect_NonBlockWithServerServerClosed()
  657. throws IOException {
  658. this.channel1.configureBlocking(false);
  659. assertFalse(this.channel1.isBlocking());
  660. connectLocalServer();
  661. // disconnect after server close
  662. this.datagramSocket1.close();
  663. assertTrue(this.channel1.isOpen());
  664. assertTrue(this.channel1.isConnected());
  665. disconnectAfterConnected();
  666. }
  667. // -------------------------------------------------------------------
  668. // Test for receive(): Behavior Without Server.
  669. // -------------------------------------------------------------------
  670. /**
  671. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  672. *
  673. * @throws Exception
  674. */
  675. public void testReceive_UnconnectedNull() throws Exception {
  676. assertFalse(this.channel1.isConnected());
  677. try {
  678. this.channel1.receive(null);
  679. fail("Should throw a NPE here."); //$NON-NLS-1$
  680. } catch (NullPointerException e) {
  681. // OK.
  682. }
  683. }
  684. /**
  685. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  686. *
  687. * @throws Exception
  688. */
  689. public void testReceive_UnconnectedReadonly() throws Exception {
  690. assertFalse(this.channel1.isConnected());
  691. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL)
  692. .asReadOnlyBuffer();
  693. assertTrue(dst.isReadOnly());
  694. try {
  695. this.channel1.receive(dst);
  696. fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$
  697. } catch (IllegalArgumentException e) {
  698. // OK.
  699. }
  700. }
  701. /**
  702. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  703. *
  704. * @throws Exception
  705. */
  706. public void testReceive_UnconnectedBufEmpty() throws Exception {
  707. this.channel1.configureBlocking(false);
  708. assertFalse(this.channel1.isConnected());
  709. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  710. assertNull(this.channel1.receive(dst));
  711. }
  712. /**
  713. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  714. *
  715. * @throws Exception
  716. */
  717. public void testReceive_UnconnectedBufZero() throws Exception {
  718. assertFalse(this.channel1.isConnected());
  719. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ZERO);
  720. assertNull(this.channel1.receive(dst));
  721. }
  722. /**
  723. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  724. *
  725. * @throws Exception
  726. */
  727. public void testReceive_UnconnectedBufNotEmpty() throws Exception {
  728. assertFalse(this.channel1.isConnected());
  729. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  730. // buf is not empty
  731. dst.put((byte) 88);
  732. assertEquals(dst.position() + CAPACITY_NORMAL - 1, dst.limit());
  733. assertNull(this.channel1.receive(dst));
  734. }
  735. /**
  736. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  737. *
  738. * @throws Exception
  739. */
  740. public void testReceive_UnconnectedBufFull() throws Exception {
  741. assertFalse(this.channel1.isConnected());
  742. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ONE);
  743. // buf is full
  744. dst.put((byte) 88);
  745. assertEquals(dst.position(), dst.limit());
  746. assertNull(this.channel1.receive(dst));
  747. }
  748. /**
  749. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  750. *
  751. * @throws Exception
  752. */
  753. public void testReceive_UnconnectedClose() throws Exception {
  754. assertFalse(this.channel1.isConnected());
  755. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  756. this.channel1.close();
  757. assertFalse(this.channel1.isOpen());
  758. try {
  759. assertNull(this.channel1.receive(dst));
  760. fail("Should throw a ClosedChannelException here."); //$NON-NLS-1$
  761. } catch (ClosedChannelException e) {
  762. // OK.
  763. }
  764. }
  765. /**
  766. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  767. *
  768. * @throws Exception
  769. */
  770. public void testReceive_UnconnectedCloseNull() throws Exception {
  771. assertFalse(this.channel1.isConnected());
  772. this.channel1.close();
  773. assertFalse(this.channel1.isOpen());
  774. // checking buffer before checking open
  775. try {
  776. this.channel1.receive(null);
  777. fail("Should throw a NPE here."); //$NON-NLS-1$
  778. } catch (NullPointerException e) {
  779. // OK.
  780. }
  781. }
  782. /**
  783. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  784. *
  785. * @throws Exception
  786. */
  787. public void testReceive_UnconnectedCloseReadonly() throws Exception {
  788. assertFalse(this.channel1.isConnected());
  789. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL)
  790. .asReadOnlyBuffer();
  791. assertTrue(dst.isReadOnly());
  792. this.channel1.close();
  793. assertFalse(this.channel1.isOpen());
  794. try {
  795. this.channel1.receive(dst);
  796. fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$
  797. } catch (IllegalArgumentException e) {
  798. // OK.
  799. }
  800. }
  801. /**
  802. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  803. *
  804. * @throws Exception
  805. */
  806. public void testReceive_NonBlockNoServerBufEmpty() throws Exception {
  807. this.channel1.configureBlocking(false);
  808. receiveNonBlockNoServer(CAPACITY_NORMAL);
  809. }
  810. /**
  811. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  812. *
  813. * @throws Exception
  814. */
  815. public void testReceive_BlockNoServerNull() throws Exception {
  816. assertTrue(this.channel1.isBlocking());
  817. receiveNoServerNull();
  818. }
  819. /**
  820. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  821. *
  822. * @throws Exception
  823. */
  824. public void testReceive_NonBlockNoServerNull() throws Exception {
  825. this.channel1.configureBlocking(false);
  826. receiveNoServerNull();
  827. }
  828. /**
  829. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  830. *
  831. * @throws Exception
  832. */
  833. public void testReceive_BlockNoServerReadonly() throws Exception {
  834. assertTrue(this.channel1.isBlocking());
  835. receiveNoServerReadonly();
  836. }
  837. /**
  838. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  839. *
  840. * @throws Exception
  841. */
  842. public void testReceive_NonBlockNoServerReadonly() throws Exception {
  843. this.channel1.configureBlocking(false);
  844. receiveNoServerReadonly();
  845. }
  846. /**
  847. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  848. *
  849. * @throws Exception
  850. */
  851. public void testReceive_NonBlockNoServerBufZero() throws Exception {
  852. this.channel1.configureBlocking(false);
  853. receiveNonBlockNoServer(CAPACITY_ZERO);
  854. }
  855. /**
  856. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  857. *
  858. * @throws Exception
  859. */
  860. public void testReceive_NonBlockNoServerBufNotEmpty() throws Exception {
  861. this.channel1.configureBlocking(false);
  862. connectWithoutServer();
  863. ByteBuffer dst = allocateNonEmptyBuf();
  864. assertNull(this.channel1.receive(dst));
  865. }
  866. /**
  867. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  868. *
  869. * @throws Exception
  870. */
  871. public void testReceive_NonBlockNoServerBufFull() throws Exception {
  872. this.channel1.configureBlocking(false);
  873. connectWithoutServer();
  874. ByteBuffer dst = allocateFullBuf();
  875. assertNull(this.channel1.receive(dst));
  876. }
  877. /**
  878. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  879. *
  880. * @throws Exception
  881. */
  882. public void testReceive_BlockNoServerChannelClose() throws Exception {
  883. assertTrue(this.channel1.isBlocking());
  884. receiveNoServerChannelClose();
  885. }
  886. /**
  887. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  888. *
  889. * @throws Exception
  890. */
  891. public void testReceive_NonBlockNoServerChannelClose() throws Exception {
  892. this.channel1.configureBlocking(false);
  893. receiveNoServerChannelClose();
  894. }
  895. /**
  896. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  897. *
  898. * @throws Exception
  899. */
  900. public void testReceive_BlockNoServerCloseNull() throws Exception {
  901. assertTrue(this.channel1.isBlocking());
  902. receiveNoServerChannelCloseNull();
  903. }
  904. /**
  905. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  906. *
  907. * @throws Exception
  908. */
  909. public void testReceive_NonBlockNoServerCloseNull() throws Exception {
  910. this.channel1.configureBlocking(false);
  911. receiveNoServerChannelCloseNull();
  912. }
  913. /**
  914. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  915. *
  916. * @throws Exception
  917. */
  918. public void testReceive_NonBlockNoServerCloseReadonly() throws Exception {
  919. this.channel1.configureBlocking(false);
  920. receiveNoServerChannelCloseReadonly();
  921. }
  922. /**
  923. * Test method for 'DatagramChannelImpl.receive(ByteBuffer)'
  924. *
  925. * @throws Exception
  926. */
  927. public void testReceive_BlockNoServerCloseReadonly() throws Exception {
  928. assertTrue(this.channel1.isBlocking());
  929. receiveNoServerChannelCloseReadonly();
  930. }
  931. private void receiveNoServerNull() throws IOException {
  932. connectWithoutServer();
  933. try {
  934. this.channel1.receive(null);
  935. fail("Should throw a NPE here."); //$NON-NLS-1$
  936. } catch (NullPointerException e) {
  937. // OK.
  938. }
  939. }
  940. private void receiveNoServerReadonly() throws IOException {
  941. connectWithoutServer();
  942. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL)
  943. .asReadOnlyBuffer();
  944. assertTrue(dst.isReadOnly());
  945. try {
  946. this.channel1.receive(dst);
  947. fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$
  948. } catch (IllegalArgumentException e) {
  949. // OK.
  950. }
  951. }
  952. private void receiveNonBlockNoServer(int size) throws IOException {
  953. connectWithoutServer();
  954. ByteBuffer dst = ByteBuffer.allocateDirect(size);
  955. assertNull(this.channel1.receive(dst));
  956. }
  957. private void receiveNoServerChannelClose() throws IOException {
  958. connectWithoutServer();
  959. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  960. this.channel1.close();
  961. assertFalse(this.channel1.isOpen());
  962. try {
  963. assertNull(this.channel1.receive(dst));
  964. fail("Should throw a ClosedChannelException here."); //$NON-NLS-1$
  965. } catch (ClosedChannelException e) {
  966. // OK.
  967. }
  968. }
  969. private void receiveNoServerChannelCloseNull() throws IOException {
  970. connectWithoutServer();
  971. this.channel1.close();
  972. assertFalse(this.channel1.isOpen());
  973. try {
  974. this.channel1.receive(null);
  975. fail("Should throw a NPE here."); //$NON-NLS-1$
  976. } catch (NullPointerException e) {
  977. // OK.
  978. }
  979. }
  980. private void receiveNoServerChannelCloseReadonly() throws IOException {
  981. connectWithoutServer();
  982. this.channel1.close();
  983. assertFalse(this.channel1.isOpen());
  984. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL)
  985. .asReadOnlyBuffer();
  986. assertTrue(dst.isReadOnly());
  987. try {
  988. this.channel1.receive(dst);
  989. fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$
  990. } catch (IllegalArgumentException e) {
  991. // OK.
  992. }
  993. }
  994. private ByteBuffer allocateFullBuf() {
  995. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ONE);
  996. // buf is full
  997. dst.put((byte) 88);
  998. assertEquals(dst.position(), dst.limit());
  999. return dst;
  1000. }
  1001. private ByteBuffer allocateNonEmptyBuf() {
  1002. ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  1003. // buf is not empty
  1004. dst.put((byte) 88);
  1005. dst.put((byte) 99);
  1006. assertEquals(dst.position() + CAPACITY_NORMAL - 2, dst.limit());
  1007. return dst;
  1008. }
  1009. // -------------------------------------------------------------------
  1010. // Test for send(): Behavior without server.
  1011. // -------------------------------------------------------------------
  1012. private void sendDataBlocking(InetSocketAddress addr, ByteBuffer writeBuf)
  1013. throws IOException {
  1014. InetSocketAddress ipAddr = addr;
  1015. assertEquals(CAPACITY_NORMAL, this.channel1.send(writeBuf, ipAddr));
  1016. assertTrue(this.channel1.isOpen());
  1017. assertTrue(this.channel1.isBlocking());
  1018. this.channel1.connect(ipAddr);
  1019. assertTrue(this.channel1.isConnected());
  1020. }
  1021. private void sendDataNonBlocking(InetSocketAddress addr, ByteBuffer writeBuf)
  1022. throws IOException {
  1023. InetSocketAddress ipAddr = addr;
  1024. this.channel1.configureBlocking(false);
  1025. assertEquals(CAPACITY_NORMAL, this.channel1.send(writeBuf, ipAddr));
  1026. assertTrue(this.channel1.isOpen());
  1027. assertFalse(this.channel1.isBlocking());
  1028. this.channel1.connect(ipAddr);
  1029. assertTrue(this.channel1.isConnected());
  1030. }
  1031. /*
  1032. * Test method for 'DatagramChannelImpl.send(ByteBuffer, SocketAddress)'
  1033. */
  1034. public void testSend_NoServerBlockingCommon() throws IOException {
  1035. ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  1036. sendDataBlocking(localAddr1, writeBuf);
  1037. }
  1038. public void testSend_NoServerNonblockingCommon() throws IOException {
  1039. ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  1040. sendDataNonBlocking(localAddr1, writeBuf);
  1041. }
  1042. public void testSend_NoServerTwice() throws IOException {
  1043. ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  1044. sendDataBlocking(localAddr1, writeBuf);
  1045. // can not buffer twice!
  1046. assertEquals(0, this.channel1.send(writeBuf, localAddr1));
  1047. try {
  1048. channel1.send(writeBuf, localAddr2);
  1049. fail("Should throw IllegalArgumentException");
  1050. } catch (IllegalArgumentException e) {
  1051. // correct
  1052. }
  1053. }
  1054. public void testSend_NoServerNonBlockingTwice() throws IOException {
  1055. ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  1056. sendDataNonBlocking(localAddr1, writeBuf);
  1057. // can not buffer twice!
  1058. assertEquals(0, this.channel1.send(writeBuf, localAddr1));
  1059. try {
  1060. channel1.send(writeBuf, localAddr2);
  1061. fail("Should throw IllegalArgumentException");
  1062. } catch (IllegalArgumentException e) {
  1063. // correct
  1064. }
  1065. }
  1066. public void testSend_NoServerBufNull() throws IOException {
  1067. try {
  1068. sendDataBlocking(localAddr1, null);
  1069. fail("Should throw a NPE here.");
  1070. } catch (NullPointerException e) {
  1071. // correct
  1072. }
  1073. }
  1074. public void testSend_NoServerBufNullTwice() throws IOException {
  1075. ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  1076. try {
  1077. sendDataBlocking(localAddr1, null);
  1078. fail("Should throw a NPE here.");
  1079. } catch (NullPointerException e) {
  1080. // correct
  1081. }
  1082. sendDataBlocking(localAddr1, writeBuf);
  1083. try {
  1084. channel1.send(null, localAddr2);
  1085. fail("Should throw NPE");
  1086. } catch (NullPointerException e) {
  1087. // correct
  1088. }
  1089. }
  1090. public void testSend_NoServerAddrNull() throws IOException {
  1091. ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  1092. try {
  1093. sendDataBlocking(null, writeBuf);
  1094. fail("Should throw a NPE here.");
  1095. } catch (NullPointerException e) {
  1096. // correct
  1097. }
  1098. }
  1099. public void testSend_NoServerAddrNullTwice() throws IOException {
  1100. ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
  1101. try {
  1102. sendDataBlocking(null, writeBuf);
  1103. fail("Should throw a NPE here.");
  1104. } catch (NullPointerException e) {
  1105. // correct
  1106. }
  1107. sendDataBlocking(localAddr1, writeBuf);
  1108. try {
  1109. channel1.send(writeBuf, null);
  1110. fail("Should throw NPE");
  1111. } catch (NullPointerException e) {
  1112. // correct
  1113. }
  1114. }
  1115. // -------------------------------------------------------------------
  1116. // Test for receive()and send(): Send and Receive with Real Data
  1117. // -------------------------------------------------------------------
  1118. public void testReceiveSend_Block_Normal() throws Exception {
  1119. this.channel1.socket().bind(localAddr2);
  1120. sendByChannel("some normal string in testReceiveSend_Normal",
  1121. localAddr2);
  1122. receiveByChannel(CAPACITY_NORMAL, localAddr2,
  1123. "some normal string in testReceiveSend_Normal");
  1124. }
  1125. public void testReceiveSend_Block_NotBound() throws Exception {
  1126. // not bound
  1127. sendByChannel("some normal string in testReceiveSend_Normal",
  1128. localAddr2);
  1129. ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL);
  1130. assertNull(channel1.receive(buf));
  1131. assertFalse(channel1.socket().isBound());
  1132. }
  1133. public void testReceiveSend_NonBlock_NotBound() throws Exception {
  1134. // not bound
  1135. this.channel1.configureBlocking(false);
  1136. this.channel2.configureBlocking(false);
  1137. sendByChannel("some normal string in testReceiveSend_Normal",
  1138. localAddr2);
  1139. ByteBuffer buf = ByteBuffer.wrap(new byte[CAPACITY_NORMAL]);
  1140. assertNull((InetSocketAddress) this.channel1.receive(buf));
  1141. }
  1142. public void testReceiveSend_Block_Normal_S2C() throws Exception {
  1143. this.channel1.socket().bind(localAddr2);
  1144. sendByDatagramSocket(
  1145. "some normal string in testReceiveSend_Normal_S2C", localAddr2);
  1146. receiveByChannel(CAPACITY_NORMAL, localAddr2,
  1147. "some normal string in testReceiveSend_Normal_S2C");
  1148. }
  1149. public void testReceiveSend_Block_Normal_C2S() throws Exception {
  1150. this.datagramSocket1 = new DatagramSocket(localAddr2.getPort());
  1151. String str1 = "some normal string in testReceiveSend_Normal_C2S";
  1152. sendByChannel(str1, localAddr2);
  1153. receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, str1);
  1154. }
  1155. public void testReceiveSend_NonBlock_Normal_C2S() throws Exception {
  1156. this.channel1.configureBlocking(false);
  1157. this.channel2.configureBlocking(false);
  1158. this.datagramSocket1 = new DatagramSocket(localAddr2.getPort());
  1159. String str1 = "some normal string in testReceiveSend_Normal_C2S";
  1160. sendByChannel(str1, localAddr2);
  1161. receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, str1);
  1162. }
  1163. public void testReceiveSend_Normal_S2S() throws Exception {
  1164. String msg = "normal string in testReceiveSend_Normal_S2S";
  1165. this.datagramSocket1 = new DatagramSocket(testPort);
  1166. DatagramPacket rdp = new DatagramPacket(msg.getBytes(), msg.length(),
  1167. localAddr2);
  1168. datagramSocket2 = new DatagramSocket(localAddr2.getPort());
  1169. this.datagramSocket1.send(rdp);
  1170. byte[] buf = new byte[CAPACITY_NORMAL];
  1171. this.datagramSocket2.setSoTimeout(TIME_UNIT);
  1172. rdp = new DatagramPacket(buf, buf.length);
  1173. this.datagramSocket2.receive(rdp);
  1174. assertEquals(new String(buf, 0, CAPACITY_NORMAL).trim(), msg);
  1175. }
  1176. public void testReceiveSend_Block_Empty() throws Exception {
  1177. this.channel1.socket().bind(localAddr2);
  1178. sendByChannel("", localAddr2);
  1179. receiveByChannel(CAPACITY_NORMAL, localAddr2, "");
  1180. }
  1181. public void testReceiveSend_NonBlock_Empty() throws Exception {
  1182. this.channel1.configureBlocking(false);
  1183. this.channel2.configureBlocking(false);
  1184. this.channel1.socket().bind(localAddr2);
  1185. sendByChannel("", localAddr2);
  1186. receiveByChannel(CAPACITY_NORMAL, localAddr2, "");
  1187. }
  1188. public void testReceiveSend_Block_Empty_S2C() throws Exception {
  1189. this.channel1.socket().bind(localAddr2);
  1190. sendByDatagramSocket("", localAddr2);
  1191. receiveByChannel(CAPACITY_NORMAL, localAddr2, "");
  1192. }
  1193. public void testReceiveSend_NonBlock_Empty_S2C() throws Exception {
  1194. this.channel1.configureBlocking(false);
  1195. this.channel2.configureBlocking(false);
  1196. this.channel1.socket().bind(localAddr2);
  1197. sendByDatagramSocket("", localAddr2);
  1198. receiveByChannel(CAPACITY_NORMAL, localAddr2, "");
  1199. }
  1200. public void testReceiveSend_Block_Empty_C2S() throws Exception {
  1201. this.datagramSocket1 = new DatagramSocket(localAddr2.getPort());
  1202. sendByChannel("", localAddr2);
  1203. receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, "");
  1204. }
  1205. public void testReceiveSend_NonBlock_Empty_C2S() throws Exception {
  1206. this.channel1.configureBlocking(false);
  1207. this.channel2.configureBlocking(false);
  1208. this.datagramSocket1 = new DatagramSocket(localAddr2.getPort());
  1209. sendByChannel("", localAddr2);
  1210. receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, "");
  1211. }
  1212. public void testReceiveSend_Empty_S2S() throws Exception {
  1213. String msg = "";
  1214. this.datagramSocket1 = new DatagramSocket(testPort);
  1215. DatagramPacket rdp = new DatagramPacket(msg.getBytes(), msg.length(),
  1216. localAddr2);
  1217. datagramSocket2 = new DatagramSocket(localAddr2.getPort());
  1218. this.datagramSocket1.send(rdp);
  1219. byte[] buf = new byte[CAPACITY_NORMAL];
  1220. this.datagramSocket2.setSoTimeout(TIME_UNIT);
  1221. rdp = new DatagramPacket(buf, buf.length);
  1222. this.datagramSocket2.receive(rdp);
  1223. assertEquals(new String(buf, 0, CAPACITY_NORMAL).trim(), msg);
  1224. }
  1225. public void testReceiveSend_Block_Oversize() throws Exception {
  1226. this.channel1.socket().bind(localAddr2);
  1227. sendByChannel("0123456789", localAddr2);
  1228. receiveByChannel(5, localAddr2, "01234");
  1229. }
  1230. public void testReceiveSend_Block_Oversize_C2S() throws Exception {
  1231. this.datagramSocket1 = new DatagramSocket(localAddr2.getPort());
  1232. sendByChannel("0123456789", localAddr2);
  1233. receiveByDatagramSocket(5, localAddr2, "01234");
  1234. }
  1235. public void testReceiveSend_NonBlock_Oversize_C2S() throws Exception {
  1236. this.channel1.configureBlocking(false);
  1237. this.channel2.configureBlocking(false);
  1238. this.datagramSocket1 = new DatagramSocket(localAddr2.getPort());
  1239. sendByChannel("0123456789", localAddr2);
  1240. receiveByDatagramSocket(5, localAddr2, "01234");
  1241. }
  1242. public void testReceiveSend_Block_Oversize_S2C() throws Exception {
  1243. this.channel1.socket().bind(localAddr2);
  1244. sendByDatagramSocket("0123456789", localAddr2);
  1245. receiveByChannel(5, localAddr2, "01234");
  1246. }
  1247. public void testReceiveSend_8K() throws Exception {
  1248. StringBuffer str8k = new StringBuffer();
  1249. for (int i = 0; i < 8 * CAPACITY_1KB; i++) {
  1250. str8k.append('a');
  1251. }
  1252. String str = str8k.toString();
  1253. this.channel1.socket().bind(localAddr2);
  1254. sendByChannel(str, localAddr2);
  1255. receiveByChannel(8 * CAPACITY_1KB, localAddr2, str);
  1256. }
  1257. public void testReceiveSend_64K() throws Exception {
  1258. StringBuffer str64k = new StringBuffer();
  1259. for (int i = 0; i < CAPACITY_64KB; i++) {
  1260. str64k.append('a');
  1261. }
  1262. String str = str64k.toString();
  1263. try {
  1264. Thread.sleep(TIME_UNIT);
  1265. channel2.send(ByteBuffer.wrap(str.getBytes()), localAddr1);
  1266. fail("Should throw SocketException!");
  1267. } catch (SocketException e) {
  1268. //expected
  1269. }
  1270. }
  1271. private void sendByChannel(String data, InetSocketAddress address)
  1272. throws Exception {
  1273. try {
  1274. assertEquals(data.length(), this.channel2.send(ByteBuffer.wrap(data
  1275. .getBytes()), address));
  1276. } finally {
  1277. this.channel2.close();
  1278. }
  1279. }
  1280. private void sendByDatagramSocket(String data, InetSocketAddress address)
  1281. throws Exception {
  1282. this.datagramSocket1 = new DatagramSocket(testPort);
  1283. DatagramPacket rdp = new DatagramPacket(data.getBytes(), data.length(),
  1284. address);
  1285. this.datagramSocket1.send(rdp);
  1286. }
  1287. private void receiveByChannel(int bufSize, InetSocketAddress address,
  1288. String expectedString) throws IOException {
  1289. try {
  1290. ByteBuffer buf = ByteBuffer.wrap(new byte[bufSize]);
  1291. InetSocketAddress returnAddr = null;
  1292. long startTime = System.currentTimeMillis();
  1293. do {
  1294. returnAddr = (InetSocketAddress) this.channel1.receive(buf);
  1295. // continue loop when channel1 is non-blocking and no data was
  1296. // received.
  1297. if (channel1.isBlocking() || null != returnAddr) {
  1298. break;
  1299. }
  1300. // avoid dead loop
  1301. assertTimeout(startTime, 10000);
  1302. } while (true);
  1303. int length = returnAddr.getAddress().getAddress().length;
  1304. for (int i = 0; i < length; i++) {
  1305. assertEquals(returnAddr.getAddress().getAddress()[i],
  1306. InetAddress.getByName("127.0.0.1").getAddress()[i]);
  1307. }
  1308. // port is NOT equal
  1309. assertFalse(returnAddr.getPort() == address.getPort());
  1310. assertEquals(new String(buf.array(), 0, bufSize).trim(),
  1311. expectedString);
  1312. } finally {
  1313. this.channel1.close();
  1314. }
  1315. }
  1316. /*
  1317. * Fails if the difference between current time and start time is greate