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