/* * Copyright (c) 2015, 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.sax; import java.io.FileOutputStream; import java.io.FilePermission; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Result; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import jaxp.library.JAXPBaseTest; import static jaxp.library.JAXPTestUtilities.USER_DIR; 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.XML_DIR; import org.apache.qetest.xsl.CheckingContentHandler; import org.apache.qetest.xsl.CheckingLexicalHandler; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertSame; import static org.testng.Assert.fail; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; import org.w3c.dom.Node; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.ext.LexicalHandler; /** * API Coverage test for the SAXResult class of TRAX. */ public class SAXResultAPITest extends JAXPBaseTest { /** * Nonsense systemId for various tests. */ private static final String NONSENSE_SYSTEMID = "file:///nonsense-system-id"; /** * Basic API coverage, constructor and set/get methods. * * @throws TransformerConfigurationException Thrown in case of * ServiceConfigurationError service configuration error or if the * implementation is not available or cannot be instantiated. */ @Test public void testCase1() throws TransformerConfigurationException { // Default no-arg ctor sets nothing SAXResult defaultSAX = new SAXResult(); assertNull(defaultSAX.getHandler()); assertNull(defaultSAX.getLexicalHandler()); assertNull(defaultSAX.getSystemId()); SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); // ctor(Handler) with identity transformer, which is both // a ContentHandler and LexicalHandler ContentHandler handler = (ContentHandler) saxFactory.newTransformerHandler(); SAXResult handlerSAX = new SAXResult(handler); assertSame(handlerSAX.getHandler(), handler); assertNull(handlerSAX.getLexicalHandler()); assertNull(handlerSAX.getSystemId()); // ctor(Handler) with LoggingContentHandler, which not // a LexicalHandler, so it can't be cast ContentHandler nonLexHandler = new CheckingContentHandler(); SAXResult otherHandlerSAX = new SAXResult(nonLexHandler); assertSame(otherHandlerSAX.getHandler(), nonLexHandler); assertNull(otherHandlerSAX.getLexicalHandler()); assertNull(otherHandlerSAX.getSystemId()); // Note the Javadoc in SAXResult which talks about // automatically casting the ContentHandler into // a LexicalHandler: this cannot be tested alone // here, since it's the Transformer that does that // internally if necessary, and it may not get set // back into the SAXResult object itself // set/getHandler API coverage SAXResult wackySAX = new SAXResult(); wackySAX.setHandler(handler); // isa LexicalHandler also assertSame(wackySAX.getHandler(), handler); assertNull(wackySAX.getLexicalHandler()); // set/getLexicalHandler API coverage LexicalHandler lexHandler = new CheckingLexicalHandler(); wackySAX.setLexicalHandler(lexHandler); // SCUU4SPPMV - does not work assertSame(wackySAX.getLexicalHandler(), lexHandler); assertSame(wackySAX.getHandler(), handler); // set/getHandler API coverage, setting to null, which // should work here but can't be used legally wackySAX.setHandler(null); assertNull(wackySAX.getHandler()); assertSame(wackySAX.getLexicalHandler(), lexHandler); wackySAX.setLexicalHandler(null); assertNull(wackySAX.getLexicalHandler()); // set/getSystemId API coverage wackySAX.setSystemId(NONSENSE_SYSTEMID); assertEquals(wackySAX.getSystemId(), NONSENSE_SYSTEMID); wackySAX.setSystemId(null); assertNull(wackySAX.getSystemId()); } /** * Save system property for restoring. */ @BeforeGroups (groups = {"accessLocalFiles"}) public void setFilePermissions() { setPermissions(new FilePermission(XML_DIR + "/-", "read"), new FilePermission(GOLDEN_DIR + "/-", "read"), new FilePermission(USER_DIR + "-", "read, write")); } /** * Restore the system property. */ @AfterGroups (groups = {"readLocalFiles"}) public void restoreFilePermissions() { setPermissions(); } /** * Basic functionality of SAXResults. * @throws Exception If any errors occur. */ @Test(groups = {"accessLocalFiles"}) public void testCase2() throws Exception { String xslFile = XML_DIR + "SAXTest.xsl"; String xmlFile = XML_DIR + "SAXTest.xml"; String goldFile = GOLDEN_DIR + "SAXTest.out"; String outputFile = USER_DIR + "SAXTest_02.out"; try (FileOutputStream fos = new FileOutputStream(outputFile)) { SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); Templates streamTemplates = saxFactory.newTemplates(new StreamSource(filenameToURL(xslFile))); // Use a TransformerHandler for serialization: this // supports ContentHandler and can replace the // Xalan/Xerces specific Serializers we used to use TransformerHandler tHandler = saxFactory.newTransformerHandler(); tHandler.setResult(new StreamResult(fos)); SAXResult saxResult = new SAXResult(tHandler); // Just do a normal transform to this result Transformer transformer = streamTemplates.newTransformer(); transformer.transform(new StreamSource(filenameToURL(xmlFile)), saxResult); } compareWithGold(goldFile, outputFile); } /** * Detailed functionality of SAXResults: setLexicalHandler. * * @throws Exception If any errors occur. */ @Test(groups = {"accessLocalFiles"}) public void testCase3() throws Exception { String xslFile = XML_DIR + "SAXdtd.xsl"; String xmlFile = XML_DIR + "SAXdtd.xml"; String goldFile = GOLDEN_DIR + "SAXdtd.out"; String outputFile = USER_DIR + "SAXdtd.out"; try (FileOutputStream fos = new FileOutputStream(outputFile)) { SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); Templates streamTemplates = saxFactory.newTemplates(new StreamSource(filenameToURL(xslFile))); TransformerHandler tHandler = saxFactory.newTransformerHandler(); tHandler.setResult(new StreamResult(fos)); SAXResult saxResult = new SAXResult(); // Add a contentHandler that logs out info about the transform, and // that passes-through calls back to the original tHandler. CheckingContentHandler lch = new CheckingContentHandler(); lch.setDefaultHandler(tHandler); saxResult.setHandler(lch); // Add a lexicalHandler that logs out info about the transform, and // that passes-through calls back to the original tHandler CheckingLexicalHandler llh = new CheckingLexicalHandler(tHandler); saxResult.setLexicalHandler(llh); // Just do a normal transform to this result Transformer transformer = streamTemplates.newTransformer(); transformer.transform(new StreamSource(filenameToURL(xmlFile)), saxResult); fail("Excepted a TransformerException"); } catch (TransformerException expected) {} compareWithGold(goldFile, outputFile); try (FileOutputStream fos = new FileOutputStream(outputFile)) { SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); TransformerHandler tHandler = saxFactory.newTransformerHandler(); Result realResult = new StreamResult(fos); tHandler.setResult(realResult); SAXResult saxResult = new SAXResult(); CheckingContentHandler lch = new CheckingContentHandler(); lch.setDefaultHandler(tHandler); saxResult.setHandler(lch); CheckingLexicalHandler llh = new CheckingLexicalHandler(tHandler); saxResult.setLexicalHandler(llh); // Do an identityTransform to this result Transformer identityTransformer = TransformerFactory.newInstance().newTransformer(); identityTransformer.transform(new StreamSource(filenameToURL(xmlFile)), saxResult); fail("Excepted a TransformerException"); } catch (TransformerException expected) {} compareWithGold(goldFile, outputFile); try (FileOutputStream fos = new FileOutputStream(outputFile)) { // Validate a DOMSource to a logging SAXResult, as above DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); Node xmlNode = docBuilder.parse(new InputSource(filenameToURL(xmlFile))); SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); // Have an actual handler that does the physical output TransformerHandler tHandler = saxFactory.newTransformerHandler(); Templates streamTemplates = saxFactory.newTemplates(new StreamSource(filenameToURL(xslFile))); tHandler.setResult(new StreamResult(fos)); SAXResult saxResult = new SAXResult(); // Add a contentHandler that logs out info about the // transform, and that passes-through calls back // to the original tHandler CheckingContentHandler lch = new CheckingContentHandler(); lch.setDefaultHandler(tHandler); saxResult.setHandler(lch); // Add a lexicalHandler that logs out info about the // transform, and that passes-through calls back // to the original tHandler CheckingLexicalHandler llh = new CheckingLexicalHandler(tHandler); saxResult.setLexicalHandler(llh); // Just do a normal transform to this result streamTemplates.newTransformer().transform(new DOMSource(xmlNode), saxResult); fail("Excepted a TransformerException"); } catch (TransformerException expected) {} compareWithGold(goldFile, outputFile); } /** * Negative test: SAXResult without a handler throws TransformerException. * * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. */ @Test(expectedExceptions = TransformerException.class) public void negativeTransform() throws TransformerException { setPermissions(new FilePermission(XML_DIR + "/-", "read")); String xslFile = XML_DIR + "SAXTest.xsl"; String xmlFile = XML_DIR + "SAXTest.xml"; SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); Templates streamTemplates = saxFactory.newTemplates(new StreamSource(filenameToURL(xslFile))); // Just do a normal transform to this result Transformer transformer = streamTemplates.newTransformer(); transformer.transform(new StreamSource(filenameToURL(xmlFile)), new SAXResult()); setPermissions(); } }