--- /dev/null 2014-09-08 10:45:56.830930409 -0700 +++ new/test/javax/xml/jaxp/functional/org/apache/qetest/trax/sax/SAXSourceAPITest.java 2014-12-31 11:41:13.712139126 -0800 @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + */ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.qetest.trax.sax; + +import java.io.FileInputStream; +import java.io.FilePermission; +import java.io.IOException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.stream.StreamResult; +import jaxp.library.JAXPBaseTest; +import static jaxp.library.JAXPTestUtilities.CLASS_DIR; +import static jaxp.library.JAXPTestUtilities.compareWithGold; +import static jaxp.library.JAXPTestUtilities.filenameToURL; +import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; +import static org.apache.qetest.trax.TraxConst.XML_DIR; +import static jaxp.library.JAXPTestUtilities.getNextFile; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; + +/** + * API Coverage test for the SAXSource class of TRAX. + */ +public class SAXSourceAPITest extends JAXPBaseTest { + + /** + * Nonsense systemId for various tests. + */ + private static final String NONSENSE_SYSTEMID = "file:///nonsense-system-id"; + + /** + * Basic API coverage, constructor and set/get methods. + * @throws javax.xml.parsers.ParserConfigurationException + * @throws SAXException for SAX error. + */ + public void testCase1() throws ParserConfigurationException, SAXException { + // Default no-arg ctor sets nothing (but needs special test for + // creating new doc when being transformed) + SAXSource defaultSAX = new SAXSource(); + assertNull(defaultSAX.getInputSource()); + assertNull(defaultSAX.getXMLReader()); + assertNull(defaultSAX.getSystemId()); + // ctor(InputSource) with an InputSource() + InputSource srcNoID = new InputSource(); + SAXSource saxSrcNoID = new SAXSource(srcNoID); + assertEquals(saxSrcNoID.getInputSource(), srcNoID, + "SAXSource(new InputSource()) has InputSource: " + + saxSrcNoID.getInputSource()); + assertNull(saxSrcNoID.getXMLReader()); + assertNull(saxSrcNoID.getSystemId()); + + // ctor(InputSource) with an InputSource("sysId") + InputSource srcWithID = new InputSource(NONSENSE_SYSTEMID); + SAXSource saxSrcWithID = new SAXSource(srcWithID); + assertEquals(saxSrcWithID.getInputSource(), srcWithID); + assertNull(saxSrcWithID.getXMLReader()); + assertEquals(saxSrcWithID.getSystemId(), NONSENSE_SYSTEMID); + + // ctor(XMLReader, InputSource) + XMLReader reader2 = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); + SAXSource saxSrcReaderID2 = new SAXSource(reader2, srcWithID); + assertEquals(saxSrcReaderID2.getInputSource(), srcWithID); + assertEquals(saxSrcReaderID2.getXMLReader(), reader2); + assertEquals(saxSrcReaderID2.getSystemId(), NONSENSE_SYSTEMID); + + // ctor(XMLReader, InputSource) + // Be sure to use the JAXP methods only! + SAXParserFactory factory = SAXParserFactory.newInstance(); + factory.setNamespaceAware(true); + SAXParser saxParser = factory.newSAXParser(); + XMLReader reader = saxParser.getXMLReader(); + SAXSource saxSrcReaderID = new SAXSource(reader, srcWithID); + assertEquals(saxSrcReaderID.getInputSource(), srcWithID); + assertEquals(saxSrcReaderID.getXMLReader(), reader); + assertEquals(saxSrcReaderID.getSystemId(), NONSENSE_SYSTEMID); + + // ctor(null InputSource) - note it won't actually + // be able to be used as a Source in real life + SAXSource saxNullSrc = new SAXSource(null); + assertNull(saxNullSrc.getInputSource()); + assertNull(saxNullSrc.getXMLReader()); + assertNull(saxNullSrc.getSystemId()); + + // ctor(null Reader, null InputSource) + SAXSource saxNullSrc2 = new SAXSource(null, null); + assertNull(saxNullSrc2.getInputSource()); + assertNull(saxNullSrc2.getXMLReader()); + assertNull(saxNullSrc2.getSystemId()); + + // Validate various simple set/get methods + SAXSource wackySAX = new SAXSource(); + + // Validate setting systemId auto-creates InputSource + // with that systemId + wackySAX.setSystemId(NONSENSE_SYSTEMID); + assertEquals(wackySAX.getSystemId(), NONSENSE_SYSTEMID); + assertNotNull(wackySAX.getInputSource()); + InputSource newIS = wackySAX.getInputSource(); + assertEquals(newIS.getSystemId(), NONSENSE_SYSTEMID); + + // API Coverage set/getSystemId + wackySAX.setSystemId("another-system-id"); + assertEquals(wackySAX.getSystemId(), "another-system-id"); + InputSource gotIS = wackySAX.getInputSource(); + assertEquals(gotIS.getSystemId(), "another-system-id"); + // setting to null explicitly + wackySAX.setSystemId(null); + assertNull(wackySAX.getSystemId()); + assertNull(wackySAX.getInputSource().getSystemId()); + + // API Coverage set/getInputSource + InputSource anotherIS = new InputSource(NONSENSE_SYSTEMID); + wackySAX.setInputSource(anotherIS); + assertEquals(wackySAX.getInputSource(), anotherIS); + assertEquals(wackySAX.getSystemId(), NONSENSE_SYSTEMID); + + // API Coverage set/getXMLReader + assertNull(wackySAX.getXMLReader(), null); + // Be sure to use the JAXP methods only! + saxParser = factory.newSAXParser(); + XMLReader wackyReader = saxParser.getXMLReader(); + wackySAX.setXMLReader(wackyReader); + assertEquals(wackySAX.getXMLReader(), wackyReader); + wackySAX.setXMLReader(null); + assertNull(wackySAX.getXMLReader()); + + } + + /** + * Basic functionality of SAXSources. Use them in simple transforms, + * with/without systemId set. + * + * @throws TransformerException If an unrecoverable error occurs during the + * course of the transformation. + * @throws IOException if any I/O operation error. + */ + public void testCase2() throws TransformerException, IOException { + setPermissions(new FilePermission(XML_DIR + "/-", "read"), + new FilePermission(CLASS_DIR + "-", "read, write")); + String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl"); + String xmlFile = XML_DIR + "SAXTest.xml"; + String xmlURI = filenameToURL(XML_DIR + "SAXTest.xml"); + String goldFile = GOLDEN_DIR + "SAXTest.out"; + SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); + try (FileInputStream xmlFis = new FileInputStream(xmlFile);) { + Templates streamTemplates = saxFactory.newTemplates(new SAXSource(new InputSource(xslURI))); + assertNotNull(streamTemplates); + + Transformer transformer1 = saxFactory.newTransformer(new SAXSource(new InputSource(xslURI))); + assertNotNull(transformer1); + // Validate process of a stylesheet using a simple SAXSource with a URL + SAXSource xslSAXSrc = new SAXSource(new InputSource(xslURI)); + Templates templates = saxFactory.newTemplates(xslSAXSrc); + assertNotNull(templates); + + xslSAXSrc = new SAXSource(new InputSource(xslURI)); + Transformer transformer2 = saxFactory.newTransformer(xslSAXSrc); + assertNotNull(transformer2); + + Transformer transformer3 = templates.newTransformer(); + assertNotNull(transformer3); + + SAXSource xmlSAXSrc = new SAXSource(new InputSource(xmlURI)); + String nextFile = getNextFile(this.getClass()); + transformer1.transform(xmlSAXSrc, new StreamResult(nextFile)); + assertTrue(compareWithGold(goldFile, nextFile)); + + nextFile = getNextFile(this.getClass()); + transformer2.transform(xmlSAXSrc, new StreamResult(nextFile)); + assertTrue(compareWithGold(goldFile, nextFile)); + + // Validate process of a stylesheet using a simple SAXSource with an + // InputStream. Note setting systemId is not necessary with this + // stylesheet. + Templates templatesStream = saxFactory.newTemplates(xslSAXSrc); + assertNotNull(templatesStream); + Transformer transformerStream = templatesStream.newTransformer(); + assertNotNull(transformerStream); + + SAXSource xmlSAXSrcStream = new SAXSource(new InputSource(xmlFis)); + nextFile = getNextFile(this.getClass()); + transformerStream.transform(xmlSAXSrcStream, new StreamResult(nextFile)); + assertTrue(compareWithGold(goldFile, nextFile)); + setPermissions(); + } + } +}