1 2 4 package org.xmlpull.v1.tests; 5 6 import junit.framework.TestCase; 8 import junit.framework.TestSuite; 9 10 import java.io.StringReader ; 11 import java.io.IOException ; 12 13 import org.xmlpull.v1.XmlPullParser; 14 import org.xmlpull.v1.XmlPullParserFactory; 15 import org.xmlpull.v1.XmlPullParserException; 16 17 23 public class TestEntityReplacement extends UtilTestCase { 24 private XmlPullParserFactory factory; 25 26 public TestEntityReplacement(String name) { 27 super(name); 28 } 29 30 protected void setUp() throws XmlPullParserException { 31 factory = factoryNewInstance(); 32 factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 33 assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 34 } 36 37 protected void tearDown() { 38 } 39 40 public void testEntityReplacement() throws IOException , XmlPullParserException 41 { 42 final String XML_ENTITY_EXPANSION = 44 "<?xml version='1.0'?>\n"+ 45 "<!DOCTYPE test [\n"+ 46 "<!ELEMENT test (#PCDATA) >\n"+ 47 "<!ENTITY % xx '%zz;'>\n"+ 48 "<!ENTITY % zz '<!ENTITY tricky \"error-prone\" >' >\n"+ 49 "%xx;\n"+ 50 "]>"+ 51 "<test>This sample shows a &tricky; method.</test>"; 52 53 XmlPullParser pp = factory.newPullParser(); 54 pp.setInput(new StringReader ( XML_ENTITY_EXPANSION ) ); 56 if(pp.getFeature( pp.FEATURE_PROCESS_DOCDECL ) == false) { 57 pp.defineEntityReplacementText("tricky", "error-prone"); 58 } 59 testEntityReplacement(pp); 60 61 pp.setInput(new StringReader ( XML_ENTITY_EXPANSION ) ); 63 try { 64 pp.setFeature( pp.FEATURE_PROCESS_DOCDECL, false ); 65 } catch( Exception ex ){ 66 } 67 if( pp.getFeature( pp.FEATURE_PROCESS_DOCDECL ) == false ) { 68 pp.defineEntityReplacementText("tricky", "error-prone"); 69 testEntityReplacement(pp); 70 } 71 72 pp.setInput(new StringReader ( XML_ENTITY_EXPANSION ) ); 74 try { 75 pp.setFeature( pp.FEATURE_PROCESS_DOCDECL, true ); 76 } catch( Exception ex ){ 78 } 79 if( pp.getFeature( pp.FEATURE_PROCESS_DOCDECL ) ) { 80 testEntityReplacement(pp); 81 } 82 83 pp.setInput(new StringReader ( XML_ENTITY_EXPANSION ) ); 85 try { 86 pp.setFeature( pp.FEATURE_VALIDATION, true ); 87 } catch( Exception ex ){ 89 } 90 if( pp.getFeature( pp.FEATURE_VALIDATION ) ) { 91 testEntityReplacement(pp); 92 } 93 94 } 95 96 public void testEntityReplacement(XmlPullParser pp) throws IOException , XmlPullParserException 97 { 98 pp.next(); 99 checkParserStateNs(pp, 1, pp.START_TAG, 100 null, 0, "", "test", null, false, 0); 101 pp.next(); 102 checkParserStateNs(pp, 1, pp.TEXT, null, 0, null, null, 103 "This sample shows a error-prone method.", false, -1); 104 pp.next(); 105 checkParserStateNs(pp, 1, pp.END_TAG, 106 null, 0, "", "test", null, false, -1); 107 pp.nextToken(); 108 checkParserStateNs(pp, 0, pp.END_DOCUMENT, null, 0, null, null, null, false, -1); 109 110 } 111 112 113 public static void main (String [] args) { 114 junit.textui.TestRunner.run (new TestSuite(TestEntityReplacement.class)); 115 } 116 117 } 118 119 | Popular Tags |