1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package tests.api.org.xml.sax.helpers; 18 19import junit.framework.TestCase; 20 21import org.xml.sax.Attributes; 22import org.xml.sax.helpers.AttributesImpl; 23 24import dalvik.annotation.TestLevel; 25import dalvik.annotation.TestTargetClass; 26import dalvik.annotation.TestTargetNew; 27 28@TestTargetClass(AttributesImpl.class) 29public class AttributesImplTest extends TestCase { 30 31 private AttributesImpl empty = new AttributesImpl(); 32 33 private AttributesImpl multi = new AttributesImpl(); 34 35 @Override 36 public void setUp() { 37 multi.addAttribute("http://some.uri", "foo", "ns1:foo", 38 "string", "abc"); 39 multi.addAttribute("http://some.uri", "bar", "ns1:bar", 40 "string", "xyz"); 41 multi.addAttribute("http://some.other.uri", "answer", "ns2:answer", 42 "int", "42"); 43 44 multi.addAttribute("", "gabbaHey", "", "string", "1-2-3-4"); 45 multi.addAttribute("", "", "gabba:hey", "string", "1-2-3-4"); 46 } 47 48 @TestTargetNew( 49 level = TestLevel.COMPLETE, 50 method = "AttributesImpl", 51 args = { } 52 ) 53 public void testAttributesImpl() { 54 assertEquals(0, empty.getLength()); 55 assertEquals(5, multi.getLength()); 56 } 57 58 @TestTargetNew( 59 level = TestLevel.COMPLETE, 60 method = "AttributesImpl", 61 args = { Attributes.class } 62 ) 63 public void testAttributesImplAttributes() { 64 // Ordinary case 65 AttributesImpl ai = new AttributesImpl(empty); 66 assertEquals(0, ai.getLength()); 67 68 // Another ordinary case 69 ai = new AttributesImpl(multi); 70 assertEquals(5, ai.getLength()); 71 72 // No Attributes 73 try { 74 ai = new AttributesImpl(null); 75 assertEquals(0, ai.getLength()); 76 fail("NullPointerException expected"); 77 } catch (NullPointerException e) { 78 // Expected 79 } 80 } 81 82 @TestTargetNew( 83 level = TestLevel.COMPLETE, 84 method = "getLength", 85 args = { } 86 ) 87 public void testGetLength() { 88 AttributesImpl ai = new AttributesImpl(empty); 89 assertEquals(0, ai.getLength()); 90 91 ai = new AttributesImpl(multi); 92 assertEquals(5, ai.getLength()); 93 94 for (int i = 4; i >= 0; i--) { 95 ai.removeAttribute(i); 96 assertEquals(i, ai.getLength()); 97 } 98 } 99 100 @TestTargetNew( 101 level = TestLevel.COMPLETE, 102 method = "getURI", 103 args = { int.class } 104 ) 105 public void testGetURI() { 106 // Ordinary cases 107 assertEquals("http://some.uri", multi.getURI(0)); 108 assertEquals("http://some.uri", multi.getURI(1)); 109 assertEquals("http://some.other.uri", multi.getURI(2)); 110 assertEquals("", multi.getURI(3)); 111 assertEquals("", multi.getURI(4)); 112 113 // Out of range 114 assertEquals(null, multi.getURI(-1)); 115 assertEquals(null, multi.getURI(5)); 116 } 117 118 @TestTargetNew( 119 level = TestLevel.COMPLETE, 120 method = "getLocalName", 121 args = { int.class } 122 ) 123 public void testGetLocalName() { 124 // Ordinary cases 125 assertEquals("foo", multi.getLocalName(0)); 126 assertEquals("bar", multi.getLocalName(1)); 127 assertEquals("answer", multi.getLocalName(2)); 128 assertEquals("gabbaHey", multi.getLocalName(3)); 129 assertEquals("", multi.getLocalName(4)); 130 131 // Out of range 132 assertEquals(null, multi.getLocalName(-1)); 133 assertEquals(null, multi.getLocalName(5)); 134 } 135 136 @TestTargetNew( 137 level = TestLevel.COMPLETE, 138 method = "getQName", 139 args = { int.class } 140 ) 141 public void testGetQName() { 142 // Ordinary cases 143 assertEquals("ns1:foo", multi.getQName(0)); 144 assertEquals("ns1:bar", multi.getQName(1)); 145 assertEquals("ns2:answer", multi.getQName(2)); 146 assertEquals("", multi.getQName(3)); 147 assertEquals("gabba:hey", multi.getQName(4)); 148 149 // Out of range 150 assertEquals(null, multi.getQName(-1)); 151 assertEquals(null, multi.getQName(5)); 152 } 153 154 @TestTargetNew( 155 level = TestLevel.COMPLETE, 156 method = "getType", 157 args = { int.class } 158 ) 159 public void testGetTypeInt() { 160 // Ordinary cases 161 assertEquals("string", multi.getType(0)); 162 assertEquals("string", multi.getType(1)); 163 assertEquals("int", multi.getType(2)); 164 assertEquals("string", multi.getType(3)); 165 assertEquals("string", multi.getType(4)); 166 167 // Out of range 168 assertEquals(null, multi.getType(-1)); 169 assertEquals(null, multi.getType(5)); 170 } 171 172 @TestTargetNew( 173 level = TestLevel.COMPLETE, 174 method = "getValue", 175 args = { int.class } 176 ) 177 public void testGetValueInt() { 178 // Ordinary cases 179 assertEquals("abc", multi.getValue(0)); 180 assertEquals("xyz", multi.getValue(1)); 181 assertEquals("42", multi.getValue(2)); 182 assertEquals("1-2-3-4", multi.getValue(3)); 183 assertEquals("1-2-3-4", multi.getValue(4)); 184 185 // Out of range 186 assertEquals(null, multi.getValue(-1)); 187 assertEquals(null, multi.getValue(5)); 188 } 189 190 @TestTargetNew( 191 level = TestLevel.COMPLETE, 192 method = "getIndex", 193 args = { String.class, String.class } 194 ) 195 public void testGetIndexStringString() { 196 // Ordinary cases 197 assertEquals(0, multi.getIndex("http://some.uri", "foo")); 198 assertEquals(1, multi.getIndex("http://some.uri", "bar")); 199 assertEquals(2, multi.getIndex("http://some.other.uri", "answer")); 200 201 // Not found 202 assertEquals(-1, multi.getIndex("john", "doe")); 203 204 // null cases 205 assertEquals(-1, multi.getIndex("http://some.uri", null)); 206 assertEquals(-1, multi.getIndex(null, "foo")); 207 } 208 209 @TestTargetNew( 210 level = TestLevel.COMPLETE, 211 method = "getIndex", 212 args = { String.class } 213 ) 214 public void testGetIndexString() { 215 // Ordinary cases 216 assertEquals(0, multi.getIndex("ns1:foo")); 217 assertEquals(1, multi.getIndex("ns1:bar")); 218 assertEquals(2, multi.getIndex("ns2:answer")); 219 assertEquals(4, multi.getIndex("gabba:hey")); 220 221 // Not found 222 assertEquals(-1, multi.getIndex("john:doe")); 223 224 // null case 225 assertEquals(-1, multi.getIndex(null)); 226 } 227 228 @TestTargetNew( 229 level = TestLevel.COMPLETE, 230 method = "getType", 231 args = { String.class, String.class } 232 ) 233 public void testGetTypeStringString() { 234 // Ordinary cases 235 assertEquals("string", multi.getType("http://some.uri", "foo")); 236 assertEquals("string", multi.getType("http://some.uri", "bar")); 237 assertEquals("int", multi.getType("http://some.other.uri", "answer")); 238 239 // Not found 240 assertEquals(null, multi.getType("john", "doe")); 241 242 // null cases 243 assertEquals(null, multi.getType("http://some.uri", null)); 244 assertEquals(null, multi.getType(null, "foo")); 245 } 246 247 @TestTargetNew( 248 level = TestLevel.COMPLETE, 249 method = "getType", 250 args = { String.class } 251 ) 252 public void testGetTypeString() { 253 // Ordinary cases 254 assertEquals("string", multi.getType("ns1:foo")); 255 assertEquals("string", multi.getType("ns1:bar")); 256 assertEquals("int", multi.getType("ns2:answer")); 257 assertEquals("string", multi.getType("gabba:hey")); 258 259 // Not found 260 assertEquals(null, multi.getType("john:doe")); 261 262 // null case 263 assertEquals(null, multi.getType(null)); 264 } 265 266 @TestTargetNew( 267 level = TestLevel.COMPLETE, 268 method = "getValue", 269 args = { String.class, String.class } 270 ) 271 public void testGetValueStringString() { 272 // Ordinary cases 273 assertEquals("abc", multi.getValue("http://some.uri", "foo")); 274 assertEquals("xyz", multi.getValue("http://some.uri", "bar")); 275 assertEquals("42", multi.getValue("http://some.other.uri", "answer")); 276 277 // Not found 278 assertEquals(null, multi.getValue("john", "doe")); 279 280 // null cases 281 assertEquals(null, multi.getValue("http://some.uri", null)); 282 assertEquals(null, multi.getValue(null, "foo")); 283 } 284 285 @TestTargetNew( 286 level = TestLevel.COMPLETE, 287 method = "getValue", 288 args = { String.class } 289 ) 290 public void testGetValueString() { 291 // Ordinary cases 292 assertEquals("abc", multi.getValue("ns1:foo")); 293 assertEquals("xyz", multi.getValue("ns1:bar")); 294 assertEquals("42", multi.getValue("ns2:answer")); 295 assertEquals("1-2-3-4", multi.getValue("gabba:hey")); 296 297 // Not found 298 assertEquals(null, multi.getValue("john:doe")); 299 300 // null case 301 assertEquals(null, multi.getValue(null)); 302 } 303 304 @TestTargetNew( 305 level = TestLevel.COMPLETE, 306 method = "clear", 307 args = { } 308 ) 309 public void testClear() { 310 assertEquals(5, multi.getLength()); 311 multi.clear(); 312 assertEquals(0, multi.getLength()); 313 } 314 315 @TestTargetNew( 316 level = TestLevel.COMPLETE, 317 method = "setAttributes", 318 args = { Attributes.class } 319 ) 320 public void testSetAttributes() { 321 // Ordinary cases 322 AttributesImpl attrs = new AttributesImpl(); 323 attrs.addAttribute("http://yet.another.uri", "doe", "john:doe", 324 "boolean", "false"); 325 326 attrs.setAttributes(empty); 327 assertEquals(0, attrs.getLength()); 328 329 attrs.setAttributes(multi); 330 assertEquals(multi.getLength(), attrs.getLength()); 331 332 for (int i = 0; i < multi.getLength(); i++) { 333 assertEquals(multi.getURI(i), attrs.getURI(i)); 334 assertEquals(multi.getLocalName(i), attrs.getLocalName(i)); 335 assertEquals(multi.getQName(i), attrs.getQName(i)); 336 assertEquals(multi.getType(i), attrs.getType(i)); 337 assertEquals(multi.getValue(i), attrs.getValue(i)); 338 } 339 340 // null case 341 try { 342 attrs.setAttributes(null); 343 fail("NullPointerException expected"); 344 } catch (NullPointerException e) { 345 // Expected, but must be empty now 346 assertEquals(0, attrs.getLength()); 347 } 348 } 349 350 @TestTargetNew( 351 level = TestLevel.COMPLETE, 352 method = "addAttribute", 353 args = { String.class, String.class, String.class, String.class, 354 String.class } 355 ) 356 public void testAddAttribute() { 357 // Ordinary case 358 multi.addAttribute("http://yet.another.uri", "doe", "john:doe", 359 "boolean", "false"); 360 361 assertEquals("http://yet.another.uri", multi.getURI(5)); 362 assertEquals("doe", multi.getLocalName(5)); 363 assertEquals("john:doe", multi.getQName(5)); 364 assertEquals("boolean", multi.getType(5)); 365 assertEquals("false", multi.getValue(5)); 366 367 // Duplicate case 368 multi.addAttribute("http://yet.another.uri", "doe", "john:doe", 369 "boolean", "false"); 370 371 assertEquals("http://yet.another.uri", multi.getURI(6)); 372 assertEquals("doe", multi.getLocalName(6)); 373 assertEquals("john:doe", multi.getQName(6)); 374 assertEquals("boolean", multi.getType(6)); 375 assertEquals("false", multi.getValue(6)); 376 377 // null case 378 multi.addAttribute(null, null, null, null, null); 379 assertEquals(null, multi.getURI(7)); 380 assertEquals(null, multi.getLocalName(7)); 381 assertEquals(null, multi.getQName(7)); 382 assertEquals(null, multi.getType(7)); 383 assertEquals(null, multi.getValue(7)); 384 } 385 386 @TestTargetNew( 387 level = TestLevel.COMPLETE, 388 method = "setAttribute", 389 args = { int.class, String.class, String.class, String.class, 390 String.class, String.class } 391 ) 392 public void testSetAttribute() { 393 // Ordinary case 394 multi.setAttribute(0, "http://yet.another.uri", "doe", "john:doe", 395 "boolean", "false"); 396 assertEquals("http://yet.another.uri", multi.getURI(0)); 397 assertEquals("doe", multi.getLocalName(0)); 398 assertEquals("john:doe", multi.getQName(0)); 399 assertEquals("boolean", multi.getType(0)); 400 assertEquals("false", multi.getValue(0)); 401 402 // null case 403 multi.setAttribute(1, null, null, null, null, null); 404 assertEquals(null, multi.getURI(1)); 405 assertEquals(null, multi.getLocalName(1)); 406 assertEquals(null, multi.getQName(1)); 407 assertEquals(null, multi.getType(1)); 408 assertEquals(null, multi.getValue(1)); 409 410 // Out of range 411 try { 412 multi.setAttribute(-1, "http://yet.another.uri", "doe", "john:doe", 413 "boolean", "false"); 414 fail("ArrayIndexOutOfBoundsException expected"); 415 } catch (ArrayIndexOutOfBoundsException e) { 416 // Expected 417 } 418 419 try { 420 multi.setAttribute(5, "http://yet.another.uri", "doe", "john:doe", 421 "boolean", "false"); 422 fail("ArrayIndexOutOfBoundsException expected"); 423 } catch (ArrayIndexOutOfBoundsException e) { 424 // Expected 425 } 426 } 427 428 @TestTargetNew( 429 level = TestLevel.COMPLETE, 430 method = "removeAttribute", 431 args = { int.class } 432 ) 433 public void testRemoveAttribute() { 434 // Ordinary case 435 multi.removeAttribute(0); 436 assertEquals("http://some.uri", multi.getURI(0)); 437 assertEquals("bar", multi.getLocalName(0)); 438 assertEquals("ns1:bar", multi.getQName(0)); 439 assertEquals("string", multi.getType(0)); 440 assertEquals("xyz", multi.getValue(0)); 441 442 // Out of range 443 try { 444 multi.removeAttribute(-1); 445 fail("ArrayIndexOutOfBoundsException expected"); 446 } catch (ArrayIndexOutOfBoundsException e) { 447 // Expected 448 } 449 450 try { 451 multi.removeAttribute(4); 452 fail("ArrayIndexOutOfBoundsException expected"); 453 } catch (ArrayIndexOutOfBoundsException e) { 454 // Expected 455 } 456 } 457 458 @TestTargetNew( 459 level = TestLevel.COMPLETE, 460 method = "setURI", 461 args = { int.class, String.class } 462 ) 463 public void testSetURI() { 464 // Ordinary case 465 multi.setURI(0, "http://yet.another.uri"); 466 assertEquals("http://yet.another.uri", multi.getURI(0)); 467 468 // null case 469 multi.setURI(1, null); 470 assertEquals(null, multi.getURI(1)); 471 472 // Out of range 473 try { 474 multi.setURI(-1, "http://yet.another.uri"); 475 fail("ArrayIndexOutOfBoundsException expected"); 476 } catch (ArrayIndexOutOfBoundsException e) { 477 // Expected 478 } 479 480 try { 481 multi.setURI(5, "http://yet.another.uri"); 482 fail("ArrayIndexOutOfBoundsException expected"); 483 } catch (ArrayIndexOutOfBoundsException e) { 484 // Expected 485 } 486 } 487 488 @TestTargetNew( 489 level = TestLevel.COMPLETE, 490 method = "setLocalName", 491 args = { int.class, String.class } 492 ) 493 public void testSetLocalName() { 494 // Ordinary case 495 multi.setLocalName(0, "john"); 496 assertEquals("john", multi.getLocalName(0)); 497 498 // null case 499 multi.setLocalName(1, null); 500 assertEquals(null, multi.getLocalName(1)); 501 502 // Out of range 503 try { 504 multi.setLocalName(-1, "john"); 505 fail("ArrayIndexOutOfBoundsException expected"); 506 } catch (ArrayIndexOutOfBoundsException e) { 507 // Expected 508 } 509 510 try { 511 multi.setLocalName(5, "john"); 512 fail("ArrayIndexOutOfBoundsException expected"); 513 } catch (ArrayIndexOutOfBoundsException e) { 514 // Expected 515 } 516 } 517 518 @TestTargetNew( 519 level = TestLevel.COMPLETE, 520 method = "setQName", 521 args = { int.class, String.class } 522 ) 523 public void testSetQName() { 524 // Ordinary case 525 multi.setQName(0, "john:doe"); 526 assertEquals("john:doe", multi.getQName(0)); 527 528 // null case 529 multi.setQName(1, null); 530 assertEquals(null, multi.getQName(1)); 531 532 // Out of range 533 try { 534 multi.setQName(-1, "john:doe"); 535 fail("ArrayIndexOutOfBoundsException expected"); 536 } catch (ArrayIndexOutOfBoundsException e) { 537 // Expected 538 } 539 540 try { 541 multi.setQName(5, "john:doe"); 542 fail("ArrayIndexOutOfBoundsException expected"); 543 } catch (ArrayIndexOutOfBoundsException e) { 544 // Expected 545 } 546 } 547 548 @TestTargetNew( 549 level = TestLevel.COMPLETE, 550 method = "setType", 551 args = { int.class, String.class } 552 ) 553 public void testSetType() { 554 // Ordinary case 555 multi.setType(0, "float"); 556 assertEquals("float", multi.getType(0)); 557 558 // null case 559 multi.setType(1, null); 560 assertEquals(null, multi.getType(1)); 561 562 // Out of range 563 try { 564 multi.setType(-1, "float"); 565 fail("ArrayIndexOutOfBoundsException expected"); 566 } catch (ArrayIndexOutOfBoundsException e) { 567 // Expected 568 } 569 570 try { 571 multi.setType(5, "float"); 572 fail("ArrayIndexOutOfBoundsException expected"); 573 } catch (ArrayIndexOutOfBoundsException e) { 574 // Expected 575 } 576 } 577 578 @TestTargetNew( 579 level = TestLevel.COMPLETE, 580 method = "setValue", 581 args = { int.class, String.class } 582 ) 583 public void testSetValue() { 584 // Ordinary case 585 multi.setValue(0, "too much"); 586 assertEquals("too much", multi.getValue(0)); 587 588 // null case 589 multi.setValue(1, null); 590 assertEquals(null, multi.getValue(1)); 591 592 // Out of range 593 try { 594 multi.setValue(-1, "too much"); 595 fail("ArrayIndexOutOfBoundsException expected"); 596 } catch (ArrayIndexOutOfBoundsException e) { 597 // Expected 598 } 599 600 try { 601 multi.setValue(5, "too much"); 602 fail("ArrayIndexOutOfBoundsException expected"); 603 } catch (ArrayIndexOutOfBoundsException e) { 604 // Expected 605 } 606 } 607 608} 609