1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.xml; 18 19 import junit.framework.TestCase; 20 import org.w3c.dom.Document; 21 import org.xml.sax.InputSource; 22 23 import javax.xml.parsers.DocumentBuilder; 24 import javax.xml.parsers.DocumentBuilderFactory; 25 import java.io.File; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.io.OutputStream; 29 30 /** 31 * Test the parsing of the XML declaration, plus the additional document fields 32 * captured during parsing. 33 */ 34 public class DeclarationTest extends TestCase { 35 36 private String systemIdA; 37 private Document documentA; 38 39 private String systemIdB; 40 private Document documentB; 41 setUp()42 @Override protected void setUp() throws Exception { 43 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 44 factory.setNamespaceAware(true); 45 DocumentBuilder builder = factory.newDocumentBuilder(); 46 47 systemIdA = stringToSystemId( 48 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\" ?><foo />"); 49 InputSource inputSourceA = new InputSource(systemIdA); 50 inputSourceA.setEncoding("US-ASCII"); 51 documentA = builder.parse(inputSourceA); 52 53 systemIdB = stringToSystemId( 54 "<?xml version=\"1.1\" encoding=\"US-ASCII\" standalone=\"yes\" ?><foo />"); 55 InputSource inputSourceB = new InputSource(systemIdB); 56 inputSourceB.setEncoding("ISO-8859-1"); 57 documentB = builder.parse(inputSourceB); 58 } 59 stringToSystemId(String contents)60 private String stringToSystemId(String contents) throws IOException { 61 File file = File.createTempFile("temp", "xml"); 62 file.deleteOnExit(); 63 OutputStream out = new FileOutputStream(file); 64 out.write(contents.getBytes("UTF-8")); 65 out.close(); 66 return "file:" + file; 67 } 68 69 /** 70 * XML parsers are advised of the document's character set via two channels: 71 * via the declaration and also the document's input source. To test that 72 * each of these winds up in the correct location in the document model, we 73 * supply different names for each. This is only safe because for the subset 74 * of characters in the document, the character sets are equivalent. 75 */ testGetInputEncoding()76 public void testGetInputEncoding() throws Exception { 77 assertEquals("US-ASCII", documentA.getInputEncoding()); 78 assertEquals("ISO-8859-1", documentB.getInputEncoding()); 79 } 80 testGetXmlEncoding()81 public void testGetXmlEncoding() throws Exception { 82 String message = "This implementation doesn't parse the encoding from the XML declaration"; 83 assertEquals(message, "ISO-8859-1", documentA.getXmlEncoding()); 84 assertEquals(message, "US-ASCII", documentB.getXmlEncoding()); 85 } 86 testGetXmlVersion()87 public void testGetXmlVersion() throws Exception { 88 String message = "This implementation doesn't parse the version from the XML declaration"; 89 assertEquals(message, "1.0", documentA.getXmlVersion()); 90 assertEquals(message, "1.1", documentB.getXmlVersion()); 91 } 92 testGetXmlStandalone()93 public void testGetXmlStandalone() throws Exception { 94 String message = "This implementation doesn't parse standalone from the XML declaration"; 95 assertEquals(message, false, documentA.getXmlStandalone()); 96 assertEquals(message, true, documentB.getXmlStandalone()); 97 } 98 testGetDocumentUri()99 public void testGetDocumentUri() throws Exception { 100 assertEquals(systemIdA, documentA.getDocumentURI()); 101 assertEquals(systemIdB, documentB.getDocumentURI()); 102 } 103 } 104