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