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 TestSimpleValidation extends UtilTestCase { 23 private XmlPullParserFactory factory; 24 25 public static void main (String [] args) { 26 junit.textui.TestRunner.run (new TestSuite(TestSimpleValidation.class)); 27 } 28 29 30 public TestSimpleValidation(String name) { 31 super(name); 32 } 33 34 protected void setUp() throws XmlPullParserException { 35 factory = factoryNewInstance(); 36 assertEquals(false, factory.isNamespaceAware()); 37 assertEquals(false, factory.isValidating()); 38 } 40 41 public void testValidation() throws Exception { 42 XmlPullParser xpp = factory.newPullParser(); 43 try { 44 xpp.setFeature(xpp.FEATURE_VALIDATION, true); 45 } catch(XmlPullParserException ex) { 46 return; 47 } 48 PackageTests.addNote("* feature "+xpp.FEATURE_VALIDATION+" is supported\n"); 49 assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL)); 51 52 53 final String XML_MIN_PROLOG = 56 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"+ 57 "<!DOCTYPE greeting [\n"+ 58 "<!ELEMENT greeting (#PCDATA)>\n"+ 59 "]>\n"; 60 61 final String XML_MIN_VALID = XML_MIN_PROLOG+ 62 "<greeting>Hello, world!</greeting>\n"; 63 64 xpp.setInput(new StringReader ( XML_MIN_VALID )); 65 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 66 assertNull(xpp.getProperty(PROPERTY_XMLDECL_VERSION)); 67 assertNull(xpp.getProperty(PROPERTY_XMLDECL_STANDALONE)); 68 assertNull(xpp.getProperty(PROPERTY_XMLDECL_CONTENT)); 69 70 xpp.next(); 71 checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "greeting", null, false, 0); 72 73 assertEquals("1.0", xpp.getProperty(PROPERTY_XMLDECL_VERSION)); 75 assertEquals(null, xpp.getProperty(PROPERTY_XMLDECL_STANDALONE)); 76 77 xpp.next(); 78 checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, "Hello, world!", false, -1); 79 xpp.next(); 80 checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "greeting", null, false, -1); 81 xpp.next(); 82 checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1); 83 84 85 86 final String XML_MIN_INVALID = XML_MIN_PROLOG+ 88 "<greet>Hello, world!</greet>\n"; 89 xpp.setInput(new StringReader ( XML_MIN_INVALID )); 90 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 91 try { 92 xpp.next(); 93 fail("exception was expected of next() for invalid document element root"); 94 } catch(XmlPullParserException ex) {} 95 96 } 97 98 } 99 100 | Popular Tags |