import java.io.PipedInputStream;
import java.io.PipedOutputStream;
-import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetClass;
-import dalvik.annotation.TestTargetNew;
-
-@TestTargetClass(PipedInputStream.class)
public class PipedInputStreamTest extends junit.framework.TestCase {
- static class PWriter implements Runnable {
- PipedOutputStream pos;
-
- public byte bytes[];
-
- public void run() {
- try {
- pos.write(bytes);
- synchronized (this) {
- notify();
- }
- } catch (Exception e) {
- e.printStackTrace(System.out);
- System.out.println("Error while running the writer thread.");
- }
- }
-
- public PWriter(PipedOutputStream pout, int nbytes) {
- pos = pout;
- bytes = new byte[nbytes];
- for (int i = 0; i < bytes.length; i++)
- bytes[i] = (byte) (System.currentTimeMillis() % 9);
- }
- }
-
- static class PWriter2 implements Runnable {
- PipedOutputStream pos;
-
- public boolean keepRunning = true;
-
- public void run() {
- try {
- pos.write(42);
- pos.close();
- while (keepRunning) {
- Thread.sleep(1000);
- }
- } catch (Exception e) {
- e.printStackTrace(System.out);
- System.out.println("Error while running the writer thread.");
- }
- }
-
- public PWriter2(PipedOutputStream pout) {
- pos = pout;
- }
+ private final int BUFFER_SIZE = 1024;
+
+ static class PWriter implements Runnable {
+ PipedOutputStream pos;
+
+ public byte bytes[];
+
+ public void run() {
+ try {
+ pos.write(bytes);
+ synchronized (this) {
+ notify();
+ }
+ } catch (IOException e) {
+ e.printStackTrace(System.out);
+ System.out.println("Could not write bytes");
+ }
+ }
+
+ public PWriter(PipedOutputStream pout, int nbytes) {
+ pos = pout;
+ bytes = new byte[nbytes];
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = (byte) (System.currentTimeMillis() % 9);
+ }
+ }
+ }
+
+ Thread t;
+
+ PWriter pw;
+
+ PipedInputStream pis;
+
+ PipedOutputStream pos;
+
+ /**
+ * @tests java.io.PipedInputStream#PipedInputStream()
+ */
+ public void test_Constructor() {
+ // Test for method java.io.PipedInputStream()
+ // Used in tests
+ }
+
+ /**
+ * @tests java.io.PipedInputStream#PipedInputStream(java.io.PipedOutputStream)
+ */
+ public void test_ConstructorLjava_io_PipedOutputStream() throws Exception {
+ // Test for method java.io.PipedInputStream(java.io.PipedOutputStream)
+ pis = new PipedInputStream(new PipedOutputStream());
+ pis.available();
}
- Thread t;
-
- PWriter pw;
-
- PipedInputStream pis;
-
- PipedOutputStream pos;
/**
- * @tests java.io.PipedInputStream#PipedInputStream()
+ * @test java.io.PipedInputStream#read()
*/
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "",
- method = "PipedInputStream",
- args = {}
- )
- public void test_Constructor() throws IOException {
+ public void test_readException() throws IOException {
pis = new PipedInputStream();
- assertEquals("There should not be any bytes available. ", 0, pis.available());
- pis.close();
- }
+ pos = new PipedOutputStream();
- /**
- * @tests java.io.PipedInputStream#PipedInputStream(java.io.PipedOutputStream)
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "",
- method = "PipedInputStream",
- args = {java.io.PipedOutputStream.class}
- )
- public void test_ConstructorLjava_io_PipedOutputStream() throws IOException {
- pos = new PipedOutputStream(new PipedInputStream());
-
try {
- pis = new PipedInputStream(pos);
- fail("IOException expected since the output stream is already connected.");
+ pis.connect(pos);
+ t = new Thread(pw = new PWriter(pos, 1000));
+ t.start();
+ assertTrue(t.isAlive());
+ while (true) {
+ pis.read();
+ t.interrupt();
+ }
} catch (IOException e) {
- // Expected.
+ if (!e.getMessage().contains("Write end dead") && !e.getMessage().contains("Pipe broken")) { // android-changed
+ throw e;
+ }
+ } finally {
+ try {
+ pis.close();
+ pos.close();
+ } catch (IOException ee) {}
}
-
- pis = new PipedInputStream(new PipedOutputStream());
- assertEquals("There should not be any bytes available. ", 0, pis.available());
-
- pis.close();
- pos.close();
}
/**
PipedInputStream pin = new PipedInputStream();
PipedOutputStream pout = new PipedOutputStream(pin);
- // We know the PipedInputStream buffer size is 1024.
// Writing another byte would cause the write to wait
// for a read before returning
- for (int i = 0; i < 1024; i++) {
- for (int i = 0; i < BUFFER_SIZE; i++)
++ for (int i = 0; i < BUFFER_SIZE; i++) {
pout.write(i);
- assertEquals("Test 2: Incorrect number of bytes available. ",
- BUFFER_SIZE, pin.available());
+ }
- assertEquals("Incorrect available count", 1024 , pin.available());
++ assertEquals("Incorrect available count", BUFFER_SIZE , pin.available());
}
- /**
- * @tests java.io.PipedInputStream#close()
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "No IOException checking because it is never thrown in the source code.",
- method = "close",
- args = {}
- )
- public void test_close() throws IOException {
- // Test for method void java.io.PipedInputStream.close()
- pis = new PipedInputStream();
- pos = new PipedOutputStream();
+ /**
+ * @tests java.io.PipedInputStream#close()
+ */
+ public void test_close() throws IOException {
+ // Test for method void java.io.PipedInputStream.close()
+ pis = new PipedInputStream();
+ pos = new PipedOutputStream();
pis.connect(pos);
pis.close();
- try {
- pos.write((byte) 127);
- fail("IOException expected.");
- } catch (IOException e) {
- // The spec for PipedInput says an exception should be thrown if
- // a write is attempted to a closed input. The PipedOuput spec
- // indicates that an exception should be thrown only when the
- // piped input thread is terminated without closing
- return;
- }
- }
-
- /**
- * @tests java.io.PipedInputStream#connect(java.io.PipedOutputStream)
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "",
- method = "connect",
- args = {java.io.PipedOutputStream.class}
- )
- public void test_connectLjava_io_PipedOutputStream() throws Exception {
- // Test for method void
- // java.io.PipedInputStream.connect(java.io.PipedOutputStream)
+ try {
+ pos.write((byte) 127);
+ fail("Failed to throw expected exception");
+ } catch (IOException e) {
+ // The spec for PipedInput saya an exception should be thrown if
+ // a write is attempted to a closed input. The PipedOuput spec
+ // indicates that an exception should be thrown only when the
+ // piped input thread is terminated without closing
+ return;
+ }
+ }
+
+ /**
+ * @tests java.io.PipedInputStream#connect(java.io.PipedOutputStream)
+ */
+ public void test_connectLjava_io_PipedOutputStream() throws Exception {
pis = new PipedInputStream();
pos = new PipedOutputStream();
- assertEquals("Test 1: Not-connected pipe returned more than zero available bytes. ",
- 0, pis.available());
+ assertEquals("Non-conected pipe returned non-zero available bytes", 0,
+ pis.available());
pis.connect(pos);
t = new Thread(pw = new PWriter(pos, 1000));
try {
pos.write(1);
while (readerAlive) {
- Thread.sleep(100);
+ ;
}
try {
- // should throw exception since reader thread
- // is now dead
+ pos.write(new byte[BUFFER_SIZE]);
+ // should throw exception since buffer is full and
+ // reader thread is now dead
pos.write(1);
} catch (IOException e) {
pass = true;