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