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