1/* 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 */ 17 18package org.apache.harmony.tests.java.io; 19 20import java.io.IOException; 21import java.io.PipedInputStream; 22import java.io.PipedOutputStream; 23import java.io.UnsupportedEncodingException; 24 25import junit.framework.TestCase; 26 27public class PipedOutputStreamTest extends TestCase { 28 29 static class PReader implements Runnable { 30 PipedInputStream reader; 31 32 public PipedInputStream getReader() { 33 return reader; 34 } 35 36 public PReader(PipedOutputStream out) { 37 try { 38 reader = new PipedInputStream(out); 39 } catch (Exception e) { 40 System.out.println("Couldn't start reader"); 41 } 42 } 43 44 public int available() { 45 try { 46 return reader.available(); 47 } catch (Exception e) { 48 return -1; 49 } 50 } 51 52 public void run() { 53 try { 54 while (true) { 55 Thread.sleep(1000); 56 Thread.yield(); 57 } 58 } catch (InterruptedException e) { 59 } 60 } 61 62 public String read(int nbytes) { 63 byte[] buf = new byte[nbytes]; 64 try { 65 reader.read(buf, 0, nbytes); 66 return new String(buf, "UTF-8"); 67 } catch (IOException e) { 68 System.out.println("Exception reading info"); 69 return "ERROR"; 70 } 71 } 72 } 73 74 Thread rt; 75 76 PReader reader; 77 78 PipedOutputStream out; 79 80 /** 81 * java.io.PipedOutputStream#PipedOutputStream() 82 */ 83 public void test_Constructor() { 84 // Used in tests 85 } 86 87 /** 88 * java.io.PipedOutputStream#PipedOutputStream(java.io.PipedInputStream) 89 */ 90 public void test_ConstructorLjava_io_PipedInputStream() throws Exception { 91 out = new PipedOutputStream(new PipedInputStream()); 92 out.write('b'); 93 } 94 95 /** 96 * java.io.PipedOutputStream#close() 97 */ 98 public void test_close() throws Exception { 99 out = new PipedOutputStream(); 100 rt = new Thread(reader = new PReader(out)); 101 rt.start(); 102 out.close(); 103 } 104 105 /** 106 * 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 * 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 * java.io.PipedOutputStream#flush() 137 */ 138 public void test_flush() throws IOException, UnsupportedEncodingException { 139 out = new PipedOutputStream(); 140 rt = new Thread(reader = new PReader(out)); 141 rt.start(); 142 out.write("HelloWorld".getBytes("UTF-8"), 0, 10); 143 assertTrue("Bytes written before flush", reader.available() != 0); 144 out.flush(); 145 assertEquals("Wrote incorrect bytes", "HelloWorld", reader.read(10)); 146 } 147 148 /** 149 * java.io.PipedOutputStream#write(byte[], int, int) 150 */ 151 public void test_write$BII() throws IOException, UnsupportedEncodingException { 152 out = new PipedOutputStream(); 153 rt = new Thread(reader = new PReader(out)); 154 rt.start(); 155 out.write("HelloWorld".getBytes("UTF-8"), 0, 10); 156 out.flush(); 157 assertEquals("Wrote incorrect bytes", "HelloWorld", reader.read(10)); 158 } 159 160 /** 161 * java.io.PipedOutputStream#write(byte[], int, int) Regression for 162 * HARMONY-387 163 */ 164 public void test_write$BII_2() throws IOException { 165 PipedInputStream pis = new PipedInputStream(); 166 PipedOutputStream pos = null; 167 try { 168 pos = new PipedOutputStream(pis); 169 pos.write(new byte[0], -1, -1); 170 fail(); 171 } catch (IndexOutOfBoundsException expected) { 172 } 173 174 // Regression for HARMONY-4311 175 try { 176 pis = new PipedInputStream(); 177 PipedOutputStream out = new PipedOutputStream(pis); 178 out.write(null, -10, 10); 179 fail("should throw NullPointerException."); 180 } catch (NullPointerException e) { 181 // expected 182 } 183 184 pis = new PipedInputStream(); 185 pos = new PipedOutputStream(pis); 186 pos.close(); 187 pos.write(new byte[0], 0, 0); 188 189 try { 190 pis = new PipedInputStream(); 191 pos = new PipedOutputStream(pis); 192 pos.write(new byte[0], -1, 0); 193 fail("IndexOutOfBoundsException expected"); 194 } catch (IndexOutOfBoundsException t) { 195 //expected 196 } 197 198 try { 199 pis = new PipedInputStream(); 200 pos = new PipedOutputStream(pis); 201 pos.write(null, -10, 0); 202 fail("should throw NullPointerException."); 203 } catch (NullPointerException e) { 204 // expected 205 } 206 207 } 208 209 /** 210 * java.io.PipedOutputStream#write(int) 211 */ 212 public void test_writeI() throws IOException { 213 out = new PipedOutputStream(); 214 rt = new Thread(reader = new PReader(out)); 215 rt.start(); 216 out.write('c'); 217 out.flush(); 218 assertEquals("Wrote incorrect byte", "c", reader.read(1)); 219 } 220 221 /** 222 * Tears down the fixture, for example, close a network connection. This 223 * method is called after a test is executed. 224 */ 225 @Override 226 protected void tearDown() { 227 if (rt != null) { 228 rt.interrupt(); 229 } 230 } 231} 232