|
20 | 20 | import static org.junit.Assert.assertTrue;
|
21 | 21 | import static org.junit.Assert.fail;
|
22 | 22 |
|
| 23 | +import java.io.EOFException; |
23 | 24 | import java.io.IOException;
|
24 | 25 | import java.io.StringReader;
|
25 | 26 |
|
@@ -496,4 +497,111 @@ public void testSubsequentMalformedProcessingInstructionNoClosingQuestionMark()
|
496 | 497 | }
|
497 | 498 | }
|
498 | 499 |
|
| 500 | + public void testMalformedXMLRootElement() |
| 501 | + throws Exception |
| 502 | + { |
| 503 | + String input = "<Y"; |
| 504 | + |
| 505 | + MXParser parser = new MXParser(); |
| 506 | + parser.setInput( new StringReader( input ) ); |
| 507 | + |
| 508 | + try |
| 509 | + { |
| 510 | + assertEquals( XmlPullParser.START_TAG, parser.nextToken() ); |
| 511 | + |
| 512 | + fail( "Should throw EOFException" ); |
| 513 | + } |
| 514 | + catch ( EOFException e ) |
| 515 | + { |
| 516 | + assertTrue( e.getMessage().contains( "no more data available - expected the opening tag <Y...>" ) ); |
| 517 | + } |
| 518 | + } |
| 519 | + |
| 520 | + public void testMalformedXMLRootElement2() |
| 521 | + throws Exception |
| 522 | + { |
| 523 | + String input = "<hello"; |
| 524 | + |
| 525 | + MXParser parser = new MXParser(); |
| 526 | + parser.setInput( new StringReader( input ) ); |
| 527 | + |
| 528 | + try |
| 529 | + { |
| 530 | + assertEquals( XmlPullParser.START_TAG, parser.nextToken() ); |
| 531 | + |
| 532 | + fail( "Should throw EOFException" ); |
| 533 | + } |
| 534 | + catch ( EOFException e ) |
| 535 | + { |
| 536 | + assertTrue( e.getMessage().contains( "no more data available - expected the opening tag <hello...>" ) ); |
| 537 | + } |
| 538 | + } |
| 539 | + |
| 540 | + public void testMalformedXMLRootElement3() |
| 541 | + throws Exception |
| 542 | + { |
| 543 | + String input = "<hello><how"; |
| 544 | + |
| 545 | + MXParser parser = new MXParser(); |
| 546 | + parser.setInput( new StringReader( input ) ); |
| 547 | + |
| 548 | + try |
| 549 | + { |
| 550 | + assertEquals( XmlPullParser.START_TAG, parser.nextToken() ); |
| 551 | + assertEquals( XmlPullParser.START_TAG, parser.nextToken() ); |
| 552 | + |
| 553 | + fail( "Should throw EOFException" ); |
| 554 | + } |
| 555 | + catch ( EOFException e ) |
| 556 | + { |
| 557 | + assertTrue( e.getMessage().contains( "no more data available - expected the opening tag <how...>" ) ); |
| 558 | + } |
| 559 | + } |
| 560 | + |
| 561 | + public void testMalformedXMLRootElement4() |
| 562 | + throws Exception |
| 563 | + { |
| 564 | + String input = "<hello>some text<how"; |
| 565 | + |
| 566 | + MXParser parser = new MXParser(); |
| 567 | + parser.setInput( new StringReader( input ) ); |
| 568 | + |
| 569 | + try |
| 570 | + { |
| 571 | + assertEquals( XmlPullParser.START_TAG, parser.nextToken() ); |
| 572 | + assertEquals( XmlPullParser.TEXT, parser.nextToken() ); |
| 573 | + assertEquals( "some text", parser.getText() ); |
| 574 | + assertEquals( XmlPullParser.START_TAG, parser.nextToken() ); |
| 575 | + |
| 576 | + fail( "Should throw EOFException" ); |
| 577 | + } |
| 578 | + catch ( EOFException e ) |
| 579 | + { |
| 580 | + assertTrue( e.getMessage().contains( "no more data available - expected the opening tag <how...>" ) ); |
| 581 | + } |
| 582 | + } |
| 583 | + |
| 584 | + public void testMalformedXMLRootElement5() |
| 585 | + throws Exception |
| 586 | + { |
| 587 | + String input = "<hello>some text</hello"; |
| 588 | + |
| 589 | + MXParser parser = new MXParser(); |
| 590 | + parser.setInput( new StringReader( input ) ); |
| 591 | + |
| 592 | + try |
| 593 | + { |
| 594 | + assertEquals( XmlPullParser.START_TAG, parser.nextToken() ); |
| 595 | + assertEquals( XmlPullParser.TEXT, parser.nextToken() ); |
| 596 | + assertEquals( "some text", parser.getText() ); |
| 597 | + assertEquals( XmlPullParser.END_TAG, parser.nextToken() ); |
| 598 | + |
| 599 | + fail( "Should throw EOFException" ); |
| 600 | + } |
| 601 | + catch ( EOFException e ) |
| 602 | + { |
| 603 | + assertTrue( e.getMessage().contains( "no more data available - expected end tag </hello> to close start tag <hello>" ) ); |
| 604 | + } |
| 605 | + } |
| 606 | + |
499 | 607 | }
|
0 commit comments