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: import junit.framework.TestCase;
025:
026: public class PipedReaderTest extends TestCase {
027:
028: static class PWriter implements Runnable {
029: public PipedWriter pw;
030:
031: public PWriter(PipedReader reader) {
032: try {
033: pw = new PipedWriter(reader);
034: } catch (Exception e) {
035: System.out.println("Couldn't create writer");
036: }
037: }
038:
039: public PWriter() {
040: pw = new PipedWriter();
041: }
042:
043: public void run() {
044: try {
045: char[] c = new char[11];
046: "Hello World".getChars(0, 11, c, 0);
047: pw.write(c);
048: Thread.sleep(10000);
049: } catch (InterruptedException e) {
050: } catch (Exception e) {
051: System.out.println("Exception occurred: "
052: + e.toString());
053: }
054: }
055: }
056:
057: PipedReader preader;
058:
059: PWriter pwriter;
060:
061: Thread t;
062:
063: /**
064: * @tests java.io.PipedReader#PipedReader()
065: */
066: public void test_Constructor() {
067: // Used in test
068: }
069:
070: /**
071: * @tests java.io.PipedReader#PipedReader(java.io.PipedWriter)
072: */
073: public void test_ConstructorLjava_io_PipedWriter()
074: throws IOException {
075: preader = new PipedReader(new PipedWriter());
076: }
077:
078: /**
079: * @tests java.io.PipedReader#close()
080: */
081: public void test_close() throws Exception {
082: char[] c = null;
083: preader = new PipedReader();
084: t = new Thread(new PWriter(preader), "");
085: t.start();
086: Thread.sleep(500); // Allow writer to start
087: c = new char[11];
088: preader.read(c, 0, 11);
089: preader.close();
090: assertEquals("Read incorrect chars", "Hello World", new String(
091: c));
092: }
093:
094: /**
095: * @tests java.io.PipedReader#connect(java.io.PipedWriter)
096: */
097: public void test_connectLjava_io_PipedWriter() throws Exception {
098: char[] c = null;
099:
100: preader = new PipedReader();
101: t = new Thread(pwriter = new PWriter(), "");
102: preader.connect(pwriter.pw);
103: t.start();
104: Thread.sleep(500); // Allow writer to start
105: c = new char[11];
106: preader.read(c, 0, 11);
107:
108: assertEquals("Read incorrect chars", "Hello World", new String(
109: c));
110: try {
111: preader.connect(pwriter.pw);
112: fail("Failed to throw exception connecting to pre-connected reader");
113: } catch (IOException e) {
114: // Expected
115: }
116: }
117:
118: /**
119: * @tests java.io.PipedReader#read()
120: */
121: public void test_read() throws Exception {
122: char[] c = null;
123: preader = new PipedReader();
124: t = new Thread(new PWriter(preader), "");
125: t.start();
126: Thread.sleep(500); // Allow writer to start
127: c = new char[11];
128: for (int i = 0; i < c.length; i++) {
129: c[i] = (char) preader.read();
130: }
131: assertEquals("Read incorrect chars", "Hello World", new String(
132: c));
133: }
134:
135: /**
136: * @tests java.io.PipedReader#read(char[], int, int)
137: */
138: public void test_read$CII() throws Exception {
139: char[] c = null;
140: preader = new PipedReader();
141: t = new Thread(new PWriter(preader), "");
142: t.start();
143: Thread.sleep(500); // Allow writer to start
144: c = new char[11];
145: int n = 0;
146: int x = n;
147: while (x < 11) {
148: n = preader.read(c, x, 11 - x);
149: x = x + n;
150: }
151: assertEquals("Read incorrect chars", "Hello World", new String(
152: c));
153: try {
154: preader.close();
155: preader.read(c, 8, 7);
156: fail("Failed to throw exception reading from closed reader");
157: } catch (IOException e) {
158: // Expected
159: }
160: }
161:
162: /**
163: * @tests java.io.PipedReader#read(char[], int, int)
164: */
165: public void test_read$CII_2() throws IOException {
166: // Regression for HARMONY-387
167: PipedWriter pw = new PipedWriter();
168: PipedReader obj = null;
169: try {
170: obj = new PipedReader(pw);
171: obj.read(new char[0], (int) 0, (int) -1);
172: fail("IndexOutOfBoundsException expected");
173: } catch (IndexOutOfBoundsException t) {
174: assertEquals(
175: "IndexOutOfBoundsException rather than a subclass expected",
176: IndexOutOfBoundsException.class, t.getClass());
177: }
178: }
179:
180: /**
181: * @tests java.io.PipedReader#read(char[], int, int)
182: */
183: public void test_read$CII_3() throws IOException {
184: PipedWriter pw = new PipedWriter();
185: PipedReader obj = null;
186: try {
187: obj = new PipedReader(pw);
188: obj.read(new char[0], (int) -1, (int) 0);
189: fail("IndexOutOfBoundsException expected");
190: } catch (ArrayIndexOutOfBoundsException t) {
191: fail("IndexOutOfBoundsException expected");
192: } catch (IndexOutOfBoundsException t) {
193: // Expected
194: }
195: }
196:
197: /**
198: * @tests java.io.PipedReader#read(char[], int, int)
199: */
200: public void test_read$CII_4() throws IOException {
201: PipedWriter pw = new PipedWriter();
202: PipedReader obj = null;
203: try {
204: obj = new PipedReader(pw);
205: obj.read(new char[0], (int) -1, (int) -1);
206: fail("IndexOutOfBoundsException expected");
207: } catch (ArrayIndexOutOfBoundsException t) {
208: fail("IndexOutOfBoundsException expected");
209: } catch (IndexOutOfBoundsException t) {
210: // Expected
211: }
212: }
213:
214: /**
215: * @tests java.io.PipedReader#read(char[], int, int)
216: */
217: public void test_read_$CII_IOException() throws IOException {
218: PipedWriter pw = new PipedWriter();
219: PipedReader pr = new PipedReader(pw);
220: char[] buf = null;
221: pr.close();
222: try {
223: pr.read(buf, 0, 10);
224: fail("Should throws IOException"); //$NON-NLS-1$
225: } catch (IOException e) {
226: // expected
227: } finally {
228: pw = null;
229: pr = null;
230: }
231:
232: pr = new PipedReader();
233: buf = null;
234: pr.close();
235: try {
236: pr.read(buf, 0, 10);
237: fail("Should throws IOException"); //$NON-NLS-1$
238: } catch (IOException e) {
239: // expected
240: } finally {
241: pr = null;
242: }
243:
244: pw = new PipedWriter();
245: pr = new PipedReader(pw);
246: buf = new char[10];
247: pr.close();
248: try {
249: pr.read(buf, -1, 0);
250: fail("Should throws IOException"); //$NON-NLS-1$
251: } catch (IOException e) {
252: // expected
253: } finally {
254: pw = null;
255: pr = null;
256: }
257:
258: pw = new PipedWriter();
259: pr = new PipedReader(pw);
260: buf = new char[10];
261: pr.close();
262: try {
263: pr.read(buf, 0, -1);
264: fail("Should throws IOException"); //$NON-NLS-1$
265: } catch (IOException e) {
266: // expected
267: } finally {
268: pw = null;
269: pr = null;
270: }
271:
272: pw = new PipedWriter();
273: pr = new PipedReader(pw);
274: buf = new char[10];
275: pr.close();
276: try {
277: pr.read(buf, 1, 10);
278: fail("Should throws IOException"); //$NON-NLS-1$
279: } catch (IOException e) {
280: // expected
281: } finally {
282: pw = null;
283: pr = null;
284: }
285:
286: pw = new PipedWriter();
287: pr = new PipedReader(pw);
288: pr.close();
289: try {
290: pr.read(new char[0], -1, -1);
291: fail("should throw IOException"); //$NON-NLS-1$
292: } catch (IOException e) {
293: // expected
294: } finally {
295: pw = null;
296: pr = null;
297: }
298:
299: pw = new PipedWriter();
300: pr = new PipedReader(pw);
301: pr.close();
302: try {
303: pr.read(null, 0, 1);
304: fail("should throw IOException"); //$NON-NLS-1$
305: } catch (IOException e) {
306: // expected
307: } finally {
308: pw = null;
309: pr = null;
310: }
311:
312: pw = new PipedWriter();
313: pr = new PipedReader(pw);
314: try {
315: pr.read(null, -1, 1);
316: fail("should throw IndexOutOfBoundsException"); //$NON-NLS-1$
317: } catch (IndexOutOfBoundsException e) {
318: // expected
319: } finally {
320: pw = null;
321: pr = null;
322: }
323:
324: pw = new PipedWriter();
325: pr = new PipedReader(pw);
326: try {
327: pr.read(null, 0, -1);
328: fail("should throw NullPointerException"); //$NON-NLS-1$
329: } catch (NullPointerException e) {
330: // expected
331: } finally {
332: pw = null;
333: pr = null;
334: }
335:
336: pw = new PipedWriter();
337: pr = new PipedReader(pw);
338: try {
339: pr.read(new char[10], 11, 0);
340: fail("should throw IndexOutOfBoundsException"); //$NON-NLS-1$
341: } catch (IndexOutOfBoundsException e) {
342: // expected
343: } finally {
344: pw = null;
345: pr = null;
346: }
347:
348: pw = new PipedWriter();
349: pr = new PipedReader(pw);
350: try {
351: pr.read(null, 1, 0);
352: fail("should throw NullPointerException"); //$NON-NLS-1$
353: } catch (NullPointerException e) {
354: // expected
355: } finally {
356: pw = null;
357: pr = null;
358: }
359: }
360:
361: /**
362: * @tests java.io.PipedReader#ready()
363: */
364: public void test_ready() throws Exception {
365: char[] c = null;
366: preader = new PipedReader();
367: t = new Thread(new PWriter(preader), "");
368: t.start();
369: Thread.sleep(500); // Allow writer to start
370: assertTrue("Reader should be ready", preader.ready());
371: c = new char[11];
372: for (int i = 0; i < c.length; i++)
373: c[i] = (char) preader.read();
374: assertFalse(
375: "Reader should not be ready after reading all chars",
376: preader.ready());
377: }
378:
379: /**
380: * Tears down the fixture, for example, close a network connection. This
381: * method is called after a test is executed.
382: */
383: protected void tearDown() throws Exception {
384: if (t != null) {
385: t.interrupt();
386: }
387: super.tearDown();
388: }
389: }
|