PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/harmony-tests/src/test/java/org/apache/harmony/tests/java/io/PipedWriterTest.java

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