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.dom;
  21 
  22 import java.io.FilePermission;
  23 import java.io.IOException;
  24 import java.util.PropertyPermission;
  25 import javax.xml.parsers.DocumentBuilder;
  26 import javax.xml.parsers.DocumentBuilderFactory;
  27 import javax.xml.parsers.ParserConfigurationException;
  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.dom.DOMResult;
  33 import javax.xml.transform.dom.DOMSource;
  34 import jaxp.library.JAXPBaseTest;
  35 import static jaxp.library.JAXPTestUtilities.compareSerializeDOMWithGold;
  36 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  37 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR;
  38 import static org.apache.qetest.trax.TraxConst.XML_DIR;
  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.testng.annotations.Test;
  44 import org.w3c.dom.Node;
  45 import org.xml.sax.InputSource;
  46 import org.xml.sax.SAXException;
  47 
  48 /**
  49  *
  50  * API Coverage test for the DOMResult class of TRAX.
  51  */
  52 public class DOMResultAPITest extends JAXPBaseTest {
  53     /**
  54      * Basic API coverage, constructor and set/get methods.
  55      * 
  56      * @throws ParserConfigurationException in case of ServiceConfigurationError
  57      *         service configuration error or if the implementation is not 
  58      *         available or cannot be instantiated.
  59      */
  60     @Test
  61     public void testCase1() throws ParserConfigurationException {
  62         // Default no-arg ctor sets nothing (but needs special test for
  63         //  creating new doc when being transformed)
  64         DOMResult defaultDOM = new DOMResult();
  65         assertNull(defaultDOM.getNode());
  66         assertNull(defaultDOM.getSystemId());
  67 
  68         // ctor(Node) with a simple node
  69         DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
  70         DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
  71         Node n = docBuilder.newDocument();
  72         DOMResult nodeDOM = new DOMResult(n);
  73         assertEquals(nodeDOM.getNode(), n);
  74         assertNull(nodeDOM.getSystemId());
  75 
  76         DOMResult nodeDOMid = new DOMResult(n, "this-is-system-id");
  77         assertEquals(nodeDOMid.getNode(), n);
  78         assertEquals(nodeDOMid.getSystemId(), "this-is-system-id");
  79 
  80         DOMResult wackyDOM = new DOMResult();
  81         Node n2 = docBuilder.newDocument();
  82         wackyDOM.setNode(n2);
  83         assertEquals(wackyDOM.getNode(), n2);
  84 
  85         wackyDOM.setSystemId("another-system-id");
  86         assertEquals(wackyDOM.getSystemId(), "another-system-id");
  87     }
  88     
  89     /**
  90      * Basic functionality of DOMResults.
  91      * Test 'blank' Result; reuse Results; swap Nodes; etc.
  92      * 
  93      * @throws TransformerException If an unrecoverable error occurs during the 
  94      *         course of the transformation.
  95      * @throws IOException if any I/O operation error.
  96      * @throws ParserConfigurationException in case of ServiceConfigurationError
  97      *         service configuration error or if the implementation is not 
  98      *         available or cannot be instantiated.
  99      * @throws SAXException for SAX error.
 100      */
 101     @Test
 102     public void testCase2() throws TransformerException, IOException, 
 103             ParserConfigurationException, SAXException {
 104         setPermissions(new PropertyPermission("test.src", "read"),
 105                 new FilePermission(System.getProperty("test.classes") + "/-",
 106                     "read, write"),
 107                 new FilePermission(System.getProperty("test.src") + "/-",
 108                     "read"));
 109         String xslURI = filenameToURL(XML_DIR + "DOMTest.xsl");
 110         String xmlURI = filenameToURL(XML_DIR + "DOMTest.xml");
 111         String goldFile = GOLDEN_DIR + "DOMTest.out";
 112 
 113         TransformerFactory factory = TransformerFactory.newInstance();
 114         DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
 115         dfactory.setNamespaceAware(true);
 116         DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
 117         Node xslNode = docBuilder.parse(new InputSource(xslURI));
 118         Node xmlNode = docBuilder.parse(new InputSource(xmlURI));
 119 
 120         // Try to get templates, transformer from node
 121         DOMSource xslSource = new DOMSource(xslNode);
 122         Templates templates = factory.newTemplates(xslSource);
 123         DOMSource xmlSource = new DOMSource(xmlNode);
 124 
 125         // Transforming into a DOMResult with a node is already
 126         //  well covered in DOMSourceAPITest and elsewhere
 127         // Verify a 'blank' Result object gets filled up properly
 128         DOMResult blankResult = new DOMResult();
 129         Transformer transformer = templates.newTransformer();
 130         transformer.transform(xmlSource, blankResult);
 131         Node blankNode = blankResult.getNode();
 132         assertNotNull(blankNode);
 133         assertTrue(compareSerializeDOMWithGold(goldFile, blankNode));
 134 
 135         // Reuse the same result for multiple transforms
 136         DOMResult reuseResult1 = new DOMResult(docBuilder.newDocument());
 137         transformer = templates.newTransformer();
 138         transformer.transform(xmlSource, reuseResult1);
 139         Node reuseNode = reuseResult1.getNode();
 140         assertTrue(compareSerializeDOMWithGold(goldFile, reuseNode));
 141         
 142         // So far it throws Exception as follows if you use same DOMResult:
 143         // org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made 
 144         // to insert a node where it is not permitted.
 145         DOMResult reuseResult2 = new DOMResult(docBuilder.newDocument());
 146         transformer = templates.newTransformer();
 147         transformer.transform(xmlSource, reuseResult2);
 148         reuseNode = reuseResult2.getNode();
 149         assertTrue(compareSerializeDOMWithGold(goldFile, reuseNode));
 150     }
 151 }