8166220: Catalog API: JAXP XML Processor Support - add StAX test coverage
authorjoehw
Mon, 19 Sep 2016 14:21:15 -0700
changeset 41023 6de6d5c0e9d5
parent 41022 b3a710767ce0
child 41024 bf109947796c
8166220: Catalog API: JAXP XML Processor Support - add StAX test coverage Reviewed-by: lancea
jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport.java
jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport1.java
jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport2.java
jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport3.java
jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport4.java
jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport5.java
jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupportBase.java
--- a/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport.java	Thu Sep 15 21:08:30 2016 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport.java	Mon Sep 19 14:21:15 2016 -0700
@@ -25,7 +25,7 @@
 
 import java.io.File;
 import java.io.StringReader;
-
+import javax.xml.stream.XMLResolver;
 import javax.xml.transform.Source;
 import javax.xml.transform.URIResolver;
 import javax.xml.transform.dom.DOMSource;
@@ -42,7 +42,7 @@
 
 /**
  * @test
- * @bug 8158084 8162438 8162442
+ * @bug 8158084 8162438 8162442 8166220
  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  * @run testng/othervm -DrunSecMngr=true catalog.CatalogSupport
  * @run testng/othervm catalog.CatalogSupport
@@ -114,6 +114,15 @@
     }
 
     /*
+       Verifies the Catalog support on XMLStreamReader.
+    */
+    @Test(dataProvider = "data_StAXA")
+    public void testStAXA(boolean setUseCatalog, boolean useCatalog, String catalog,
+            String xml, XMLResolver resolver, String expected) throws Exception {
+        testStAX(setUseCatalog, useCatalog, catalog, xml, resolver, expected);
+    }
+
+    /*
        Verifies the Catalog support on resolving DTD, xsd import and include in
     Schema files.
     */
@@ -212,6 +221,20 @@
         };
     }
 
+    /*
+       DataProvider: for testing the StAX parser
+       Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
+     */
+    @DataProvider(name = "data_StAXA")
+    public Object[][] getDataStAX() {
+
+        return new Object[][]{
+            {false, true, xml_catalog, xml_system, null, expectedWCatalog},
+            {false, true, xml_catalog, xml_system, null, expectedWResolver},
+            {true, true, xml_catalog, xml_system, null, expectedWResolver}
+        };
+    }
+
     MyEntityHandler getMyEntityHandler(String elementName, String[] systemIds, InputSource... returnValues) {
        return new MyEntityHandler(systemIds, returnValues, elementName);
     }
@@ -262,8 +285,8 @@
         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
         ss.setSystemId(xml_val_test_id);
 
-        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
-        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
+        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
+        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 
         StreamSource source = new StreamSource(new File(xml_val_test));
 
@@ -271,8 +294,7 @@
         XmlInput[] returnValues = {new XmlInput(null, dtd_system, null), new XmlInput(null, xsd_val_test, null)};
         LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues);
 
-        StAXSource stax2 = getStaxSource(xml_val_test, xml_val_test_id);
-        StAXSource stax3 = getStaxSource(xml_val_test, xml_val_test_id);
+        StAXSource stax2 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 
         return new Object[][]{
             // use catalog
--- a/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport1.java	Thu Sep 15 21:08:30 2016 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport1.java	Mon Sep 19 14:21:15 2016 -0700
@@ -30,6 +30,7 @@
 import java.io.StringReader;
 
 import javax.xml.catalog.CatalogFeatures.Feature;
+import javax.xml.stream.XMLResolver;
 import javax.xml.transform.Source;
 import javax.xml.transform.URIResolver;
 import javax.xml.transform.dom.DOMSource;
@@ -47,7 +48,7 @@
 
 /*
  * @test
- * @bug 8158084 8162438 8162442
+ * @bug 8158084 8162438 8162442 8166220
  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  * @run testng/othervm -DrunSecMngr=true catalog.CatalogSupport1
  * @run testng/othervm catalog.CatalogSupport1
@@ -110,6 +111,15 @@
     }
 
     /*
+       Verifies the Catalog support on XMLStreamReader.
+    */
+    @Test(dataProvider = "data_StAXC")
+    public void testStAXC(boolean setUseCatalog, boolean useCatalog, String catalog,
+            String xml, XMLResolver resolver, String expected) throws Exception {
+        testStAX(setUseCatalog, useCatalog, catalog, xml, resolver, expected);
+    }
+
+    /*
        Verifies the Catalog support on resolving DTD, xsd import and include in
     Schema files.
     */
@@ -190,6 +200,18 @@
     }
 
     /*
+       DataProvider: for testing the StAX parser
+       Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
+     */
+    @DataProvider(name = "data_StAXC")
+    public Object[][] getDataStAX() {
+
+        return new Object[][]{
+            {false, true, null, xml_system, null, expectedWCatalog},
+        };
+    }
+
+    /*
        DataProvider: for testing Schema validation
        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
      */
