/* * Copyright (c) 2014, 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.dom; import java.io.FilePermission; import java.io.IOException; import java.util.PropertyPermission; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import jaxp.library.JAXPBaseTest; import static jaxp.library.JAXPTestUtilities.compareSerializeDOMWithGold; 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 static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import org.w3c.dom.Node; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * * API Coverage test for the DOMResult class of TRAX. */ public class DOMResultAPITest extends JAXPBaseTest { /** * Basic API coverage, constructor and set/get methods. * * @throws ParserConfigurationException in case of ServiceConfigurationError * service configuration error or if the implementation is not * available or cannot be instantiated. */ @Test public void testCase1() throws ParserConfigurationException { // Default no-arg ctor sets nothing (but needs special test for // creating new doc when being transformed) DOMResult defaultDOM = new DOMResult(); assertNull(defaultDOM.getNode()); assertNull(defaultDOM.getSystemId()); // ctor(Node) with a simple node DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); Node n = docBuilder.newDocument(); DOMResult nodeDOM = new DOMResult(n); assertEquals(nodeDOM.getNode(), n); assertNull(nodeDOM.getSystemId()); DOMResult nodeDOMid = new DOMResult(n, "this-is-system-id"); assertEquals(nodeDOMid.getNode(), n); assertEquals(nodeDOMid.getSystemId(), "this-is-system-id"); DOMResult wackyDOM = new DOMResult(); Node n2 = docBuilder.newDocument(); wackyDOM.setNode(n2); assertEquals(wackyDOM.getNode(), n2); wackyDOM.setSystemId("another-system-id"); assertEquals(wackyDOM.getSystemId(), "another-system-id"); } /** * Basic functionality of DOMResults. * Test 'blank' Result; reuse Results; swap Nodes; etc. * * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. * @throws IOException if any I/O operation error. * @throws ParserConfigurationException in case of ServiceConfigurationError * service configuration error or if the implementation is not * available or cannot be instantiated. * @throws SAXException for SAX error. */ @Test public void testCase2() throws TransformerException, IOException, ParserConfigurationException, SAXException { setPermissions(new PropertyPermission("test.src", "read"), new FilePermission(System.getProperty("test.classes") + "/-", "read, write"), new FilePermission(System.getProperty("test.src") + "/-", "read")); String xslURI = filenameToURL(XML_DIR + "DOMTest.xsl"); String xmlURI = filenameToURL(XML_DIR + "DOMTest.xml"); String goldFile = GOLDEN_DIR + "DOMTest.out"; TransformerFactory factory = TransformerFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); Node xslNode = docBuilder.parse(new InputSource(xslURI)); Node xmlNode = docBuilder.parse(new InputSource(xmlURI)); // Try to get templates, transformer from node DOMSource xslSource = new DOMSource(xslNode); Templates templates = factory.newTemplates(xslSource); DOMSource xmlSource = new DOMSource(xmlNode); // Transforming into a DOMResult with a node is already // well covered in DOMSourceAPITest and elsewhere // Verify a 'blank' Result object gets filled up properly DOMResult blankResult = new DOMResult(); Transformer transformer = templates.newTransformer(); transformer.transform(xmlSource, blankResult); Node blankNode = blankResult.getNode(); assertNotNull(blankNode); assertTrue(compareSerializeDOMWithGold(goldFile, blankNode)); // Reuse the same result for multiple transforms DOMResult reuseResult1 = new DOMResult(docBuilder.newDocument()); transformer = templates.newTransformer(); transformer.transform(xmlSource, reuseResult1); Node reuseNode = reuseResult1.getNode(); assertTrue(compareSerializeDOMWithGold(goldFile, reuseNode)); // So far it throws Exception as follows if you use same DOMResult: // org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made // to insert a node where it is not permitted. DOMResult reuseResult2 = new DOMResult(docBuilder.newDocument()); transformer = templates.newTransformer(); transformer.transform(xmlSource, reuseResult2); reuseNode = reuseResult2.getNode(); assertTrue(compareSerializeDOMWithGold(goldFile, reuseNode)); } }