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 validation;
  25 
  26 import javax.xml.XMLConstants;
  27 import javax.xml.parsers.DocumentBuilder;
  28 import javax.xml.parsers.DocumentBuilderFactory;
  29 import javax.xml.transform.Transformer;
  30 import javax.xml.transform.dom.DOMResult;
  31 import javax.xml.transform.dom.DOMSource;
  32 import javax.xml.transform.sax.SAXTransformerFactory;
  33 import javax.xml.transform.sax.TransformerHandler;
  34 import javax.xml.validation.Schema;
  35 import javax.xml.validation.SchemaFactory;
  36 import javax.xml.validation.Validator;
  37 
  38 import org.testng.Assert;
  39 import org.testng.annotations.Listeners;
  40 import org.testng.annotations.Test;
  41 import org.w3c.dom.Document;
  42 import org.w3c.dom.Element;
  43 import org.w3c.dom.Node;
  44 import org.xml.sax.InputSource;
  45 import org.xml.sax.XMLReader;
  46 import org.xml.sax.helpers.XMLReaderFactory;
  47 
  48 /*
  49  * @bug 5072946
  50  * @summary Test Validator.validate(DOMSource,DOMResult) outputs to the result.
  51  */
  52 @Listeners({jaxp.library.FilePolicy.class})
  53 public class Bug5072946 {
  54 
  55     @Test
  56     public void test1() throws Exception {
  57 
  58         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  59         dbf.setNamespaceAware(true);
  60         DocumentBuilder parser = dbf.newDocumentBuilder();
  61         Document dom = parser.parse(Bug5072946.class.getResourceAsStream("Bug5072946.xml"));
  62 
  63         SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  64         Schema s = sf.newSchema(Bug5072946.class.getResource("Bug5072946.xsd"));
  65         Validator v = s.newValidator();
  66 
  67         DOMResult r = new DOMResult();
  68         // r.setNode(dbf.newDocumentBuilder().newDocument());
  69         v.validate(new DOMSource(dom), r);
  70 
  71         Node node = r.getNode();
  72         Assert.assertNotNull(node);
  73         Node fc = node.getFirstChild();
  74         Assert.assertTrue(fc instanceof Element);
  75         Element e = (Element) fc;
  76 
  77         Assert.assertEquals("value", e.getAttribute("foo"));
  78     }
  79 
  80     /**
  81      * Tests if the identity transformer correctly sets the output node.
  82      */
  83     @Test
  84     public void test2() throws Exception {
  85         SAXTransformerFactory sf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
  86         TransformerHandler th = sf.newTransformerHandler();
  87         DOMResult r = new DOMResult();
  88         th.setResult(r);
  89 
  90         XMLReader reader = XMLReaderFactory.createXMLReader();
  91         reader.setContentHandler(th);
  92         reader.parse(new InputSource(Bug5072946.class.getResourceAsStream("Bug5072946.xml")));
  93 
  94         Assert.assertNotNull(r.getNode());
  95     }
  96 
  97     @Test
  98     public void test3() throws Exception {
  99         SAXTransformerFactory sf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
 100         Transformer t = sf.newTransformer();
 101 
 102         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 103         dbf.setNamespaceAware(true);
 104         DocumentBuilder parser = dbf.newDocumentBuilder();
 105         Document dom = parser.parse(Bug5072946.class.getResourceAsStream("Bug5072946.xml"));
 106 
 107         DOMResult r = new DOMResult();
 108 
 109         t.transform(new DOMSource(dom), r);
 110         Assert.assertNotNull(r.getNode());
 111 
 112         Node n = r.getNode().getFirstChild();
 113         r.setNode(n);
 114         t.transform(new DOMSource(dom), r);
 115         Assert.assertNotNull(r.getNode());
 116         Assert.assertSame(r.getNode(), n);
 117 
 118         r.setNextSibling(r.getNode().getFirstChild());
 119         t.transform(new DOMSource(dom), r);
 120         Assert.assertNotNull(r.getNode());
 121         Assert.assertSame(r.getNode(), n);
 122     }
 123 }