@@ -218,8 +240,8 @@
         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
         ss.setSystemId(xml_val_test_id);
 
-        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
-        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
+        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
+        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 
         StreamSource source = new StreamSource(new File(xml_val_test));
 
@@ -227,8 +249,7 @@
         XmlInput[] returnValues = {new XmlInput(null, dtd_system, null), new XmlInput(null, xsd_val_test, null)};
         LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues);
 
-        StAXSource stax2 = getStaxSource(xml_val_test, xml_val_test_id);
-        StAXSource stax3 = getStaxSource(xml_val_test, xml_val_test_id);
+        StAXSource stax2 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 
         return new Object[][]{
             // use catalog
--- a/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport2.java	Thu Sep 15 21:08:30 2016 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport2.java	Mon Sep 19 14:21:15 2016 -0700
@@ -30,6 +30,8 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.StringReader;
+import javax.xml.stream.XMLResolver;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.URIResolver;
@@ -50,7 +52,7 @@
 
 /*
  * @test
- * @bug 8158084 8162438 8162442 8163535
+ * @bug 8158084 8162438 8162442 8163535 8166220
  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  * @run testng/othervm -DrunSecMngr=true catalog.CatalogSupport2
  * @run testng/othervm catalog.CatalogSupport2
@@ -129,6 +131,15 @@
     }
 
     /*
+       Verifies the Catalog support on XMLStreamReader.
+    */
+    @Test(dataProvider = "data_StAXC")
+    public void testStAXC(boolean setUseCatalog, boolean useCatalog, String catalog,
+            String xml, XMLResolver resolver, String expected) throws Exception {
+        testStAXNegative(setUseCatalog, useCatalog, catalog, xml, resolver, expected);
+    }
+
+    /*
        Verifies the Catalog support on resolving DTD, xsd import and include in
     Schema files.
     */
@@ -205,6 +216,17 @@
     }
 
     /*
+       DataProvider: for testing the StAX parser
+       Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
+     */
+    @DataProvider(name = "data_StAXC")
+    public Object[][] getDataStAX() {
+        return new Object[][]{
+            {false, true, xml_catalog, xml_system, null, "null"},
+        };
+    }
+
+    /*
        DataProvider: for testing Schema validation
        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
      */
@@ -233,8 +255,8 @@
         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
         ss.setSystemId(xml_val_test_id);
 
-        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
-        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
+        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
+        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 
         StreamSource source = new StreamSource(new File(xml_val_test));
 
--- a/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport3.java	Thu Sep 15 21:08:30 2016 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport3.java	Mon Sep 19 14:21:15 2016 -0700
@@ -29,6 +29,8 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.StringReader;
+import javax.xml.stream.XMLResolver;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.URIResolver;
@@ -49,7 +51,7 @@
 
 /*
  * @test
- * @bug 8158084 8162438 8162442 8163535
+ * @bug 8158084 8162438 8162442 8163535 8166220
  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  * @run testng/othervm -DrunSecMngr=true catalog.CatalogSupport3
  * @run testng/othervm catalog.CatalogSupport3
@@ -125,6 +127,15 @@
     }
 
     /*
+       Verifies the Catalog support on XMLStreamReader.
+    */
+    @Test(dataProvider = "data_StAXC")
+    public void testStAXC(boolean setUseCatalog, boolean useCatalog, String catalog,
+            String xml, XMLResolver resolver, String expected) throws Exception {
+        testStAXNegative(setUseCatalog, useCatalog, catalog, xml, resolver, expected);
+    }
+
+    /*
        Verifies the Catalog support on resolving DTD, xsd import and include in
     Schema files.
     */
@@ -206,6 +217,17 @@
     }
 
     /*
+       DataProvider: for testing the StAX parser
+       Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
+     */
+    @DataProvider(name = "data_StAXC")
+    public Object[][] getDataStAX() {
+        return new Object[][]{
+            {true, false, xml_catalog, xml_system, null, "null"},
+        };
+    }
+
+    /*
        DataProvider: for testing Schema validation
        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
      */
@@ -234,8 +256,8 @@
         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
         ss.setSystemId(xml_val_test_id);
 
-        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
-        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
+        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
+        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 
         StreamSource source = new StreamSource(new File(xml_val_test));
 
--- a/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport4.java	Thu Sep 15 21:08:30 2016 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport4.java	Mon Sep 19 14:21:15 2016 -0700
@@ -28,7 +28,7 @@
 
 import java.io.File;
 import java.io.StringReader;
