提交 7b1b777e 编写于 作者: A aefimov

8144593: Suppress not recognized property/feature warning messages from SAXParser

Reviewed-by: joehw
上级 831ff018
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl;
import javax.xml.XMLConstants;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
/*
* Test implementation of SAXParser. It is extended from JDK parser and two methods
* are overriden to disable support of specific features and properties.
* This class is used in ValidationWarningsTest and TransformationWarningsTest
* to generate multiple warnings during xml validation and transformation processes.
*/
public class TestSAXDriver extends SAXParserImpl.JAXPSAXParser {
@Override
public synchronized void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name)) {
throw new SAXNotRecognizedException(name+" feature is not recognised by test SAX parser intentionally.");
} else {
super.setFeature(name, value);
}
}
@Override
public synchronized void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
if (XMLConstants.ACCESS_EXTERNAL_DTD.equals(name) || ENT_EXP_LIMIT_PROP.equals(name)) {
throw new SAXNotRecognizedException(name+" property is not recognised by test SAX parser intentionally.");
} else {
super.setProperty(name, value);
}
}
private static final String ENT_EXP_LIMIT_PROP = "http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit";
}
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
/*
* @test
* @bug 8144593
* @summary Check that warnings about unsupported properties from parsers
* are suppressed during the transformation process.
* @compile -XDignore.symbol.file TestSAXDriver.java
* @run testng/othervm TransformationWarningsTest
*/
public class TransformationWarningsTest extends WarningsTestBase {
@BeforeClass
public void setup() {
//Set test SAX driver implementation.
System.setProperty("org.xml.sax.driver", "TestSAXDriver");
}
@Test
public void testTransformation() throws Exception {
startTest();
}
//One iteration of xml transformation test case. It will be called from each
//TestWorker task defined in WarningsTestBase class.
void doOneTestIteration() throws Exception {
// Prepare output stream
StringWriter xmlResultString = new StringWriter();
StreamResult xmlResultStream = new StreamResult(xmlResultString);
// Prepare xml source stream
Source src = new StreamSource(new StringReader(xml));
Transformer t = createTransformer();
//Transform the xml
t.transform(src, xmlResultStream);
}
//Create transformer from xsl test string
Transformer createTransformer() throws Exception {
// Prepare sources for transormation
Source xslsrc = new StreamSource(new StringReader(xsl));
// Create factory and transformer
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer(xslsrc);
// Set URI Resolver to return the newly constructed xml
// stream source object from xml test string
t.setURIResolver((String href, String base) -> new StreamSource(new StringReader(xml)));
return t;
}
//Xsl and Xml contents used in the transformation test
private static final String xsl = "<xsl:stylesheet version='2.0'"
+ " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"
+ " <xsl:output method='xml' indent='yes' omit-xml-declaration='yes'/>"
+ " <xsl:template match='/'>"
+ " <test>Simple Transformation Result. No warnings should be printed to console</test>"
+ " </xsl:template>"
+ "</xsl:stylesheet>";
private static final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root></root>";
}
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.xml.sax.InputSource;
/*
* @test
* @bug 8144593
* @summary Check that warnings about unsupported properties from SAX
* parsers are suppressed during the xml validation process.
* @compile -XDignore.symbol.file TestSAXDriver.java
* @run testng/othervm ValidationWarningsTest
*/
public class ValidationWarningsTest extends WarningsTestBase {
@BeforeClass
public void setup() {
//Set test SAX driver implementation.
System.setProperty("org.xml.sax.driver", "TestSAXDriver");
}
@Test
public void testValidation() throws Exception {
startTest();
}
//One iteration of xml validation test case. It will be called from each
//TestWorker task defined in WarningsTestBase class.
void doOneTestIteration() throws Exception {
Source src = new StreamSource(new StringReader(xml));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
SAXSource xsdSource = new SAXSource(new InputSource(new ByteArrayInputStream(xsd.getBytes())));
Schema schema = schemaFactory.newSchema(xsdSource);
Validator v = schema.newValidator();
v.validate(src);
}
//Xsd and Xml contents used in the validation test
private static final String xsd = "<?xml version='1.0'?>"
+ " <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
+ " <xs:element name='test' type='xs:string'/>\n"
+ " </xs:schema>";
private static final String xml = "<?xml version='1.0'?><test>Element</test>";
}
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.XMLConstants;
import org.testng.Assert;
/*
* This class helps to test suppression of unsupported parser properties
* messages printed to standard error output.
* It launches THREADS_COUNT tasks. Each task does ITERATIONS_PER_THREAD
* sequential calls to doOneIteration method implemented by specific test class.
*/
public abstract class WarningsTestBase {
/*
* Abstract method that should be implemented by test class.
* It is repeatedly called by each TestWorker task.
*/
abstract void doOneTestIteration() throws Exception;
/*
* Launches parallel test tasks and check the output for the number of
* generated warning messages. There should be no more than one message of
* each type.
*/
void startTest() throws Exception {
//Save standard error stream
PrintStream defStdErr = System.err;
//Set new byte array stream as standard error stream
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
System.setErr(new PrintStream(byteStream));
//Execute multiple TestWorker tasks
for (int id = 0; id < THREADS_COUNT; id++) {
EXECUTOR.execute(new TestWorker(id));
}
//Initiate shutdown of previously submitted task
EXECUTOR.shutdown();
//Wait for termination of submitted tasks
if (!EXECUTOR.awaitTermination(THREADS_COUNT, TimeUnit.SECONDS)) {
//If not all tasks terminates during the time out force them to shutdown
EXECUTOR.shutdownNow();
}
//Restore default standard error stream
System.setErr(defStdErr);
//Print tasks stderr output
String errContent = byteStream.toString();
System.out.println("Standard error output content:");
System.out.println(errContent);
//Check tasks stderr output for quatity of warning messages
Assert.assertTrue(warningPrintedOnce(XMLConstants.ACCESS_EXTERNAL_DTD, errContent));
Assert.assertTrue(warningPrintedOnce(ENT_EXP_PROPERTY, errContent));
Assert.assertTrue(warningPrintedOnce(XMLConstants.FEATURE_SECURE_PROCESSING, errContent));
}
// Count occurences of warning messages in standard error and check if warning is printed
// not more than once
private boolean warningPrintedOnce(String propertyName, String testOutput) {
//Count for property name in test output
Pattern p = Pattern.compile(propertyName);
Matcher m = p.matcher(testOutput);
int count = 0;
while (m.find()) {
count += 1;
}
System.out.println("'" + propertyName + "' print count: " + count);
//If count is more than 1 then consider test failed
return count <= 1;
}
//TestWorker task that sequentially calls test method
private class TestWorker implements Runnable {
// Task id
private final int id;
TestWorker(int id) {
this.id = id;
}
@Override
public void run() {
try {
System.out.printf("%d: waiting for barrier%n", id);
//Synchronize startup of all tasks
BARRIER.await();
System.out.printf("%d: starting iterations%n", id);
//Call test method multiple times
for (int i = 0; i < ITERATIONS_PER_THREAD; i++) {
doOneTestIteration();
}
} catch (Exception ex) {
throw new RuntimeException("TestWorker id:" + id + " failed", ex);
}
}
}
//Entity expansion limit property name
private static final String ENT_EXP_PROPERTY = "http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit";
//Number of simultaneous test threads
private static final int THREADS_COUNT = 10;
//Number of iterations per one thread
private static final int ITERATIONS_PER_THREAD = 4;
//Test thread pool
private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
//Cyclic barrier for threads startup synchronisation
private static final CyclicBarrier BARRIER = new CyclicBarrier(THREADS_COUNT);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册