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