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.PipedReader; 22import java.io.PipedWriter; 23 24import junit.framework.TestCase; 25 26public class PipedReaderTest extends TestCase { 27 28 static class PWriter implements Runnable { 29 public PipedWriter pw; 30 31 public PWriter(PipedReader reader) { 32 try { 33 pw = new PipedWriter(reader); 34 } catch (Exception e) { 35 System.out.println("Couldn't create writer"); 36 } 37 } 38 39 public PWriter() { 40 pw = new PipedWriter(); 41 } 42 43 public void run() { 44 try { 45 char[] c = new char[11]; 46 "Hello World".getChars(0, 11, c, 0); 47 pw.write(c); 48 Thread.sleep(10000); 49 } catch (InterruptedException e) { 50 } catch (Exception e) { 51 System.out.println("Exception occurred: " + e.toString()); 52 } 53 } 54 } 55 56 PipedReader preader; 57 58 PWriter pwriter; 59 60 Thread t; 61 62 /** 63 * java.io.PipedReader#PipedReader() 64 */ 65 public void test_Constructor() { 66 // Used in test 67 } 68 69 /** 70 * java.io.PipedReader#PipedReader(java.io.PipedWriter) 71 */ 72 public void test_ConstructorLjava_io_PipedWriter() throws IOException { 73 preader = new PipedReader(new PipedWriter()); 74 } 75 76 /** 77 * java.io.PipedReader#PipedReader(java.io.PipedWriter, 78 *int) 79 * @since 1.6 80 */ 81 public void test_Constructor_LPipedWriter_I() throws Exception { 82 // Test for method java.io.PipedReader(java.io.PipedWriter, 83 // int) 84 try { 85 preader = new PipedReader(null, -1); 86 fail("Should throw IllegalArgumentException"); //$NON-NLS-1$ 87 } catch (IllegalArgumentException e) { 88 // expected 89 } 90 91 try { 92 preader = new PipedReader(null, 0); 93 fail("Should throw IllegalArgumentException"); //$NON-NLS-1$ 94 } catch (IllegalArgumentException e) { 95 // expected 96 } 97 } 98 99 /** 100 * java.io.PipedReader#PipedReader(int) 101 * @since 1.6 102 */ 103 public void test_Constructor_I() throws Exception { 104 // Test for method java.io.PipedReader(int) 105 try { 106 preader = new PipedReader(-1); 107 fail("Should throw IllegalArgumentException"); //$NON-NLS-1$ 108 } catch (IllegalArgumentException e) { 109 // expected 110 } 111 112 try { 113 preader = new PipedReader(0); 114 fail("Should throw IllegalArgumentException"); //$NON-NLS-1$ 115 } catch (IllegalArgumentException e) { 116 // expected 117 } 118 } 119 120 /** 121 * java.io.PipedReader#close() 122 */ 123 public void test_close() throws Exception { 124 char[] c = null; 125 preader = new PipedReader(); 126 t = new Thread(new PWriter(preader), ""); 127 t.start(); 128 Thread.sleep(500); // Allow writer to start 129 c = new char[11]; 130 preader.read(c, 0, 11); 131 preader.close(); 132 assertEquals("Read incorrect chars", "Hello World", new String(c)); 133 } 134 135 /** 136 * java.io.PipedReader#connect(java.io.PipedWriter) 137 */ 138 public void test_connectLjava_io_PipedWriter() throws Exception { 139 char[] c = null; 140 141 preader = new PipedReader(); 142 t = new Thread(pwriter = new PWriter(), ""); 143 preader.connect(pwriter.pw); 144 t.start(); 145 Thread.sleep(500); // Allow writer to start 146 c = new char[11]; 147 preader.read(c, 0, 11); 148 149 assertEquals("Read incorrect chars", "Hello World", new String(c)); 150 try { 151 preader.connect(pwriter.pw); 152 fail("Failed to throw exception connecting to pre-connected reader"); 153 } catch (IOException e) { 154 // Expected 155 } 156 } 157 158 /** 159 * java.io.PipedReader#read() 160 */ 161 public void test_read() throws Exception { 162 char[] c = null; 163 preader = new PipedReader(); 164 t = new Thread(new PWriter(preader), ""); 165 t.start(); 166 Thread.sleep(500); // Allow writer to start 167 c = new char[11]; 168 for (int i = 0; i < c.length; i++) { 169 c[i] = (char) preader.read(); 170 } 171 assertEquals("Read incorrect chars", "Hello World", new String(c)); 172 } 173 174 /** 175 * java.io.PipedReader#read(char[], int, int) 176 */ 177 public void test_read$CII() throws Exception { 178 char[] c = null; 179 preader = new PipedReader(); 180 t = new Thread(new PWriter(preader), ""); 181 t.start(); 182 Thread.sleep(500); // Allow writer to start 183 c = new char[11]; 184 int n = 0; 185 int x = n; 186 while (x < 11) { 187 n = preader.read(c, x, 11 - x); 188 x = x + n; 189 } 190 assertEquals("Read incorrect chars", "Hello World", new String(c)); 191 try { 192 preader.close(); 193 preader.read(c, 8, 7); 194 fail("Failed to throw exception reading from closed reader"); 195 } catch (IOException e) { 196 // Expected 197 } 198 } 199 200 public void test_read$CII_ExceptionPriority() throws IOException { 201 // Regression for HARMONY-387 202 PipedWriter pw = new PipedWriter(); 203 PipedReader obj = null; 204 try { 205 obj = new PipedReader(pw); 206 obj.read(new char[0], (int) 0, (int) -1); 207 fail(); 208 } catch (IndexOutOfBoundsException expected) { 209 } 210 } 211 212 public void test_read$CII_ExceptionPriority2() throws IOException { 213 PipedWriter pw = new PipedWriter(); 214 PipedReader obj = null; 215 try { 216 obj = new PipedReader(pw); 217 obj.read(new char[0], (int) -1, (int) 0); 218 fail(); 219 } catch (IndexOutOfBoundsException expected) { 220 } 221 } 222 223 public void test_read$CII_ExceptionPriority3() throws IOException { 224 PipedWriter pw = new PipedWriter(); 225 PipedReader obj = null; 226 try { 227 obj = new PipedReader(pw); 228 obj.read(new char[0], (int) -1, (int) -1); 229 fail(); 230 } catch (IndexOutOfBoundsException expected) { 231 } 232 } 233 234 public void test_read$CII_ExceptionPriority4() throws IOException { 235 PipedWriter pw = new PipedWriter(); 236 PipedReader pr = new PipedReader(pw); 237 try { 238 pr.read(null, -1, 1); 239 fail(); 240 } catch (NullPointerException expected) { 241 } catch (IndexOutOfBoundsException expected) { 242 } 243 } 244 245 public void test_read_$CII_IOException() throws IOException { 246 PipedWriter pw = new PipedWriter(); 247 PipedReader pr = new PipedReader(pw); 248 pr.close(); 249 try { 250 pr.read(null, 0, 10); 251 fail("Should throws IOException"); //$NON-NLS-1$ 252 } catch (IOException e) { 253 // expected 254 } finally { 255 pw = null; 256 pr = null; 257 } 258 259 pr = new PipedReader(); 260 pr.close(); 261 try { 262 pr.read(null, 0, 10); 263 fail("Should throws IOException"); //$NON-NLS-1$ 264 } catch (IOException e) { 265 // expected 266 } finally { 267 pr = null; 268 } 269 270 pw = new PipedWriter(); 271 pr = new PipedReader(pw); 272 pr.close(); 273 try { 274 pr.read(new char[10], -1, 0); 275 fail("Should throws IOException"); //$NON-NLS-1$ 276 } catch (IOException e) { 277 // expected 278 } finally { 279 pw = null; 280 pr = null; 281 } 282 283 pw = new PipedWriter(); 284 pr = new PipedReader(pw); 285 pr.close(); 286 try { 287 pr.read(new char[10], 0, -1); 288 fail("Should throws IOException"); //$NON-NLS-1$ 289 } catch (IOException e) { 290 // expected 291 } finally { 292 pw = null; 293 pr = null; 294 } 295 296 pw = new PipedWriter(); 297 pr = new PipedReader(pw); 298 pr.close(); 299 try { 300 pr.read(new char[10], 1, 10); 301 fail("Should throws IOException"); //$NON-NLS-1$ 302 } catch (IOException e) { 303 // expected 304 } finally { 305 pw = null; 306 pr = null; 307 } 308 309 pw = new PipedWriter(); 310 pr = new PipedReader(pw); 311 pr.close(); 312 try { 313 pr.read(new char[0], -1, -1); 314 fail("should throw IOException"); //$NON-NLS-1$ 315 } catch (IOException e) { 316 // expected 317 } finally { 318 pw = null; 319 pr = null; 320 } 321 322 pw = new PipedWriter(); 323 pr = new PipedReader(pw); 324 pr.close(); 325 try { 326 pr.read(null, 0, 1); 327 fail("should throw IOException"); //$NON-NLS-1$ 328 } catch (IOException e) { 329 // expected 330 } finally { 331 pw = null; 332 pr = null; 333 } 334 335 pw = new PipedWriter(); 336 pr = new PipedReader(pw); 337 try { 338 pr.read(null, 0, -1); 339 fail("should throw NullPointerException"); //$NON-NLS-1$ 340 } catch (NullPointerException e) { 341 // expected 342 } finally { 343 pw = null; 344 pr = null; 345 } 346 347 pw = new PipedWriter(); 348 pr = new PipedReader(pw); 349 try { 350 pr.read(new char[10], 11, 0); 351 fail("should throw IndexOutOfBoundsException"); //$NON-NLS-1$ 352 } catch (IndexOutOfBoundsException e) { 353 // expected 354 } finally { 355 pw = null; 356 pr = null; 357 } 358 359 pw = new PipedWriter(); 360 pr = new PipedReader(pw); 361 try { 362 pr.read(null, 1, 0); 363 fail("should throw NullPointerException"); //$NON-NLS-1$ 364 } catch (NullPointerException e) { 365 // expected 366 } finally { 367 pw = null; 368 pr = null; 369 } 370 } 371 372 /** 373 * java.io.PipedReader#ready() 374 */ 375 public void test_ready() throws Exception { 376 char[] c = null; 377 preader = new PipedReader(); 378 t = new Thread(new PWriter(preader), ""); 379 t.start(); 380 Thread.sleep(500); // Allow writer to start 381 assertTrue("Reader should be ready", preader.ready()); 382 c = new char[11]; 383 for (int i = 0; i < c.length; i++) 384 c[i] = (char) preader.read(); 385 assertFalse("Reader should not be ready after reading all chars", 386 preader.ready()); 387 } 388 389 /** 390 * Tears down the fixture, for example, close a network connection. This 391 * method is called after a test is executed. 392 */ 393 protected void tearDown() throws Exception { 394 if (t != null) { 395 t.interrupt(); 396 } 397 super.tearDown(); 398 } 399} 400