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 java.io.File; 27 import java.io.IOException; 28 import java.io.StringWriter; 29 30 import javax.xml.XMLConstants; 31 import javax.xml.parsers.DocumentBuilder; 32 import javax.xml.parsers.DocumentBuilderFactory; 33 import javax.xml.parsers.ParserConfigurationException; 34 import javax.xml.transform.Transformer; 35 import javax.xml.transform.TransformerConfigurationException; 36 import javax.xml.transform.TransformerException; 37 import javax.xml.transform.TransformerFactory; 38 import javax.xml.transform.dom.DOMResult; 39 import javax.xml.transform.dom.DOMSource; 40 import javax.xml.transform.stream.StreamResult; 41 import javax.xml.validation.Schema; 42 import javax.xml.validation.SchemaFactory; 43 import javax.xml.validation.Validator; 44 45 import org.testng.Assert; 46 import org.testng.annotations.Listeners; 47 import org.testng.annotations.Test; 48 import org.w3c.dom.Document; 49 import org.xml.sax.SAXException; 50 51 /* 52 * @test 53 * @bug 6467424 54 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 55 * @run testng/othervm -DrunSecMngr=true validation.Bug6467424Test 56 * @run testng/othervm validation.Bug6467424Test 57 * @summary Test Validator augments the default delement value if feature element-default is on. 58 */ 59 @Listeners({jaxp.library.FilePolicy.class}) 60 public class Bug6467424Test { 61 static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 62 static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 63 64 @Test 65 public void test() { 66 // System.setSecurityManager(new SecurityManager()); 67 try { 68 SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); 69 File schemaLocation = new File(getClass().getResource("Bug6467424.xsd").getFile()); 70 Schema schema = factory.newSchema(schemaLocation); 71 Validator validator = schema.newValidator(); 72 73 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 74 domFactory.setNamespaceAware(true); // never forget this 75 DocumentBuilder builder = domFactory.newDocumentBuilder(); 76 Document doc = builder.parse(new File(getClass().getResource("Bug6467424.xml").getFile())); 77 78 DOMSource source = new DOMSource(doc); 79 DOMResult result = new DOMResult(); 80 81 validator.validate(source, result); 82 Document augmented = (Document) result.getNode(); 83 84 TransformerFactory tFactory = TransformerFactory.newInstance(); 85 86 Transformer transformer = tFactory.newTransformer(); 87 88 DOMSource domSource = new DOMSource(augmented); 89 StringWriter sw = new StringWriter(); 90 // StreamResult streamResult = new StreamResult(System.out); 91 StreamResult streamResult = new StreamResult(sw); 92 transformer.transform(domSource, streamResult); 93 String s = sw.toString(); 94 if (s.indexOf("Schema Validation") == -1) { 95 Assert.fail("Failed: result is expected to be augmented"); 96 } 97 } 98 99 catch (TransformerConfigurationException e) { 100 // e.printStackTrace(); 101 System.out.println(e.getMessage()); 102 } catch (TransformerException e) { 103 System.out.println(e.getMessage()); 104 } catch (SAXException e) { 105 System.out.println(e.getMessage()); 106 } catch (ParserConfigurationException e) { 107 System.out.println(e.getMessage()); 108 } catch (IOException e) { 109 System.out.println(e.getMessage()); 110 } 111 } 112 113 /** 114 * this test is to make sure the element-default feature works for 115 * validation using DOM parser reference: parser feature: 116 * http://xerces.apache.org/xerces2-j/feature.html#validation 117 */ 118 @Test 119 public void testDOMValidation() throws Exception { 120 try { 121 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 122 // domFactory.setFeature("http://xml.org/sax/features/validation", 123 // true); 124 domFactory.setNamespaceAware(true); // never forget this 125 domFactory.setValidating(true); 126 127 domFactory.setAttribute(SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); 128 domFactory.setAttribute(SCHEMA_SOURCE, Bug6467424Test.class.getResource("Bug6467424.xsd").toExternalForm()); 129 130 domFactory.setFeature("http://apache.org/xml/features/validation/schema", true); 131 domFactory.setFeature("http://apache.org/xml/features/validation/schema/element-default", true); 132 DocumentBuilder builder = domFactory.newDocumentBuilder(); 133 Document doc = builder.parse(new File(getClass().getResource("Bug6467424.xml").getFile())); 134 135 TransformerFactory tFactory = TransformerFactory.newInstance(); 136 137 Transformer transformer = tFactory.newTransformer(); 138 DOMSource domSource = new DOMSource(doc); 139 StringWriter sw = new StringWriter(); 140 // StreamResult streamResult = new StreamResult(System.out); 141 StreamResult streamResult = new StreamResult(sw); 142 transformer.transform(domSource, streamResult); 143 String s = sw.toString(); 144 if (s.indexOf("Schema Validation") == -1) { 145 Assert.fail("Failed: result is expected to be augmented"); 146 } 147 148 } 149 150 catch (TransformerConfigurationException e) { 151 System.out.println(e.getMessage()); 152 } catch (TransformerException e) { 153 System.out.println(e.getMessage()); 154 } catch (SAXException e) { 155 System.out.println(e.getMessage()); 156 } catch (ParserConfigurationException e) { 157 System.out.println(e.getMessage()); 158 } catch (IOException e) { 159 System.out.println(e.getMessage()); 160 } 161 } 162 163 @Test 164 public void testDOMValidation1() throws Exception { 165 try { 166 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 167 // domFactory.setFeature("http://xml.org/sax/features/validation", 168 // true); 169 domFactory.setNamespaceAware(true); // never forget this 170 domFactory.setValidating(true); 171 172 domFactory.setAttribute(SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); 173 domFactory.setAttribute(SCHEMA_SOURCE, Bug6467424Test.class.getResource("Bug6467424.xsd").toExternalForm()); 174 175 domFactory.setFeature("http://apache.org/xml/features/validation/schema", true); 176 domFactory.setFeature("http://apache.org/xml/features/validation/schema/element-default", false); 177 DocumentBuilder builder = domFactory.newDocumentBuilder(); 178 Document doc = builder.parse(new File(getClass().getResource("Bug6467424.xml").getFile())); 179 180 TransformerFactory tFactory = TransformerFactory.newInstance(); 181 182 Transformer transformer = tFactory.newTransformer(); 183 DOMSource domSource = new DOMSource(doc); 184 StringWriter sw = new StringWriter(); 185 // StreamResult streamResult = new StreamResult(System.out); 186 StreamResult streamResult = new StreamResult(sw); 187 transformer.transform(domSource, streamResult); 188 String s = sw.toString(); 189 if (s.indexOf("Schema Validation") > 0) { 190 Assert.fail("Failed: result is not expected to be augmented"); 191 } 192 193 } 194 195 catch (TransformerConfigurationException e) { 196 System.out.println(e.getMessage()); 197 } catch (TransformerException e) { 198 System.out.println(e.getMessage()); 199 } catch (SAXException e) { 200 System.out.println(e.getMessage()); 201 } catch (ParserConfigurationException e) { 202 System.out.println(e.getMessage()); 203 } catch (IOException e) { 204 System.out.println(e.getMessage()); 205 } 206 } 207 } 208