The Android Open Source Project | 0eec464 | 2012-04-01 00:00:00 -0700 | [diff] [blame] | 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 | |
| 18 | package tests.api.java.io; |
| 19 | |
| 20 | import java.io.CharArrayReader; |
| 21 | import java.io.IOException; |
| 22 | |
| 23 | import dalvik.annotation.AndroidOnly; |
| 24 | import dalvik.annotation.TestLevel; |
| 25 | import dalvik.annotation.TestTargetClass; |
| 26 | import dalvik.annotation.TestTargetNew; |
| 27 | |
| 28 | @TestTargetClass(CharArrayReader.class) |
| 29 | public class CharArrayReaderTest extends junit.framework.TestCase { |
| 30 | |
| 31 | char[] hw = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' }; |
| 32 | |
| 33 | CharArrayReader cr; |
| 34 | |
| 35 | /** |
| 36 | * @tests java.io.CharArrayReader#CharArrayReader(char[]) |
| 37 | */ |
| 38 | @TestTargetNew( |
| 39 | level = TestLevel.COMPLETE, |
| 40 | notes = "Verifies CharArrayReader(char[] buf) constructor.", |
| 41 | method = "CharArrayReader", |
| 42 | args = {char[].class} |
| 43 | ) |
| 44 | public void test_Constructor$C() { |
| 45 | // Test for method java.io.CharArrayReader(char []) |
| 46 | |
| 47 | try { |
| 48 | cr = new CharArrayReader(hw); |
| 49 | assertTrue("Failed to create reader", cr.ready()); |
| 50 | } catch (IOException e) { |
| 51 | fail("Exception determining ready state : " + e.getMessage()); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @tests java.io.CharArrayReader#CharArrayReader(char[], int, int) |
| 57 | */ |
| 58 | @TestTargetNew( |
| 59 | level = TestLevel.COMPLETE, |
| 60 | method = "CharArrayReader", |
| 61 | args = {char[].class, int.class, int.class} |
| 62 | ) |
| 63 | public void test_Constructor$CII() throws IOException { |
| 64 | try { |
| 65 | cr = new CharArrayReader(null, 0, 0); |
| 66 | fail("Test 1: NullPointerException expected."); |
| 67 | } catch (NullPointerException e) { |
| 68 | // Expected. |
| 69 | } |
| 70 | try { |
| 71 | cr = new CharArrayReader(hw, -1, 0); |
| 72 | fail("Test 2: IllegalArgumentException expected."); |
| 73 | } catch (IllegalArgumentException e) { |
| 74 | // Expected. |
| 75 | } |
| 76 | try { |
| 77 | cr = new CharArrayReader(hw, 0, -1); |
| 78 | fail("Test 3: IllegalArgumentException expected."); |
| 79 | } catch (IllegalArgumentException e) { |
| 80 | // Expected. |
| 81 | } |
| 82 | try { |
| 83 | cr = new CharArrayReader(hw, hw.length + 1, 1); |
| 84 | fail("Test 4: IllegalArgumentException expected."); |
| 85 | } catch (IllegalArgumentException e) { |
| 86 | // Expected. |
| 87 | } |
| 88 | |
| 89 | cr = new CharArrayReader(hw, 5, 5); |
| 90 | assertTrue("Test 5: Failed to create reader", cr.ready()); |
| 91 | assertEquals("Test 6: Incorrect character read;", |
| 92 | 'W', cr.read()); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @tests java.io.CharArrayReader#close() |
| 97 | */ |
| 98 | @TestTargetNew( |
| 99 | level = TestLevel.COMPLETE, |
| 100 | notes = "Verifies close() method.", |
| 101 | method = "close", |
| 102 | args = {} |
| 103 | ) |
| 104 | public void test_close() { |
| 105 | cr = new CharArrayReader(hw); |
| 106 | cr.close(); |
| 107 | try { |
| 108 | cr.read(); |
| 109 | fail("Failed to throw exception on read from closed stream"); |
| 110 | } catch (IOException e) { |
| 111 | // Expected. |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @tests java.io.CharArrayReader#mark(int) |
| 118 | */ |
| 119 | @TestTargetNew( |
| 120 | level = TestLevel.COMPLETE, |
| 121 | method = "mark", |
| 122 | args = {int.class} |
| 123 | ) |
| 124 | public void test_markI() throws IOException { |
| 125 | // Test for method void java.io.CharArrayReader.mark(int) |
| 126 | cr = new CharArrayReader(hw); |
| 127 | cr.skip(5L); |
| 128 | cr.mark(100); |
| 129 | cr.read(); |
| 130 | cr.reset(); |
| 131 | assertEquals("Test 1: Failed to mark correct position;", |
| 132 | 'W', cr.read()); |
| 133 | |
| 134 | cr.close(); |
| 135 | try { |
| 136 | cr.mark(100); |
| 137 | fail("Test 2: IOException expected."); |
| 138 | } catch (IOException e) { |
| 139 | // Expected. |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @tests java.io.CharArrayReader#markSupported() |
| 145 | */ |
| 146 | @TestTargetNew( |
| 147 | level = TestLevel.COMPLETE, |
| 148 | notes = "Verifies markSupported() method.", |
| 149 | method = "markSupported", |
| 150 | args = {} |
| 151 | ) |
| 152 | public void test_markSupported() { |
| 153 | // Test for method boolean java.io.CharArrayReader.markSupported() |
| 154 | cr = new CharArrayReader(hw); |
| 155 | assertTrue("markSupported returned false", cr.markSupported()); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @tests java.io.CharArrayReader#read() |
| 160 | */ |
| 161 | @TestTargetNew( |
| 162 | level = TestLevel.COMPLETE, |
| 163 | method = "read", |
| 164 | args = {} |
| 165 | ) |
| 166 | public void test_read() throws IOException { |
| 167 | cr = new CharArrayReader(hw); |
| 168 | assertEquals("Test 1: Read returned incorrect char;", |
| 169 | 'H', cr.read()); |
| 170 | cr = new CharArrayReader(new char[] { '\u8765' }); |
| 171 | assertTrue("Test 2: Incorrect double byte char;", |
| 172 | cr.read() == '\u8765'); |
| 173 | |
| 174 | cr.close(); |
| 175 | try { |
| 176 | cr.read(); |
| 177 | fail("Test 3: IOException expected."); |
| 178 | } catch (IOException e) { |
| 179 | // Expected. |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @tests java.io.CharArrayReader#read(char[], int, int) |
| 185 | */ |
| 186 | @TestTargetNew( |
| 187 | level = TestLevel.COMPLETE, |
| 188 | method = "read", |
| 189 | args = {char[].class, int.class, int.class} |
| 190 | ) |
| 191 | @AndroidOnly("The RI throws an IndexOutOfBoundsException instead of an" + |
| 192 | "ArrayIndexOutOfBoundsException. The RI specification does not" + |
| 193 | "define the expected behavior.") |
| 194 | public void test_read$CII() throws IOException { |
| 195 | // Test for method int java.io.CharArrayReader.read(char [], int, int) |
| 196 | char[] c = new char[11]; |
| 197 | cr = new CharArrayReader(hw); |
| 198 | cr.read(c, 1, 10); |
| 199 | assertTrue("Test 1: Read returned incorrect chars.", |
| 200 | new String(c, 1, 10).equals(new String(hw, 0, 10))); |
| 201 | |
| 202 | // Illegal argument checks. |
| 203 | try { |
| 204 | cr.read(null, 1, 0); |
| 205 | fail("Test 2: NullPointerException expected."); |
| 206 | } catch (NullPointerException e) { |
| 207 | // Expected. |
| 208 | } |
| 209 | |
| 210 | try { |
| 211 | cr.read(c , -1, 1); |
| 212 | fail("Test 3: ArrayIndexOutOfBoundsException expected."); |
| 213 | } catch (IndexOutOfBoundsException e) { |
| 214 | // Expected |
| 215 | } |
| 216 | |
| 217 | try { |
| 218 | cr.read(c , 1, -1); |
| 219 | fail("Test 4: ArrayIndexOutOfBoundsException expected."); |
| 220 | } catch (IndexOutOfBoundsException e) { |
| 221 | // Expected |
| 222 | } |
| 223 | |
| 224 | try { |
| 225 | cr.read(c, 1, c.length); |
| 226 | fail("Test 5: ArrayIndexOutOfBoundsException expected."); |
| 227 | } catch (IndexOutOfBoundsException e) { |
| 228 | // Expected |
| 229 | } |
| 230 | |
| 231 | cr.close(); |
| 232 | try { |
| 233 | cr.read(c, 1, 1); |
| 234 | fail("Test 6: IOException expected."); |
| 235 | } catch (IOException e) { |
| 236 | // Expected. |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @tests java.io.CharArrayReader#ready() |
| 242 | */ |
| 243 | @TestTargetNew( |
| 244 | level = TestLevel.COMPLETE, |
| 245 | notes = "Verifies ready() method.", |
| 246 | method = "ready", |
| 247 | args = {} |
| 248 | ) |
| 249 | public void test_ready() { |
| 250 | // Test for method boolean java.io.CharArrayReader.ready() |
| 251 | cr = new CharArrayReader(hw); |
| 252 | boolean expectException = false; |
| 253 | try { |
| 254 | assertTrue("ready returned false", cr.ready()); |
| 255 | cr.skip(1000); |
| 256 | assertTrue("ready returned true", !cr.ready()); |
| 257 | cr.close(); |
| 258 | expectException = true; |
| 259 | cr.ready(); |
| 260 | fail("No exception 1"); |
| 261 | } catch (IOException e) { |
| 262 | if (!expectException) |
| 263 | fail("Unexpected: " + e); |
| 264 | } |
| 265 | try { |
| 266 | cr = new CharArrayReader(hw); |
| 267 | cr.close(); |
| 268 | cr.ready(); |
| 269 | fail("No exception 2"); |
| 270 | } catch (IOException e) { |
| 271 | } |
| 272 | |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @tests java.io.CharArrayReader#reset() |
| 277 | */ |
| 278 | @TestTargetNew( |
| 279 | level = TestLevel.COMPLETE, |
| 280 | method = "reset", |
| 281 | args = {} |
| 282 | ) |
| 283 | public void test_reset() throws IOException { |
| 284 | cr = new CharArrayReader(hw); |
| 285 | cr.skip(5L); |
| 286 | cr.mark(100); |
| 287 | cr.read(); |
| 288 | cr.reset(); |
| 289 | assertEquals("Test 1: Reset failed to return to marker position.", |
| 290 | 'W', cr.read()); |
| 291 | |
| 292 | cr.close(); |
| 293 | try { |
| 294 | cr.reset(); |
| 295 | fail("Test 2: IOException expected."); |
| 296 | } catch (IOException e) { |
| 297 | // Expected. |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @tests java.io.CharArrayReader#skip(long) |
| 303 | */ |
| 304 | @TestTargetNew( |
| 305 | level = TestLevel.COMPLETE, |
| 306 | method = "skip", |
| 307 | args = {long.class} |
| 308 | ) |
| 309 | public void test_skipJ() throws IOException { |
| 310 | long skipped = 0; |
| 311 | cr = new CharArrayReader(hw); |
| 312 | skipped = cr.skip(5L); |
| 313 | assertEquals("Test 1: Failed to skip correct number of chars;", |
| 314 | 5L, skipped); |
| 315 | assertEquals("Test 2: Skip skipped wrong chars;", |
| 316 | 'W', cr.read()); |
| 317 | |
| 318 | cr.close(); |
| 319 | try { |
| 320 | cr.skip(1); |
| 321 | fail("Test 3: IOException expected."); |
| 322 | } catch (IOException e) { |
| 323 | // Expected. |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Sets up the fixture, for example, open a network connection. This method |
| 329 | * is called before a test is executed. |
| 330 | */ |
| 331 | protected void setUp() { |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Tears down the fixture, for example, close a network connection. This |
| 336 | * method is called after a test is executed. |
| 337 | */ |
| 338 | protected void tearDown() { |
| 339 | if (cr != null) |
| 340 | cr.close(); |
| 341 | } |
| 342 | } |