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