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