1 /*
   2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package parsers.xinclude;
  25 
  26 import static java.lang.System.lineSeparator;
  27 import static org.testng.Assert.assertEquals;
  28 
  29 import java.io.File;
  30 import java.io.StringWriter;
  31 
  32 import javax.xml.parsers.DocumentBuilder;
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 import javax.xml.transform.OutputKeys;
  35 import javax.xml.transform.Transformer;
  36 import javax.xml.transform.TransformerFactory;
  37 import javax.xml.transform.dom.DOMSource;
  38 import javax.xml.transform.stream.StreamResult;
  39 
  40 import org.testng.annotations.Listeners;
  41 import org.testng.annotations.Test;
  42 import org.w3c.dom.Document;
  43 import org.w3c.dom.NodeList;
  44 
  45 /*
  46  * @test
  47  * @bug 6794483 8080908
  48  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  49  * @run testng/othervm -DrunSecMngr=true parsers.xinclude.Bug6794483Test
  50  * @run testng/othervm parsers.xinclude.Bug6794483Test
  51  * @summary Test JAXP parser can resolve the included content properly if the
  52  * included xml contains an empty tag that ends with "/>", refer to XERCESJ-1134.
  53  */
  54 @Listeners({jaxp.library.FilePolicy.class})
  55 public class Bug6794483Test {
  56 
  57     @Test
  58     public final void test() throws Exception {
  59         Document doc = parseXmlFile(getClass().getResource("test1.xml").getPath());
  60 
  61         // check node4
  62         NodeList nodeList = doc.getElementsByTagName("node4");
  63         assertEquals(nodeList.getLength(), 1);
  64         assertEquals(nodeList.item(0).getTextContent(), "Node4 Value", "The data of node4 is missed in parsing: " + lineSeparator() + printXmlDoc(doc));
  65 
  66         // check node6
  67         nodeList = doc.getElementsByTagName("node6");
  68         assertEquals(nodeList.getLength(), 1);
  69         assertEquals(nodeList.item(0).getTextContent(), "Node6 Value", "The data of node6 is missed in parsing: " + lineSeparator() + printXmlDoc(doc));
  70     }
  71 
  72     public String printXmlDoc(Document doc) throws Exception {
  73         StringWriter sw = new StringWriter();
  74         StreamResult result = new StreamResult(sw);
  75 
  76         TransformerFactory transformerFact = TransformerFactory.newInstance();
  77         transformerFact.setAttribute("indent-number", new Integer(4));
  78         Transformer transformer;
  79 
  80         transformer = transformerFact.newTransformer();
  81         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  82         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  83         transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
  84 
  85         transformer.transform(new DOMSource(doc), result);
  86         return sw.toString();
  87     }
  88 
  89     public Document parseXmlFile(String fileName) throws Exception {
  90         System.out.println("Parsing XML file... " + fileName);
  91         DocumentBuilder docBuilder = null;
  92         Document doc = null;
  93         DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  94         docBuilderFactory.setCoalescing(true);
  95         docBuilderFactory.setXIncludeAware(true);
  96         System.out.println("Include: " + docBuilderFactory.isXIncludeAware());
  97         docBuilderFactory.setNamespaceAware(true);
  98         docBuilderFactory.setExpandEntityReferences(true);
  99 
 100         docBuilder = docBuilderFactory.newDocumentBuilder();
 101 
 102         File sourceFile = new File(fileName);
 103         doc = docBuilder.parse(sourceFile);
 104 
 105         System.out.println("XML file parsed");
 106         return doc;
 107 
 108     }
 109 }