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 dom;
  25 
  26 import java.io.ByteArrayInputStream;
  27 import java.io.IOException;
  28 import java.io.InputStreamReader;
  29 
  30 import javax.xml.parsers.DocumentBuilder;
  31 import javax.xml.parsers.DocumentBuilderFactory;
  32 import javax.xml.parsers.ParserConfigurationException;
  33 
  34 import org.testng.Assert;
  35 import org.testng.annotations.Listeners;
  36 import org.testng.annotations.Test;
  37 import org.w3c.dom.DOMException;
  38 import org.w3c.dom.Document;
  39 import org.w3c.dom.Entity;
  40 import org.xml.sax.InputSource;
  41 import org.xml.sax.SAXException;
  42 
  43 /*
  44  * @bug 6517707
  45  * @summary Test Node.setNodeValue(value) shall throw DOMException.NO_MODIFICATION_ALLOWED_ERR if the node is read-only.
  46  */
  47 @Listeners({jaxp.library.BasePolicy.class})
  48 public class CR6517707Test {
  49 
  50     @Test
  51     public void testCanonicalForm001() {
  52         String data = "<?xml version=\"1.0\" ?>" + "<!DOCTYPE root [" + "<!ELEMENT root ANY>" + "<!ENTITY ent \"foo\">"
  53                 + "<!NOTATION not PUBLIC \"http://xxx.xxx.xx/x.txt\">" + "]>" + "<root>" + "</root>";
  54 
  55         Document document = null;
  56         try {
  57             DocumentBuilderFactory docBF = DocumentBuilderFactory.newInstance();
  58             docBF.setNamespaceAware(true);
  59             DocBuilderWrapper docBuilder = new DocBuilderWrapper(docBF.newDocumentBuilder());
  60             document = docBuilder.parse(data);
  61         } catch (ParserConfigurationException e) {
  62             // return Status.failed(e.toString());
  63         } catch (IOException e) {
  64             // return Status.failed(e.toString());
  65         } catch (SAXException e) {
  66             // return Status.failed(e.toString());
  67         }
  68 
  69         Entity anEntity = (Entity) document.getDoctype().getEntities().item(0);
  70         boolean success = false;
  71         try {
  72             anEntity.setNodeValue("someValue"); // on jdk 6, not even throwing
  73                                                 // exception
  74 
  75             System.out.println("Should throw DOMException: NO_MODIFICATION_ALLOWED_ERR ");
  76         } catch (DOMException e) {
  77             if (e.code == DOMException.NO_MODIFICATION_ALLOWED_ERR) {
  78                 System.out.println(e.getMessage());
  79                 success = true;
  80             } else {
  81                 System.out.println("should throw DOMException.NO_MODIFICATION_ALLOWED_ERR (7). The error returned is " + e.code);
  82             }
  83         }
  84         if (!success) {
  85             Assert.fail("should throw DOMException.NO_MODIFICATION_ALLOWED_ERR (7).");
  86         }
  87     }
  88 
  89     class DocBuilderWrapper {
  90 
  91         private DocumentBuilder docBuilder;
  92         private final String ENCODING = "UTF-8";
  93 
  94         public DocBuilderWrapper() throws ParserConfigurationException {
  95             this.docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  96         }
  97 
  98         public DocBuilderWrapper(DocumentBuilder docBuilder) {
  99             setDocumentBuilder(docBuilder);
 100         }
 101 
 102         public DocumentBuilder getDocumentBuilder() {
 103             return docBuilder;
 104         }
 105 
 106         public void setDocumentBuilder(DocumentBuilder docBuilder) {
 107             if (docBuilder == null) {
 108                 new IllegalArgumentException("DocumentBuilder cannot be null");
 109             }
 110 
 111             this.docBuilder = docBuilder;
 112         }
 113 
 114         public Document parse(String xmlData) throws IOException, SAXException {
 115             if (xmlData == null) {
 116                 new IllegalArgumentException("String cannot be null");
 117             }
 118 
 119             ByteArrayInputStream bis = new ByteArrayInputStream(xmlData.getBytes(ENCODING));
 120             InputStreamReader isr = new InputStreamReader(bis, ENCODING);
 121             InputSource source = new InputSource(isr);
 122             return docBuilder.parse(source);
 123         }
 124     }
 125 
 126 }