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 org.apache.harmony.tests.java.text; 19 20 import java.text.CharacterIterator; 21 import java.text.StringCharacterIterator; 22 23 import junit.framework.TestCase; 24 25 public class StringCharacterIteratorTest extends TestCase { 26 27 /** 28 * @tests java.text.StringCharacterIterator.StringCharacterIterator(String, 29 * int) 30 */ 31 public void test_ConstructorI() { 32 assertNotNull(new StringCharacterIterator("value", 0)); 33 assertNotNull(new StringCharacterIterator("value", "value".length())); 34 assertNotNull(new StringCharacterIterator("", 0)); 35 try { 36 new StringCharacterIterator(null, 0); 37 fail("Assert 0: no null pointer"); 38 } catch (NullPointerException e) { 39 // expected 40 } 41 42 try { 43 new StringCharacterIterator("value", -1); 44 fail("Assert 1: no illegal argument"); 45 } catch (IllegalArgumentException e) { 46 // expected 47 } 48 49 try { 50 new StringCharacterIterator("value", "value".length() + 1); 51 fail("Assert 2: no illegal argument"); 52 } catch (IllegalArgumentException e) { 53 // expected 54 } 55 } 56 57 /** 58 * @tests java.text.StringCharacterIterator(String, int, int, int) 59 */ 60 public void test_ConstructorIII() { 61 assertNotNull(new StringCharacterIterator("value", 0, "value".length(), 62 0)); 63 assertNotNull(new StringCharacterIterator("value", 0, "value".length(), 64 1)); 65 assertNotNull(new StringCharacterIterator("", 0, 0, 0)); 66 67 try { 68 new StringCharacterIterator(null, 0, 0, 0); 69 fail("no null pointer"); 70 } catch (NullPointerException e) { 71 // Expected 72 } 73 74 try { 75 new StringCharacterIterator("value", -1, "value".length(), 0); 76 fail("no illegal argument: invalid begin"); 77 } catch (IllegalArgumentException e) { 78 // Expected 79 } 80 81 try { 82 new StringCharacterIterator("value", 0, "value".length() + 1, 0); 83 fail("no illegal argument: invalid end"); 84 } catch (IllegalArgumentException e) { 85 // Expected 86 } 87 88 try { 89 new StringCharacterIterator("value", 2, 1, 0); 90 fail("no illegal argument: start greater than end"); 91 } catch (IllegalArgumentException e) { 92 // Expected 93 } 94 95 try { 96 new StringCharacterIterator("value", 2, 1, 2); 97 fail("no illegal argument: start greater than end"); 98 } catch (IllegalArgumentException e) { 99 // Expected 100 } 101 102 try { 103 new StringCharacterIterator("value", 2, 4, 1); 104 fail("no illegal argument: location greater than start"); 105 } catch (IllegalArgumentException e) { 106 // Expected 107 } 108 109 try { 110 new StringCharacterIterator("value", 0, 2, 3); 111 fail("no illegal argument: location greater than start"); 112 } catch (IllegalArgumentException e) { 113 // Expected 114 } 115 } 116 117 /** 118 * @tests java.text.StringCharacterIterator.equals(Object) 119 */ 120 public void test_equalsLjava_lang_Object() { 121 StringCharacterIterator sci0 = new StringCharacterIterator("fixture"); 122 assertEquals(sci0, sci0); 123 assertFalse(sci0.equals(null)); 124 assertFalse(sci0.equals("fixture")); 125 126 StringCharacterIterator sci1 = new StringCharacterIterator("fixture"); 127 assertEquals(sci0, sci1); 128 129 sci1.next(); 130 assertFalse(sci0.equals(sci1)); 131 sci0.next(); 132 assertEquals(sci0, sci1); 133 134 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 135 6, 4); 136 StringCharacterIterator it2 = new StringCharacterIterator("xxstinx", 2, 137 6, 4); 138 assertTrue("Range is equal", !it1.equals(it2)); 139 StringCharacterIterator it3 = new StringCharacterIterator("testing", 2, 140 6, 2); 141 it3.setIndex(4); 142 assertTrue("Not equal", it1.equals(it3)); 143 } 144 145 /** 146 * @tests java.text.StringCharacterIterator.clone() 147 */ 148 public void test_clone() { 149 StringCharacterIterator sci0 = new StringCharacterIterator("fixture"); 150 assertSame(sci0, sci0); 151 StringCharacterIterator sci1 = (StringCharacterIterator) sci0.clone(); 152 assertNotSame(sci0, sci1); 153 assertEquals(sci0, sci1); 154 155 StringCharacterIterator it = new StringCharacterIterator("testing", 2, 156 6, 4); 157 StringCharacterIterator clone = (StringCharacterIterator) it.clone(); 158 assertTrue("Clone not equal", it.equals(clone)); 159 } 160 161 /** 162 * @tests java.text.StringCharacterIterator.current() 163 */ 164 public void test_current() { 165 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 166 assertEquals('f', fixture.current()); 167 fixture.next(); 168 assertEquals('i', fixture.current()); 169 170 StringCharacterIterator it = 171 new StringCharacterIterator("testing", 2, 6, 4); 172 assertEquals("Wrong current char", 'i', it.current()); 173 } 174 175 /** 176 * @tests java.text.StringCharacterIterator.first() 177 */ 178 public void test_first() { 179 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 180 assertEquals('f', fixture.first()); 181 fixture.next(); 182 assertEquals('f', fixture.first()); 183 fixture = new StringCharacterIterator("fixture", 1); 184 assertEquals('f', fixture.first()); 185 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 186 2); 187 assertEquals('i', fixture.first()); 188 189 StringCharacterIterator it1 = 190 new StringCharacterIterator("testing", 2, 6, 4); 191 assertEquals("Wrong first char", 's', it1.first()); 192 assertEquals("Wrong next char", 't', it1.next()); 193 it1 = new StringCharacterIterator("testing", 2, 2, 2); 194 assertTrue("Not DONE", it1.first() == CharacterIterator.DONE); 195 } 196 197 /** 198 * @tests java.text.StringCharacterIterator.getBeginIndex() 199 */ 200 public void test_getBeginIndex() { 201 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 202 assertEquals(0, fixture.getBeginIndex()); 203 fixture = new StringCharacterIterator("fixture", 1); 204 assertEquals(0, fixture.getBeginIndex()); 205 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 206 2); 207 assertEquals(1, fixture.getBeginIndex()); 208 209 StringCharacterIterator it1 = 210 new StringCharacterIterator("testing", 2, 6, 4); 211 assertEquals("Wrong begin index 2", 2, it1.getBeginIndex()); 212 } 213 214 /** 215 * @tests java.text.StringCharacterIterator.getEndIndex() 216 */ 217 public void test_getEndIndex() { 218 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 219 assertEquals("fixture".length(), fixture.getEndIndex()); 220 fixture = new StringCharacterIterator("fixture", 1); 221 assertEquals("fixture".length(), fixture.getEndIndex()); 222 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 223 2); 224 assertEquals("fixture".length(), fixture.getEndIndex()); 225 fixture = new StringCharacterIterator("fixture", 1, 4, 2); 226 assertEquals(4, fixture.getEndIndex()); 227 228 StringCharacterIterator it1 = 229 new StringCharacterIterator("testing", 2, 6, 4); 230 assertEquals("Wrong end index 6", 6, it1.getEndIndex()); 231 } 232 233 /** 234 * @tests java.text.StringCharacterIterator.getIndex() 235 */ 236 public void testGetIndex() { 237 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 238 assertEquals(0, fixture.getIndex()); 239 fixture = new StringCharacterIterator("fixture", 1); 240 assertEquals(1, fixture.getIndex()); 241 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 242 2); 243 assertEquals(2, fixture.getIndex()); 244 fixture = new StringCharacterIterator("fixture", 1, 4, 2); 245 assertEquals(2, fixture.getIndex()); 246 } 247 248 /** 249 * @tests java.text.StringCharacterIterator.last() 250 */ 251 public void testLast() { 252 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 253 assertEquals('e', fixture.last()); 254 fixture.next(); 255 assertEquals('e', fixture.last()); 256 fixture = new StringCharacterIterator("fixture", 1); 257 assertEquals('e', fixture.last()); 258 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 259 2); 260 assertEquals('e', fixture.last()); 261 fixture = new StringCharacterIterator("fixture", 1, 4, 2); 262 assertEquals('t', fixture.last()); 263 } 264 265 /** 266 * @tests java.text.StringCharacterIterator.next() 267 */ 268 public void test_next() { 269 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 270 assertEquals(0, fixture.getIndex()); 271 assertEquals('i', fixture.next()); 272 assertEquals(1, fixture.getIndex()); 273 assertEquals('x', fixture.next()); 274 assertEquals(2, fixture.getIndex()); 275 assertEquals('t', fixture.next()); 276 assertEquals(3, fixture.getIndex()); 277 assertEquals('u', fixture.next()); 278 assertEquals(4, fixture.getIndex()); 279 assertEquals('r', fixture.next()); 280 assertEquals(5, fixture.getIndex()); 281 assertEquals('e', fixture.next()); 282 assertEquals(6, fixture.getIndex()); 283 assertEquals(CharacterIterator.DONE, fixture.next()); 284 assertEquals(7, fixture.getIndex()); 285 assertEquals(CharacterIterator.DONE, fixture.next()); 286 assertEquals(7, fixture.getIndex()); 287 assertEquals(CharacterIterator.DONE, fixture.next()); 288 assertEquals(7, fixture.getIndex()); 289 290 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 291 6, 3); 292 char result = it1.next(); 293 assertEquals("Wrong next char1", 'i', result); 294 assertEquals("Wrong next char2", 'n', it1.next()); 295 assertTrue("Wrong next char3", it1.next() == CharacterIterator.DONE); 296 assertTrue("Wrong next char4", it1.next() == CharacterIterator.DONE); 297 int index = it1.getIndex(); 298 assertEquals("Wrong index", 6, index); 299 assertTrue("Wrong current char", 300 it1.current() == CharacterIterator.DONE); 301 } 302 303 /** 304 * @tests java.text.StringCharacterIterator.previous() 305 */ 306 public void test_previous() { 307 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 308 assertEquals(CharacterIterator.DONE, fixture.previous()); 309 assertEquals('i', fixture.next()); 310 assertEquals('x', fixture.next()); 311 assertEquals('t', fixture.next()); 312 assertEquals('u', fixture.next()); 313 assertEquals('r', fixture.next()); 314 assertEquals('e', fixture.next()); 315 assertEquals(CharacterIterator.DONE, fixture.next()); 316 assertEquals(CharacterIterator.DONE, fixture.next()); 317 assertEquals(CharacterIterator.DONE, fixture.next()); 318 assertEquals(7, fixture.getIndex()); 319 assertEquals('e', fixture.previous()); 320 assertEquals(6, fixture.getIndex()); 321 assertEquals('r', fixture.previous()); 322 assertEquals(5, fixture.getIndex()); 323 assertEquals('u', fixture.previous()); 324 assertEquals(4, fixture.getIndex()); 325 assertEquals('t', fixture.previous()); 326 assertEquals(3, fixture.getIndex()); 327 assertEquals('x', fixture.previous()); 328 assertEquals(2, fixture.getIndex()); 329 assertEquals('i', fixture.previous()); 330 assertEquals(1, fixture.getIndex()); 331 assertEquals('f', fixture.previous()); 332 assertEquals(0, fixture.getIndex()); 333 assertEquals(CharacterIterator.DONE, fixture.previous()); 334 assertEquals(0, fixture.getIndex()); 335 336 StringCharacterIterator it1 = 337 new StringCharacterIterator("testing", 2, 6, 4); 338 assertEquals("Wrong previous char1", 't', it1.previous()); 339 assertEquals("Wrong previous char2", 's', it1.previous()); 340 assertTrue("Wrong previous char3", 341 it1.previous() == CharacterIterator.DONE); 342 assertTrue("Wrong previous char4", 343 it1.previous() == CharacterIterator.DONE); 344 assertEquals("Wrong index", 2, it1.getIndex()); 345 assertEquals("Wrong current char", 's', it1.current()); 346 } 347 348 /** 349 * @tests java.text.StringCharacterIterator.setIndex(int) 350 */ 351 public void test_setIndex() { 352 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 353 while (fixture.next() != CharacterIterator.DONE) { 354 // empty 355 } 356 assertEquals("fixture".length(), fixture.getIndex()); 357 fixture.setIndex(0); 358 assertEquals(0, fixture.getIndex()); 359 assertEquals('f', fixture.current()); 360 fixture.setIndex("fixture".length() - 1); 361 assertEquals('e', fixture.current()); 362 try { 363 fixture.setIndex(-1); 364 fail("no illegal argument"); 365 } catch (IllegalArgumentException e) { 366 // expected 367 } 368 369 try { 370 fixture.setIndex("fixture".length() + 1); 371 fail("no illegal argument"); 372 } catch (IllegalArgumentException e) { 373 // expected 374 } 375 } 376 377 /** 378 * @tests java.text.StringCharacterIterator.setText(String) 379 */ 380 public void test_setText() { 381 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 382 fixture.setText("fix"); 383 assertEquals('f', fixture.current()); 384 assertEquals('x', fixture.last()); 385 386 try { 387 fixture.setText(null); 388 fail("no null pointer"); 389 } catch (NullPointerException e) { 390 // expected 391 } 392 } 393 394 /** 395 * @tests java.text.StringCharacterIterator#StringCharacterIterator(java.lang.String) 396 */ 397 public void test_ConstructorLjava_lang_String() { 398 assertNotNull(new StringCharacterIterator("value")); 399 assertNotNull(new StringCharacterIterator("")); 400 try { 401 new StringCharacterIterator(null); 402 fail("Assert 0: no null pointer"); 403 } catch (NullPointerException e) { 404 // expected 405 } 406 407 StringCharacterIterator it = new StringCharacterIterator("testing"); 408 assertEquals("Wrong begin index", 0, it.getBeginIndex()); 409 assertEquals("Wrong end index", 7, it.getEndIndex()); 410 assertEquals("Wrong current index", 0, it.getIndex()); 411 assertEquals("Wrong current char", 't', it.current()); 412 assertEquals("Wrong next char", 'e', it.next()); 413 } 414 415 /** 416 * @tests java.text.StringCharacterIterator#StringCharacterIterator(java.lang.String, 417 * int) 418 */ 419 public void test_ConstructorLjava_lang_StringI() { 420 StringCharacterIterator it = new StringCharacterIterator("testing", 3); 421 assertEquals("Wrong begin index", 0, it.getBeginIndex()); 422 assertEquals("Wrong end index", 7, it.getEndIndex()); 423 assertEquals("Wrong current index", 3, it.getIndex()); 424 assertEquals("Wrong current char", 't', it.current()); 425 assertEquals("Wrong next char", 'i', it.next()); 426 } 427 428 /** 429 * @tests java.text.StringCharacterIterator#StringCharacterIterator(java.lang.String, 430 * int, int, int) 431 */ 432 public void test_ConstructorLjava_lang_StringIII() { 433 StringCharacterIterator it = new StringCharacterIterator("testing", 2, 434 6, 4); 435 assertEquals("Wrong begin index", 2, it.getBeginIndex()); 436 assertEquals("Wrong end index", 6, it.getEndIndex()); 437 assertEquals("Wrong current index", 4, it.getIndex()); 438 assertEquals("Wrong current char", 'i', it.current()); 439 assertEquals("Wrong next char", 'n', it.next()); 440 } 441 442 /** 443 * @tests java.text.StringCharacterIterator#getIndex() 444 */ 445 public void test_getIndex() { 446 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 447 6, 4); 448 assertEquals("Wrong index 4", 4, it1.getIndex()); 449 it1.next(); 450 assertEquals("Wrong index 5", 5, it1.getIndex()); 451 it1.last(); 452 assertEquals("Wrong index 4/2", 5, it1.getIndex()); 453 } 454 455 /** 456 * @tests java.text.StringCharacterIterator#hashCode() 457 */ 458 public void test_hashCode() { 459 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 460 6, 4); 461 StringCharacterIterator it2 = new StringCharacterIterator("xxstinx", 2, 462 6, 4); 463 assertTrue("Hash is equal", it1.hashCode() != it2.hashCode()); 464 StringCharacterIterator it3 = new StringCharacterIterator("testing", 2, 465 6, 2); 466 assertTrue("Hash equal1", it1.hashCode() != it3.hashCode()); 467 it3 = new StringCharacterIterator("testing", 0, 6, 4); 468 assertTrue("Hash equal2", it1.hashCode() != it3.hashCode()); 469 it3 = new StringCharacterIterator("testing", 2, 5, 4); 470 assertTrue("Hash equal3", it1.hashCode() != it3.hashCode()); 471 it3 = new StringCharacterIterator("froging", 2, 6, 4); 472 assertTrue("Hash equal4", it1.hashCode() != it3.hashCode()); 473 474 StringCharacterIterator sci0 = new StringCharacterIterator("fixture"); 475 assertEquals(sci0.hashCode(), sci0.hashCode()); 476 477 StringCharacterIterator sci1 = new StringCharacterIterator("fixture"); 478 assertEquals(sci0.hashCode(), sci1.hashCode()); 479 480 sci1.next(); 481 sci0.next(); 482 assertEquals(sci0.hashCode(), sci1.hashCode()); 483 } 484 485 /** 486 * @tests java.text.StringCharacterIterator#last() 487 */ 488 public void test_last() { 489 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 490 6, 3); 491 assertEquals("Wrong last char", 'n', it1.last()); 492 assertEquals("Wrong previous char", 'i', it1.previous()); 493 it1 = new StringCharacterIterator("testing", 2, 2, 2); 494 assertTrue("Not DONE", it1.last() == CharacterIterator.DONE); 495 } 496 497 /** 498 * @tests java.text.StringCharacterIterator#setIndex(int) 499 */ 500 public void test_setIndexI() { 501 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 502 6, 4); 503 assertEquals("Wrong result1", 's', it1.setIndex(2)); 504 char result = it1.next(); 505 assertTrue("Wrong next char: " + result, result == 't'); 506 assertTrue("Wrong result2", it1.setIndex(6) == CharacterIterator.DONE); 507 assertEquals("Wrong previous char", 'n', it1.previous()); 508 } 509 510 /** 511 * @tests java.text.StringCharacterIterator#setText(java.lang.String) 512 */ 513 public void test_setTextLjava_lang_String() { 514 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 515 6, 4); 516 it1.setText("frog"); 517 assertEquals("Wrong begin index", 0, it1.getBeginIndex()); 518 assertEquals("Wrong end index", 4, it1.getEndIndex()); 519 assertEquals("Wrong current index", 0, it1.getIndex()); 520 } 521 } 522