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.
17 package tests.api.java.io;
19 import java.io.IOException;
20 import java.io.PipedInputStream;
21 import java.io.PipedOutputStream;
23 import dalvik.annotation.TestLevel;
24 import dalvik.annotation.TestTargetClass;
25 import dalvik.annotation.TestTargetNew;
27 @TestTargetClass(PipedInputStream.class)
28 public class PipedInputStreamTest extends junit.framework.TestCase {
30 static class PWriter implements Runnable {
31 PipedOutputStream pos;
41 } catch (Exception e) {
42 e.printStackTrace(System.out);
43 System.out.println("Error while running the writer thread.");
47 public PWriter(PipedOutputStream pout, int nbytes) {
49 bytes = new byte[nbytes];
50 for (int i = 0; i < bytes.length; i++)
51 bytes[i] = (byte) (System.currentTimeMillis() % 9);
55 static class PWriter2 implements Runnable {
56 PipedOutputStream pos;
58 public boolean keepRunning = true;
67 } catch (Exception e) {
68 e.printStackTrace(System.out);
69 System.out.println("Error while running the writer thread.");
73 public PWriter2(PipedOutputStream pout) {
84 PipedOutputStream pos;
87 * @tests java.io.PipedInputStream#PipedInputStream()
90 level = TestLevel.COMPLETE,
92 method = "PipedInputStream",
95 public void test_Constructor() throws IOException {
96 pis = new PipedInputStream();
97 assertEquals("There should not be any bytes available. ", 0, pis.available());
102 * @tests java.io.PipedInputStream#PipedInputStream(java.io.PipedOutputStream)
105 level = TestLevel.COMPLETE,
107 method = "PipedInputStream",
108 args = {java.io.PipedOutputStream.class}
110 public void test_ConstructorLjava_io_PipedOutputStream() throws IOException {
111 pos = new PipedOutputStream(new PipedInputStream());
114 pis = new PipedInputStream(pos);
115 fail("IOException expected since the output stream is already connected.");
116 } catch (IOException e) {
120 pis = new PipedInputStream(new PipedOutputStream());
121 assertEquals("There should not be any bytes available. ", 0, pis.available());
128 * @tests java.io.PipedInputStream#available()
131 level = TestLevel.COMPLETE,
132 notes = "No IOException checking because it is never thrown in the source code.",
133 method = "available",
136 public void test_available() throws Exception {
137 // Test for method int java.io.PipedInputStream.available()
138 pis = new PipedInputStream();
139 pos = new PipedOutputStream();
142 t = new Thread(pw = new PWriter(pos, 1000));
148 assertEquals("Test 1: Incorrect number of bytes available. ",
149 1000, pis.available());
151 PipedInputStream pin = new PipedInputStream();
152 PipedOutputStream pout = new PipedOutputStream(pin);
153 // We know the PipedInputStream buffer size is 1024.
154 // Writing another byte would cause the write to wait
155 // for a read before returning
156 for (int i = 0; i < 1024; i++)
158 assertEquals("Test 2: Incorrect number of bytes available. ",
159 1024 , pin.available());
163 * @tests java.io.PipedInputStream#close()
166 level = TestLevel.COMPLETE,
167 notes = "No IOException checking because it is never thrown in the source code.",
171 public void test_close() throws IOException {
172 // Test for method void java.io.PipedInputStream.close()
173 pis = new PipedInputStream();
174 pos = new PipedOutputStream();
178 pos.write((byte) 127);
179 fail("IOException expected.");
180 } catch (IOException e) {
181 // The spec for PipedInput says an exception should be thrown if
182 // a write is attempted to a closed input. The PipedOuput spec
183 // indicates that an exception should be thrown only when the
184 // piped input thread is terminated without closing
190 * @tests java.io.PipedInputStream#connect(java.io.PipedOutputStream)
193 level = TestLevel.COMPLETE,
196 args = {java.io.PipedOutputStream.class}
198 public void test_connectLjava_io_PipedOutputStream() throws Exception {
199 // Test for method void
200 // java.io.PipedInputStream.connect(java.io.PipedOutputStream)
201 pis = new PipedInputStream();
202 pos = new PipedOutputStream();
203 assertEquals("Test 1: Not-connected pipe returned more than zero available bytes. ",
207 t = new Thread(pw = new PWriter(pos, 1000));
213 assertEquals("Test 2: Unexpected number of bytes available. ",
214 1000, pis.available());
218 fail("Test 3: IOException expected when reconnecting the pipe.");
219 } catch (IOException e) {
228 * @tests java.io.PipedInputStream#read()
231 level = TestLevel.PARTIAL_COMPLETE,
236 public void test_read() throws Exception {
237 pis = new PipedInputStream();
238 pos = new PipedOutputStream();
242 fail("Test 1: IOException expected since the stream is not connected.");
243 } catch (IOException e) {
248 t = new Thread(pw = new PWriter(pos, 100));
254 assertEquals("Test 2: Unexpected number of bytes available. ",
255 100, pis.available());
257 for (int i = 0; i < 100; i++) {
258 assertEquals("Test 3: read() returned incorrect byte. ",
259 pw.bytes[i], (byte) pis.read());
264 fail("Test 4: IOException expected since the thread that has " +
265 "written to the pipe is no longer alive.");
266 } catch (IOException e) {
273 fail("Test 5: IOException expected since the stream is closed.");
274 } catch (IOException e) {
280 * @tests java.io.PipedInputStream#read()
283 level = TestLevel.PARTIAL_COMPLETE,
284 notes = "Checks that read returns -1 if the PipedOutputStream connected to this PipedInputStream is closed.",
288 public void test_read_2() throws Exception {
292 pos = new PipedOutputStream();
293 pis = new PipedInputStream(pos);
294 writerThread = new Thread(pwriter = new PWriter2(pos));
295 writerThread.start();
297 synchronized (pwriter) {
301 assertEquals("Test 1: No more data indication expected. ", -1, pis.read());
302 pwriter.keepRunning = false;
308 * @tests java.io.PipedInputStream#read(byte[], int, int)
311 level = TestLevel.PARTIAL_COMPLETE,
312 notes = "Tests read from unconnected, connected and closed pipe.",
314 args = {byte[].class, int.class, int.class}
316 public void test_read$BII() throws Exception {
317 byte[] buf = new byte[400];
318 pis = new PipedInputStream();
319 pos = new PipedOutputStream();
322 pis.read(buf, 0, 10);
323 fail("Test 1: IOException expected since the stream is not connected.");
324 } catch (IOException e) {
329 t = new Thread(pw = new PWriter(pos, 1000));
335 assertEquals("Test 2: Unexpected number of bytes available. ",
336 1000, pis.available());
337 pis.read(buf, 0, 400);
338 for (int i = 0; i < 400; i++) {
339 assertEquals("Test 3: read() returned incorrect byte. ",
340 pw.bytes[i], buf[i]);
345 pis.read(buf, 0, 10);
346 fail("Test 4: IOException expected since the stream is closed.");
347 } catch (IOException e) {
353 * @tests java.io.PipedInputStream#read(byte[], int, int)
354 * Regression for HARMONY-387
357 level = TestLevel.PARTIAL_COMPLETE,
358 notes = "Tests illegal length argument.",
360 args = {byte[].class, int.class, int.class}
362 public void test_read$BII_2() throws IOException {
363 PipedInputStream obj = new PipedInputStream();
365 obj.read(new byte[0], 0, -1);
366 fail("IndexOutOfBoundsException expected.");
367 } catch (IndexOutOfBoundsException t) {
369 "IndexOutOfBoundsException rather than a subclass expected.",
370 IndexOutOfBoundsException.class, t.getClass());
375 * @tests java.io.PipedInputStream#read(byte[], int, int)
378 level = TestLevel.PARTIAL_COMPLETE,
379 notes = "Tests illegal offset argument.",
381 args = {byte[].class, int.class, int.class}
383 public void test_read$BII_3() throws IOException {
384 PipedInputStream obj = new PipedInputStream();
386 obj.read(new byte[10], -1, 1);
387 fail("IndexOutOfBoundsException expected.");
388 } catch (IndexOutOfBoundsException e) {
390 assertTrue(e.getClass().equals(IndexOutOfBoundsException.class));
393 obj.read(new byte[10], 0, -1);
394 fail("IndexOutOfBoundsException expected.");
395 } catch (IndexOutOfBoundsException e) {
397 assertTrue(e.getClass().equals(IndexOutOfBoundsException.class));
400 obj.read(new byte[10], 9, 2);
401 fail("IndexOutOfBoundsException expected.");
402 } catch (IndexOutOfBoundsException e) {
404 assertTrue(e.getClass().equals(IndexOutOfBoundsException.class));
409 * @tests java.io.PipedInputStream#receive(int)
412 level = TestLevel.COMPLETE,
417 public void test_receive() throws IOException {
418 pis = new PipedInputStream();
419 pos = new PipedOutputStream();
421 // test if writer recognizes dead reader
423 class WriteRunnable implements Runnable {
425 boolean pass = false;
427 boolean readerAlive = true;
432 while (readerAlive) {
436 // should throw exception since reader thread
439 } catch (IOException e) {
442 } catch (IOException e) {
444 } catch (InterruptedException e) {
449 WriteRunnable writeRunnable = new WriteRunnable();
450 Thread writeThread = new Thread(writeRunnable);
451 class ReadRunnable implements Runnable {
459 } catch (IOException e) {}
463 ReadRunnable readRunnable = new ReadRunnable();
464 Thread readThread = new Thread(readRunnable);
467 while (readThread.isAlive())
469 writeRunnable.readerAlive = false;
470 assertTrue("reader thread failed to read", readRunnable.pass);
471 while (writeThread.isAlive())
473 assertTrue("writer thread failed to recognize dead reader",
476 // attempt to write to stream after writer closed
477 pis = new PipedInputStream();
478 pos = new PipedOutputStream();
481 class MyRunnable implements Runnable {
488 } catch (IOException e) {
493 MyRunnable myRun = new MyRunnable();
495 t = new Thread(myRun);
496 // thread t will be blocked inside pos.write(1)
497 // when it tries to call the synchronized method pis.receive
498 // because we hold the monitor for object pis
501 // wait for thread t to get to the call to pis.receive
503 } catch (InterruptedException e) {}
507 // we have exited the synchronized block, so now thread t will make
508 // a call to pis.receive AFTER the output stream was closed,
509 // in which case an IOException should be thrown
510 while (t.isAlive()) {
514 "write failed to throw IOException on closed PipedOutputStream",
519 * Tears down the fixture, for example, close a network connection. This
520 * method is called after a test is executed.
522 protected void tearDown() throws Exception {
527 } catch (Exception ignore) {