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 java.io.IOException; 20 21import junit.framework.TestCase; 22 23import org.xml.sax.Attributes; 24import org.xml.sax.ContentHandler; 25import org.xml.sax.DTDHandler; 26import org.xml.sax.EntityResolver; 27import org.xml.sax.ErrorHandler; 28import org.xml.sax.InputSource; 29import org.xml.sax.Locator; 30import org.xml.sax.SAXException; 31import org.xml.sax.SAXNotRecognizedException; 32import org.xml.sax.SAXNotSupportedException; 33import org.xml.sax.SAXParseException; 34import org.xml.sax.XMLReader; 35import org.xml.sax.helpers.AttributesImpl; 36import org.xml.sax.helpers.LocatorImpl; 37import org.xml.sax.helpers.XMLFilterImpl; 38 39import tests.api.org.xml.sax.support.MethodLogger; 40import tests.api.org.xml.sax.support.MockFilter; 41import tests.api.org.xml.sax.support.MockHandler; 42import tests.api.org.xml.sax.support.MockResolver; 43import dalvik.annotation.TestLevel; 44import dalvik.annotation.TestTargetClass; 45import dalvik.annotation.TestTargetNew; 46import dalvik.annotation.TestTargets; 47 48@TestTargetClass(XMLFilterImpl.class) 49public class XMLFilterImplTest extends TestCase { 50 51 // Note: In many cases we can only test that delegation works 52 // properly. The rest is outside the scope of the specification. 53 54 private MethodLogger logger = new MethodLogger(); 55 56 private MockHandler handler = new MockHandler(logger); 57 58 private XMLFilterImpl parent = new MockFilter(logger); 59 60 private XMLFilterImpl child = new XMLFilterImpl(parent); 61 62 private XMLFilterImpl orphan = new XMLFilterImpl(); 63 64 private void assertEquals(Object[] a, Object[] b) { 65 assertEquals(a.length, b.length); 66 67 for (int i = 0; i < a.length; i++) { 68 assertEquals("Element #" + i + " must be equal", a[i], b[i]); 69 } 70 } 71 72 public void setUp() { 73 parent.setContentHandler(handler); 74 parent.setDTDHandler(handler); 75 parent.setErrorHandler(handler); 76 77 child.setContentHandler(handler); 78 child.setDTDHandler(handler); 79 child.setErrorHandler(handler); 80 } 81 82 @TestTargetNew( 83 level = TestLevel.COMPLETE, 84 method = "XMLFilterImpl", 85 args = { } 86 ) 87 public void testXMLFilterImpl() { 88 assertEquals(null, parent.getParent()); 89 } 90 91 @TestTargetNew( 92 level = TestLevel.COMPLETE, 93 method = "XMLFilterImpl", 94 args = { XMLReader.class } 95 ) 96 public void testXMLFilterImplXMLReader() { 97 // Ordinary case 98 assertEquals(null, parent.getParent()); 99 100 // null case 101 XMLFilterImpl filter = new XMLFilterImpl(null); 102 assertEquals(null, filter.getParent()); 103 } 104 105 @TestTargets({ 106 @TestTargetNew( 107 level = TestLevel.COMPLETE, 108 method = "getParent", 109 args = { } 110 ), 111 @TestTargetNew( 112 level = TestLevel.COMPLETE, 113 method = "setParent", 114 args = { XMLReader.class } 115 ) 116 }) 117 public void testGetSetParent() { 118 child.setParent(null); 119 assertEquals(null, child.getParent()); 120 121 child.setParent(parent); 122 assertEquals(parent, child.getParent()); 123 } 124 125 @TestTargets({ 126 @TestTargetNew( 127 level = TestLevel.COMPLETE, 128 method = "getFeature", 129 args = { String.class } 130 ), 131 @TestTargetNew( 132 level = TestLevel.COMPLETE, 133 method = "setFeature", 134 args = { String.class, boolean.class } 135 ) 136 }) 137 public void testGetSetFeature() { 138 // Ordinary case 139 try { 140 child.setFeature("foo", true); 141 assertEquals(true, child.getFeature("foo")); 142 143 child.setFeature("foo", false); 144 assertEquals(false, child.getFeature("foo")); 145 } catch (SAXNotRecognizedException e) { 146 throw new RuntimeException("Unexpected exception", e); 147 } catch (SAXNotSupportedException e) { 148 throw new RuntimeException("Unexpected exception", e); 149 } 150 151 // No parent case 152 try { 153 orphan.setFeature("foo", false); 154 fail("SAXNotRecognizedException expected"); 155 } catch (SAXNotRecognizedException e) { 156 // Expected 157 } catch (SAXNotSupportedException e) { 158 throw new RuntimeException("Unexpected exception", e); 159 } 160 } 161 162 @TestTargets({ 163 @TestTargetNew( 164 level = TestLevel.COMPLETE, 165 method = "getProperty", 166 args = { String.class } 167 ), 168 @TestTargetNew( 169 level = TestLevel.COMPLETE, 170 method = "setProperty", 171 args = { String.class, Object.class } 172 ) 173 }) 174 public void testGetSetProperty() { 175 // Ordinary case 176 try { 177 child.setProperty("foo", "bar"); 178 assertEquals("bar", child.getProperty("foo")); 179 180 child.setProperty("foo", null); 181 assertEquals(null, child.getProperty("foo")); 182 } catch (SAXNotRecognizedException e) { 183 throw new RuntimeException("Unexpected exception", e); 184 } catch (SAXNotSupportedException e) { 185 throw new RuntimeException("Unexpected exception", e); 186 } 187 188 // No parent case 189 try { 190 orphan.setProperty("foo", "bar"); 191 fail("SAXNotRecognizedException expected"); 192 } catch (SAXNotRecognizedException e) { 193 // Expected 194 } catch (SAXNotSupportedException e) { 195 throw new RuntimeException("Unexpected exception", e); 196 } 197 } 198 199 @TestTargets({ 200 @TestTargetNew( 201 level = TestLevel.COMPLETE, 202 method = "getEntityResolver", 203 args = { } 204 ), 205 @TestTargetNew( 206 level = TestLevel.COMPLETE, 207 method = "setEntityResolver", 208 args = { EntityResolver.class } 209 ) 210 }) 211 public void testGetSetEntityResolver() { 212 EntityResolver resolver = new MockResolver(); 213 214 parent.setEntityResolver(resolver); 215 assertEquals(resolver, parent.getEntityResolver()); 216 217 parent.setEntityResolver(null); 218 assertEquals(null, parent.getEntityResolver()); 219 } 220 221 @TestTargets({ 222 @TestTargetNew( 223 level = TestLevel.COMPLETE, 224 method = "getDTDHandler", 225 args = { } 226 ), 227 @TestTargetNew( 228 level = TestLevel.COMPLETE, 229 method = "setDTDHandler", 230 args = { DTDHandler.class } 231 ) 232 }) 233 public void testGetSetDTDHandler() { 234 parent.setDTDHandler(null); 235 assertEquals(null, parent.getDTDHandler()); 236 237 parent.setDTDHandler(handler); 238 assertEquals(handler, parent.getDTDHandler()); 239 } 240 241 @TestTargets({ 242 @TestTargetNew( 243 level = TestLevel.COMPLETE, 244 method = "getContentHandler", 245 args = { } 246 ), 247 @TestTargetNew( 248 level = TestLevel.COMPLETE, 249 method = "setContentHandler", 250 args = { ContentHandler.class } 251 ) 252 }) 253 public void testGetSetContentHandler() { 254 parent.setContentHandler(null); 255 assertEquals(null, parent.getContentHandler()); 256 257 parent.setContentHandler(handler); 258 assertEquals(handler, parent.getContentHandler()); 259 } 260 261 @TestTargets({ 262 @TestTargetNew( 263 level = TestLevel.COMPLETE, 264 method = "getErrorHandler", 265 args = { } 266 ), 267 @TestTargetNew( 268 level = TestLevel.COMPLETE, 269 method = "setErrorHandler", 270 args = { ErrorHandler.class } 271 ) 272 }) 273 public void testGetSetErrorHandler() { 274 parent.setErrorHandler(null); 275 assertEquals(null, parent.getErrorHandler()); 276 277 parent.setErrorHandler(handler); 278 assertEquals(handler, parent.getErrorHandler()); 279 } 280 281 @TestTargetNew( 282 level = TestLevel.COMPLETE, 283 method = "parse", 284 args = { InputSource.class } 285 ) 286 public void testParseInputSource() { 287 InputSource is = new InputSource(); 288 289 // Ordinary case 290 try { 291 child.parse(is); 292 } catch (SAXException e) { 293 throw new RuntimeException("Unexpected exception", e); 294 } catch (IOException e) { 295 throw new RuntimeException("Unexpected exception", e); 296 } 297 298 assertEquals(1, logger.size()); 299 assertEquals("parse", logger.getMethod()); 300 301 // No parent case 302 try { 303 orphan.parse(is); 304 fail("NullPointerException expected"); 305 } catch (NullPointerException e) { 306 // Expected 307 } catch (SAXException e) { 308 throw new RuntimeException("Unexpected exception", e); 309 } catch (IOException e) { 310 throw new RuntimeException("Unexpected exception", e); 311 } 312 } 313 314 @TestTargetNew( 315 level = TestLevel.COMPLETE, 316 method = "parse", 317 args = { String.class } 318 ) 319 public void testParseString() { 320 // Ordinary case 321 try { 322 child.parse("foo"); 323 } catch (SAXException e) { 324 throw new RuntimeException("Unexpected exception", e); 325 } catch (IOException e) { 326 throw new RuntimeException("Unexpected exception", e); 327 } 328 329 assertEquals(1, logger.size()); 330 assertEquals("parse", logger.getMethod()); 331 332 // No parent case 333 try { 334 orphan.parse("foo"); 335 fail("NullPointerException expected"); 336 } catch (NullPointerException e) { 337 // Expected 338 } catch (SAXException e) { 339 throw new RuntimeException("Unexpected exception", e); 340 } catch (IOException e) { 341 throw new RuntimeException("Unexpected exception", e); 342 } 343 } 344 345 @TestTargetNew( 346 level = TestLevel.COMPLETE, 347 method = "resolveEntity", 348 args = { String.class, String.class } 349 ) 350 public void testResolveEntity() { 351 InputSource expected = new InputSource(); 352 353 MockResolver resolver = new MockResolver(); 354 resolver.addEntity("foo", "bar", expected); 355 356 InputSource result = null; 357 358 parent.setEntityResolver(resolver); 359 360 // Ordinary case 361 try { 362 result = parent.resolveEntity("foo", "bar"); 363 } catch (SAXException e) { 364 throw new RuntimeException("Unexpected exception", e); 365 } catch (IOException e) { 366 throw new RuntimeException("Unexpected exception", e); 367 } 368 369 assertEquals(expected, result); 370 371 // No entity resolver case 372 parent.setEntityResolver(null); 373 374 try { 375 result = parent.resolveEntity("foo", "bar"); 376 } catch (SAXException e) { 377 throw new RuntimeException("Unexpected exception", e); 378 } catch (IOException e) { 379 throw new RuntimeException("Unexpected exception", e); 380 } 381 382 assertEquals(null, result); 383 } 384 385 @TestTargetNew( 386 level = TestLevel.COMPLETE, 387 method = "notationDecl", 388 args = { String.class, String.class, String.class } 389 ) 390 public void testNotationDecl() { 391 try { 392 parent.notationDecl("foo", "bar", "foobar"); 393 } catch (SAXException e) { 394 throw new RuntimeException("Unexpected exception", e); 395 } 396 397 assertEquals(logger.size(), 1); 398 assertEquals("notationDecl", logger.getMethod()); 399 assertEquals(new Object[] { "foo", "bar", "foobar" }, 400 logger.getArgs()); 401 } 402 403 @TestTargetNew( 404 level = TestLevel.COMPLETE, 405 method = "unparsedEntityDecl", 406 args = { String.class, String.class, String.class, String.class } 407 ) 408 public void testUnparsedEntityDecl() { 409 try { 410 parent.unparsedEntityDecl("foo", "bar", "gabba", "hey"); 411 } catch (SAXException e) { 412 throw new RuntimeException("Unexpected exception", e); 413 } 414 415 assertEquals(logger.size(), 1); 416 assertEquals("unparsedEntityDecl", logger.getMethod()); 417 assertEquals(new Object[] { "foo", "bar", "gabba", "hey" }, 418 logger.getArgs()); 419 } 420 421 @TestTargetNew( 422 level = TestLevel.COMPLETE, 423 method = "setDocumentLocator", 424 args = { Locator.class } 425 ) 426 public void testSetDocumentLocator() { 427 Locator l = new LocatorImpl(); 428 429 child.setDocumentLocator(l); 430 431 assertEquals(logger.size(), 1); 432 assertEquals("setDocumentLocator", logger.getMethod()); 433 assertEquals(new Object[] { l }, logger.getArgs()); 434 435 child.setDocumentLocator(null); 436 437 assertEquals(logger.size(), 2); 438 assertEquals("setDocumentLocator", logger.getMethod()); 439 assertEquals(new Object[] { null }, logger.getArgs()); 440 } 441 442 @TestTargetNew( 443 level = TestLevel.COMPLETE, 444 method = "startDocument", 445 args = { } 446 ) 447 public void testStartDocument() { 448 try { 449 parent.startDocument(); 450 } catch (SAXException e) { 451 throw new RuntimeException("Unexpected exception", e); 452 } 453 454 assertEquals(logger.size(), 1); 455 assertEquals("startDocument", logger.getMethod()); 456 assertEquals(new Object[] {}, logger.getArgs()); 457 } 458 459 @TestTargetNew( 460 level = TestLevel.COMPLETE, 461 method = "endDocument", 462 args = { } 463 ) 464 public void testEndDocument() { 465 try { 466 parent.endDocument(); 467 } catch (SAXException e) { 468 throw new RuntimeException("Unexpected exception", e); 469 } 470 471 assertEquals(logger.size(), 1); 472 assertEquals("endDocument", logger.getMethod()); 473 assertEquals(new Object[] {}, logger.getArgs()); 474 } 475 476 @TestTargetNew( 477 level = TestLevel.COMPLETE, 478 method = "startPrefixMapping", 479 args = { String.class, String.class } 480 ) 481 public void testStartPrefixMapping() { 482 try { 483 parent.startPrefixMapping("foo", "http://some.uri"); 484 } catch (SAXException e) { 485 throw new RuntimeException("Unexpected exception", e); 486 } 487 488 assertEquals(logger.size(), 1); 489 assertEquals("startPrefixMapping", logger.getMethod()); 490 assertEquals(new Object[] { "foo", "http://some.uri" }, 491 logger.getArgs()); 492 } 493 494 @TestTargetNew( 495 level = TestLevel.COMPLETE, 496 method = "endPrefixMapping", 497 args = { String.class } 498 ) 499 public void testEndPrefixMapping() { 500 try { 501 parent.endPrefixMapping("foo"); 502 } catch (SAXException e) { 503 throw new RuntimeException("Unexpected exception", e); 504 } 505 506 assertEquals(logger.size(), 1); 507 assertEquals("endPrefixMapping", logger.getMethod()); 508 assertEquals(new Object[] { "foo" }, logger.getArgs()); 509 } 510 511 @TestTargetNew( 512 level = TestLevel.COMPLETE, 513 method = "startElement", 514 args = { String.class, String.class, String.class, Attributes.class } 515 ) 516 public void testStartElement() { 517 Attributes atts = new AttributesImpl(); 518 519 try { 520 parent.startElement("http://some.uri", "bar", "foo:bar", atts); 521 } catch (SAXException e) { 522 throw new RuntimeException("Unexpected exception", e); 523 } 524 525 assertEquals(logger.size(), 1); 526 assertEquals("startElement", logger.getMethod()); 527 assertEquals(new Object[] { "http://some.uri", "bar", "foo:bar", atts }, 528 logger.getArgs()); 529 } 530 531 @TestTargetNew( 532 level = TestLevel.COMPLETE, 533 method = "endElement", 534 args = { String.class, String.class, String.class } 535 ) 536 public void testEndElement() { 537 try { 538 parent.endElement("http://some.uri", "bar", "foo:bar"); 539 } catch (SAXException e) { 540 throw new RuntimeException("Unexpected exception", e); 541 } 542 543 assertEquals(logger.size(), 1); 544 assertEquals("endElement", logger.getMethod()); 545 assertEquals(new Object[] { "http://some.uri", "bar", "foo:bar" }, 546 logger.getArgs()); 547 } 548 549 @TestTargetNew( 550 level = TestLevel.COMPLETE, 551 method = "characters", 552 args = { char[].class, int.class, int.class } 553 ) 554 public void testCharacters() { 555 char[] ch = "Android".toCharArray(); 556 557 try { 558 parent.characters(ch, 2, 5); 559 } catch (SAXException e) { 560 throw new RuntimeException("Unexpected exception", e); 561 } 562 563 assertEquals(logger.size(), 1); 564 assertEquals("characters", logger.getMethod()); 565 assertEquals(new Object[] { ch, 2, 5 }, logger.getArgs()); 566 } 567 568 @TestTargetNew( 569 level = TestLevel.COMPLETE, 570 method = "ignorableWhitespace", 571 args = { char[].class, int.class, int.class } 572 ) 573 public void testIgnorableWhitespace() { 574 char[] ch = " ".toCharArray(); 575 576 try { 577 parent.ignorableWhitespace(ch, 0, 5); 578 } catch (SAXException e) { 579 throw new RuntimeException("Unexpected exception", e); 580 } 581 582 assertEquals(logger.size(), 1); 583 assertEquals("ignorableWhitespace", logger.getMethod()); 584 assertEquals(new Object[] { ch, 0, 5 }, logger.getArgs()); 585 } 586 587 @TestTargetNew( 588 level = TestLevel.COMPLETE, 589 method = "processingInstruction", 590 args = { String.class, String.class } 591 ) 592 public void testProcessingInstruction() { 593 try { 594 parent.processingInstruction("foo", "bar"); 595 } catch (SAXException e) { 596 throw new RuntimeException("Unexpected exception", e); 597 } 598 599 assertEquals(logger.size(), 1); 600 assertEquals("processingInstruction", logger.getMethod()); 601 assertEquals(new Object[] { "foo", "bar" }, logger.getArgs()); 602 } 603 604 @TestTargetNew( 605 level = TestLevel.COMPLETE, 606 method = "skippedEntity", 607 args = { String.class } 608 ) 609 public void testSkippedEntity() { 610 try { 611 parent.skippedEntity("foo"); 612 } catch (SAXException e) { 613 throw new RuntimeException("Unexpected exception", e); 614 } 615 616 assertEquals(logger.size(), 1); 617 assertEquals("skippedEntity", logger.getMethod()); 618 assertEquals(new Object[] { "foo" }, logger.getArgs()); 619 } 620 621 @TestTargetNew( 622 level = TestLevel.COMPLETE, 623 method = "warning", 624 args = { SAXParseException.class } 625 ) 626 public void testWarning() { 627 SAXParseException exception = new SAXParseException("Oops!", null); 628 629 try { 630 parent.warning(exception); 631 } catch (SAXException e) { 632 throw new RuntimeException("Unexpected exception", e); 633 } 634 635 assertEquals(logger.size(), 1); 636 assertEquals("warning", logger.getMethod()); 637 assertEquals(new Object[] { exception }, logger.getArgs()); 638 } 639 640 @TestTargetNew( 641 level = TestLevel.COMPLETE, 642 method = "error", 643 args = { SAXParseException.class } 644 ) 645 public void testError() { 646 SAXParseException exception = new SAXParseException("Oops!", null); 647 648 try { 649 parent.error(exception); 650 } catch (SAXException e) { 651 throw new RuntimeException("Unexpected exception", e); 652 } 653 654 assertEquals(logger.size(), 1); 655 assertEquals("error", logger.getMethod()); 656 assertEquals(new Object[] { exception }, logger.getArgs()); 657 } 658 659 @TestTargetNew( 660 level = TestLevel.COMPLETE, 661 method = "fatalError", 662 args = { SAXParseException.class } 663 ) 664 public void testFatalError() { 665 SAXParseException exception = new SAXParseException("Oops!", null); 666 667 try { 668 parent.fatalError(exception); 669 } catch (SAXException e) { 670 throw new RuntimeException("Unexpected exception", e); 671 } 672 673 assertEquals(logger.size(), 1); 674 assertEquals("fatalError", logger.getMethod()); 675 assertEquals(new Object[] { exception }, logger.getArgs()); 676 } 677 678} 679