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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package tests.api.java.io;
20 import java.io.IOException;
21 import java.io.PipedReader;
22 import java.io.PipedWriter;
24 import junit.framework.TestCase;
26 public class PipedReaderTest extends TestCase {
28 static class PWriter implements Runnable {
29 public PipedWriter pw;
31 public PWriter(PipedReader reader) {
33 pw = new PipedWriter(reader);
34 } catch (Exception e) {
35 System.out.println("Couldn't create writer");
40 pw = new PipedWriter();
45 char[] c = new char[11];
46 "Hello World".getChars(0, 11, c, 0);
49 } catch (InterruptedException e) {
50 } catch (Exception e) {
51 System.out.println("Exception occurred: " + e.toString());
63 * @tests java.io.PipedReader#PipedReader()
65 public void test_Constructor() {
70 * @tests java.io.PipedReader#PipedReader(java.io.PipedWriter)
72 public void test_ConstructorLjava_io_PipedWriter() throws IOException {
73 preader = new PipedReader(new PipedWriter());
77 * @tests java.io.PipedReader#PipedReader(java.io.PipedWriter,
81 public void test_Constructor_LPipedWriter_I() throws Exception {
82 // Test for method java.io.PipedReader(java.io.PipedWriter,
85 preader = new PipedReader(null, -1);
86 fail("Should throw IllegalArgumentException");
87 } catch (IllegalArgumentException e) {
92 preader = new PipedReader(null, 0);
93 fail("Should throw IllegalArgumentException");
94 } catch (IllegalArgumentException e) {
100 * @tests java.io.PipedReader#PipedReader(int)
103 public void test_Constructor_I() throws Exception {
104 // Test for method java.io.PipedReader(int)
106 preader = new PipedReader(-1);
107 fail("Should throw IllegalArgumentException");
108 } catch (IllegalArgumentException e) {
113 preader = new PipedReader(0);
114 fail("Should throw IllegalArgumentException");
115 } catch (IllegalArgumentException e) {
121 * @tests java.io.PipedReader#close()
123 public void test_close() throws Exception {
125 preader = new PipedReader();
126 t = new Thread(new PWriter(preader), "");
128 Thread.sleep(500); // Allow writer to start
130 preader.read(c, 0, 11);
132 assertEquals("Read incorrect chars", "Hello World", new String(c));
136 * @tests java.io.PipedReader#connect(java.io.PipedWriter)
138 public void test_connectLjava_io_PipedWriter() throws Exception {
141 preader = new PipedReader();
142 t = new Thread(pwriter = new PWriter(), "");
143 preader.connect(pwriter.pw);
145 Thread.sleep(500); // Allow writer to start
147 preader.read(c, 0, 11);
149 assertEquals("Read incorrect chars", "Hello World", new String(c));
151 preader.connect(pwriter.pw);
152 fail("Failed to throw exception connecting to pre-connected reader");
153 } catch (IOException e) {
159 * @tests java.io.PipedReader#read()
161 public void test_read() throws Exception {
163 preader = new PipedReader();
164 t = new Thread(new PWriter(preader), "");
166 Thread.sleep(500); // Allow writer to start
168 for (int i = 0; i < c.length; i++) {
169 c[i] = (char) preader.read();
171 assertEquals("Read incorrect chars", "Hello World", new String(c));
175 * @tests java.io.PipedReader#read(char[], int, int)
177 public void test_read$CII() throws Exception {
179 preader = new PipedReader();
180 t = new Thread(new PWriter(preader), "");
182 Thread.sleep(500); // Allow writer to start
187 n = preader.read(c, x, 11 - x);
190 assertEquals("Read incorrect chars", "Hello World", new String(c));
193 preader.read(c, 8, 7);
194 fail("Failed to throw exception reading from closed reader");
195 } catch (IOException e) {
201 * @tests java.io.PipedReader#read(char[], int, int)
203 public void test_read$CII_2() throws IOException {
204 // Regression for HARMONY-387
205 PipedWriter pw = new PipedWriter();
206 PipedReader obj = null;
208 obj = new PipedReader(pw);
209 obj.read(new char[0], (int) 0, (int) -1);
210 fail("IndexOutOfBoundsException expected");
211 } catch (IndexOutOfBoundsException t) {
213 "IndexOutOfBoundsException rather than a subclass expected",
214 IndexOutOfBoundsException.class, t.getClass());
219 * @tests java.io.PipedReader#read(char[], int, int)
221 public void test_read$CII_3() throws IOException {
222 PipedWriter pw = new PipedWriter();
223 PipedReader obj = null;
225 obj = new PipedReader(pw);
226 obj.read(new char[0], (int) -1, (int) 0);
227 fail("IndexOutOfBoundsException expected");
228 } catch (ArrayIndexOutOfBoundsException t) {
229 fail("IndexOutOfBoundsException expected");
230 } catch (IndexOutOfBoundsException t) {
236 * @tests java.io.PipedReader#read(char[], int, int)
238 public void test_read$CII_4() throws IOException {
239 PipedWriter pw = new PipedWriter();
240 PipedReader obj = null;
242 obj = new PipedReader(pw);
243 obj.read(new char[0], (int) -1, (int) -1);
244 fail("IndexOutOfBoundsException expected");
245 } catch (ArrayIndexOutOfBoundsException t) {
246 fail("IndexOutOfBoundsException expected");
247 } catch (IndexOutOfBoundsException t) {
253 * @tests java.io.PipedReader#read(char[], int, int)
255 public void test_read_$CII_IOException() throws IOException {
256 PipedWriter pw = new PipedWriter();
257 PipedReader pr = new PipedReader(pw);
262 fail("Should throws IOException");
263 } catch (IOException e) {
270 pr = new PipedReader();
275 fail("Should throws IOException");
276 } catch (IOException e) {
282 pw = new PipedWriter();
283 pr = new PipedReader(pw);
288 fail("Should throws IOException");
289 } catch (IOException e) {
296 pw = new PipedWriter();
297 pr = new PipedReader(pw);
302 fail("Should throws IOException");
303 } catch (IOException e) {
310 pw = new PipedWriter();
311 pr = new PipedReader(pw);
316 fail("Should throws IOException");
317 } catch (IOException e) {
324 pw = new PipedWriter();
325 pr = new PipedReader(pw);
328 pr.read(new char[0], -1, -1);
329 fail("should throw IOException");
330 } catch (IOException e) {
337 pw = new PipedWriter();
338 pr = new PipedReader(pw);
342 fail("should throw IOException");
343 } catch (IOException e) {
350 pw = new PipedWriter();
351 pr = new PipedReader(pw);
353 pr.read(null, -1, 1);
354 fail("should throw IndexOutOfBoundsException");
355 } catch (NullPointerException expected) { // android-added
356 } catch (IndexOutOfBoundsException e) {
363 pw = new PipedWriter();
364 pr = new PipedReader(pw);
366 pr.read(null, 0, -1);
367 fail("should throw NullPointerException");
368 } catch (NullPointerException e) {
375 pw = new PipedWriter();
376 pr = new PipedReader(pw);
378 pr.read(new char[10], 11, 0);
379 fail("should throw IndexOutOfBoundsException");
380 } catch (IndexOutOfBoundsException e) {
387 pw = new PipedWriter();
388 pr = new PipedReader(pw);
391 fail("should throw NullPointerException");
392 } catch (NullPointerException e) {
401 * @tests java.io.PipedReader#ready()
403 public void test_ready() throws Exception {
405 preader = new PipedReader();
406 t = new Thread(new PWriter(preader), "");
408 Thread.sleep(500); // Allow writer to start
409 assertTrue("Reader should be ready", preader.ready());
411 for (int i = 0; i < c.length; i++)
412 c[i] = (char) preader.read();
413 assertFalse("Reader should not be ready after reading all chars",
418 * Tears down the fixture, for example, close a network connection. This
419 * method is called after a test is executed.
421 protected void tearDown() throws Exception {