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 org
.apache
.harmony
.tests
.java
.io
;
19 import java
.io
.IOException
;
20 import java
.io
.PipedInputStream
;
21 import java
.io
.PipedOutputStream
;
22 import java
.util
.concurrent
.CountDownLatch
;
24 public class PipedInputStreamTest
extends junit
.framework
.TestCase
{
26 static class PWriter
implements Runnable
{
27 PipedOutputStream pos
;
37 } catch (IOException e
) {
38 e
.printStackTrace(System
.out
);
39 System
.out
.println("Could not write bytes");
43 public PWriter(PipedOutputStream pout
, int nbytes
) {
45 bytes
= new byte[nbytes
];
46 for (int i
= 0; i
< bytes
.length
; i
++) {
47 bytes
[i
] = (byte) (System
.currentTimeMillis() % 9);
58 PipedOutputStream pos
;
61 * java.io.PipedInputStream#PipedInputStream()
63 public void test_Constructor() {
64 // Test for method java.io.PipedInputStream()
69 * java.io.PipedInputStream#PipedInputStream(java.io.PipedOutputStream)
71 public void test_ConstructorLjava_io_PipedOutputStream() throws Exception
{
72 // Test for method java.io.PipedInputStream(java.io.PipedOutputStream)
73 pis
= new PipedInputStream(new PipedOutputStream());
78 public void test_readException() throws IOException
{
79 pis
= new PipedInputStream();
80 pos
= new PipedOutputStream();
84 t
= new Thread(pw
= new PWriter(pos
, 1000));
90 } catch (IOException expected
) {
95 } catch (IOException ee
) {
101 * java.io.PipedInputStream#available()
103 public void test_available() throws Exception
{
104 pis
= new PipedInputStream();
105 pos
= new PipedOutputStream();
108 t
= new Thread(pw
= new PWriter(pos
, 1000));
114 assertTrue("Available returned incorrect number of bytes: "
115 + pis
.available(), pis
.available() == 1000);
117 PipedInputStream pin
= new PipedInputStream();
118 PipedOutputStream pout
= new PipedOutputStream(pin
);
119 // We know the PipedInputStream buffer size is 1024.
120 // Writing another byte would cause the write to wait
121 // for a read before returning
122 for (int i
= 0; i
< 1024; i
++) {
125 assertEquals("Incorrect available count", 1024, pin
.available());
129 * java.io.PipedInputStream#close()
131 public void test_close() throws IOException
{
132 // Test for method void java.io.PipedInputStream.close()
133 pis
= new PipedInputStream();
134 pos
= new PipedOutputStream();
138 pos
.write((byte) 127);
139 fail("Failed to throw expected exception");
140 } catch (IOException e
) {
141 // The spec for PipedInput saya an exception should be thrown if
142 // a write is attempted to a closed input. The PipedOuput spec
143 // indicates that an exception should be thrown only when the
144 // piped input thread is terminated without closing
150 * java.io.PipedInputStream#connect(java.io.PipedOutputStream)
152 public void test_connectLjava_io_PipedOutputStream() throws Exception
{
153 pis
= new PipedInputStream();
154 pos
= new PipedOutputStream();
155 assertEquals("Non-conected pipe returned non-zero available bytes", 0,
159 t
= new Thread(pw
= new PWriter(pos
, 1000));
165 assertEquals("Available returned incorrect number of bytes", 1000, pis
170 * java.io.PipedInputStream#read()
172 public void test_read() throws Exception
{
173 pis
= new PipedInputStream();
174 pos
= new PipedOutputStream();
177 t
= new Thread(pw
= new PWriter(pos
, 1000));
183 assertEquals("Available returned incorrect number of bytes", 1000, pis
185 assertEquals("read returned incorrect byte", pw
.bytes
[0], (byte) pis
190 * java.io.PipedInputStream#read(byte[], int, int)
192 public void test_read$
BII() throws Exception
{
193 pis
= new PipedInputStream();
194 pos
= new PipedOutputStream();
197 t
= new Thread(pw
= new PWriter(pos
, 1000));
200 byte[] buf
= new byte[400];
204 assertTrue("Available returned incorrect number of bytes: "
205 + pis
.available(), pis
.available() == 1000);
206 pis
.read(buf
, 0, 400);
207 for (int i
= 0; i
< 400; i
++) {
208 assertEquals("read returned incorrect byte[]", pw
.bytes
[i
], buf
[i
]);
213 * java.io.PipedInputStream#read(byte[], int, int)
214 * Regression for HARMONY-387
216 public void test_read$
BII_2() throws IOException
{
217 PipedInputStream obj
= new PipedInputStream();
219 obj
.read(new byte[0], 0, -1);
221 } catch (IndexOutOfBoundsException expected
) {
226 * java.io.PipedInputStream#read(byte[], int, int)
228 public void test_read$
BII_3() throws IOException
{
229 PipedInputStream obj
= new PipedInputStream();
231 obj
.read(new byte[0], -1, 0);
233 } catch (IndexOutOfBoundsException expected
) {
238 * java.io.PipedInputStream#read(byte[], int, int)
240 public void test_read$
BII_4() throws IOException
{
241 PipedInputStream obj
= new PipedInputStream();
243 obj
.read(new byte[0], -1, -1);
245 } catch (IndexOutOfBoundsException expected
) {
250 * java.io.PipedInputStream#receive(int)
252 public void test_write_failsAfterReaderDead() throws Exception
{
253 pis
= new PipedInputStream();
254 pos
= new PipedOutputStream();
256 // test if writer recognizes dead reader
259 class WriteRunnable
implements Runnable
{
261 final CountDownLatch readerAlive
= new CountDownLatch(1);
269 } catch (InterruptedException ie
) {
275 // should throw exception since reader thread
279 } catch (IOException expected
) {
281 } catch (IOException e
) {
286 class ReadRunnable
implements Runnable
{
290 } catch (IOException e
) {
296 WriteRunnable writeRunnable
= new WriteRunnable();
297 Thread writeThread
= new Thread(writeRunnable
);
299 ReadRunnable readRunnable
= new ReadRunnable();
300 Thread readThread
= new Thread(readRunnable
);
305 writeRunnable
.readerAlive
.countDown();
309 static final class PipedInputStreamWithPublicReceive
extends PipedInputStream
{
311 public void receive(int oneByte
) throws IOException
{
312 super.receive(oneByte
);
317 public void test_receive_failsIfWriterClosed() throws Exception
{
318 // attempt to write to stream after writer closed
319 PipedInputStreamWithPublicReceive pis
= new PipedInputStreamWithPublicReceive();
321 pos
= new PipedOutputStream();
327 } catch (IOException expected
) {
331 static class Worker
extends Thread
{
332 PipedOutputStream out
;
334 Worker(PipedOutputStream pos
) {
343 } catch (Exception e
) {
348 public void test_read_after_write_close() throws Exception
{
349 PipedInputStream in
= new PipedInputStream();
350 PipedOutputStream out
= new PipedOutputStream();
352 Thread worker
= new Worker(out
);
355 assertEquals("Should read 20.", 20, in
.read());
357 assertEquals("Write end is closed, should return -1", -1, in
.read());
358 byte[] buf
= new byte[1];
359 assertEquals("Write end is closed, should return -1", -1, in
.read(buf
, 0, 1));
360 assertEquals("Buf len 0 should return first", 0, in
.read(buf
, 0, 0));
366 * Tears down the fixture, for example, close a network connection. This
367 * method is called after a test is executed.
369 protected void tearDown() throws Exception
{
374 } catch (Exception ignore
) {
381 * java.io.PipedInputStream#PipedInputStream(java.io.PipedOutputStream,
385 public void test_Constructor_LPipedOutputStream_I() throws Exception
{
386 // Test for method java.io.PipedInputStream(java.io.PipedOutputStream,
388 MockPipedInputStream mpis
= new MockPipedInputStream(
389 new PipedOutputStream(), 100);
390 int bufferLength
= mpis
.bufferLength();
391 assertEquals(100, bufferLength
);
394 pis
= new PipedInputStream(null, -1);
395 fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
396 } catch (IllegalArgumentException e
) {
401 pis
= new PipedInputStream(null, 0);
402 fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
403 } catch (IllegalArgumentException e
) {
409 * java.io.PipedInputStream#PipedInputStream(int)
412 public void test_Constructor_I() throws Exception
{
413 // Test for method java.io.PipedInputStream(int)
414 MockPipedInputStream mpis
= new MockPipedInputStream(100);
415 int bufferLength
= mpis
.bufferLength();
416 assertEquals(100, bufferLength
);
419 pis
= new PipedInputStream(-1);
420 fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
421 } catch (IllegalArgumentException e
) {
426 pis
= new PipedInputStream(0);
427 fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
428 } catch (IllegalArgumentException e
) {
433 static class MockPipedInputStream
extends PipedInputStream
{
435 public MockPipedInputStream(java
.io
.PipedOutputStream src
,
436 int bufferSize
) throws IOException
{
437 super(src
, bufferSize
);
440 public MockPipedInputStream(int bufferSize
) {
444 public int bufferLength() {
445 return super.buffer
.length
;