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