1 2 4 package org.xmlpull.v1.tests; 5 6 import junit.framework.TestCase; 8 import junit.framework.TestSuite; 9 10 import java.io.ByteArrayInputStream ; 11 import java.io.StringReader ; 12 13 import org.xmlpull.v1.XmlPullParser; 14 import org.xmlpull.v1.XmlPullParserFactory; 15 import org.xmlpull.v1.XmlPullParserException; 16 17 22 public class TestSimple extends UtilTestCase { 23 private XmlPullParserFactory factory; 24 25 public TestSimple(String name) { 26 super(name); 27 } 28 29 protected void setUp() throws XmlPullParserException { 30 factory = factoryNewInstance(); 31 assertEquals(false, factory.isNamespaceAware()); 34 assertEquals(false, factory.isValidating()); 35 } 36 37 protected void tearDown() { 38 } 39 40 public void testSimple() throws Exception { 41 XmlPullParser xpp = factory.newPullParser(); 42 assertEquals(false, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 43 44 assertEquals("START_DOCUMENT", xpp.TYPES[xpp.START_DOCUMENT]); 46 assertEquals("END_DOCUMENT", xpp.TYPES[xpp.END_DOCUMENT]); 47 assertEquals("START_TAG", xpp.TYPES[xpp.START_TAG]); 48 assertEquals("END_TAG", xpp.TYPES[xpp.END_TAG]); 49 assertEquals("TEXT", xpp.TYPES[xpp.TEXT]); 50 assertEquals("CDSECT", xpp.TYPES[xpp.CDSECT]); 51 assertEquals("ENTITY_REF", xpp.TYPES[xpp.ENTITY_REF]); 52 assertEquals("IGNORABLE_WHITESPACE", xpp.TYPES[xpp.IGNORABLE_WHITESPACE]); 53 assertEquals("PROCESSING_INSTRUCTION", xpp.TYPES[xpp.PROCESSING_INSTRUCTION]); 54 assertEquals("COMMENT", xpp.TYPES[xpp.COMMENT]); 55 assertEquals("DOCDECL", xpp.TYPES[xpp.DOCDECL]); 56 57 assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType()); 59 try { 60 xpp.next(); 61 fail("exception was expected of next() if no input was set on parser"); 62 } catch(XmlPullParserException ex) {} 63 64 xpp.setInput(null); 65 assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType()); 66 try { 67 xpp.next(); 68 fail("exception was expected of next() if no input was set on parser"); 69 } catch(XmlPullParserException ex) {} 70 71 try { 72 xpp.setInput(null, null); 73 fail("exception was expected of setInput() if input stream is null"); 74 } catch(IllegalArgumentException ex) {} 75 76 xpp.setInput(null); 77 assertTrue("line number must be -1 or >= 1 not "+xpp.getLineNumber(), 78 xpp.getLineNumber() == -1 || xpp.getLineNumber() >= 1); 79 assertTrue("column number must be -1 or >= 0 not "+xpp.getColumnNumber(), 80 xpp.getColumnNumber() == -1 || xpp.getColumnNumber() >= 0); 81 82 xpp.setInput(new StringReader ("<foo></foo>")); 84 assertEquals(null, xpp.getInputEncoding()); 85 checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1); 86 xpp.next(); 87 checkParserState(xpp, 1, xpp.START_TAG, "foo", null, false , 0); 88 xpp.next(); 89 checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1); 90 xpp.next(); 91 checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1); 92 93 94 byte[] binput = "<foo/>".getBytes("UTF-8"); xpp.setInput(new ByteArrayInputStream ( binput ), "UTF-8" ); 97 assertEquals("UTF-8", xpp.getInputEncoding()); 98 checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1); 100 xpp.next(); 101 checkParserState(xpp, 1, xpp.START_TAG, "foo", null, true , 0); 102 xpp.next(); 103 checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1); 104 xpp.next(); 105 checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1); 106 107 xpp.setInput(new StringReader ("<foo attrName='attrVal'>bar<p:t>\r\n\t </p:t></foo>")); 109 checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1); 110 xpp.next(); 111 checkParserState(xpp, 1, xpp.START_TAG, "foo", null, false, 1); 112 checkAttrib(xpp, 0, "attrName", "attrVal"); 113 xpp.next(); 114 checkParserState(xpp, 1, xpp.TEXT, null, "bar", false, -1); 115 assertEquals(false, xpp.isWhitespace()); 116 xpp.next(); 117 checkParserState(xpp, 2, xpp.START_TAG, "p:t", null, false, 0); 118 xpp.next(); 119 checkParserState(xpp, 2, xpp.TEXT, null, "\n\t ", false, -1); 120 assertTrue(xpp.isWhitespace()); 121 xpp.next(); 122 checkParserState(xpp, 2, xpp.END_TAG, "p:t", null, false, -1); 123 xpp.next(); 124 checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1); 125 xpp.next(); 126 checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1); 127 128 129 } 130 131 public static void main (String [] args) { 132 junit.textui.TestRunner.run (new TestSuite(TestSimple.class)); 133 } 134 135 } 136 137 | Popular Tags |