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.sax;
  21 
  22 import java.io.FileOutputStream;
  23 import java.io.FilePermission;
  24 import java.io.IOException;
  25 import java.util.Properties;
  26 import javax.xml.parsers.ParserConfigurationException;
  27 import javax.xml.parsers.SAXParserFactory;
  28 import javax.xml.transform.Result;
  29 import javax.xml.transform.Source;
  30 import javax.xml.transform.Templates;
  31 import javax.xml.transform.Transformer;
  32 import javax.xml.transform.TransformerConfigurationException;
  33 import javax.xml.transform.TransformerException;
  34 import javax.xml.transform.TransformerFactory;
  35 import javax.xml.transform.sax.SAXTransformerFactory;
  36 import javax.xml.transform.sax.TemplatesHandler;
  37 import javax.xml.transform.stream.StreamResult;
  38 import javax.xml.transform.stream.StreamSource;
  39 import jaxp.library.JAXPBaseTest;
  40 import static jaxp.library.JAXPTestUtilities.CLASS_DIR;
  41 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  42 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  43 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR;
  44 import static org.apache.qetest.trax.TraxConst.XML_DIR;
  45 import static org.testng.Assert.assertEquals;
  46 import static org.testng.Assert.assertNotNull;
  47 import static org.testng.Assert.assertNull;
  48 import org.testng.annotations.Test;
  49 import org.xml.sax.SAXException;
  50 import org.xml.sax.XMLReader;
  51 
  52 /**
  53  * API Coverage test for the TemplatesHandler class of TRAX.
  54  */
  55 public class TemplatesHandlerAPITest extends JAXPBaseTest {
  56     /**
  57      * Nonsense systemId for various tests.
  58      */
  59     private static final String NONSENSE_SYSTEMID = "file:///nonsense/system/id/";
  60 
  61     /**
  62      * Basic API coverage of set/get methods. Note that most of the
  63      * functionality of this class goes far beyond what we test in this
  64      * testCase.
  65      * 
  66      * @throws TransformerConfigurationException Thrown in case of 
  67      *         ServiceConfigurationError service configuration error or if the
  68      *         implementation is not available or cannot be instantiated.
  69      */
  70     @Test
  71     public void testCase1() throws TransformerConfigurationException {
  72         SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
  73         // Basic construction
  74         TemplatesHandler tHandler = saxFactory.newTemplatesHandler();
  75         assertNotNull(tHandler);
  76 
  77         // getTemplates API coverage - simple
  78         Templates templates = tHandler.getTemplates();
  79         assertNull(templates);
  80 
  81         // set/getSystemId API coverage
  82         tHandler.setSystemId(NONSENSE_SYSTEMID);
  83         assertEquals(tHandler.getSystemId(), NONSENSE_SYSTEMID);
  84         tHandler.setSystemId(null);
  85         assertNull(tHandler.getSystemId());
  86 
  87     }
  88 
  89     /**
  90      * Template API test on transform process.
  91      * 
  92      * @throws SAXException for SAX error.
  93      * @throws IOException if any I/O operation error.
  94      * @throws ParserConfigurationException if  the implementation is not 
  95      *         available or cannot be instantiated.
  96      * @throws TransformerException If an unrecoverable error occurs during the 
  97      *         course of the transformation.
  98      */
  99     @Test
 100     public void testCase2() throws SAXException, IOException,
 101             ParserConfigurationException, TransformerException {
 102         setPermissions(new FilePermission(XML_DIR + "/-", "read"),
 103                 new FilePermission(GOLDEN_DIR + "/-", "read"),
 104                 new FilePermission(CLASS_DIR + "-", "read, write"));
 105         String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl");
 106         String xmlURI = filenameToURL(XML_DIR + "SAXTest.xml");
 107         String goldFile = GOLDEN_DIR + "SAXTest.out";
 108         String outputFile1 = CLASS_DIR + "TemplatesHandlerAPITest1.out";
 109 
 110         String xslImpInclURI = filenameToURL(XML_DIR + "SAXImpIncl.xsl");
 111         String xmlImpInclURI = filenameToURL(XML_DIR + "SAXImpIncl.xml");
 112         String goldImpInclFile = GOLDEN_DIR + "SAXImpIncl.out";
 113         String outputFile2 = CLASS_DIR + "TemplatesHandlerAPITest2.out";
 114 
 115         SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
 116         try (FileOutputStream fos = new FileOutputStream(outputFile1)) {
 117             // Validate a templatesHandler can create a valid stylesheet
 118             TemplatesHandler templatesHandler = saxFactory.newTemplatesHandler();
 119 
 120             XMLReader reader = getJAXPXMLReader();
 121             reader.setContentHandler(templatesHandler);
 122             // Parse the stylesheet, which means we should be able to getTemplates()
 123             reader.parse(xslURI);
 124 
 125             //Get the Templates object from the ContentHandler
 126             Templates templates = templatesHandler.getTemplates();
 127             assertNotNull(templates);
 128             Properties xslOutProps = templates.getOutputProperties();
 129             assertNotNull(xslOutProps);
 130             // validate transformer not null
 131             Transformer transformer = templates.newTransformer();
 132             assertNotNull(transformer);
 133 
 134             // Validate that this transformer actually works
 135             Result result = new StreamResult(fos);
 136             Source xmlSource = new StreamSource(xmlURI);
 137             transformer.transform(xmlSource, result);
 138             compareWithGold(goldFile, outputFile1);
 139         }
 140         compareWithGold(goldFile, outputFile1);
 141 
 142         try (FileOutputStream fos = new FileOutputStream(outputFile2)) {
 143             // Validate a templatesHandler can create a stylesheet
 144             //  with imports/includes, with the default systemId
 145             TemplatesHandler templatesHandler = saxFactory.newTemplatesHandler();
 146 
 147             XMLReader reader = getJAXPXMLReader();
 148             reader.setContentHandler(templatesHandler);
 149 
 150             // Parse the stylesheet, which means we should be able to getTemplates()
 151             reader.parse(xmlImpInclURI);
 152 
 153             //Get the Templates object from the ContentHandler
 154             Templates templates = templatesHandler.getTemplates();
 155             assertNotNull(templates);
 156             Properties xslOutProps = templates.getOutputProperties();
 157             assertNotNull(xslOutProps);
 158             Transformer transformer = templates.newTransformer();
 159             assertNotNull(transformer);
 160 
 161             Result result = new StreamResult(fos);
 162             Source xmlSource = new StreamSource(xmlImpInclURI);
 163             transformer.transform(xmlSource, result);
 164 
 165         }
 166         compareWithGold(goldImpInclFile, outputFile2);
 167         setPermissions();
 168     }
 169 
 170     /**
 171      * Worker method to get an XMLReader. Not the most efficient of methods, but
 172      * makes the code simpler.
 173      *
 174      * @return a new XMLReader for use, with setNamespaceAware(true)
 175      * @throws ParserConfigurationException if a parser cannot be created which
 176      * satisfies the requested configuration.
 177      * @throws SAXException for SAX errors.
 178      */
 179     private XMLReader getJAXPXMLReader() throws SAXException,
 180             ParserConfigurationException {
 181         // Be sure to use the JAXP methods only!
 182         SAXParserFactory factory = SAXParserFactory.newInstance();
 183         factory.setNamespaceAware(true);
 184         return factory.newSAXParser().getXMLReader();
 185     }
 186 }