1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.qetest.trax; 21 22 import java.io.FileInputStream; 23 import java.io.FileOutputStream; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.InputStreamReader; 27 import javax.xml.parsers.DocumentBuilder; 28 import javax.xml.parsers.DocumentBuilderFactory; 29 import javax.xml.parsers.ParserConfigurationException; 30 import javax.xml.transform.Source; 31 import javax.xml.transform.TransformerException; 32 import javax.xml.transform.TransformerFactory; 33 import javax.xml.transform.dom.DOMSource; 34 import javax.xml.transform.stream.StreamResult; 35 import javax.xml.transform.stream.StreamSource; 36 import jaxp.library.JAXPFileBaseTest; 37 import static jaxp.library.JAXPTestUtilities.compareWithGold; 38 import static jaxp.library.JAXPTestUtilities.filenameToURL; 39 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; 40 import static org.apache.qetest.trax.TraxConst.SRC_DIR; 41 import static org.apache.qetest.trax.TraxConst.XML_DIR; 42 import static jaxp.library.JAXPTestUtilities.getNextFile; 43 import static org.testng.Assert.assertTrue; 44 import org.testng.annotations.Test; 45 import org.w3c.dom.Node; 46 import org.xml.sax.InputSource; 47 import org.xml.sax.SAXException; 48 49 /** 50 * Test behavior of imports/includes with various setSystemId sources. 51 * Note: This test is directory-dependent, so if there are any fails, check the 52 * code to see what the test file is expecting the path/directory/etc to be. 53 */ 54 public class SystemIdImpInclTest extends JAXPFileBaseTest { 55 /** 56 * Gold filename for level0, i.e. one directory above the test file. 57 */ 58 private static final String GOLDEN_File_LEVEL0 = "SystemIdImpInclLevel0.out"; 59 60 /** 61 * Gold filename for level1, i.e. the directory of the test file. 62 */ 63 private static final String GOLDEN_File_LEVEL1 = "SystemIdImpInclLevel1.out"; 64 65 /** 66 * Gold filename for level2, i.e. a directory below the test file. 67 */ 68 private static final String GOLDEN_File_LEVEL2 = "SystemIdImpInclLevel2.out"; 69 70 /** 71 * Test style-sheet file name. 72 */ 73 private static final String XSL_FILE = XML_DIR + "SystemIdImpIncl.xsl"; 74 75 /** 76 * Test style-sheet file name. 77 */ 78 private static final String LEVEL0_INCLUDE_XSL = SRC_DIR + "SystemIdImpIncl.xsl"; 79 80 /** 81 * Test style-sheet file name. 82 */ 83 private static final String LEVEL1_INCLUDE_XSL = XSL_FILE; 84 85 /** 86 * Test style-sheet file name. 87 */ 88 private static final String LEVEL2_INCLUDE_XSL = XML_DIR + "systemid/SystemIdImpIncl.xsl"; 89 90 /** 91 * Test XML file name. 92 */ 93 private static final String XML_FILE = XML_DIR + "SystemIdImpIncl.xml"; 94 95 /** 96 * Simple StreamSources with different setSystemIds. 97 * @throws IOException if an I/O error occurs reading from the file or a 98 * malformed or unmappable byte sequence is read. 99 * @throws TransformerException If an unrecoverable error occurs 100 * during the course of the transformation. 101 * @throws ParserConfigurationException if the implementation is not 102 * available or cannot be instantiated. 103 * @throws SAXException for SAX error. 104 */ 105 @Test 106 public void testCase1() throws IOException, TransformerException, 107 ParserConfigurationException, SAXException { 108 String goldFile = GOLDEN_DIR + GOLDEN_File_LEVEL1; 109 String outputFile = getNextFile(this.getClass()); 110 111 try(InputStream xmlStream = new FileInputStream(XML_FILE); 112 FileOutputStream fos = new FileOutputStream(outputFile);) { 113 TransformerFactory factory = TransformerFactory.newInstance(); 114 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 115 dbf.setNamespaceAware(true); 116 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 117 Node xslNode = docBuilder.parse(new InputSource(filenameToURL(XSL_FILE))); 118 Source xslSource = new DOMSource(xslNode); 119 xslSource.setSystemId(filenameToURL(LEVEL1_INCLUDE_XSL)); 120 121 Source xmlSource = new StreamSource(xmlStream); 122 xmlSource.setSystemId(filenameToURL(XML_FILE)); 123 factory.newTemplates(xslSource).newTransformer(). 124 transform(xmlSource, new StreamResult(fos)); 125 } 126 assertTrue(compareWithGold(goldFile, outputFile)); 127 } 128 129 /** 130 * Verify simple SAXSources with systemIds. 131 * @throws IOException if an I/O error occurs reading from the file or a 132 * malformed or unmappable byte sequence is read. 133 * @throws TransformerException If an unrecoverable error occurs 134 * during the course of the transformation. 135 * @throws ParserConfigurationException if the implementation is not 136 * available or cannot be instantiated. 137 * @throws SAXException for SAX error. 138 */ 139 @Test 140 public void testCase2() throws IOException, TransformerException, 141 ParserConfigurationException, SAXException { 142 String goldFile = GOLDEN_DIR + GOLDEN_File_LEVEL0; 143 String outputFile = getNextFile(this.getClass()); 144 145 try(InputStream xslStream = new FileInputStream(XSL_FILE); 146 InputStream xmlStream = new FileInputStream(XML_FILE); 147 FileOutputStream fos = new FileOutputStream(outputFile);) { 148 TransformerFactory factory = TransformerFactory.newInstance(); 149 Source xslSource = new StreamSource(new InputStreamReader(xslStream)); 150 xslSource.setSystemId(filenameToURL(LEVEL0_INCLUDE_XSL)); 151 152 Source xmlSource = new StreamSource(xmlStream); 153 xmlSource.setSystemId(filenameToURL(XML_FILE)); 154 factory.newTemplates(xslSource).newTransformer(). 155 transform(xmlSource, new StreamResult(fos)); 156 } 157 assertTrue(compareWithGold(goldFile, outputFile)); 158 } 159 160 /** 161 * Verify simple SAXSources with systemIds. 162 * @throws IOException if an I/O error occurs reading from the file or a 163 * malformed or unmappable byte sequence is read. 164 * @throws TransformerException If an unrecoverable error occurs 165 * during the course of the transformation. 166 */ 167 @Test 168 public void testCase3() throws IOException, TransformerException { 169 String goldFile = GOLDEN_DIR + GOLDEN_File_LEVEL2; 170 String outputFile = getNextFile(this.getClass()); 171 172 try(InputStream xslStream = new FileInputStream(XSL_FILE); 173 InputStream xmlStream = new FileInputStream(XML_FILE); 174 FileOutputStream fos = new FileOutputStream(outputFile);) { 175 TransformerFactory factory = TransformerFactory.newInstance(); 176 Source xslSource = new StreamSource(new InputStreamReader(xslStream, "UTF-8")); 177 xslSource.setSystemId(filenameToURL(LEVEL2_INCLUDE_XSL)); 178 179 Source xmlSource = new StreamSource(xmlStream); 180 xmlSource.setSystemId(filenameToURL(XML_FILE)); 181 factory.newTemplates(xslSource).newTransformer(). 182 transform(xmlSource, new StreamResult(fos)); 183 } 184 assertTrue(compareWithGold(goldFile, outputFile)); 185 } 186 }