8166220: Catalog API: JAXP XML Processor Support - add StAX test coverage

Reviewed-by: lancea
This commit is contained in:
Joe Wang 2016-09-19 14:21:15 -07:00
parent 609854cd89
commit b32db3c78f
7 changed files with 227 additions and 39 deletions

View File

@ -25,7 +25,7 @@ package catalog;
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 @@ import org.xml.sax.InputSource;
/**
* @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
@ -113,6 +113,15 @@ public class CatalogSupport extends CatalogSupportBase {
testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
}
/*
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 @@ public class CatalogSupport extends CatalogSupportBase {
};
}
/*
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 @@ public class CatalogSupport extends CatalogSupportBase {
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 @@ public class CatalogSupport extends CatalogSupportBase {
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

View File

@ -30,6 +30,7 @@ import java.io.File;
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 @@ import org.xml.sax.InputSource;
/*
* @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
@ -109,6 +110,15 @@ public class CatalogSupport1 extends CatalogSupportBase {
testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
}
/*
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.
@ -189,6 +199,18 @@ public class CatalogSupport1 extends CatalogSupportBase {
};
}
/*
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 @@ public class CatalogSupport1 extends CatalogSupportBase {
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 @@ public class CatalogSupport1 extends CatalogSupportBase {
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

View File

@ -30,6 +30,8 @@ import static jaxp.library.JAXPTestUtilities.setSystemProperty;
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 @@ import org.xml.sax.SAXParseException;
/*
* @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
@ -128,6 +130,15 @@ public class CatalogSupport2 extends CatalogSupportBase {
testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
}
/*
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.
@ -204,6 +215,17 @@ public class CatalogSupport2 extends CatalogSupportBase {
};
}
/*
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 @@ public class CatalogSupport2 extends CatalogSupportBase {
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));

View File

@ -29,6 +29,8 @@ import static jaxp.library.JAXPTestUtilities.setSystemProperty;
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 @@ import org.xml.sax.SAXParseException;
/*
* @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
@ -124,6 +126,15 @@ public class CatalogSupport3 extends CatalogSupportBase {
testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
}
/*
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 @@ public class CatalogSupport3 extends CatalogSupportBase {
};
}
/*
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 @@ public class CatalogSupport3 extends CatalogSupportBase {
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));

View File

@ -28,7 +28,7 @@ import static jaxp.library.JAXPTestUtilities.setSystemProperty;
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 @@ import org.xml.sax.InputSource;
/**
* @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
@ -117,6 +117,15 @@ public class CatalogSupport4 extends CatalogSupportBase {
testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
}
/*
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 @@ public class CatalogSupport4 extends CatalogSupportBase {
};
}
/*
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 @@ public class CatalogSupport4 extends CatalogSupportBase {
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 @@ public class CatalogSupport4 extends CatalogSupportBase {
{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},
};

View File

@ -25,6 +25,8 @@ package catalog;
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 @@ import org.xml.sax.SAXException;
/*
* @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
@ -106,6 +108,15 @@ public class CatalogSupport5 extends CatalogSupportBase {
testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
}
/*
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.
@ -182,6 +193,18 @@ public class CatalogSupport5 extends CatalogSupportBase {
};
}
/*
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 @@ public class CatalogSupport5 extends CatalogSupportBase {
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));

View File

@ -318,6 +318,31 @@ public class CatalogSupportBase {
assertEquals(expected, result.trim(), "Catalog support for DOM");
}
/*
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 @@ public class CatalogSupportBase {
*
* @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 @@ public class CatalogSupportBase {
/**
* 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 @@ public class CatalogSupportBase {
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 @@ public class CatalogSupportBase {
* @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();
}
}
/**