/* * 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; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import jaxp.library.JAXPFileBaseTest; 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.SRC_DIR; import static org.apache.qetest.trax.TraxConst.XML_DIR; import static jaxp.library.JAXPTestUtilities.getNextFile; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import org.w3c.dom.Node; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * Test behavior of imports/includes with various setSystemId sources. * Note: This test is directory-dependent, so if there are any fails, check the * code to see what the test file is expecting the path/directory/etc to be. */ public class SystemIdImpInclTest extends JAXPFileBaseTest { /** * Gold filename for level0, i.e. one directory above the test file. */ private static final String GOLDEN_File_LEVEL0 = "SystemIdImpInclLevel0.out"; /** * Gold filename for level1, i.e. the directory of the test file. */ private static final String GOLDEN_File_LEVEL1 = "SystemIdImpInclLevel1.out"; /** * Gold filename for level2, i.e. a directory below the test file. */ private static final String GOLDEN_File_LEVEL2 = "SystemIdImpInclLevel2.out"; /** * Test style-sheet file name. */ private static final String XSL_FILE = XML_DIR + "SystemIdImpIncl.xsl"; /** * Test style-sheet file name. */ private static final String LEVEL0_INCLUDE_XSL = SRC_DIR + "SystemIdImpIncl.xsl"; /** * Test style-sheet file name. */ private static final String LEVEL1_INCLUDE_XSL = XSL_FILE; /** * Test style-sheet file name. */ private static final String LEVEL2_INCLUDE_XSL = XML_DIR + "systemid/SystemIdImpIncl.xsl"; /** * Test XML file name. */ private static final String XML_FILE = XML_DIR + "SystemIdImpIncl.xml"; /** * Simple StreamSources with different setSystemIds. * @throws IOException if an I/O error occurs reading from the file or a * malformed or unmappable byte sequence is read. * @throws TransformerException If an unrecoverable error occurs * during the course of the transformation. * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws SAXException for SAX error. */ @Test public void testCase1() throws IOException, TransformerException, ParserConfigurationException, SAXException { String goldFile = GOLDEN_DIR + GOLDEN_File_LEVEL1; String outputFile = getNextFile(this.getClass()); try(InputStream xmlStream = new FileInputStream(XML_FILE); FileOutputStream fos = new FileOutputStream(outputFile);) { TransformerFactory factory = TransformerFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Node xslNode = docBuilder.parse(new InputSource(filenameToURL(XSL_FILE))); Source xslSource = new DOMSource(xslNode); xslSource.setSystemId(filenameToURL(LEVEL1_INCLUDE_XSL)); Source xmlSource = new StreamSource(xmlStream); xmlSource.setSystemId(filenameToURL(XML_FILE)); factory.newTemplates(xslSource).newTransformer(). transform(xmlSource, new StreamResult(fos)); } assertTrue(compareWithGold(goldFile, outputFile)); } /** * Verify simple SAXSources with systemIds. * @throws IOException if an I/O error occurs reading from the file or a * malformed or unmappable byte sequence is read. * @throws TransformerException If an unrecoverable error occurs * during the course of the transformation. * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws SAXException for SAX error. */ @Test public void testCase2() throws IOException, TransformerException, ParserConfigurationException, SAXException { String goldFile = GOLDEN_DIR + GOLDEN_File_LEVEL0; String outputFile = getNextFile(this.getClass()); try(InputStream xslStream = new FileInputStream(XSL_FILE); InputStream xmlStream = new FileInputStream(XML_FILE); FileOutputStream fos = new FileOutputStream(outputFile);) { TransformerFactory factory = TransformerFactory.newInstance(); Source xslSource = new StreamSource(new InputStreamReader(xslStream)); xslSource.setSystemId(filenameToURL(LEVEL0_INCLUDE_XSL)); Source xmlSource = new StreamSource(xmlStream); xmlSource.setSystemId(filenameToURL(XML_FILE)); factory.newTemplates(xslSource).newTransformer(). transform(xmlSource, new StreamResult(fos)); } assertTrue(compareWithGold(goldFile, outputFile)); } /** * Verify simple SAXSources with systemIds. * @throws IOException if an I/O error occurs reading from the file or a * malformed or unmappable byte sequence is read. * @throws TransformerException If an unrecoverable error occurs * during the course of the transformation. */ @Test public void testCase3() throws IOException, TransformerException { String goldFile = GOLDEN_DIR + GOLDEN_File_LEVEL2; String outputFile = getNextFile(this.getClass()); try(InputStream xslStream = new FileInputStream(XSL_FILE); InputStream xmlStream = new FileInputStream(XML_FILE); FileOutputStream fos = new FileOutputStream(outputFile);) { TransformerFactory factory = TransformerFactory.newInstance(); Source xslSource = new StreamSource(new InputStreamReader(xslStream, "UTF-8")); xslSource.setSystemId(filenameToURL(LEVEL2_INCLUDE_XSL)); Source xmlSource = new StreamSource(xmlStream); xmlSource.setSystemId(filenameToURL(XML_FILE)); factory.newTemplates(xslSource).newTransformer(). transform(xmlSource, new StreamResult(fos)); } assertTrue(compareWithGold(goldFile, outputFile)); } }