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.IOException;
  27 import java.io.StringReader;
  28 
  29 import javax.xml.parsers.DocumentBuilder;
  30 import javax.xml.parsers.DocumentBuilderFactory;
  31 import javax.xml.parsers.ParserConfigurationException;
  32 
  33 import org.testng.Assert;
  34 import org.testng.annotations.Test;
  35 import org.w3c.dom.DOMException;
  36 import org.w3c.dom.Document;
  37 import org.w3c.dom.Entity;
  38 import org.xml.sax.InputSource;
  39 import org.xml.sax.SAXException;
  40 
  41 /*
  42  * @bug 6517717
  43  * @summary Test Node.setPrefix(prefix) shall throw DOMException.NO_MODIFICATION_ALLOWED_ERR if the node is read-only.
  44  */
  45 public class CR6517717Test {
  46 
  47     @Test
  48     public void testCanonicalForm001() {
  49         String data = "<?xml version=\"1.0\" ?>" + "<!DOCTYPE test:root [" + "<!ELEMENT test:root ANY>" + "<!ENTITY ent \"foo\">"
  50                 + "<!ATTLIST test:root test:a CDATA #FIXED \"qqq\">" + "]>" + "<test:root xmlns:test=\"http://xxxx.xx/\">" + "</test:root>";
  51 
  52         Document document = null;
  53         try {
  54             DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  55             document = docBuilder.parse(new InputSource(new StringReader(data)));
  56         } catch (ParserConfigurationException e) {
  57             System.out.println(e.toString());
  58         } catch (IOException e) {
  59             System.out.println(e.toString());
  60         } catch (SAXException e) {
  61             System.out.println(e.toString());
  62         }
  63 
  64         Entity anEntity = (Entity) document.getDoctype().getEntities().item(0);
  65         boolean success = false;
  66         try {
  67             anEntity.setPrefix("test1");
  68             System.out.println("Should throw DOMException: NO_MODIFICATION_ALLOWED_ERR ");
  69         } catch (DOMException e) {
  70             if (e.code == DOMException.NO_MODIFICATION_ALLOWED_ERR) {
  71                 System.out.println("OK");
  72                 success = true;
  73             } else {
  74                 System.out.println("should throw DOMException.NO_MODIFICATION_ALLOWED_ERR (7). The error returned is (" + e.code + ")" + e.getMessage());
  75             }
  76         }
  77         if (!success) {
  78             Assert.fail("should throw DOMException.NO_MODIFICATION_ALLOWED_ERR (7).");
  79         }
  80 
  81     }
  82 }