-
+import javax.xml.stream.XMLResolver;
 import javax.xml.transform.Source;
 import javax.xml.transform.URIResolver;
 import javax.xml.transform.dom.DOMSource;
@@ -46,7 +46,7 @@
 
 /**
  * @test
- * @bug 8158084 8162438 8162442
+ * @bug 8158084 8162438 8162442 8166220
  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  * @run testng/othervm -DrunSecMngr=true catalog.CatalogSupport4
  * @run testng/othervm catalog.CatalogSupport4
@@ -118,6 +118,15 @@
     }
 
     /*
+       Verifies the Catalog support on XMLStreamReader.
+    */
+    @Test(dataProvider = "data_StAXA")
+    public void testStAXA(boolean setUseCatalog, boolean useCatalog, String catalog,
+            String xml, XMLResolver resolver, String expected) throws Exception {
+        testStAX(setUseCatalog, useCatalog, catalog, xml, resolver, expected);
+    }
+
+    /*
        Verifies the Catalog support on resolving DTD, xsd import and include in
     Schema files.
     */
@@ -199,6 +208,18 @@
         };
     }
 
+    /*
+       DataProvider: for testing the StAX parser
+       Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
+     */
+    @DataProvider(name = "data_StAXA")
+    public Object[][] getDataStAX() {
+
+        return new Object[][]{
+            {true, true, xml_catalog, xml_system, null, expectedWCatalog},
+        };
+    }
+
     MyEntityHandler getMyEntityHandler(String elementName, String[] systemIds, InputSource... returnValues) {
        return new MyEntityHandler(systemIds, returnValues, elementName);
     }
@@ -230,8 +251,8 @@
         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
         ss.setSystemId(xml_val_test_id);
 
-        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
-        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
+        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
+        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
 
         StreamSource source = new StreamSource(new File(xml_val_test));
 
@@ -241,8 +262,8 @@
             {false, true, true, ds, null, null, null, xml_catalog},
             {true, false, true, ss, null, null, xml_catalog, null},
             {false, true, true, ss, null, null, null, xml_catalog},
-            {true, false, true, stax, null, null, xml_catalog, null},
-            {false, true, true, stax1, null, null, null, xml_catalog},
+            {true, false, true, stax, null, null, xml_catalog, xml_catalog},
+            {false, true, true, stax1, null, null, xml_catalog, xml_catalog},
             {true, false, true, source, null, null, xml_catalog, null},
             {false, true, true, source, null, null, null, xml_catalog},
         };
--- a/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport5.java	Thu Sep 15 21:08:30 2016 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupport5.java	Mon Sep 19 14:21:15 2016 -0700
@@ -25,6 +25,8 @@
 
 import java.io.File;
 import java.io.StringReader;
+import javax.xml.stream.XMLResolver;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.URIResolver;
@@ -43,7 +45,7 @@
 
 /*
  * @test
- * @bug 8158084 8163232
+ * @bug 8158084 8163232 8166220
  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  * @run testng/othervm -DrunSecMngr=true catalog.CatalogSupport5
  * @run testng/othervm catalog.CatalogSupport5
@@ -107,6 +109,15 @@
     }
 
     /*
+       Verifies the Catalog support on XMLStreamReader.
+    */
+    @Test(dataProvider = "data_StAXC", expectedExceptions = XMLStreamException.class)
+    public void testStAXC(boolean setUseCatalog, boolean useCatalog, String catalog,
+            String xml, XMLResolver resolver, String expected) throws Exception {
+        testStAX(setUseCatalog, useCatalog, catalog, xml, resolver, expected);
+    }
+
+    /*
        Verifies the Catalog support on resolving DTD, xsd import and include in
     Schema files.
     */
@@ -183,6 +194,18 @@
     }
 
     /*
+       DataProvider: for testing the StAX parser
+       Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
+     */
+    @DataProvider(name = "data_StAXC")
+    public Object[][] getDataStAX() {
+
+        return new Object[][]{
+            {false, true, xml_bogus_catalog, xml_system, null, expectedWCatalog},
+        };
+    }
+
+    /*
        DataProvider: for testing Schema validation
        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
      */
@@ -211,8 +234,8 @@
         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
         ss.setSystemId(xml_val_test_id);
 
-        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
-        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
+        StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
+        StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 
         StreamSource source = new StreamSource(new File(xml_val_test));
 
