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