diff options
author | Jesse Wilson <jessewilson@google.com> | 2010-02-26 12:17:32 -0800 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2010-02-26 12:50:57 -0800 |
commit | 5213a38431ea28c97d29e76c5b2d8e156c2d1705 (patch) | |
tree | 05983b55de5441a5c77c61af2485c9c86448a648 /xml/src/test | |
parent | 13a40719e8f66d75ea3277fdd4305772623cf84c (diff) | |
download | libcore-5213a38431ea28c97d29e76c5b2d8e156c2d1705.zip libcore-5213a38431ea28c97d29e76c5b2d8e156c2d1705.tar.gz libcore-5213a38431ea28c97d29e76c5b2d8e156c2d1705.tar.bz2 |
Adding tests to parse document attributes from the DOM.
This adds implementation and tests for these methods:
getInputEncoding(), getDocumentURI().
And tests for these methods:
getXmlEncoding(), getXmlVersion(), getXmlStandalone().
Diffstat (limited to 'xml/src/test')
-rw-r--r-- | xml/src/test/java/tests/xml/AllTests.java | 1 | ||||
-rw-r--r-- | xml/src/test/java/tests/xml/DeclarationTest.java | 103 |
2 files changed, 104 insertions, 0 deletions
diff --git a/xml/src/test/java/tests/xml/AllTests.java b/xml/src/test/java/tests/xml/AllTests.java index 597e35e..beabd08 100644 --- a/xml/src/test/java/tests/xml/AllTests.java +++ b/xml/src/test/java/tests/xml/AllTests.java @@ -24,6 +24,7 @@ public class AllTests { public static Test suite() { TestSuite suite = tests.TestSuiteFactory.createTestSuite(); + suite.addTestSuite(DeclarationTest.class); suite.addTestSuite(DomTest.class); suite.addTestSuite(SimpleParserTest.class); suite.addTestSuite(SimpleBuilderTest.class); diff --git a/xml/src/test/java/tests/xml/DeclarationTest.java b/xml/src/test/java/tests/xml/DeclarationTest.java new file mode 100644 index 0000000..8fea844 --- /dev/null +++ b/xml/src/test/java/tests/xml/DeclarationTest.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tests.xml; + +import junit.framework.TestCase; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * Test the parsing of the XML declaration, plus the additional document fields + * captured during parsing. + */ +public class DeclarationTest extends TestCase { + + private String systemIdA; + private Document documentA; + + private String systemIdB; + private Document documentB; + + @Override protected void setUp() throws Exception { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + + systemIdA = stringToSystemId( + "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\" ?><foo />"); + InputSource inputSourceA = new InputSource(systemIdA); + inputSourceA.setEncoding("US-ASCII"); + documentA = builder.parse(inputSourceA); + + systemIdB = stringToSystemId( + "<?xml version=\"1.1\" encoding=\"US-ASCII\" standalone=\"yes\" ?><foo />"); + InputSource inputSourceB = new InputSource(systemIdB); + inputSourceB.setEncoding("ISO-8859-1"); + documentB = builder.parse(inputSourceB); + } + + private String stringToSystemId(String contents) throws IOException { + File file = File.createTempFile("temp", "xml"); + file.deleteOnExit(); + OutputStream out = new FileOutputStream(file); + out.write(contents.getBytes("UTF-8")); + out.close(); + return "file:" + file; + } + + /** + * XML parsers are advised of the document's character set via two channels: + * via the declaration and also the document's input source. To test that + * each of these winds up in the correct location in the document model, we + * supply different names for each. This is only safe because for the subset + * of characters in the document, the character sets are equivalent. + */ + public void testGetInputEncoding() throws Exception { + assertEquals("US-ASCII", documentA.getInputEncoding()); + assertEquals("ISO-8859-1", documentB.getInputEncoding()); + } + + public void testGetXmlEncoding() throws Exception { + String message = "This implementation doesn't parse the encoding from the XML declaration"; + assertEquals(message, "ISO-8859-1", documentA.getXmlEncoding()); + assertEquals(message, "US-ASCII", documentB.getXmlEncoding()); + } + + public void testGetXmlVersion() throws Exception { + String message = "This implementation doesn't parse the version from the XML declaration"; + assertEquals(message, "1.0", documentA.getXmlVersion()); + assertEquals(message, "1.1", documentB.getXmlVersion()); + } + + public void testGetXmlStandalone() throws Exception { + String message = "This implementation doesn't parse standalone from the XML declaration"; + assertEquals(message, false, documentA.getXmlStandalone()); + assertEquals(message, true, documentB.getXmlStandalone()); + } + + public void testGetDocumentUri() throws Exception { + assertEquals(systemIdA, documentA.getDocumentURI()); + assertEquals(systemIdB, documentB.getDocumentURI()); + } +} |