--- a/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupportBase.java	Thu Sep 15 21:08:30 2016 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogSupportBase.java	Mon Sep 19 14:21:15 2016 -0700
@@ -319,6 +319,31 @@
     }
 
     /*
+       Verifies the Catalog support on StAX parser.
+    */
+    public void testStAX(boolean setUseCatalog, boolean useCatalog, String catalog,
+            String xml, XMLResolver resolver, String expected) throws Exception {
+
+            XMLStreamReader streamReader = getStreamReader(
+                    setUseCatalog, useCatalog, catalog, xml, resolver);
+            String text = getText(streamReader, XMLStreamConstants.CHARACTERS);
+            assertEquals(expected, text.trim(), "Catalog support for StAX");
+    }
+
+    /*
+       Verifies that the Catalog support for StAX parser is disabled when
+       USE_CATALOG == false.
+    */
+    public void testStAXNegative(boolean setUseCatalog, boolean useCatalog, String catalog,
+            String xml, XMLResolver resolver, String expected) throws Exception {
+
+            XMLStreamReader streamReader = getStreamReader(
+                    setUseCatalog, useCatalog, catalog, xml, resolver);
+            String text = getText(streamReader, XMLStreamConstants.ENTITY_REFERENCE);
+            assertEquals(expected, text.trim(), "Catalog support for StAX");
+    }
+
+    /*
        Verifies the Catalog support on resolving DTD, xsd import and include in
     Schema files.
     */
@@ -514,15 +539,24 @@
      *
      * @param xmlFile the XML source file
      * @param xmlFileId the systemId of the source
+     * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set
+     * through the factory
+     * @param useCatalog the value of USE_CATALOG
+     * @param catalog a catalog
      * @return a StAXSource
      * @throws XMLStreamException
      * @throws FileNotFoundException
      */
-    StAXSource getStaxSource(String xmlFile, String xmlFileId) {
+    StAXSource getStaxSource(String xmlFile, String xmlFileId, boolean setUseCatalog,
+            boolean useCatalog, String catalog) {
         StAXSource ss = null;
         try {
-            ss = new StAXSource(
-                    XMLInputFactory.newFactory().createXMLEventReader(
+            XMLInputFactory xif = XMLInputFactory.newFactory();
+            if (setUseCatalog) {
+                xif.setProperty(XMLConstants.USE_CATALOG, useCatalog);
+            }
+            xif.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
+            ss = new StAXSource(xif.createXMLEventReader(
                         xmlFileId, new FileInputStream(xmlFile)));
         } catch (Exception e) {}
 
@@ -531,6 +565,10 @@
 
     /**
      * Creates an XMLStreamReader.
+     *
+     * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set
+     * through the factory
+     * @param useCatalog the value of USE_CATALOG
      * @param catalog the path to a catalog
      * @param xml the xml to be parsed
      * @param resolver a resolver to be set on the reader
@@ -542,9 +580,17 @@
             String catalog, String xml, XMLResolver resolver)
             throws FileNotFoundException, XMLStreamException {
         XMLInputFactory factory = XMLInputFactory.newInstance();
-        factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
+        if (catalog != null) {
+            factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
+        }
+
+        factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
         factory.setProperty(XMLInputFactory.IS_COALESCING, true);
-        factory.setProperty(XMLInputFactory.RESOLVER, resolver);
+
+        if (resolver != null) {
+            factory.setProperty(XMLInputFactory.RESOLVER, resolver);
+        }
+
         if (setUseCatalog) {
             factory.setProperty(XMLConstants.USE_CATALOG, useCatalog);
         }
@@ -560,17 +606,28 @@
      * @return the text of the first element
      * @throws XMLStreamException
      */
-    String getText(XMLStreamReader streamReader) throws XMLStreamException {
+    String getText(XMLStreamReader streamReader, int type) throws XMLStreamException {
+        StringBuilder text = new StringBuilder();
+        StringBuilder entityRef = new StringBuilder();
+
         while(streamReader.hasNext()){
-            int eventType = streamReader.next() ;
-            if(eventType == XMLStreamConstants.START_ELEMENT){
-                eventType = streamReader.next() ;
-                if(eventType == XMLStreamConstants.CHARACTERS){
-                    return streamReader.getText() ;
-                }
+            int eventType = streamReader.next();
+            switch (eventType) {
+                case XMLStreamConstants.START_ELEMENT:
+                    break;
+                case XMLStreamConstants.CHARACTERS:
+                    text.append(streamReader.getText());
+                    break;
+                case XMLStreamConstants.ENTITY_REFERENCE:
+                    entityRef.append(streamReader.getText());
+                    break;
             }
         }
-        return null;
+        if (type == XMLStreamConstants.CHARACTERS) {
+            return text.toString();
+        } else {
+            return entityRef.toString();
+        }
     }
 
     /**