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 libcore.java.io; 19 20import java.io.IOException; 21import java.io.PipedInputStream; 22import java.io.PipedOutputStream; 23 24public class OldPipedOutputStreamTest extends junit.framework.TestCase { 25 26 static class PReader implements Runnable { 27 PipedInputStream reader; 28 29 public PipedInputStream getReader() { 30 return reader; 31 } 32 33 public PReader(PipedOutputStream out) { 34 try { 35 reader = new PipedInputStream(out); 36 } catch (Exception e) { 37 System.out.println("Exception setting up reader: " 38 + e.toString()); 39 } 40 } 41 42 public int available() { 43 try { 44 return reader.available(); 45 } catch (Exception e) { 46 return -1; 47 } 48 } 49 50 public void run() { 51 try { 52 while (true) { 53 Thread.sleep(1000); 54 Thread.yield(); 55 } 56 } catch (InterruptedException e) { 57 } 58 } 59 60 public String read(int nbytes) { 61 byte[] buf = new byte[nbytes]; 62 try { 63 reader.read(buf, 0, nbytes); 64 return new String(buf); 65 } catch (IOException e) { 66 System.out.println("Exception reading (" 67 + Thread.currentThread().getName() + "): " 68 + e.toString()); 69 return "ERROR"; 70 } 71 } 72 } 73 74 static final String testString = "Lorem ipsum dolor sit amet,\n" + 75 "consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut" + 76 "labore et dolore magna aliqua.\n"; 77 static final int testLength = testString.length(); 78 79 Thread rt; 80 81 PReader reader; 82 83 PipedOutputStream out; 84 85 public void test_Constructor() { 86 out = new PipedOutputStream(); 87 assertNotNull(out); 88 try { 89 out.close(); 90 } catch (IOException e) { 91 fail("Unexpeceted IOException."); 92 } 93 } 94 95 public void test_ConstructorLjava_io_PipedInputStream() throws IOException { 96 // Test for method java.io.PipedOutputStream(java.io.PipedInputStream) 97 98 try { 99 out = new PipedOutputStream(new PipedInputStream()); 100 out.write('b'); 101 } catch (Exception e) { 102 fail("Test 1: Constructor failed: " + e.getMessage()); 103 } 104 out.close(); 105 106 PipedInputStream pis = new PipedInputStream(new PipedOutputStream()); 107 try { 108 out = new PipedOutputStream(pis); 109 fail("Test 2: IOException expected because the input stream is already connected."); 110 } catch (IOException e) { 111 // Expected. 112 } 113 } 114 115 public void test_close() { 116 out = new PipedOutputStream(); 117 rt = new Thread(reader = new PReader(out)); 118 rt.start(); 119 try { 120 out.close(); 121 } catch (IOException e) { 122 fail("Test 1: Unexpected IOException: " + e.getMessage()); 123 } 124 } 125 126 public void test_connectLjava_io_PipedInputStream() throws IOException { 127 out = new PipedOutputStream(); 128 129 try { 130 out.connect(new PipedInputStream()); 131 } catch (Exception e) { 132 fail("Test 1: Unexpected exception when connecting: " + 133 e.getLocalizedMessage()); 134 } 135 136 try { 137 out.write('B'); 138 } catch (IOException e) { 139 fail("Test 2: Unexpected IOException when writing after connecting."); 140 } 141 142 try { 143 out.connect(new PipedInputStream()); 144 fail("Test 3: IOException expected when reconnecting the stream."); 145 } catch (IOException e) { 146 // Expected. 147 } 148 149 try { 150 out.connect(null); 151 fail("Test 4: NullPointerException expected."); 152 } catch (NullPointerException e) { 153 // Expected. 154 } 155 } 156 157 public void test_flush() throws Exception { 158 out = new PipedOutputStream(); 159 rt = new Thread(reader = new PReader(out)); 160 rt.start(); 161 out.write(testString.getBytes(), 0, 10); 162 assertTrue("Test 1: Bytes have been written before flush.", reader.available() != 0); 163 out.flush(); 164 assertEquals("Test 2: Flush failed. ", 165 testString.substring(0, 10), reader.read(10)); 166 } 167 168 public void test_write$BII() throws IOException { 169 out = new PipedOutputStream(); 170 171 try { 172 out.write(testString.getBytes(), 0, 5); 173 fail("Test 1: IOException expected."); 174 } catch (IOException e) { 175 // Expected. 176 } 177 178 out = new PipedOutputStream(new PipedInputStream()); 179 180 try { 181 out.write(testString.getBytes(), -1, 10); 182 fail("Test 2: IndexOutOfBoundsException expected."); 183 } catch (IndexOutOfBoundsException e) { 184 // Expected. 185 } 186 187 try { 188 out.write(testString.getBytes(), 0, -1); 189 fail("Test 3: IndexOutOfBoundsException expected."); 190 } catch (IndexOutOfBoundsException e) { 191 // Expected. 192 } 193 194 try { 195 out.write(testString.getBytes(), 5, testString.length()); 196 fail("Test 4: IndexOutOfBoundsException expected."); 197 } catch (IndexOutOfBoundsException e) { 198 // Expected. 199 } 200 201 out.close(); 202 out = new PipedOutputStream(); 203 try { 204 rt = new Thread(reader = new PReader(out)); 205 rt.start(); 206 out.write(testString.getBytes(), 0, testString.length()); 207 out.flush(); 208 assertEquals("Test 5: Bytes read do not match the bytes written. ", 209 testString, reader.read(testString.length())); 210 } catch (IOException e) { 211 fail("Test 5: Unexpected IOException: " + e.getMessage()); 212 } 213 214 reader.getReader().close(); 215 try { 216 out.write(testString.getBytes(), 0, 5); 217 fail("Test 7: IOException expected."); 218 } catch (IOException e) { 219 // Expected. 220 } 221 } 222 223 224 public void test_writeI() throws IOException { 225 out = new PipedOutputStream(); 226 227 try { 228 out.write(42); 229 fail("Test 1: IOException expected."); 230 } catch (IOException e) { 231 // Expected. 232 } 233 234 rt = new Thread(reader = new PReader(out)); 235 rt.start(); 236 out.write('c'); 237 out.flush(); 238 assertEquals("Test 2: The byte read does not match the byte written. ", 239 "c", reader.read(1)); 240 241/* Test disabled due to incomplete implementation, see ticket #92. 242 rt.interrupt(); 243 244 try { 245 out.write(42); 246 fail("Test 3: IOException expected."); 247 } catch (IOException e) { 248 // Expected. 249 } 250 } 251*/ 252 reader.getReader().close(); 253 try { 254 out.write(42); 255 fail("Test 4: IOException expected."); 256 } catch (IOException e) { 257 // Expected. 258 } 259 } 260 261 /** 262 * Tears down the fixture, for example, close a network connection. This 263 * method is called after a test is executed. 264 */ 265 protected void tearDown() throws Exception { 266 if (rt != null) 267 rt.interrupt(); 268 super.tearDown(); 269 } 270} 271