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.FileInputStream;
  23 import java.io.FilePermission;
  24 import java.io.IOException;
  25 import javax.xml.parsers.ParserConfigurationException;
  26 import javax.xml.parsers.SAXParser;
  27 import javax.xml.parsers.SAXParserFactory;
  28 import javax.xml.transform.Templates;
  29 import javax.xml.transform.Transformer;
  30 import javax.xml.transform.TransformerException;
  31 import javax.xml.transform.TransformerFactory;
  32 import javax.xml.transform.sax.SAXSource;
  33 import javax.xml.transform.sax.SAXTransformerFactory;
  34 import javax.xml.transform.stream.StreamResult;
  35 import jaxp.library.JAXPBaseTest;
  36 import static jaxp.library.JAXPTestUtilities.CLASS_DIR;
  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.XML_DIR;
  41 import static jaxp.library.JAXPTestUtilities.getNextFile;
  42 import static org.testng.Assert.assertEquals;
  43 import static org.testng.Assert.assertNotNull;
  44 import static org.testng.Assert.assertNull;
  45 import static org.testng.Assert.assertTrue;
  46 import org.xml.sax.InputSource;
  47 import org.xml.sax.SAXException;
  48 import org.xml.sax.XMLReader;
  49 
  50 /**
  51  * API Coverage test for the SAXSource class of TRAX.
  52  */
  53 public class SAXSourceAPITest extends JAXPBaseTest {
  54 
  55     /**
  56      * Nonsense systemId for various tests.
  57      */
  58     private static final String NONSENSE_SYSTEMID = "file:///nonsense-system-id";
  59 
  60     /**
  61      * Basic API coverage, constructor and set/get methods.
  62      * @throws javax.xml.parsers.ParserConfigurationException
  63      * @throws SAXException for SAX error.
  64      */
  65     public void testCase1() throws ParserConfigurationException, SAXException {
  66         // Default no-arg ctor sets nothing (but needs special test for
  67         // creating new doc when being transformed)
  68         SAXSource defaultSAX = new SAXSource();
  69         assertNull(defaultSAX.getInputSource());
  70         assertNull(defaultSAX.getXMLReader());
  71         assertNull(defaultSAX.getSystemId());
  72         // ctor(InputSource) with an InputSource()
  73         InputSource srcNoID = new InputSource();
  74         SAXSource saxSrcNoID = new SAXSource(srcNoID);
  75         assertEquals(saxSrcNoID.getInputSource(), srcNoID,
  76                 "SAXSource(new InputSource()) has InputSource: "
  77                 + saxSrcNoID.getInputSource());
  78         assertNull(saxSrcNoID.getXMLReader());
  79         assertNull(saxSrcNoID.getSystemId());
  80 
  81         // ctor(InputSource) with an InputSource("sysId")
  82         InputSource srcWithID = new InputSource(NONSENSE_SYSTEMID);
  83         SAXSource saxSrcWithID = new SAXSource(srcWithID);
  84         assertEquals(saxSrcWithID.getInputSource(), srcWithID);
  85         assertNull(saxSrcWithID.getXMLReader());
  86         assertEquals(saxSrcWithID.getSystemId(), NONSENSE_SYSTEMID);
  87 
  88         // ctor(XMLReader, InputSource)
  89         XMLReader reader2 = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
  90         SAXSource saxSrcReaderID2 = new SAXSource(reader2, srcWithID);
  91         assertEquals(saxSrcReaderID2.getInputSource(), srcWithID);
  92         assertEquals(saxSrcReaderID2.getXMLReader(), reader2);
  93         assertEquals(saxSrcReaderID2.getSystemId(), NONSENSE_SYSTEMID);
  94 
  95         // ctor(XMLReader, InputSource)
  96         // Be sure to use the JAXP methods only!
  97         SAXParserFactory factory = SAXParserFactory.newInstance();
  98         factory.setNamespaceAware(true);
  99         SAXParser saxParser = factory.newSAXParser();
 100         XMLReader reader = saxParser.getXMLReader();
 101         SAXSource saxSrcReaderID = new SAXSource(reader, srcWithID);
 102         assertEquals(saxSrcReaderID.getInputSource(), srcWithID);
 103         assertEquals(saxSrcReaderID.getXMLReader(), reader);
 104         assertEquals(saxSrcReaderID.getSystemId(), NONSENSE_SYSTEMID);
 105 
 106         // ctor(null InputSource) - note it won't actually
 107         //  be able to be used as a Source in real life
 108         SAXSource saxNullSrc = new SAXSource(null);
 109         assertNull(saxNullSrc.getInputSource());
 110         assertNull(saxNullSrc.getXMLReader());
 111         assertNull(saxNullSrc.getSystemId());
 112 
 113         // ctor(null Reader, null InputSource)
 114         SAXSource saxNullSrc2 = new SAXSource(null, null);
 115         assertNull(saxNullSrc2.getInputSource());
 116         assertNull(saxNullSrc2.getXMLReader());
 117         assertNull(saxNullSrc2.getSystemId());
 118 
 119         // Validate various simple set/get methods
 120         SAXSource wackySAX = new SAXSource();
 121 
 122         // Validate setting systemId auto-creates InputSource
 123         //  with that systemId
 124         wackySAX.setSystemId(NONSENSE_SYSTEMID);
 125         assertEquals(wackySAX.getSystemId(), NONSENSE_SYSTEMID);
 126         assertNotNull(wackySAX.getInputSource());
 127         InputSource newIS = wackySAX.getInputSource();
 128         assertEquals(newIS.getSystemId(), NONSENSE_SYSTEMID);
 129 
 130         // API Coverage set/getSystemId
 131         wackySAX.setSystemId("another-system-id");
 132         assertEquals(wackySAX.getSystemId(), "another-system-id");
 133         InputSource gotIS = wackySAX.getInputSource();
 134         assertEquals(gotIS.getSystemId(), "another-system-id");
 135         // setting to null explicitly
 136         wackySAX.setSystemId(null);
 137         assertNull(wackySAX.getSystemId());
 138         assertNull(wackySAX.getInputSource().getSystemId());
 139 
 140         // API Coverage set/getInputSource
 141         InputSource anotherIS = new InputSource(NONSENSE_SYSTEMID);
 142         wackySAX.setInputSource(anotherIS);
 143         assertEquals(wackySAX.getInputSource(), anotherIS);
 144         assertEquals(wackySAX.getSystemId(), NONSENSE_SYSTEMID);
 145 
 146         // API Coverage set/getXMLReader
 147         assertNull(wackySAX.getXMLReader(), null);
 148         // Be sure to use the JAXP methods only!
 149         saxParser = factory.newSAXParser();
 150         XMLReader wackyReader = saxParser.getXMLReader();
 151         wackySAX.setXMLReader(wackyReader);
 152         assertEquals(wackySAX.getXMLReader(), wackyReader);
 153         wackySAX.setXMLReader(null);
 154         assertNull(wackySAX.getXMLReader());
 155 
 156     }
 157 
 158     /**
 159      * Basic functionality of SAXSources. Use them in simple transforms,
 160      * with/without systemId set.
 161      *
 162      * @throws TransformerException If an unrecoverable error occurs during the 
 163      *         course of the transformation.
 164      * @throws IOException if any I/O operation error.
 165      */
 166     public void testCase2() throws TransformerException, IOException {
 167         setPermissions(new FilePermission(XML_DIR + "/-", "read"),
 168                 new FilePermission(CLASS_DIR + "-", "read, write"));
 169         String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl");
 170         String xmlFile = XML_DIR + "SAXTest.xml";
 171         String xmlURI = filenameToURL(XML_DIR + "SAXTest.xml");
 172         String goldFile = GOLDEN_DIR + "SAXTest.out";
 173         SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
 174         try (FileInputStream xmlFis = new FileInputStream(xmlFile);) {
 175             Templates streamTemplates = saxFactory.newTemplates(new SAXSource(new InputSource(xslURI)));
 176             assertNotNull(streamTemplates);
 177 
 178             Transformer transformer1 = saxFactory.newTransformer(new SAXSource(new InputSource(xslURI)));
 179             assertNotNull(transformer1);
 180             // Validate process of a stylesheet using a simple SAXSource with a URL
 181             SAXSource xslSAXSrc = new SAXSource(new InputSource(xslURI));
 182             Templates templates = saxFactory.newTemplates(xslSAXSrc);
 183             assertNotNull(templates);
 184 
 185             xslSAXSrc = new SAXSource(new InputSource(xslURI));
 186             Transformer transformer2 = saxFactory.newTransformer(xslSAXSrc);
 187             assertNotNull(transformer2);
 188 
 189             Transformer transformer3 = templates.newTransformer();
 190             assertNotNull(transformer3);
 191 
 192             SAXSource xmlSAXSrc = new SAXSource(new InputSource(xmlURI));
 193             String nextFile = getNextFile(this.getClass());
 194             transformer1.transform(xmlSAXSrc, new StreamResult(nextFile));
 195             assertTrue(compareWithGold(goldFile, nextFile));
 196 
 197             nextFile = getNextFile(this.getClass());
 198             transformer2.transform(xmlSAXSrc, new StreamResult(nextFile));
 199             assertTrue(compareWithGold(goldFile, nextFile));
 200 
 201             // Validate process of a stylesheet using a simple SAXSource with an 
 202             // InputStream. Note setting systemId is not necessary with this 
 203             // stylesheet. 
 204             Templates templatesStream = saxFactory.newTemplates(xslSAXSrc);
 205             assertNotNull(templatesStream);
 206             Transformer transformerStream = templatesStream.newTransformer();
 207             assertNotNull(transformerStream);
 208 
 209             SAXSource xmlSAXSrcStream = new SAXSource(new InputSource(xmlFis));
 210             nextFile = getNextFile(this.getClass());
 211             transformerStream.transform(xmlSAXSrcStream, new StreamResult(nextFile));
 212             assertTrue(compareWithGold(goldFile, nextFile));
 213             setPermissions();
 214         }
 215     }
 216 }