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