PageRenderTime 31ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/external/apache-harmony/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedWriterTest.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Java | 472 lines | 320 code | 43 blank | 109 comment | 18 complexity | e207494e1924365f85c2c91aaca26b0d 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.luni.tests.java.io;
  18. import java.io.IOException;
  19. import java.io.PipedReader;
  20. import java.io.PipedWriter;
  21. public class PipedWriterTest extends junit.framework.TestCase {
  22. static class PReader implements Runnable {
  23. public PipedReader pr;
  24. public char[] buf = new char[10];
  25. public PReader(PipedWriter pw) {
  26. try {
  27. pr = new PipedReader(pw);
  28. } catch (IOException e) {
  29. System.out.println("Exception setting up reader: "
  30. + e.toString());
  31. }
  32. }
  33. public PReader(PipedReader pr) {
  34. this.pr = pr;
  35. }
  36. public void run() {
  37. try {
  38. int r = 0;
  39. for (int i = 0; i < buf.length; i++) {
  40. r = pr.read();
  41. if (r == -1)
  42. break;
  43. buf[i] = (char) r;
  44. }
  45. } catch (Exception e) {
  46. System.out.println("Exception reading ("
  47. + Thread.currentThread().getName() + "): "
  48. + e.toString());
  49. }
  50. }
  51. }
  52. Thread rdrThread;
  53. PReader reader;
  54. PipedWriter pw;
  55. /**
  56. * @tests java.io.PipedWriter#PipedWriter()
  57. */
  58. public void test_Constructor() {
  59. // Test for method java.io.PipedWriter()
  60. // Used in tests
  61. }
  62. /**
  63. * @tests java.io.PipedWriter#PipedWriter(java.io.PipedReader)
  64. */
  65. public void test_ConstructorLjava_io_PipedReader() throws Exception {
  66. // Test for method java.io.PipedWriter(java.io.PipedReader)
  67. char[] buf = new char[10];
  68. "HelloWorld".getChars(0, 10, buf, 0);
  69. PipedReader rd = new PipedReader();
  70. pw = new PipedWriter(rd);
  71. rdrThread = new Thread(reader = new PReader(rd), "Constructor(Reader)");
  72. rdrThread.start();
  73. pw.write(buf);
  74. pw.close();
  75. rdrThread.join(500);
  76. assertEquals("Failed to construct writer", "HelloWorld", new String(
  77. reader.buf));
  78. }
  79. /**
  80. * @tests java.io.PipedWriter#close()
  81. */
  82. public void test_close() throws Exception {
  83. // Test for method void java.io.PipedWriter.close()
  84. char[] buf = new char[10];
  85. "HelloWorld".getChars(0, 10, buf, 0);
  86. PipedReader rd = new PipedReader();
  87. pw = new PipedWriter(rd);
  88. reader = new PReader(rd);
  89. pw.close();
  90. try {
  91. pw.write(buf);
  92. fail("Should have thrown exception when attempting to write to closed writer.");
  93. } catch (Exception e) {
  94. // correct
  95. }
  96. }
  97. /**
  98. * @tests java.io.PipedWriter#connect(java.io.PipedReader)
  99. */
  100. public void test_connectLjava_io_PipedReader() throws Exception {
  101. // Test for method void java.io.PipedWriter.connect(java.io.PipedReader)
  102. char[] buf = new char[10];
  103. "HelloWorld".getChars(0, 10, buf, 0);
  104. PipedReader rd = new PipedReader();
  105. pw = new PipedWriter();
  106. pw.connect(rd);
  107. rdrThread = new Thread(reader = new PReader(rd), "connect");
  108. rdrThread.start();
  109. pw.write(buf);
  110. pw.close();
  111. rdrThread.join(500);
  112. assertEquals("Failed to write correct chars", "HelloWorld", new String(
  113. reader.buf));
  114. }
  115. /**
  116. * @tests java.io.PipedWriter#flush()
  117. */
  118. public void test_flush() throws Exception {
  119. // Test for method void java.io.PipedWriter.flush()
  120. char[] buf = new char[10];
  121. "HelloWorld".getChars(0, 10, buf, 0);
  122. pw = new PipedWriter();
  123. rdrThread = new Thread(reader = new PReader(pw), "flush");
  124. rdrThread.start();
  125. pw.write(buf);
  126. pw.flush();
  127. rdrThread.join(700);
  128. assertEquals("Failed to flush chars", "HelloWorld", new String(
  129. reader.buf));
  130. }
  131. /**
  132. * @tests java.io.PipedWriter#flush()
  133. * Regression HARMONY-6293
  134. */
  135. public void test_flushAfterClose() throws Exception {
  136. PipedReader pr = new PipedReader();
  137. pw = new PipedWriter(pr);
  138. pw.close();
  139. try {
  140. pw.flush();
  141. fail("should throw IOException");
  142. } catch (IOException e) {
  143. // expected
  144. }
  145. pr = new PipedReader();
  146. pw = new PipedWriter(pr);
  147. pr.close();
  148. try {
  149. pw.flush();
  150. fail("should throw IOException");
  151. } catch (IOException e) {
  152. // expected
  153. }
  154. }
  155. /**
  156. * @tests java.io.PipedWriter#write(char[], int, int)
  157. */
  158. public void test_write$CII() throws Exception {
  159. // Test for method void java.io.PipedWriter.write(char [], int, int)
  160. char[] buf = new char[10];
  161. "HelloWorld".getChars(0, 10, buf, 0);
  162. pw = new PipedWriter();
  163. rdrThread = new Thread(reader = new PReader(pw), "writeCII");
  164. rdrThread.start();
  165. pw.write(buf, 0, 10);
  166. pw.close();
  167. rdrThread.join(1000);
  168. assertEquals("Failed to write correct chars", "HelloWorld", new String(
  169. reader.buf));
  170. }
  171. /**
  172. * @tests java.io.PipedWriter#write(char[], int, int) Regression for
  173. * HARMONY-387
  174. */
  175. public void test_write$CII_2() throws IOException {
  176. PipedReader pr = new PipedReader();
  177. PipedWriter obj = null;
  178. try {
  179. obj = new java.io.PipedWriter(pr);
  180. obj.write(new char[0], (int) 0, (int) -1);
  181. fail("IndexOutOfBoundsException expected");
  182. } catch (IndexOutOfBoundsException expected) {
  183. }
  184. }
  185. /**
  186. * @tests java.io.PipedWriter#write(char[], int, int)
  187. */
  188. public void test_write$CII_3() throws IOException {
  189. PipedReader pr = new PipedReader();
  190. PipedWriter obj = null;
  191. try {
  192. obj = new java.io.PipedWriter(pr);
  193. obj.write(new char[0], (int) -1, (int) 0);
  194. fail();
  195. } catch (IndexOutOfBoundsException expected) {}
  196. }
  197. /**
  198. * @tests java.io.PipedWriter#write(char[], int, int)
  199. */
  200. public void test_write$CII_4() throws IOException {
  201. PipedReader pr = new PipedReader();
  202. PipedWriter obj = null;
  203. try {
  204. obj = new java.io.PipedWriter(pr);
  205. obj.write(new char[0], (int) -1, (int) -1);
  206. fail();
  207. } catch (IndexOutOfBoundsException expected) {}
  208. }
  209. /**
  210. * @tests java.io.PipedWriter#write(char[], int, int)
  211. */
  212. public void test_write$CII_5() throws IOException {
  213. PipedReader pr = new PipedReader();
  214. PipedWriter obj = null;
  215. try {
  216. obj = new PipedWriter(pr);
  217. obj.write((char[]) null, (int) -1, (int) 0);
  218. fail("NullPointerException expected");
  219. } catch (IndexOutOfBoundsException t) {
  220. fail("NullPointerException expected");
  221. } catch (NullPointerException t) {}
  222. }
  223. /**
  224. * @tests java.io.PipedWriter#write(char[], int, int)
  225. */
  226. public void test_write$CII_6() throws IOException {
  227. PipedReader pr = new PipedReader();
  228. PipedWriter obj = null;
  229. try {
  230. obj = new PipedWriter(pr);
  231. obj.write((char[]) null, (int) -1, (int) -1);
  232. fail("NullPointerException expected");
  233. } catch (IndexOutOfBoundsException t) {
  234. fail("NullPointerException expected");
  235. } catch (NullPointerException t) {}
  236. }
  237. /**
  238. * @tests java.io.PipedWriter#write(char[], int, int)
  239. */
  240. public void test_write$CII_notConnected() throws IOException {
  241. // Regression test for Harmony-2404
  242. // create not connected pipe
  243. PipedWriter obj = new PipedWriter();
  244. // char array is null
  245. try {
  246. obj.write((char[]) null, 0, 1);
  247. fail("IOException expected");
  248. } catch (IOException ioe) {
  249. // expected
  250. }
  251. // negative offset
  252. try {
  253. obj.write( new char[] { 1 }, -10, 1);
  254. fail("IOException expected");
  255. } catch (IOException ioe) {
  256. // expected
  257. }
  258. // wrong offset
  259. try {
  260. obj.write( new char[] { 1 }, 10, 1);
  261. fail("IOException expected");
  262. } catch (IOException ioe) {
  263. // expected
  264. }
  265. // negative length
  266. try {
  267. obj.write( new char[] { 1 }, 0, -10);
  268. fail("IOException expected");
  269. } catch (IOException ioe) {
  270. // expected
  271. }
  272. // all valid params
  273. try {
  274. obj.write( new char[] { 1, 1 }, 0, 1);
  275. fail("IOException expected");
  276. } catch (IOException ioe) {
  277. // expected
  278. }
  279. }
  280. /**
  281. * @tests java.io.PipedWriter#write(int)
  282. */
  283. public void test_write_I_MultiThread() throws IOException {
  284. final PipedReader pr = new PipedReader();
  285. final PipedWriter pw = new PipedWriter();
  286. // test if writer recognizes dead reader
  287. pr.connect(pw);
  288. class WriteRunnable implements Runnable {
  289. boolean pass = false;
  290. volatile boolean readerAlive = true;
  291. public void run() {
  292. try {
  293. pw.write(1);
  294. while (readerAlive) {
  295. // wait the reader thread dead
  296. }
  297. try {
  298. // should throw exception since reader thread
  299. // is now dead
  300. pw.write(1);
  301. } catch (IOException e) {
  302. pass = true;
  303. }
  304. } catch (IOException e) {
  305. //ignore
  306. }
  307. }
  308. }
  309. WriteRunnable writeRunnable = new WriteRunnable();
  310. Thread writeThread = new Thread(writeRunnable);
  311. class ReadRunnable implements Runnable {
  312. boolean pass;
  313. public void run() {
  314. try {
  315. pr.read();
  316. pass = true;
  317. } catch (IOException e) {
  318. //ignore
  319. }
  320. }
  321. }
  322. ReadRunnable readRunnable = new ReadRunnable();
  323. Thread readThread = new Thread(readRunnable);
  324. writeThread.start();
  325. readThread.start();
  326. while (readThread.isAlive()) {
  327. //wait the reader thread dead
  328. }
  329. writeRunnable.readerAlive = false;
  330. assertTrue("reader thread failed to read", readRunnable.pass);
  331. while (writeThread.isAlive()) {
  332. //wait the writer thread dead
  333. }
  334. assertTrue("writer thread failed to recognize dead reader",
  335. writeRunnable.pass);
  336. }
  337. /**
  338. * @tests java.io.PipedWriter#write(char[],int,int)
  339. */
  340. public void test_write_$CII_MultiThread() throws Exception {
  341. final PipedReader pr = new PipedReader();
  342. final PipedWriter pw = new PipedWriter();
  343. // test if writer recognizes dead reader
  344. pr.connect(pw);
  345. class WriteRunnable implements Runnable {
  346. boolean pass = false;
  347. volatile boolean readerAlive = true;
  348. public void run() {
  349. try {
  350. pw.write(1);
  351. while (readerAlive) {
  352. // wait the reader thread dead
  353. }
  354. try {
  355. // should throw exception since reader thread
  356. // is now dead
  357. char[] buf = new char[10];
  358. pw.write(buf, 0, 10);
  359. } catch (IOException e) {
  360. pass = true;
  361. }
  362. } catch (IOException e) {
  363. //ignore
  364. }
  365. }
  366. }
  367. WriteRunnable writeRunnable = new WriteRunnable();
  368. Thread writeThread = new Thread(writeRunnable);
  369. class ReadRunnable implements Runnable {
  370. boolean pass;
  371. public void run() {
  372. try {
  373. pr.read();
  374. pass = true;
  375. } catch (IOException e) {
  376. //ignore
  377. }
  378. }
  379. }
  380. ReadRunnable readRunnable = new ReadRunnable();
  381. Thread readThread = new Thread(readRunnable);
  382. writeThread.start();
  383. readThread.start();
  384. while (readThread.isAlive()) {
  385. //wait the reader thread dead
  386. }
  387. writeRunnable.readerAlive = false;
  388. assertTrue("reader thread failed to read", readRunnable.pass);
  389. while (writeThread.isAlive()) {
  390. //wait the writer thread dead
  391. }
  392. assertTrue("writer thread failed to recognize dead reader",
  393. writeRunnable.pass);
  394. }
  395. /**
  396. * @tests java.io.PipedWriter#write(int)
  397. */
  398. public void test_writeI() throws Exception {
  399. // Test for method void java.io.PipedWriter.write(int)
  400. pw = new PipedWriter();
  401. rdrThread = new Thread(reader = new PReader(pw), "writeI");
  402. rdrThread.start();
  403. pw.write(1);
  404. pw.write(2);
  405. pw.write(3);
  406. pw.close();
  407. rdrThread.join(1000);
  408. assertTrue("Failed to write correct chars: " + (int) reader.buf[0]
  409. + " " + (int) reader.buf[1] + " " + (int) reader.buf[2],
  410. reader.buf[0] == 1 && reader.buf[1] == 2 && reader.buf[2] == 3);
  411. }
  412. /**
  413. * Tears down the fixture, for example, close a network connection. This
  414. * method is called after a test is executed.
  415. */
  416. protected void tearDown() throws Exception {
  417. try {
  418. if (rdrThread != null) {
  419. rdrThread.interrupt();
  420. }
  421. } catch (Exception ignore) {}
  422. try {
  423. if (pw != null) {
  424. pw.close();
  425. }
  426. } catch (Exception ignore) {}
  427. super.tearDown();
  428. }
  429. }