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.StringWriter ; 12 13 import org.xmlpull.v1.XmlPullParser; 14 import org.xmlpull.v1.XmlPullParserFactory; 15 import org.xmlpull.v1.XmlPullParserException; 16 17 22 public class TestSimpleToken extends UtilTestCase { 23 private XmlPullParserFactory factory; 24 25 26 public static void main (String [] args) { 27 junit.textui.TestRunner.run (new TestSuite(TestSimpleToken.class)); 28 } 29 30 public TestSimpleToken(String name) { 31 super(name); 32 } 33 34 protected void setUp() throws XmlPullParserException { 35 factory = factoryNewInstance(); 36 factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 37 assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 38 } 40 41 protected void tearDown() { 42 } 43 44 public void testVerySimpleToken() throws Exception { 45 XmlPullParser xpp = factory.newPullParser(); 46 assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 47 xpp.setInput(new StringReader ("<foo><!--comment--><?target pi?></foo>")); 48 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 49 xpp.nextToken(); 50 checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, false, 0); 51 xpp.nextToken(); 52 checkParserStateNs(xpp, 1, xpp.COMMENT, null, 0, null, null, "comment", false, -1); 53 xpp.nextToken(); 54 checkParserStateNs(xpp, 1, xpp.PROCESSING_INSTRUCTION, null, 0, null, null, "target pi", false, -1); 55 xpp.nextToken(); 56 checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1); 57 xpp.nextToken(); 58 checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1); 59 } 60 61 public void testSimpleToken() throws Exception { 62 XmlPullParser xpp = factory.newPullParser(); 63 assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 64 65 assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType()); 67 try { 68 xpp.nextToken(); 69 fail("exception was expected of nextToken() if no input was set on parser"); 70 } catch(XmlPullParserException ex) {} 71 72 xpp.setInput(null); 73 assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType()); 74 try { 75 xpp.nextToken(); 76 fail("exception was expected of next() if no input was set on parser"); 77 } catch(XmlPullParserException ex) {} 78 79 xpp.setInput(null); try { 82 xpp.setFeature(FEATURE_XML_ROUNDTRIP, true); 83 } catch(Exception ex) { 84 } 85 boolean roundtripSupported = xpp.getFeature(FEATURE_XML_ROUNDTRIP); 87 88 89 for(int i = 1; i <= 2; ++i) { 91 xpp.setInput(new StringReader (i == 1 ? "<foo/>" : "<foo></foo>")); 92 boolean empty = (i == 1); 93 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 94 xpp.nextToken(); 95 checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, empty, 0); 96 if(roundtripSupported) { 97 if(empty) { 98 assertEquals("empty tag roundtrip", 103 printable("<foo/>"), 104 printable(xpp.getText())); 105 } else { 106 assertEquals("start tag roundtrip", 107 printable("<foo>"), 108 printable(xpp.getText())); 109 } 110 } 111 xpp.nextToken(); 112 checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1); 113 if(roundtripSupported) { 114 if(empty) { 115 assertEquals("empty tag roundtrip", 116 printable("<foo/>"), 117 printable(xpp.getText())); 118 } else { 119 assertEquals("end tag roundtrip", 120 printable("</foo>"), 121 printable(xpp.getText())); 122 } 123 } 124 xpp.nextToken(); 125 checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1); 126 } 127 } 128 129 130 public void testNormalization() throws Exception { 131 XmlPullParser xpp = factory.newPullParser(); 132 assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 133 testNormalization(xpp); 134 try{ xpp.setFeature(FEATURE_XML_ROUNDTRIP, false); } catch(Exception ex) {} 135 testNormalization(xpp); 136 try{ xpp.setFeature(FEATURE_XML_ROUNDTRIP, true); } catch(Exception ex) {} 137 testNormalization(xpp); 138 } 139 140 public void testNormalization(XmlPullParser xpp) throws Exception { 141 xpp.setInput(new StringReader ("<foo>\n \r\n \n\r</foo>")); 142 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 143 xpp.nextToken(); 144 checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, false, 0); 145 146 boolean roundtrip = xpp.getFeature(FEATURE_XML_ROUNDTRIP) == false; 147 String text = nextTokenGathered(xpp, XmlPullParser.TEXT, true); 148 if(roundtrip) { 149 assertEquals(printable("\n \n \n\n"), printable(text)); 150 assertEquals("\n \n \n\n", text); 151 } else { 152 assertEquals(printable("\n \r\n \n\r"), printable(text)); 153 assertEquals("\n \r\n \n\r", text); 154 } 155 156 checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1); 157 xpp.nextToken(); 158 checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1); 159 } 160 } 161 162 | Popular Tags |