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.CharArrayReader; 21import java.io.FilterReader; 22import java.io.IOException; 23import java.io.PushbackReader; 24import java.io.Reader; 25import java.io.StringReader; 26 27public class PushbackReaderTest extends junit.framework.TestCase { 28 29 PushbackReader pbr; 30 31 String pbString = "Hello World"; 32 33 /** 34 * java.io.PushbackReader#PushbackReader(java.io.Reader) 35 */ 36 public void test_ConstructorLjava_io_Reader() { 37 // Test for method java.io.PushbackReader(java.io.Reader) 38 try { 39 pbr.close(); 40 pbr = new PushbackReader(new StringReader(pbString)); 41 char buf[] = new char[5]; 42 pbr.read(buf, 0, 5); 43 pbr.unread(buf); 44 } catch (IOException e) { 45 // Correct 46 return; 47 } 48 fail("Created reader with buffer larger than 1"); 49 } 50 51 /** 52 * java.io.PushbackReader#PushbackReader(java.io.Reader, int) 53 */ 54 public void test_ConstructorLjava_io_ReaderI() { 55 // Test for method java.io.PushbackReader(java.io.Reader, int) 56 assertTrue("Used to test", true); 57 } 58 59 /** 60 * java.io.PushbackReader#close() 61 */ 62 public void test_close() { 63 // Test for method void java.io.PushbackReader.close() 64 try { 65 pbr.close(); 66 pbr.read(); 67 } catch (Exception e) { 68 return; 69 } 70 fail("Failed to throw exception reading from closed reader"); 71 } 72 73 /** 74 * java.io.PushbackReader#mark(int) 75 */ 76 public void test_markI() { 77 try { 78 pbr.mark(3); 79 } catch (IOException e) { 80 // correct 81 return; 82 } 83 fail("mark failed to throw expected IOException"); 84 } 85 86 /** 87 * java.io.PushbackReader#markSupported() 88 */ 89 public void test_markSupported() { 90 // Test for method boolean java.io.PushbackReader.markSupported() 91 assertTrue("markSupported returned true", !pbr.markSupported()); 92 } 93 94 /** 95 * java.io.PushbackReader#read() 96 */ 97 public void test_read() { 98 // Test for method int java.io.PushbackReader.read() 99 try { 100 char c; 101 pbr.read(); 102 c = (char) pbr.read(); 103 assertTrue("Failed to read char: " + c, c == pbString.charAt(1)); 104 Reader reader = new PushbackReader(new CharArrayReader( 105 new char[] { '\u8765' })); 106 assertTrue("Wrong double byte character", reader.read() == '\u8765'); 107 } catch (IOException e) { 108 fail("IOException during read test : " + e.getMessage()); 109 } 110 } 111 112 /** 113 * java.io.PushbackReader#read(char[], int, int) 114 */ 115 public void test_read$CII() { 116 // Test for method int java.io.PushbackReader.read(char [], int, int) 117 try { 118 char[] c = new char[5]; 119 pbr.read(c, 0, 5); 120 assertTrue("Failed to read chars", new String(c).equals(pbString 121 .substring(0, 5))); 122 } catch (IOException e) { 123 fail("IOException during read test : " + e.getMessage()); 124 } 125 } 126 127 public void test_read_$CII_Exception() throws IOException { 128 pbr = new PushbackReader(new StringReader(pbString), 10); 129 130 char[] charArray = new char[10]; 131 132 try { 133 pbr.read(null, 1, 0); 134 fail(); 135 } catch (NullPointerException expected) { 136 } 137 try { 138 pbr.read(charArray, 0, -1); 139 fail(); 140 } catch (IndexOutOfBoundsException expected) { 141 } 142 try { 143 pbr.read(charArray, -1, 0); 144 fail(); 145 } catch (IndexOutOfBoundsException expected) { 146 } 147 148 pbr.read(charArray, 0, 0); 149 pbr.read(charArray, 0, charArray.length); 150 pbr.read(charArray, charArray.length, 0); 151 152 try { 153 pbr.read(charArray, charArray.length + 1, 0); 154 fail(); 155 } catch (IndexOutOfBoundsException expected) { 156 } 157 try { 158 pbr.read(charArray, 0, charArray.length + 1); 159 fail(); 160 } catch (IndexOutOfBoundsException expected) { 161 } 162 163 // Can't read from a closed PushbackReader. 164 pbr.close(); 165 try { 166 pbr.read(charArray, 0, 1); 167 fail(); 168 } catch (IOException expected) { 169 } 170 } 171 172 /** 173 * java.io.PushbackReader#ready() 174 */ 175 public void test_ready() { 176 // Test for method boolean java.io.PushbackReader.ready() 177 try { 178 char[] c = new char[11]; 179 if (c.length > 0) 180 ;// use c to avoid warning msg 181 assertTrue("Ready stream returned false to ready()", pbr.ready()); 182 } catch (IOException e) { 183 fail("IOException during ready() test : " + e.getMessage()); 184 } 185 } 186 187 /** 188 * java.io.PushbackReader#reset() 189 */ 190 public void test_reset() { 191 try { 192 pbr.reset(); 193 } catch (IOException e) { 194 // correct 195 return; 196 } 197 fail("mark failed to throw expected IOException"); 198 } 199 200 /** 201 * java.io.PushbackReader#unread(char[]) 202 */ 203 public void test_unread$C() { 204 // Test for method void java.io.PushbackReader.unread(char []) 205 try { 206 char[] c = new char[5]; 207 pbr.read(c, 0, 5); 208 pbr.unread(c); 209 pbr.read(c, 0, 5); 210 assertTrue("Failed to unread chars", new String(c).equals(pbString 211 .substring(0, 5))); 212 } catch (IOException e) { 213 fail("IOException during read test : " + e.getMessage()); 214 } 215 } 216 217 /** 218 * java.io.PushbackReader#skip(long) 219 */ 220 public void test_skip$J() { 221 char chars[] = new char[] { 'h', 'e', 'l', 'l', 'o' }; 222 for (int i = 0; i < 3; i++) { 223 Reader reader, reader2; 224 switch (i) { 225 case 0: 226 reader = new StringReader(new String(chars)); 227 reader2 = new StringReader(new String(chars)); 228 break; 229 case 1: 230 reader = new FilterReader(new StringReader(new String(chars))) { 231 }; 232 reader2 = new FilterReader(new StringReader(new String(chars))) { 233 }; 234 break; 235 default: 236 reader = new CharArrayReader(chars); 237 reader2 = new CharArrayReader(chars); 238 } 239 PushbackReader pReader = new PushbackReader(reader, 2); 240 PushbackReader pReader2 = new PushbackReader(reader2, 2); 241 boolean skipped = false; 242 long numSkipped = 0; 243 try { 244 numSkipped = pReader2.skip(3); 245 pReader2.unread('a'); 246 pReader2.unread('b'); 247 numSkipped += pReader2.skip(10); 248 numSkipped += pReader2.skip(10); 249 numSkipped += pReader2.skip(10); 250 numSkipped += pReader2.skip(10); 251 numSkipped += pReader2.skip(10); 252 numSkipped += pReader2.skip(10); 253 assertEquals("Did not skip correct number of characters", 254 7, numSkipped); 255 numSkipped = 0; 256 numSkipped += pReader.skip(2); 257 pReader.unread('i'); 258 numSkipped += pReader.skip(2); 259 numSkipped += pReader.skip(0); 260 skipped = true; 261 numSkipped += pReader.skip(-1); 262 fail("Failed to throw " 263 + new IllegalArgumentException().getClass().getName()); 264 } catch (IllegalArgumentException e) { 265 assertTrue("Failed to skip characters" + e, skipped); 266 } catch (IOException e) { 267 fail("Failed to skip characters" + e); 268 } 269 try { 270 numSkipped += pReader.skip(1); 271 numSkipped += pReader.skip(1); 272 numSkipped += pReader.skip(1); 273 assertEquals("Failed to skip all characters", 6, numSkipped); 274 long nextSkipped = pReader.skip(1); 275 assertEquals("skipped empty reader", 0, nextSkipped); 276 } catch (IOException e) { 277 fail("Failed to skip more characters" + e); 278 } 279 } 280 } 281 282 /** 283 * java.io.PushbackReader#unread(char[], int, int) 284 */ 285 public void test_unread$CII() { 286 // Test for method void java.io.PushbackReader.unread(char [], int, int) 287 try { 288 char[] c = new char[5]; 289 pbr.read(c, 0, 5); 290 pbr.unread(c, 0, 2); 291 pbr.read(c, 0, 5); 292 assertTrue("Failed to unread chars", new String(c).equals(pbString 293 .substring(0, 2) 294 + pbString.substring(5, 8))); 295 } catch (IOException e) { 296 fail("IOException during unread test : " + e.getMessage()); 297 } 298 } 299 300 /** 301 * java.io.PushbackReader#unread(char[], int, int) 302 */ 303 public void test_unread_$CII_NullPointerException() throws IOException { 304 //a pushback reader with one character buffer 305 pbr = new PushbackReader(new StringReader(pbString)); 306 307 try { 308 pbr.unread(null, 0, 1); 309 fail("should throw NullPointerException"); 310 } catch (NullPointerException e) { 311 // expected 312 } 313 } 314 315 /** 316 * java.io.PushbackReader#unread(char[], int, int) 317 */ 318 public void test_unread_$CII_Exception_InsufficientBuffer() throws IOException { 319 //a pushback reader with one character buffer 320 pbr = new PushbackReader(new StringReader(pbString)); 321 322 //if count > buffer's size , should throw IOException 323 try { 324 pbr.unread(new char[pbString.length()], 0, 2); 325 fail("should throw IOException"); 326 } catch (IOException e) { 327 // expected 328 } 329 } 330 331 /** 332 * java.io.PushbackReader#unread(char[], int, int) 333 */ 334 public void test_unread_$CII_ArrayIndexOutOfBoundsException() throws IOException { 335 //a pushback reader with one character buffer 336 pbr = new PushbackReader(new StringReader(pbString)); 337 338 try { 339 pbr.unread(new char[pbString.length()], -1, -1); 340 fail("should throw ArrayIndexOutOfBoundsException"); 341 } catch (ArrayIndexOutOfBoundsException e) { 342 // expected 343 } 344 } 345 346 /** 347 * java.io.PushbackReader#unread(int) 348 */ 349 public void test_unreadI() { 350 // Test for method void java.io.PushbackReader.unread(int) 351 352 try { 353 int c; 354 pbr.read(); 355 c = pbr.read(); 356 pbr.unread(c); 357 assertTrue("Failed to unread char", pbr.read() == c); 358 } catch (IOException e) { 359 fail("IOException during unread test : " + e.getMessage()); 360 } 361 } 362 363 /** 364 * Sets up the fixture, for example, open a network connection. This method 365 * is called before a test is executed. 366 */ 367 protected void setUp() { 368 pbr = new PushbackReader(new StringReader(pbString), 10); 369 } 370 371 /** 372 * Tears down the fixture, for example, close a network connection. This 373 * method is called after a test is executed. 374 */ 375 protected void tearDown() { 376 try { 377 pbr.close(); 378 } catch (IOException e) { 379 } 380 } 381} 382