CVS Update: xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests

Aleksander Andrzej Slominski <[email protected]>
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
aslom       02/08/23 14:11:52

  Modified:    src/java/tests/org/xmlpull/v1/tests PackageTests.java
                        TestEntityReplacement.java TestMisc.java
                        TestSimpleToken.java UtilTestCase.java
  Added:       src/java/tests/org/xmlpull/v1/tests TestProcessDocdecl.java
                        TestSimpleProcessDocdecl.java
                        TestSimpleValidation.java
  Log:
  updated tests to check implementations of
    PROCESS_DOCDECL and VALIDATION
  when used with next() method (still needed checks for nextToken())
  
  Revision  Changes    Path
  1.14      +5 -1      xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/PackageTests.java
  
  Index: PackageTests.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/PackageTests.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -b -t -w -r1.13 -r1.14
  --- PackageTests.java	2002/07/31 21:32:05	1.13
  +++ PackageTests.java	2002/08/23 19:11:51	1.14
  @@ -44,6 +44,10 @@
           suite.addTestSuite(TestMisc.class);
           suite.addTestSuite(TestSetInput.class);
   
  +        suite.addTestSuite(TestSimpleProcessDocdecl.class);
  +        suite.addTestSuite(TestSimpleValidation.class);
  +        suite.addTestSuite(TestProcessDocdecl.class);
  +
           // finally run tests based on XML input files
           suite.addTestSuite(TestBootstrapXmlTests.class);
           suite.addTestSuite(TestXmlSimple.class);
  
  
  
  1.8       +2 -2      xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestEntityReplacement.java
  
  Index: TestEntityReplacement.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestEntityReplacement.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -b -t -w -r1.7 -r1.8
  --- TestEntityReplacement.java	2002/07/31 21:32:05	1.7
  +++ TestEntityReplacement.java	2002/08/23 19:11:51	1.8
  @@ -73,7 +73,7 @@
           pp.setInput(new StringReader( XML_ENTITY_EXPANSION ) );
           try {
               pp.setFeature( pp.FEATURE_PROCESS_DOCDECL, true );
  -            PackageTests.addNote("* feature "+pp.FEATURE_PROCESS_DOCDECL+" is supported\n");
  +            //PackageTests.addNote("* feature "+pp.FEATURE_PROCESS_DOCDECL+" is supported\n");
           } catch( Exception ex ){
           }
           if( pp.getFeature( pp.FEATURE_PROCESS_DOCDECL ) ) {
  @@ -84,7 +84,7 @@
           pp.setInput(new StringReader( XML_ENTITY_EXPANSION ) );
           try {
               pp.setFeature( pp.FEATURE_VALIDATION, true );
  -            PackageTests.addNote("* feature "+pp.FEATURE_VALIDATION+" is supported\n");
  +            //PackageTests.addNote("* feature "+pp.FEATURE_VALIDATION+" is supported\n");
           } catch( Exception ex ){
           }
           if( pp.getFeature( pp.FEATURE_VALIDATION ) ) {
  
  
  
  1.11      +79 -16    xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestMisc.java
  
  Index: TestMisc.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestMisc.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -b -t -w -r1.10 -r1.11
  --- TestMisc.java	2002/07/31 21:32:05	1.10
  +++ TestMisc.java	2002/08/23 19:11:51	1.11
  @@ -185,6 +185,69 @@
   
       }
   
  +    public void testPI() throws Exception {
  +        XmlPullParser pp = factory.newPullParser();
  +        //System.out.println(getClass()+"-pp="+pp);
  +
  +        pp.setInput( new StringReader( "<foo><?XmLsdsd test?></foo>" ) );
  +        pp.require( pp.START_DOCUMENT, null, null);
  +        pp.next();
  +        pp.require( pp.START_TAG, null, "foo");
  +        pp.next();
  +        pp.require( pp.END_TAG, null, "foo");
  +
  +        pp.setInput( new StringReader( "<?xml version='1.0'?><foo/>" ) );
  +        pp.require( pp.START_DOCUMENT, null, null);
  +        pp.next();
  +        pp.require( pp.START_TAG, null, "foo");
  +        pp.next();
  +        pp.require( pp.END_TAG, null, "foo");
  +        pp.next();
  +        pp.require( pp.END_DOCUMENT, null, null);
  +
  +        pp.setInput( new StringReader(
  +                        "<?xml  version=\"1.0\" \t encoding='UTF-8' \nstandalone='yes' ?><foo/>" ));
  +        pp.require( pp.START_DOCUMENT, null, null);
  +        pp.next();
  +        pp.require( pp.START_TAG, null, "foo");
  +
  +        pp.setInput( new StringReader(
  +                        "<?xml  version=\"1.0\" \t encoding='UTF-8' \nstandalone='yes' ?><foo/>" ));
  +        pp.require( pp.START_DOCUMENT, null, null);
  +        pp.next();
  +        pp.require( pp.START_TAG, null, "foo");
  +
  +        // XML decl without required verion
  +        pp.setInput( new StringReader( "<?xml test?><foo/>" ) );
  +        pp.require( pp.START_DOCUMENT, null, null);
  +        try {
  +            pp.next();
  +            fail("expected exception for invalid XML declaration wiuthout verion");
  +        } catch(XmlPullParserException ex){}
  +
  +
  +        pp.setInput( new StringReader( "<foo><?XmL test?></foo>" ) );
  +        pp.require( pp.START_DOCUMENT, null, null);
  +        pp.next();
  +        pp.require( pp.START_TAG, null, "foo");
  +        try {
  +            pp.next();
  +            fail("expected exception for invalid PI starting with xml");
  +        } catch(XmlPullParserException ex){}
  +
  +    }
  +
  +    public void testComments() throws Exception {
  +        XmlPullParser pp = factory.newPullParser();
  +        pp.setInput( new StringReader( "<foo><!-- B+, B or B---></foo>" ) );
  +        pp.require( pp.START_DOCUMENT, null, null);
  +        pp.next();
  +        pp.require( pp.START_TAG, null, "foo");
  +        try {
  +            pp.next();
  +            fail("expected eception for invalid comment ending with --->");
  +        } catch(XmlPullParserException ex){}
  +    }
   
       public void testReportNamespaceAttributes() throws Exception {
           XmlPullParser pp = factory.newPullParser();
  
  
  
  1.19      +17 -0     xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestSimpleToken.java
  
  Index: TestSimpleToken.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestSimpleToken.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -b -t -w -r1.18 -r1.19
  --- TestSimpleToken.java	2002/08/08 05:05:37	1.18
  +++ TestSimpleToken.java	2002/08/23 19:11:51	1.19
  @@ -35,11 +35,28 @@
           factory = factoryNewInstance();
           factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
           assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
  +        //System.out.println(getClass()+"-factory="+factory);
       }
   
       protected void tearDown() {
       }
   
  +    public void testVerySimpleToken() throws Exception {
  +        XmlPullParser xpp = factory.newPullParser();
  +        assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
  +        xpp.setInput(new StringReader("<foo><!--comment--><?target pi?></foo>"));
  +        checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
  +        xpp.nextToken();
  +        checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, false, 0);
  +        xpp.nextToken();
  +        checkParserStateNs(xpp, 1, xpp.COMMENT, null, 0, null, null, "comment", false, -1);
  +        xpp.nextToken();
  +        checkParserStateNs(xpp, 1, xpp.PROCESSING_INSTRUCTION, null, 0, null, null, "target pi", false, -1);
  +        xpp.nextToken();
  +        checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1);
  +        xpp.nextToken();
  +        checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
  +    }
   
       public void testSimpleToken() throws Exception {
           XmlPullParser xpp = factory.newPullParser();
  
  
  
  1.16      +28 -11    xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/UtilTestCase.java
  
  Index: UtilTestCase.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/UtilTestCase.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -b -t -w -r1.15 -r1.16
  --- UtilTestCase.java	2002/08/08 05:05:37	1.15
  +++ UtilTestCase.java	2002/08/23 19:11:51	1.16
  @@ -139,7 +139,6 @@
           int nsCount,
           String namespace,
           String name,
  -        String text,
           boolean isEmpty,
           int attribCount
       ) throws XmlPullParserException, IOException
  @@ -160,6 +159,33 @@
           assertEquals("getNamespacesCount(getDepth())", nsCount, xpp.getNamespaceCount(depth));
           assertEquals("getNamespace()", namespace, xpp.getNamespace());
   
  +        if(type == xpp.START_TAG) {
  +            assertEquals("isEmptyElementTag()", isEmpty, xpp.isEmptyElementTag());
  +        } else {
  +            try {
  +                xpp.isEmptyElementTag();
  +                fail("isEmptyElementTag() must throw exception if parser not on START_TAG");
  +            } catch(XmlPullParserException ex) {
  +            }
  +        }
  +        assertEquals("getAttributeCount()", attribCount, xpp.getAttributeCount());
  +    }
  +
  +    public void checkParserStateNs(
  +        XmlPullParser xpp,
  +        int depth,
  +        int type,
  +        String prefix,
  +        int nsCount,
  +        String namespace,
  +        String name,
  +        String text,
  +        boolean isEmpty,
  +        int attribCount
  +    ) throws XmlPullParserException, IOException
  +    {
  +        checkParserStateNs(xpp, depth, type, prefix, nsCount, namespace, name, isEmpty, attribCount);
  +
           if(xpp.getEventType() != xpp.START_TAG && xpp.getEventType() != xpp.END_TAG) {
               assertEquals("getText()", printable(text), printable(xpp.getText()));
   
  @@ -179,16 +205,7 @@
   
           }
   
  -        if(type == xpp.START_TAG) {
  -            assertEquals("isEmptyElementTag()", isEmpty, xpp.isEmptyElementTag());
  -        } else {
  -            try {
  -                xpp.isEmptyElementTag();
  -                fail("isEmptyElementTag() must throw exception if parser not on START_TAG");
  -            } catch(XmlPullParserException ex) {
  -            }
  -        }
  -        assertEquals("getAttributeCount()", attribCount, xpp.getAttributeCount());
  +
       }
   
       public void checkAttrib(
  
  
  
  1.1                  xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestProcessDocdecl.java
  
  Index: TestProcessDocdecl.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
  
  package org.xmlpull.v1.tests;
  
  //import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import java.io.ByteArrayInputStream;
  import java.io.StringReader;
  
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserFactory;
  import org.xmlpull.v1.XmlPullParserException;
  
  /**
   * Test FEATURE_PROCESS_DOCDECL  (when supported)
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class TestProcessDocdecl extends UtilTestCase {
      private XmlPullParserFactory factory;
  
      public static void main (String[] args) {
          junit.textui.TestRunner.run (new TestSuite(TestProcessDocdecl.class));
      }
  
  
      public TestProcessDocdecl(String name) {
          super(name);
      }
  
      protected void setUp() throws XmlPullParserException {
          factory = factoryNewInstance();
          //assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
          //assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_VALIDATION));
          assertEquals(false, factory.isNamespaceAware());
          assertEquals(false, factory.isValidating());
          //System.out.println("factory="+factory);
      }
  
      private XmlPullParser newParser(boolean useNamespaces, boolean useValidation)
          throws XmlPullParserException
      {
          XmlPullParser xpp = factory.newPullParser();
          xpp.setFeature(xpp.FEATURE_PROCESS_NAMESPACES, useNamespaces);
          assertEquals(useNamespaces, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
          try {
              xpp.setFeature(xpp.FEATURE_PROCESS_DOCDECL, true);
          } catch(XmlPullParserException ex) {
              return null;
          }
          assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL));
          try {
              xpp.setFeature(xpp.FEATURE_VALIDATION, useValidation);
          } catch(XmlPullParserException ex) {
              return null;
          }
          assertEquals(useValidation, xpp.getFeature(XmlPullParser.FEATURE_VALIDATION));
          return xpp;
      }
  
      protected void tearDown() {
      }
  
      public void testSimpleEntity() throws Exception {
          testSimpleEntity(false, false);
          testSimpleEntity(true, false);
          testSimpleEntity(false, true);
          testSimpleEntity(true, true);
      }
  
      public void testAttributeNormalization() throws Exception {
          //TODO check that Xerces NON-validating normalization of NMTOKEN attributes
          //http://www.w3.org/TR/REC-xml#AVNormalize
  
      }
  
      public void testSimpleEntity(boolean useNamespaces, boolean useValidation) throws Exception {
          XmlPullParser xpp = newParser(useNamespaces, useValidation);
          if(xpp == null) return;
  
          //http://www.w3.org/TR/REC-xml#intern-replacement
          final String XML_SIMPLE_ENT_PROLOG =
              "<?xml version='1.0'?>\n"+
              "<!DOCTYPE test [\n"+
              "<!ENTITY % YN '\"Yes\"' >\n"+
              //"<!ENTITY WhatHeSaid \"He said %YN;\" >\n"+
              "<!ELEMENT test (#PCDATA) >\n"+
              //          "<!ENTITY % pub    \"&#xc9;ditions Gallimard\" >\n"+
              //          "<!ENTITY   rights \"All rights reserved\" >\n"+
              //          "<!ENTITY   book   \"La Peste: Albert Camus,\n"+
              "<!ENTITY   pub    \"&#xc9;ditions Gallimard\" >\n"+
              "<!ENTITY   rights \"All rights reserved\" >\n"+
              "<!ENTITY   book   \"La Peste: Albert Camus,\n"+
              "&#xA9; 1947 &pub;. &rights;\" >\n"+
              "]>\n";
          final String XML_SIMPLE_ENT = XML_SIMPLE_ENT_PROLOG+
              "<test>Publication: &book; </test>\n";
  
          //        final String PUB_ENTITY_INTERNAL_REPLACEMENT =
          //            "La Peste: Albert Camus,\n"+
          //            "© 1947 Éditions Gallimard. &rights;";
          final String PUB_ENTITY_INTERNAL_REPLACEMENT =
              "La Peste: Albert Camus,\n"+
              "&pub;. &rights;";
  
  
          final String PUB_ENTITY_REPLACEMENT =
              "La Peste: Albert Camus,\n"+
              "© 1947 Éditions Gallimard. All rights reserved";
  
          //next
          xpp.setInput(new StringReader( XML_SIMPLE_ENT ));
          checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "test", null, false/*empty*/, 0);
          xpp.next();
          String expectedContent = "Publication: "+PUB_ENTITY_REPLACEMENT+" ";
          checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, expectedContent, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "test", null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
  
          //nextToken
      }
  
      public void testEntityWithMarkup() throws Exception {
          testEntityWithMarkup(false, false);
          testEntityWithMarkup(true, false);
          testEntityWithMarkup(true, true);
          testEntityWithMarkup(false, true);
      }
  
      public void testEntityWithMarkup(boolean useNamespaces, boolean useValidation)
          throws Exception
      {
          XmlPullParser xpp = newParser(useNamespaces, useValidation);
          if(xpp == null) return;
  
          //http://www.w3.org/TR/REC-xml#sec-entexpand
          // derived from
          final String XML_REPLACE_ENT =
              "<?xml version='1.0'?>\n"+
              "<!DOCTYPE test [\n"+
              "<!ELEMENT test (#PCDATA|p)* >\n"+
              "<!ELEMENT p (#PCDATA) >\n"+
              "<!ENTITY example \"<p>An ampersand (&#38;#38;) may be escaped\n"+
              "numerically (&#38;#38;#38;) or with a general entity\n"+
              "(&amp;amp;).</p>\" >\n"+
              "]>\n"+
              "<test>&example; </test>     ";
  
          final String EXAMPLE_ENTITY_INTERNAL_REPLACEMENT =
              "<p>An ampersand (&#38;) may be escaped\n"+
              "numerically (&#38;#38;) or with a general entity\n"+
              "(&amp;amp;).</p>\n";
  
          final String EXAMPLE_ENTITY_TEXT_EVENT =
              "An ampersand (&) may be escaped\n"+
              "numerically (&#38;) or with a general entity\n"+
              "(&amp;).";
  
          //next
          xpp.setInput(new StringReader( XML_REPLACE_ENT ));
          checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "test", null, false/*empty*/, 0);
          xpp.next();
          checkParserStateNs(xpp, 2, xpp.START_TAG, null, 0, "", "p", null, false/*empty*/, 0);
          xpp.next();
          String expectedContent = EXAMPLE_ENTITY_TEXT_EVENT;
          checkParserStateNs(xpp, 2, xpp.TEXT, null, 0, null, null, expectedContent, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 2, xpp.END_TAG, null, 0, "", "p", null, false/*empty*/, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, " ", false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "test", null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
      }
  
      public void testTricky() throws Exception {
          testTricky(false, false);
          testTricky(true, false);
          testTricky(true, true);
          testTricky(false, true);
      }
  
      public void testTricky(boolean useNamespaces, boolean useValidation)
          throws Exception
      {
          XmlPullParser xpp = newParser(useNamespaces, useValidation);
          if(xpp == null) return;
  
          // derived from
          final String XML_TRICKY =
              "<?xml version='1.0'?>\n"+
              "<!DOCTYPE test [\n"+
              "<!ELEMENT test (#PCDATA) >\n"+
              "<!ENTITY % xx '&#37;zz;'>\n"+
              "<!ENTITY % zz '&#60;!ENTITY tricky \"error-prone\" >' >\n"+
              "%xx;\n"+
              "]>\n"+
              "<test>This sample shows a &tricky; method.</test>\n";
  
  
          final String EXPECTED_XML_TRICKY =
              "This sample shows a error-prone method.";
  
          //next
          xpp.setInput(new StringReader( XML_TRICKY ));
          checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "test", null, false/*empty*/, 0);
          xpp.next();
          String expectedContent = EXPECTED_XML_TRICKY;
          checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, expectedContent, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "test", null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
  
          //TODO nextToken()
  
      }
  
  }
  
  
  
  
  1.1                  xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestSimpleProcessDocdecl.java
  
  Index: TestSimpleProcessDocdecl.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
  
  package org.xmlpull.v1.tests;
  
  //import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import java.io.ByteArrayInputStream;
  import java.io.StringReader;
  
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserFactory;
  import org.xmlpull.v1.XmlPullParserException;
  
  /**
   * Test FEATURE_PROCESS_DOCDECL  (when supported)
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class TestSimpleProcessDocdecl extends UtilTestCase {
      private XmlPullParserFactory factory;
  
      public static void main (String[] args) {
          junit.textui.TestRunner.run (new TestSuite(TestSimpleProcessDocdecl.class));
      }
  
  
      public TestSimpleProcessDocdecl(String name) {
          super(name);
      }
  
      protected void setUp() throws XmlPullParserException {
          factory = factoryNewInstance();
          assertEquals(false, factory.isNamespaceAware());
          assertEquals(false, factory.isValidating());
          //System.out.println("factory="+factory);
      }
  
      public void testProcessDocdecl() throws Exception {
          XmlPullParser xpp = factory.newPullParser();
          try {
              xpp.setFeature(xpp.FEATURE_PROCESS_DOCDECL, true);
          } catch(XmlPullParserException ex) {
              return;
          }
          PackageTests.addNote("* feature "+xpp.FEATURE_PROCESS_DOCDECL+" is supported\n");
          // setting validation MUST enables also PROCESS_DOCDECL
          assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL));
          // default is to have non-validating parser
          assertEquals(false, xpp.getFeature(XmlPullParser.FEATURE_VALIDATION));
  
          //http://www.w3.org/TR/REC-xml#NT-extSubsetDecl
          // minimum validation
          final String XML_MIN_PROLOG =
              "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"+
              "<!DOCTYPE greeting [\n"+
              "<!ENTITY name \"world\">\n"+
              "]>\n";
  
          final String XML_MIN_VALID = XML_MIN_PROLOG+
              "<greeting>Hello, &name;!</greeting>\n";
  
          xpp.setInput(new StringReader( XML_MIN_VALID ));
          checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "greeting", null, false/*empty*/, 0);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, "Hello, world!", false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "greeting", null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
  
  
  
          //AND WRONG
          final String XML_MIN_INVALID = XML_MIN_PROLOG+
              "<greet>Hello, &world;!</greet>\n";
          xpp.setInput(new StringReader( XML_MIN_INVALID ));
          checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "greet", null, false/*empty*/, 0);
          try {
              xpp.next();
              fail("exception was expected of next() for undeclared entity");
          } catch(XmlPullParserException ex) {}
  
      }
  
  }
  
  
  
  
  1.1                  xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestSimpleValidation.java
  
  Index: TestSimpleValidation.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
  
  package org.xmlpull.v1.tests;
  
  //import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import java.io.ByteArrayInputStream;
  import java.io.StringReader;
  
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserFactory;
  import org.xmlpull.v1.XmlPullParserException;
  
  /**
   * Test FEATURE_VALIDATION  (when supported)
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class TestSimpleValidation extends UtilTestCase {
      private XmlPullParserFactory factory;
  
      public static void main (String[] args) {
          junit.textui.TestRunner.run (new TestSuite(TestSimpleValidation.class));
      }
  
  
      public TestSimpleValidation(String name) {
          super(name);
      }
  
      protected void setUp() throws XmlPullParserException {
          factory = factoryNewInstance();
          assertEquals(false, factory.isNamespaceAware());
          assertEquals(false, factory.isValidating());
          //System.out.println("factory="+factory);
      }
  
      public void testValidation() throws Exception {
          XmlPullParser xpp = factory.newPullParser();
          try {
              xpp.setFeature(xpp.FEATURE_VALIDATION, true);
          } catch(XmlPullParserException ex) {
              return;
          }
          PackageTests.addNote("* feature "+xpp.FEATURE_VALIDATION+" is supported\n");
          // setting validation MUST enables also PROCESS_DOCDECL
          assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL));
  
  
          //http://www.w3.org/TR/REC-xml#NT-extSubsetDecl
          // minimum validation
          final String XML_MIN_PROLOG =
              "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"+
              "<!DOCTYPE greeting [\n"+
              "<!ELEMENT greeting (#PCDATA)>\n"+
              "]>\n";
  
          final String XML_MIN_VALID = XML_MIN_PROLOG+
              "<greeting>Hello, world!</greeting>\n";
  
          xpp.setInput(new StringReader( XML_MIN_VALID ));
          checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "greeting", null, false/*empty*/, 0);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, "Hello, world!", false, -1);
          xpp.next();
          checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "greeting", null, false, -1);
          xpp.next();
          checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
  
  
  
          //AND WRONG
          final String XML_MIN_INVALID = XML_MIN_PROLOG+
              "<greet>Hello, world!</greet>\n";
          xpp.setInput(new StringReader( XML_MIN_INVALID ));
          checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
          try {
              xpp.next();
              fail("exception was expected of next() for invalid document element root");
          } catch(XmlPullParserException ex) {}
  
      }
  
  }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.