/* * 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.FileOutputStream; import java.io.FilePermission; import java.io.IOException; import java.util.Properties; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TemplatesHandler; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; 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 org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import org.testng.annotations.Test; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; /** * API Coverage test for the TemplatesHandler class of TRAX. */ public class TemplatesHandlerAPITest extends JAXPBaseTest { /** * Nonsense systemId for various tests. */ private static final String NONSENSE_SYSTEMID = "file:///nonsense/system/id/"; /** * Basic API coverage of set/get methods. Note that most of the * functionality of this class goes far beyond what we test in this * testCase. * * @throws TransformerConfigurationException Thrown in case of * ServiceConfigurationError service configuration error or if the * implementation is not available or cannot be instantiated. */ @Test public void testCase1() throws TransformerConfigurationException { SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); // Basic construction TemplatesHandler tHandler = saxFactory.newTemplatesHandler(); assertNotNull(tHandler); // getTemplates API coverage - simple Templates templates = tHandler.getTemplates(); assertNull(templates); // set/getSystemId API coverage tHandler.setSystemId(NONSENSE_SYSTEMID); assertEquals(tHandler.getSystemId(), NONSENSE_SYSTEMID); tHandler.setSystemId(null); assertNull(tHandler.getSystemId()); } /** * Template API test on transform process. * * @throws SAXException for SAX error. * @throws IOException if any I/O operation error. * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. */ @Test public void testCase2() throws SAXException, IOException, ParserConfigurationException, TransformerException { setPermissions(new FilePermission(XML_DIR + "/-", "read"), new FilePermission(GOLDEN_DIR + "/-", "read"), new FilePermission(CLASS_DIR + "-", "read, write")); String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl"); String xmlURI = filenameToURL(XML_DIR + "SAXTest.xml"); String goldFile = GOLDEN_DIR + "SAXTest.out"; String outputFile1 = CLASS_DIR + "TemplatesHandlerAPITest1.out"; String xslImpInclURI = filenameToURL(XML_DIR + "SAXImpIncl.xsl"); String xmlImpInclURI = filenameToURL(XML_DIR + "SAXImpIncl.xml"); String goldImpInclFile = GOLDEN_DIR + "SAXImpIncl.out"; String outputFile2 = CLASS_DIR + "TemplatesHandlerAPITest2.out"; SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); try (FileOutputStream fos = new FileOutputStream(outputFile1)) { // Validate a templatesHandler can create a valid stylesheet TemplatesHandler templatesHandler = saxFactory.newTemplatesHandler(); XMLReader reader = getJAXPXMLReader(); reader.setContentHandler(templatesHandler); // Parse the stylesheet, which means we should be able to getTemplates() reader.parse(xslURI); //Get the Templates object from the ContentHandler Templates templates = templatesHandler.getTemplates(); assertNotNull(templates); Properties xslOutProps = templates.getOutputProperties(); assertNotNull(xslOutProps); // validate transformer not null Transformer transformer = templates.newTransformer(); assertNotNull(transformer); // Validate that this transformer actually works Result result = new StreamResult(fos); Source xmlSource = new StreamSource(xmlURI); transformer.transform(xmlSource, result); compareWithGold(goldFile, outputFile1); } compareWithGold(goldFile, outputFile1); try (FileOutputStream fos = new FileOutputStream(outputFile2)) { // Validate a templatesHandler can create a stylesheet // with imports/includes, with the default systemId TemplatesHandler templatesHandler = saxFactory.newTemplatesHandler(); XMLReader reader = getJAXPXMLReader(); reader.setContentHandler(templatesHandler); // Parse the stylesheet, which means we should be able to getTemplates() reader.parse(xmlImpInclURI); //Get the Templates object from the ContentHandler Templates templates = templatesHandler.getTemplates(); assertNotNull(templates); Properties xslOutProps = templates.getOutputProperties(); assertNotNull(xslOutProps); Transformer transformer = templates.newTransformer(); assertNotNull(transformer); Result result = new StreamResult(fos); Source xmlSource = new StreamSource(xmlImpInclURI); transformer.transform(xmlSource, result); } compareWithGold(goldImpInclFile, outputFile2); setPermissions(); } /** * Worker method to get an XMLReader. Not the most efficient of methods, but * makes the code simpler. * * @return a new XMLReader for use, with setNamespaceAware(true) * @throws ParserConfigurationException if a parser cannot be created which * satisfies the requested configuration. * @throws SAXException for SAX errors. */ private XMLReader getJAXPXMLReader() throws SAXException, ParserConfigurationException { // Be sure to use the JAXP methods only! SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); return factory.newSAXParser().getXMLReader(); } }