001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.IOException;
021: import java.io.PipedReader;
022: import java.io.PipedWriter;
023:
024: public class PipedWriterTest extends junit.framework.TestCase {
025:
026: static class PReader implements Runnable {
027: public PipedReader pr;
028:
029: public char[] buf = new char[10];
030:
031: public PReader(PipedWriter pw) {
032: try {
033: pr = new PipedReader(pw);
034: } catch (IOException e) {
035: System.out.println("Exception setting up reader: "
036: + e.toString());
037: }
038: }
039:
040: public PReader(PipedReader pr) {
041: this .pr = pr;
042: }
043:
044: public void run() {
045: try {
046: int r = 0;
047: for (int i = 0; i < buf.length; i++) {
048: r = pr.read();
049: if (r == -1)
050: break;
051: buf[i] = (char) r;
052: }
053: } catch (Exception e) {
054: System.out.println("Exception reading ("
055: + Thread.currentThread().getName() + "): "
056: + e.toString());
057: }
058: }
059: }
060:
061: Thread rdrThread;
062:
063: PReader reader;
064:
065: PipedWriter pw;
066:
067: /**
068: * @tests java.io.PipedWriter#PipedWriter()
069: */
070: public void test_Constructor() {
071: // Test for method java.io.PipedWriter()
072: // Used in tests
073: }
074:
075: /**
076: * @tests java.io.PipedWriter#PipedWriter(java.io.PipedReader)
077: */
078: public void test_ConstructorLjava_io_PipedReader() throws Exception {
079: // Test for method java.io.PipedWriter(java.io.PipedReader)
080: char[] buf = new char[10];
081: "HelloWorld".getChars(0, 10, buf, 0);
082: PipedReader rd = new PipedReader();
083: pw = new PipedWriter(rd);
084: rdrThread = new Thread(reader = new PReader(rd),
085: "Constructor(Reader)");
086: rdrThread.start();
087: pw.write(buf);
088: pw.close();
089: rdrThread.join(500);
090: assertEquals("Failed to construct writer", "HelloWorld",
091: new String(reader.buf));
092: }
093:
094: /**
095: * @tests java.io.PipedWriter#close()
096: */
097: public void test_close() throws Exception {
098: // Test for method void java.io.PipedWriter.close()
099: char[] buf = new char[10];
100: "HelloWorld".getChars(0, 10, buf, 0);
101: PipedReader rd = new PipedReader();
102: pw = new PipedWriter(rd);
103: reader = new PReader(rd);
104: pw.close();
105: try {
106: pw.write(buf);
107: fail("Should have thrown exception when attempting to write to closed writer.");
108: } catch (Exception e) {
109: // correct
110: }
111: }
112:
113: /**
114: * @tests java.io.PipedWriter#connect(java.io.PipedReader)
115: */
116: public void test_connectLjava_io_PipedReader() throws Exception {
117: // Test for method void java.io.PipedWriter.connect(java.io.PipedReader)
118: char[] buf = new char[10];
119: "HelloWorld".getChars(0, 10, buf, 0);
120: PipedReader rd = new PipedReader();
121: pw = new PipedWriter();
122: pw.connect(rd);
123: rdrThread = new Thread(reader = new PReader(rd), "connect");
124: rdrThread.start();
125: pw.write(buf);
126: pw.close();
127: rdrThread.join(500);
128: assertEquals("Failed to write correct chars", "HelloWorld",
129: new String(reader.buf));
130: }
131:
132: /**
133: * @tests java.io.PipedWriter#flush()
134: */
135: public void test_flush() throws Exception {
136: // Test for method void java.io.PipedWriter.flush()
137: char[] buf = new char[10];
138: "HelloWorld".getChars(0, 10, buf, 0);
139: pw = new PipedWriter();
140: rdrThread = new Thread(reader = new PReader(pw), "flush");
141: rdrThread.start();
142: pw.write(buf);
143: pw.flush();
144: rdrThread.join(700);
145: assertEquals("Failed to flush chars", "HelloWorld", new String(
146: reader.buf));
147: }
148:
149: /**
150: * @tests java.io.PipedWriter#write(char[], int, int)
151: */
152: public void test_write$CII() throws Exception {
153: // Test for method void java.io.PipedWriter.write(char [], int, int)
154: char[] buf = new char[10];
155: "HelloWorld".getChars(0, 10, buf, 0);
156: pw = new PipedWriter();
157: rdrThread = new Thread(reader = new PReader(pw), "writeCII");
158: rdrThread.start();
159: pw.write(buf, 0, 10);
160: pw.close();
161: rdrThread.join(1000);
162: assertEquals("Failed to write correct chars", "HelloWorld",
163: new String(reader.buf));
164: }
165:
166: /**
167: * @tests java.io.PipedWriter#write(char[], int, int) Regression for
168: * HARMONY-387
169: */
170: public void test_write$CII_2() throws IOException {
171: PipedReader pr = new PipedReader();
172: PipedWriter obj = null;
173: try {
174: obj = new java.io.PipedWriter(pr);
175: obj.write(new char[0], (int) 0, (int) -1);
176: fail("IndexOutOfBoundsException expected");
177: } catch (IndexOutOfBoundsException t) {
178: assertEquals(
179: "IndexOutOfBoundsException rather than a subclass expected",
180: IndexOutOfBoundsException.class, t.getClass());
181: }
182: }
183:
184: /**
185: * @tests java.io.PipedWriter#write(char[], int, int)
186: */
187: public void test_write$CII_3() throws IOException {
188: PipedReader pr = new PipedReader();
189: PipedWriter obj = null;
190: try {
191: obj = new java.io.PipedWriter(pr);
192: obj.write(new char[0], (int) -1, (int) 0);
193: fail("IndexOutOfBoundsException expected");
194: } catch (ArrayIndexOutOfBoundsException t) {
195: fail("IndexOutOfBoundsException expected");
196: } catch (IndexOutOfBoundsException t) {
197: }
198: }
199:
200: /**
201: * @tests java.io.PipedWriter#write(char[], int, int)
202: */
203: public void test_write$CII_4() throws IOException {
204: PipedReader pr = new PipedReader();
205: PipedWriter obj = null;
206: try {
207: obj = new java.io.PipedWriter(pr);
208: obj.write(new char[0], (int) -1, (int) -1);
209: fail("IndexOutOfBoundsException expected");
210: } catch (ArrayIndexOutOfBoundsException t) {
211: fail("IndexOutOfBoundsException expected");
212: } catch (IndexOutOfBoundsException t) {
213: }
214: }
215:
216: /**
217: * @tests java.io.PipedWriter#write(char[], int, int)
218: */
219: public void test_write$CII_5() throws IOException {
220: PipedReader pr = new PipedReader();
221: PipedWriter obj = null;
222: try {
223: obj = new PipedWriter(pr);
224: obj.write((char[]) null, (int) -1, (int) 0);
225: fail("NullPointerException expected");
226: } catch (IndexOutOfBoundsException t) {
227: fail("NullPointerException expected");
228: } catch (NullPointerException t) {
229: }
230: }
231:
232: /**
233: * @tests java.io.PipedWriter#write(char[], int, int)
234: */
235: public void test_write$CII_6() throws IOException {
236: PipedReader pr = new PipedReader();
237: PipedWriter obj = null;
238: try {
239: obj = new PipedWriter(pr);
240: obj.write((char[]) null, (int) -1, (int) -1);
241: fail("NullPointerException expected");
242: } catch (IndexOutOfBoundsException t) {
243: fail("NullPointerException expected");
244: } catch (NullPointerException t) {
245: }
246: }
247:
248: /**
249: * @tests java.io.PipedWriter#write(char[], int, int)
250: */
251: public void test_write$CII_notConnected() throws IOException {
252: // Regression test for Harmony-2404
253: // create not connected pipe
254: PipedWriter obj = new PipedWriter();
255:
256: // char array is null
257: try {
258: obj.write((char[]) null, 0, 1);
259: fail("IOException expected");
260: } catch (IOException ioe) {
261: // expected
262: }
263:
264: // negative offset
265: try {
266: obj.write(new char[] { 1 }, -10, 1);
267: fail("IOException expected");
268: } catch (IOException ioe) {
269: // expected
270: }
271:
272: // wrong offset
273: try {
274: obj.write(new char[] { 1 }, 10, 1);
275: fail("IOException expected");
276: } catch (IOException ioe) {
277: // expected
278: }
279:
280: // negative length
281: try {
282: obj.write(new char[] { 1 }, 0, -10);
283: fail("IOException expected");
284: } catch (IOException ioe) {
285: // expected
286: }
287:
288: // all valid params
289: try {
290: obj.write(new char[] { 1, 1 }, 0, 1);
291: fail("IOException expected");
292: } catch (IOException ioe) {
293: // expected
294: }
295: }
296:
297: /**
298: * @tests java.io.PipedWriter#write(int)
299: */
300: public void test_write_I_MultiThread() throws IOException {
301: final PipedReader pr = new PipedReader();
302: final PipedWriter pw = new PipedWriter();
303: // test if writer recognizes dead reader
304: pr.connect(pw);
305:
306: class WriteRunnable implements Runnable {
307: boolean pass = false;
308: boolean readerAlive = true;
309:
310: public void run() {
311: try {
312: pw.write(1);
313: while (readerAlive) {
314: // wait the reader thread dead
315: }
316: try {
317: // should throw exception since reader thread
318: // is now dead
319: pw.write(1);
320: } catch (IOException e) {
321: pass = true;
322: }
323: } catch (IOException e) {
324: //ignore
325: }
326: }
327: }
328: WriteRunnable writeRunnable = new WriteRunnable();
329: Thread writeThread = new Thread(writeRunnable);
330: class ReadRunnable implements Runnable {
331: boolean pass;
332:
333: public void run() {
334: try {
335: pr.read();
336: pass = true;
337: } catch (IOException e) {
338: //ignore
339: }
340: }
341: }
342: ReadRunnable readRunnable = new ReadRunnable();
343: Thread readThread = new Thread(readRunnable);
344: writeThread.start();
345: readThread.start();
346: while (readThread.isAlive()) {
347: //wait the reader thread dead
348: }
349: writeRunnable.readerAlive = false;
350: assertTrue("reader thread failed to read", readRunnable.pass);
351: while (writeThread.isAlive()) {
352: //wait the writer thread dead
353: }
354: assertTrue("writer thread failed to recognize dead reader",
355: writeRunnable.pass);
356: }
357:
358: /**
359: * @tests java.io.PipedWriter#write(char[],int,int)
360: */
361: public void test_write_$CII_MultiThread() throws Exception {
362: final PipedReader pr = new PipedReader();
363: final PipedWriter pw = new PipedWriter();
364:
365: // test if writer recognizes dead reader
366: pr.connect(pw);
367:
368: class WriteRunnable implements Runnable {
369: boolean pass = false;
370:
371: boolean readerAlive = true;
372:
373: public void run() {
374: try {
375: pw.write(1);
376: while (readerAlive) {
377: // wait the reader thread dead
378: }
379: try {
380: // should throw exception since reader thread
381: // is now dead
382: char[] buf = new char[10];
383: pw.write(buf, 0, 10);
384: } catch (IOException e) {
385: pass = true;
386: }
387: } catch (IOException e) {
388: //ignore
389: }
390: }
391: }
392: WriteRunnable writeRunnable = new WriteRunnable();
393: Thread writeThread = new Thread(writeRunnable);
394: class ReadRunnable implements Runnable {
395: boolean pass;
396:
397: public void run() {
398: try {
399: pr.read();
400: pass = true;
401: } catch (IOException e) {
402: //ignore
403: }
404: }
405: }
406: ReadRunnable readRunnable = new ReadRunnable();
407: Thread readThread = new Thread(readRunnable);
408: writeThread.start();
409: readThread.start();
410: while (readThread.isAlive()) {
411: //wait the reader thread dead
412: }
413: writeRunnable.readerAlive = false;
414: assertTrue("reader thread failed to read", readRunnable.pass);
415: while (writeThread.isAlive()) {
416: //wait the writer thread dead
417: }
418: assertTrue("writer thread failed to recognize dead reader",
419: writeRunnable.pass);
420: }
421:
422: /**
423: * @tests java.io.PipedWriter#write(int)
424: */
425: public void test_writeI() throws Exception {
426: // Test for method void java.io.PipedWriter.write(int)
427:
428: pw = new PipedWriter();
429: rdrThread = new Thread(reader = new PReader(pw), "writeI");
430: rdrThread.start();
431: pw.write(1);
432: pw.write(2);
433: pw.write(3);
434: pw.close();
435: rdrThread.join(1000);
436: assertTrue("Failed to write correct chars: "
437: + (int) reader.buf[0] + " " + (int) reader.buf[1] + " "
438: + (int) reader.buf[2], reader.buf[0] == 1
439: && reader.buf[1] == 2 && reader.buf[2] == 3);
440: }
441:
442: /**
443: * Tears down the fixture, for example, close a network connection. This
444: * method is called after a test is executed.
445: */
446: protected void tearDown() throws Exception {
447: try {
448: if (rdrThread != null) {
449: rdrThread.interrupt();
450: }
451: } catch (Exception ignore) {
452: }
453: try {
454: if (pw != null) {
455: pw.close();
456: }
457: } catch (Exception ignore) {
458: }
459: super.tearDown();
460: }
461: }
|