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.Document;
  37 import org.xml.sax.InputSource;
  38 import org.xml.sax.SAXException;
  39 
  40 /*
  41  * @test
  42  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  43  * @run testng/othervm -DrunSecMngr=true dom.TCKEncodingTest
  44  * @run testng/othervm dom.TCKEncodingTest
  45  * @summary Test Document.getInputEncoding().
  46  */
  47 @Listeners({jaxp.library.BasePolicy.class})
  48 public class TCKEncodingTest {
  49 
  50     /**
  51      * Assertion testing
  52      * for public String getInputEncoding(),
  53      * An attribute specifying the actual encoding of this document..
  54      */
  55     @Test
  56     public void testGetInputEncoding001() {
  57         String data = "<?xml version=\"1.0\"?>" + "<!DOCTYPE root [" + "<!ELEMENT root ANY>" + "]>" + "<root/>";
  58 
  59         Document doc = null;
  60         try {
  61             DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  62             InputSource inSource = new InputSource(new StringReader(data));
  63             inSource.setEncoding("UTF-8");
  64             inSource.setSystemId("test.xml");
  65             doc = docBuilder.parse(inSource);
  66         } catch (ParserConfigurationException e) {
  67             Assert.fail(e.toString());
  68         } catch (IOException e) {
  69             Assert.fail(e.toString());
  70         } catch (SAXException e) {
  71             Assert.fail(e.toString());
  72         }
  73 
  74         String encoding = doc.getInputEncoding();
  75         if (encoding == null || !encoding.equals("UTF-8")) {
  76             Assert.fail("expected encoding: UTF-8, returned: " + encoding);
  77         }
  78 
  79         System.out.println("OK");
  80     }
  81 
  82     /**
  83      * Assertion testing
  84      * for public String getInputEncoding(),
  85      * Encoding is not specified. getInputEncoding returns null..
  86      */
  87     @Test
  88     public void testGetInputEncoding002() {
  89         Document doc = null;
  90         try {
  91             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  92             doc = db.newDocument();
  93         } catch (ParserConfigurationException e) {
  94             Assert.fail(e.toString());
  95         }
  96 
  97         String encoding = doc.getInputEncoding();
  98         if (encoding != null) {
  99             Assert.fail("expected encoding: null, returned: " + encoding);
 100         }
 101 
 102         System.out.println("OK");
 103     }
 104 }
 105