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

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 505 lines · 377 code · 50 blank · 78 comment · 27 complexity · a3214b01396c1c7d6d57f9eb3ca77583 MD5 · raw file

  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.harmony.nio.tests.java.nio.channels;
  17. import java.io.IOException;
  18. import java.net.InetAddress;
  19. import java.net.InetSocketAddress;
  20. import java.nio.ByteBuffer;
  21. import java.nio.channels.ClosedChannelException;
  22. import java.nio.channels.Pipe;
  23. import java.nio.channels.SelectionKey;
  24. import java.nio.channels.ServerSocketChannel;
  25. import java.nio.channels.SocketChannel;
  26. import junit.framework.TestCase;
  27. /**
  28. * Tests for Pipe.SinkChannel class
  29. */
  30. public class SinkChannelTest extends TestCase {
  31. private static final int BUFFER_SIZE = 5;
  32. private static final String ISO8859_1 = "ISO8859-1";
  33. private Pipe pipe;
  34. private Pipe.SinkChannel sink;
  35. private Pipe.SourceChannel source;
  36. private ByteBuffer buffer;
  37. private ByteBuffer positionedBuffer;
  38. protected void setUp() throws Exception {
  39. super.setUp();
  40. pipe = Pipe.open();
  41. sink = pipe.sink();
  42. source = pipe.source();
  43. buffer = ByteBuffer.wrap("bytes".getBytes(ISO8859_1));
  44. positionedBuffer = ByteBuffer.wrap("12345bytes".getBytes(ISO8859_1));
  45. positionedBuffer.position(BUFFER_SIZE);
  46. }
  47. /**
  48. * @tests java.nio.channels.Pipe.SinkChannel#validOps()
  49. */
  50. public void test_validOps() {
  51. assertEquals(SelectionKey.OP_WRITE, sink.validOps());
  52. }
  53. /**
  54. * @tests java.nio.channels.Pipe.SinkChannel#write(ByteBuffer)
  55. */
  56. public void test_write_LByteBuffer() throws IOException {
  57. ByteBuffer[] bufArray = { buffer, positionedBuffer };
  58. boolean[] sinkBlockingMode = { true, true, false, false };
  59. boolean[] sourceBlockingMode = { true, false, true, false };
  60. int oldPosition;
  61. int currentPosition;
  62. for (int i = 0; i < sinkBlockingMode.length; ++i) {
  63. sink.configureBlocking(sinkBlockingMode[i]);
  64. source.configureBlocking(sourceBlockingMode[i]);
  65. // if sink and source both are blocking mode, source only needs read
  66. // once to get what sink write.
  67. boolean isBlocking = sinkBlockingMode[i] && sourceBlockingMode[i];
  68. for (ByteBuffer buf : bufArray) {
  69. buf.mark();
  70. oldPosition = buf.position();
  71. sink.write(buf);
  72. ByteBuffer readBuf = ByteBuffer.allocate(BUFFER_SIZE);
  73. int totalCount = 0;
  74. do {
  75. int count = source.read(readBuf);
  76. if (count > 0) {
  77. totalCount += count;
  78. }
  79. } while (totalCount != BUFFER_SIZE && !isBlocking);
  80. currentPosition = buf.position();
  81. assertEquals(BUFFER_SIZE, currentPosition - oldPosition);
  82. assertEquals("bytes", new String(readBuf.array(), ISO8859_1));
  83. buf.reset();
  84. }
  85. }
  86. }
  87. /**
  88. * @tests java.nio.channels.Pipe.SinkChannel#write(ByteBuffer)
  89. */
  90. public void test_write_LByteBuffer_mutliThread() throws IOException,
  91. InterruptedException {
  92. final int THREAD_NUM = 20;
  93. final byte[] strbytes = "bytes".getBytes(ISO8859_1);
  94. Thread[] thread = new Thread[THREAD_NUM];
  95. for (int i = 0; i < THREAD_NUM; i++) {
  96. thread[i] = new Thread() {
  97. public void run() {
  98. try {
  99. sink.write(ByteBuffer.wrap(strbytes));
  100. } catch (IOException e) {
  101. throw new RuntimeException(e);
  102. }
  103. }
  104. };
  105. }
  106. for (int i = 0; i < THREAD_NUM; i++) {
  107. thread[i].start();
  108. }
  109. for (int i = 0; i < THREAD_NUM; i++) {
  110. thread[i].join();
  111. }
  112. ByteBuffer readBuf = ByteBuffer.allocate(THREAD_NUM * BUFFER_SIZE);
  113. long totalCount = 0;
  114. do {
  115. long count = source.read(readBuf);
  116. if (count < 0) {
  117. break;
  118. }
  119. totalCount += count;
  120. } while (totalCount != (THREAD_NUM * BUFFER_SIZE));
  121. StringBuffer buf = new StringBuffer();
  122. for (int i = 0; i < THREAD_NUM; i++) {
  123. buf.append("bytes");
  124. }
  125. String readString = buf.toString();
  126. assertEquals(readString, new String(readBuf.array(), ISO8859_1));
  127. }
  128. /**
  129. * @tests java.nio.channels.Pipe.SinkChannel#write(ByteBuffer)
  130. */
  131. public void test_write_LByteBuffer_Exception() throws IOException {
  132. // write null ByteBuffer
  133. ByteBuffer nullBuf = null;
  134. try {
  135. sink.write(nullBuf);
  136. fail("should throw NullPointerException");
  137. } catch (NullPointerException e) {
  138. // expected
  139. }
  140. }
  141. public void test_write_LByteBuffer_SourceClosed() throws IOException {
  142. source.close();
  143. try {
  144. int written = sink.write(buffer);
  145. fail();
  146. } catch (IOException expected) {
  147. }
  148. }
  149. public void test_write_LByteBuffer_SinkClosed() throws IOException {
  150. sink.close();
  151. try {
  152. sink.write(buffer);
  153. fail("should throw ClosedChannelException");
  154. } catch (ClosedChannelException expected) {
  155. }
  156. }
  157. /**
  158. * @tests java.nio.channels.Pipe.SinkChannel#write(ByteBuffer[])
  159. */
  160. public void test_write_$LByteBuffer() throws IOException {
  161. ByteBuffer[] bufArray = { buffer, positionedBuffer };
  162. boolean[] sinkBlockingMode = { true, true, false, false };
  163. boolean[] sourceBlockingMode = { true, false, true, false };
  164. for (int i = 0; i < sinkBlockingMode.length; ++i) {
  165. sink.configureBlocking(sinkBlockingMode[i]);
  166. source.configureBlocking(sourceBlockingMode[i]);
  167. buffer.position(0);
  168. positionedBuffer.position(BUFFER_SIZE);
  169. sink.write(bufArray);
  170. // if sink and source both are blocking mode, source only needs read
  171. // once to get what sink write.
  172. boolean isBlocking = sinkBlockingMode[i] && sourceBlockingMode[i];
  173. for (int j = 0; j < bufArray.length; ++j) {
  174. ByteBuffer readBuf = ByteBuffer.allocate(BUFFER_SIZE);
  175. int totalCount = 0;
  176. do {
  177. int count = source.read(readBuf);
  178. if (count < 0) {
  179. break;
  180. }
  181. totalCount += count;
  182. } while (totalCount != BUFFER_SIZE && !isBlocking);
  183. assertEquals("bytes", new String(readBuf.array(), ISO8859_1));
  184. }
  185. assertEquals(BUFFER_SIZE, buffer.position());
  186. assertEquals(10, positionedBuffer.position());
  187. }
  188. }
  189. /**
  190. * @tests java.nio.channels.Pipe.SinkChannel#write(ByteBuffer[])
  191. */
  192. public void test_write_$LByteBuffer_Exception() throws IOException {
  193. // write null ByteBuffer[]
  194. ByteBuffer[] nullBufArrayRef = null;
  195. try {
  196. sink.write(nullBufArrayRef);
  197. fail("should throw NullPointerException");
  198. } catch (NullPointerException e) {
  199. // expected
  200. }
  201. // write ByteBuffer[] contains null element
  202. ByteBuffer nullBuf = null;
  203. ByteBuffer[] nullBufArray = { buffer, nullBuf };
  204. try {
  205. sink.write(nullBufArray);
  206. fail("should throw NullPointerException");
  207. } catch (NullPointerException e) {
  208. // expected
  209. }
  210. }
  211. public void test_write_$LByteBuffer_SourceClosed() throws IOException {
  212. ByteBuffer[] bufArray = { buffer };
  213. source.close();
  214. try {
  215. long written = sink.write(bufArray);
  216. fail();
  217. } catch (IOException expected) {
  218. }
  219. }
  220. /**
  221. * @tests java.nio.channels.Pipe.SinkChannel#write(ByteBuffer[])
  222. */
  223. public void test_write_$LByteBuffer_SinkClosed() throws IOException {
  224. ByteBuffer[] bufArray = { buffer };
  225. sink.close();
  226. try {
  227. sink.write(bufArray);
  228. fail("should throw ClosedChannelException");
  229. } catch (ClosedChannelException e) {
  230. // expected
  231. }
  232. ByteBuffer[] nullBufArrayRef = null;
  233. try {
  234. sink.write(nullBufArrayRef);
  235. fail("should throw NullPointerException");
  236. } catch (NullPointerException e) {
  237. // expected
  238. }
  239. ByteBuffer nullBuf = null;
  240. ByteBuffer[] nullBufArray = { nullBuf };
  241. // write ByteBuffer[] contains null element
  242. try {
  243. sink.write(nullBufArray);
  244. fail("should throw ClosedChannelException");
  245. } catch (ClosedChannelException e) {
  246. // expected
  247. }
  248. }
  249. /**
  250. * @tests java.nio.channels.Pipe.SinkChannel#write(ByteBuffer[], int, int)
  251. */
  252. public void test_write_$LByteBufferII() throws IOException {
  253. ByteBuffer[] bufArray = { buffer, positionedBuffer };
  254. boolean[] sinkBlockingMode = { true, true, false, false };
  255. boolean[] sourceBlockingMode = { true, false, true, false };
  256. for (int i = 0; i < sinkBlockingMode.length; ++i) {
  257. sink.configureBlocking(sinkBlockingMode[i]);
  258. source.configureBlocking(sourceBlockingMode[i]);
  259. positionedBuffer.position(BUFFER_SIZE);
  260. sink.write(bufArray, 1, 1);
  261. // if sink and source both are blocking mode, source only needs read
  262. // once to get what sink write.
  263. boolean isBlocking = sinkBlockingMode[i] && sourceBlockingMode[i];
  264. ByteBuffer readBuf = ByteBuffer.allocate(BUFFER_SIZE);
  265. int totalCount = 0;
  266. do {
  267. int count = source.read(readBuf);
  268. if (count < 0) {
  269. break;
  270. }
  271. totalCount += count;
  272. } while (totalCount != BUFFER_SIZE && !isBlocking);
  273. assertEquals("bytes", new String(readBuf.array(), ISO8859_1));
  274. assertEquals(10, positionedBuffer.position());
  275. }
  276. }
  277. public void test_write_$LByteBufferII_Exception() throws IOException {
  278. try {
  279. sink.write(null, 0, 1);
  280. fail();
  281. } catch (NullPointerException expected) {
  282. }
  283. try {
  284. sink.write(new ByteBuffer[2], 0, -1);
  285. fail();
  286. } catch (IndexOutOfBoundsException expected) {
  287. }
  288. // write ByteBuffer[] contains null element
  289. ByteBuffer nullBuf = null;
  290. ByteBuffer[] nullBufArray = { nullBuf };
  291. try {
  292. sink.write(nullBufArray, 0, 1);
  293. fail("should throw NullPointerException");
  294. } catch (NullPointerException e) {
  295. // expected
  296. }
  297. try {
  298. sink.write(nullBufArray, 0, -1);
  299. fail("should throw IndexOutOfBoundsException");
  300. } catch (IndexOutOfBoundsException e) {
  301. // expected
  302. }
  303. ByteBuffer[] bufArray = { buffer, nullBuf };
  304. try {
  305. sink.write(bufArray, 0, -1);
  306. fail("should throw IndexOutOfBoundsException");
  307. } catch (IndexOutOfBoundsException e) {
  308. // expected
  309. }
  310. try {
  311. sink.write(bufArray, -1, 0);
  312. fail("should throw IndexOutOfBoundsException");
  313. } catch (IndexOutOfBoundsException e) {
  314. // expected
  315. }
  316. try {
  317. sink.write(bufArray, -1, 1);
  318. fail("should throw IndexOutOfBoundsException");
  319. } catch (IndexOutOfBoundsException e) {
  320. // expected
  321. }
  322. try {
  323. sink.write(bufArray, 0, 3);
  324. fail("should throw IndexOutOfBoundsException");
  325. } catch (IndexOutOfBoundsException e) {
  326. // expected
  327. }
  328. try {
  329. sink.write(bufArray, 0, 2);
  330. fail("should throw NullPointerException");
  331. } catch (NullPointerException e) {
  332. // expected
  333. }
  334. }
  335. public void test_write_$LByteBufferII_SourceClosed() throws IOException {
  336. ByteBuffer[] bufArray = { buffer };
  337. source.close();
  338. try {
  339. long written = sink.write(bufArray, 0, 1);
  340. fail();
  341. } catch (IOException expected) {
  342. }
  343. }
  344. public void test_write_$LByteBufferII_SinkClosed() throws IOException {
  345. ByteBuffer[] bufArray = { buffer };
  346. sink.close();
  347. try {
  348. sink.write(bufArray, 0, 1);
  349. fail();
  350. } catch (ClosedChannelException expected) {
  351. }
  352. try {
  353. sink.write(null, 0, 1);
  354. fail();
  355. } catch (NullPointerException expected) {
  356. }
  357. try {
  358. sink.write(new ByteBuffer[2], 0, -1);
  359. fail();
  360. } catch (IndexOutOfBoundsException expected) {
  361. }
  362. // write ByteBuffer[] contains null element
  363. ByteBuffer nullBuf = null;
  364. ByteBuffer[] nullBufArray = { nullBuf };
  365. try {
  366. sink.write(nullBufArray, 0, 1);
  367. fail("should throw ClosedChannelException");
  368. } catch (ClosedChannelException e) {
  369. // expected
  370. }
  371. // illegal array index
  372. try {
  373. sink.write(nullBufArray, 0, -1);
  374. fail("should throw IndexOutOfBoundsException");
  375. } catch (IndexOutOfBoundsException e) {
  376. // expected
  377. }
  378. ByteBuffer[] bufArray2 = { buffer, nullBuf };
  379. // illegal array index
  380. try {
  381. sink.write(bufArray2, 0, -1);
  382. fail("should throw IndexOutOfBoundsException");
  383. } catch (IndexOutOfBoundsException e) {
  384. // expected
  385. }
  386. try {
  387. sink.write(bufArray2, -1, 0);
  388. fail("should throw IndexOutOfBoundsException");
  389. } catch (IndexOutOfBoundsException e) {
  390. // expected
  391. }
  392. try {
  393. sink.write(bufArray2, -1, 1);
  394. fail("should throw IndexOutOfBoundsException");
  395. } catch (IndexOutOfBoundsException e) {
  396. // expected
  397. }
  398. try {
  399. sink.write(bufArray2, 0, 3);
  400. fail("should throw IndexOutOfBoundsException");
  401. } catch (IndexOutOfBoundsException e) {
  402. // expected
  403. }
  404. try {
  405. sink.write(bufArray2, 0, 2);
  406. fail("should throw ClosedChannelException");
  407. } catch (ClosedChannelException e) {
  408. // expected
  409. }
  410. }
  411. public void test_close() throws IOException {
  412. sink.close();
  413. assertFalse(sink.isOpen());
  414. }
  415. public void test_socketChannel_read_close() throws Exception {
  416. ServerSocketChannel ssc = ServerSocketChannel.open();
  417. ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(),49999));
  418. SocketChannel sc = SocketChannel.open();
  419. ByteBuffer buf = null;
  420. try{
  421. sc.write(buf);
  422. fail("should throw NPE");
  423. }catch (NullPointerException e){
  424. // expected
  425. }
  426. sc.connect(new InetSocketAddress(InetAddress.getLocalHost(),49999));
  427. SocketChannel sock = ssc.accept();
  428. ssc.close();
  429. sc.close();
  430. try{
  431. sc.write(buf);
  432. fail("should throw NPE");
  433. }catch (NullPointerException e){
  434. // expected
  435. }
  436. sock.close();
  437. }
  438. public void test_socketChannel_read_write() throws Exception {
  439. ServerSocketChannel ssc = ServerSocketChannel.open();
  440. ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(),49999));
  441. SocketChannel sc = SocketChannel.open();
  442. sc.connect(new InetSocketAddress(InetAddress.getLocalHost(),49999));
  443. SocketChannel sock = ssc.accept();
  444. ByteBuffer[] buf = {ByteBuffer.allocate(10),null};
  445. try {
  446. sc.write(buf,0,2);
  447. fail("should throw NPE");
  448. } catch (NullPointerException expected) {
  449. }
  450. ssc.close();
  451. sc.close();
  452. ByteBuffer target = ByteBuffer.allocate(10);
  453. assertEquals(-1, sock.read(target));
  454. }
  455. }