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