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