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.PipedInputStream;
022: import java.io.PipedOutputStream;
023:
024: import junit.framework.TestCase;
025:
026: public class PipedOutputStreamTest extends TestCase {
027:
028: static class PReader implements Runnable {
029: PipedInputStream reader;
030:
031: public PipedInputStream getReader() {
032: return reader;
033: }
034:
035: public PReader(PipedOutputStream out) {
036: try {
037: reader = new PipedInputStream(out);
038: } catch (Exception e) {
039: System.out.println("Couldn't start reader");
040: }
041: }
042:
043: public int available() {
044: try {
045: return reader.available();
046: } catch (Exception e) {
047: return -1;
048: }
049: }
050:
051: public void run() {
052: try {
053: while (true) {
054: Thread.sleep(1000);
055: Thread.yield();
056: }
057: } catch (InterruptedException e) {
058: }
059: }
060:
061: public String read(int nbytes) {
062: byte[] buf = new byte[nbytes];
063: try {
064: reader.read(buf, 0, nbytes);
065: return new String(buf);
066: } catch (IOException e) {
067: System.out.println("Exception reading info");
068: return "ERROR";
069: }
070: }
071: }
072:
073: Thread rt;
074:
075: PReader reader;
076:
077: PipedOutputStream out;
078:
079: /**
080: * @tests java.io.PipedOutputStream#PipedOutputStream()
081: */
082: public void test_Constructor() {
083: // Used in tests
084: }
085:
086: /**
087: * @tests java.io.PipedOutputStream#PipedOutputStream(java.io.PipedInputStream)
088: */
089: public void test_ConstructorLjava_io_PipedInputStream()
090: throws Exception {
091: out = new PipedOutputStream(new PipedInputStream());
092: out.write('b');
093: }
094:
095: /**
096: * @tests java.io.PipedOutputStream#close()
097: */
098: public void test_close() throws Exception {
099: out = new PipedOutputStream();
100: rt = new Thread(reader = new PReader(out));
101: rt.start();
102: out.close();
103: }
104:
105: /**
106: * @tests java.io.PipedOutputStream#connect(java.io.PipedInputStream)
107: */
108: public void test_connectLjava_io_PipedInputStream_Exception()
109: throws IOException {
110: out = new PipedOutputStream();
111: out.connect(new PipedInputStream());
112: try {
113: out.connect(null);
114: fail("should throw NullPointerException"); //$NON-NLS-1$
115: } catch (NullPointerException e) {
116: // expected
117: }
118: }
119:
120: /**
121: * @tests java.io.PipedOutputStream#connect(java.io.PipedInputStream)
122: */
123: public void test_connectLjava_io_PipedInputStream() {
124: try {
125: out = new PipedOutputStream();
126: rt = new Thread(reader = new PReader(out));
127: rt.start();
128: out.connect(new PipedInputStream());
129: fail("Failed to throw exception attempting connect on already connected stream");
130: } catch (IOException e) {
131: // Expected
132: }
133: }
134:
135: /**
136: * @tests java.io.PipedOutputStream#flush()
137: */
138: public void test_flush() throws IOException {
139: out = new PipedOutputStream();
140: rt = new Thread(reader = new PReader(out));
141: rt.start();
142: out.write("HelloWorld".getBytes(), 0, 10);
143: assertTrue("Bytes written before flush",
144: reader.available() != 0);
145: out.flush();
146: assertEquals("Wrote incorrect bytes", "HelloWorld", reader
147: .read(10));
148: }
149:
150: /**
151: * @tests java.io.PipedOutputStream#write(byte[], int, int)
152: */
153: public void test_write$BII() throws IOException {
154: out = new PipedOutputStream();
155: rt = new Thread(reader = new PReader(out));
156: rt.start();
157: out.write("HelloWorld".getBytes(), 0, 10);
158: out.flush();
159: assertEquals("Wrote incorrect bytes", "HelloWorld", reader
160: .read(10));
161: }
162:
163: /**
164: * @tests java.io.PipedOutputStream#write(byte[], int, int) Regression for
165: * HARMONY-387
166: */
167: public void test_write$BII_2() throws IOException {
168: PipedInputStream pis = new PipedInputStream();
169: PipedOutputStream pos = null;
170: try {
171: pos = new PipedOutputStream(pis);
172: pos.write(new byte[0], -1, -1);
173: fail("IndexOutOfBoundsException expected");
174: } catch (IndexOutOfBoundsException t) {
175: assertEquals(
176: "IndexOutOfBoundsException rather than a subclass expected",
177: IndexOutOfBoundsException.class, t.getClass());
178: }
179:
180: // Regression for HARMONY-4311
181: try {
182: pis = new PipedInputStream();
183: PipedOutputStream out = new PipedOutputStream(pis);
184: out.write(null, -10, 10);
185: fail("should throw NullPointerException.");
186: } catch (NullPointerException e) {
187: // expected
188: }
189: }
190:
191: /**
192: * @tests java.io.PipedOutputStream#write(int)
193: */
194: public void test_writeI() throws IOException {
195: out = new PipedOutputStream();
196: rt = new Thread(reader = new PReader(out));
197: rt.start();
198: out.write('c');
199: out.flush();
200: assertEquals("Wrote incorrect byte", "c", reader.read(1));
201: }
202:
203: /**
204: * Tears down the fixture, for example, close a network connection. This
205: * method is called after a test is executed.
206: */
207: @Override
208: protected void tearDown() {
209: if (rt != null) {
210: rt.interrupt();
211: }
212: }
213: }
|