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