diff options
author | Jesse Wilson <jessewilson@google.com> | 2010-02-28 01:39:43 -0800 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2010-02-28 01:39:43 -0800 |
commit | d94a8629a2fb8cf01369c8935ad782dfb98645af (patch) | |
tree | 729247e34f2c1c6d630cb9e546ccaf7588ed50e2 /xml/src/test/java | |
parent | 33dedce20a436f3404c3a8b4b69891c5750e818a (diff) | |
parent | 1433c5180c27b65eac56a57c3c119acef0b36aa1 (diff) | |
download | libcore-d94a8629a2fb8cf01369c8935ad782dfb98645af.zip libcore-d94a8629a2fb8cf01369c8935ad782dfb98645af.tar.gz libcore-d94a8629a2fb8cf01369c8935ad782dfb98645af.tar.bz2 |
am 4795e033: Merge "Implementing still more DOM API for text nodes. - Text.isElementContentWhitespace() - Text.getWholeText() - Text.replaceWholeText()"
Merge commit '4795e033e5d93d72438d5004dd77ea82014f4657' into dalvik-dev
* commit '4795e033e5d93d72438d5004dd77ea82014f4657':
Implementing still more DOM API for text nodes.
Diffstat (limited to 'xml/src/test/java')
4 files changed, 141 insertions, 30 deletions
diff --git a/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderTest.java b/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderTest.java index cfa62dc..ea77a65 100644 --- a/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderTest.java +++ b/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderTest.java @@ -561,24 +561,18 @@ public class DocumentBuilderTest extends TestCase { method = "parse", args = {java.lang.String.class} ) - public void test_parseLjava_lang_String() { + public void test_parseLjava_lang_String() throws Exception { // case 1: Trivial use. File f = new File(getClass().getResource("/simple.xml").getFile()); - try { - Document d = db.parse(f.getAbsolutePath()); - assertNotNull(d); + Document d = db.parse(f.getAbsolutePath()); + assertNotNull(d); // TBD getXmlEncoding() is not supported // assertEquals("ISO-8859-1", d.getXmlEncoding()); - assertEquals(2, d.getChildNodes().getLength()); - assertEquals("#comment", - d.getChildNodes().item(0).getNodeName()); - assertEquals("breakfast_menu", - d.getChildNodes().item(1).getNodeName()); - } catch (IOException ioe) { - fail("Unexpected IOException " + ioe.toString()); - } catch (SAXException sax) { - fail("Unexpected SAXException " + sax.toString()); - } + assertEquals(2, d.getChildNodes().getLength()); + assertEquals("#comment", + d.getChildNodes().item(0).getNodeName()); + assertEquals("breakfast_menu", + d.getChildNodes().item(1).getNodeName()); // case 2: Try to call parse with null argument try { @@ -586,10 +580,6 @@ public class DocumentBuilderTest extends TestCase { fail("Expected IllegalArgumentException was not thrown"); } catch (IllegalArgumentException iae) { // expected - } catch (IOException ioe) { - fail("Unexpected IOException " + ioe.toString()); - } catch (SAXException sax) { - fail("Unexpected SAXException " + sax.toString()); } // case 3: Try to parse a non-existent uri @@ -598,8 +588,6 @@ public class DocumentBuilderTest extends TestCase { fail("Expected IOException was not thrown"); } catch (IOException ioe) { // expected - } catch (SAXException sax) { - fail("Unexpected SAXException " + sax.toString()); } // case 4: Try to parse incorrect xml file @@ -607,8 +595,6 @@ public class DocumentBuilderTest extends TestCase { f = new File(getClass().getResource("/wrong.xml").getFile()); db.parse(f.getAbsolutePath()); fail("Expected SAXException was not thrown"); - } catch (IOException ioe) { - fail("Unexpected IOException " + ioe.toString()); } catch (SAXException sax) { // expected } diff --git a/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserTest.java b/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserTest.java index b7e3f74..d44107b 100644 --- a/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserTest.java +++ b/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserTest.java @@ -676,8 +676,7 @@ public class SAXParserTest extends TestCase { MyDefaultHandler dh = new MyDefaultHandler(); InputStream is = new FileInputStream(list_wf[i]); parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID); - assertTrue(SAXParserTestSupport.equalsMaps(hm, - dh.createData())); + assertEquals(hm, dh.createData()); } catch (IOException ioe) { fail("Unexpected IOException " + ioe.toString()); } catch (SAXException sax) { diff --git a/xml/src/test/java/tests/xml/AllTests.java b/xml/src/test/java/tests/xml/AllTests.java index 0042967..b77ade8 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 = new TestSuite(); + suite.addTestSuite(DomTest.class); suite.addTestSuite(SimpleParserTest.class); suite.addTestSuite(SimpleBuilderTest.class); suite.addTestSuite(NodeTest.class); diff --git a/xml/src/test/java/tests/xml/DomTest.java b/xml/src/test/java/tests/xml/DomTest.java index 5f0a19a..69e8b37 100644 --- a/xml/src/test/java/tests/xml/DomTest.java +++ b/xml/src/test/java/tests/xml/DomTest.java @@ -109,7 +109,6 @@ public class DomTest extends TestCase { factory.setNamespaceAware(true); builder = factory.newDocumentBuilder(); domImplementation = builder.getDOMImplementation(); - document = builder.parse(new InputSource(new StringReader(xml))); // doctype nodes @@ -448,7 +447,6 @@ public class DomTest extends TestCase { document.appendChild(root); EntityReference entityReference = document.createEntityReference("sp"); - entityReference.setNodeValue("Maple Syrup"); root.appendChild(entityReference); try { @@ -461,8 +459,7 @@ public class DomTest extends TestCase { public void testAttributeSetTextContent() throws TransformerException { String original = domToString(document); standard.setTextContent("foobar"); - String expected = original.replaceFirst( - "standard=\"strawberry\"", "standard=\"foobar\""); + String expected = original.replace("standard=\"strawberry\"", "standard=\"foobar\""); assertEquals(expected, domToString(document)); } @@ -614,8 +611,136 @@ public class DomTest extends TestCase { } } - private String domToString(Document document) - throws TransformerException { + public void testIsElementContentWhitespaceWithoutDeclaration() throws Exception { + String xml = "<menu> <item/> </menu>"; + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + Text text = (Text) factory.newDocumentBuilder() + .parse(new InputSource(new StringReader(xml))) + .getDocumentElement().getChildNodes().item(0); + assertFalse(text.isElementContentWhitespace()); + } + + public void testIsElementContentWhitespaceWithDeclaration() throws Exception { + String xml = "<!DOCTYPE menu [\n" + + " <!ELEMENT menu (item)*>\n" + + " <!ELEMENT item (#PCDATA)>\n" + + "]><menu> <item/> </menu>"; + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + Text text = (Text) factory.newDocumentBuilder() + .parse(new InputSource(new StringReader(xml))) + .getDocumentElement().getChildNodes().item(0); + assertTrue("This implementation does not recognize element content whitespace", + text.isElementContentWhitespace()); + } + + public void testGetWholeTextFirst() { + assertEquals("Belgian waffles & strawberries (< 5g of fat)", + descriptionText1.getWholeText()); + } + + public void testGetWholeTextMiddle() { + assertEquals("This implementation doesn't include preceding nodes in getWholeText()", + "Belgian waffles & strawberries (< 5g of fat)", descriptionText2.getWholeText()); + } + + public void testGetWholeTextLast() { + assertEquals("This implementation doesn't include preceding nodes in getWholeText()", + "Belgian waffles & strawberries (< 5g of fat)", descriptionText3.getWholeText()); + } + + public void testGetWholeTextOnly() { + assertEquals("60%", vitamincText.getWholeText()); + } + + public void testGetWholeTextWithEntityReference() { + EntityReference spReference = document.createEntityReference("sp"); + description.insertBefore(spReference, descriptionText2); + + assertEquals("This implementation doesn't resolve entity references in getWholeText()", + "BelgianMaple Syrup waffles & strawberries (< 5g of fat)", + descriptionText1.getWholeText()); + } + + public void testReplaceWholeTextFirst() throws TransformerException { + String original = domToString(document); + Text replacement = descriptionText1.replaceWholeText("Eggos"); + assertSame(descriptionText1, replacement); + String expected = original.replace( + "Belgian<![CDATA[ waffles & strawberries (< 5g ]]>of fat)", "Eggos"); + assertEquals(expected, domToString(document)); + } + + public void testReplaceWholeTextMiddle() throws TransformerException { + String original = domToString(document); + Text replacement = descriptionText2.replaceWholeText("Eggos"); + assertSame(descriptionText2, replacement); + String expected = original.replace( + "Belgian<![CDATA[ waffles & strawberries (< 5g ]]>of fat)", "<![CDATA[Eggos]]>"); + assertEquals("This implementation doesn't remove preceding nodes in replaceWholeText()", + expected, domToString(document)); + } + + public void testReplaceWholeTextLast() throws TransformerException { + String original = domToString(document); + Text replacement = descriptionText3.replaceWholeText("Eggos"); + assertSame(descriptionText3, replacement); + String expected = original.replace( + "Belgian<![CDATA[ waffles & strawberries (< 5g ]]>of fat)", "Eggos"); + assertEquals("This implementation doesn't remove preceding nodes in replaceWholeText()", + expected, domToString(document)); + } + + public void testReplaceWholeTextOnly() throws TransformerException { + String original = domToString(document); + Text replacement = vitamincText.replaceWholeText("70%"); + assertEquals(Node.TEXT_NODE, replacement.getNodeType()); + assertSame(vitamincText, replacement); + String expected = original.replace("60%", "70%"); + assertEquals(expected, domToString(document)); + } + + public void testReplaceWholeTextFirstWithNull() throws TransformerException { + String original = domToString(document); + assertNull(descriptionText1.replaceWholeText(null)); + String expected = original.replaceFirst(">.*</description>", "/>"); + assertEquals("This implementation doesn't remove adjacent nodes in replaceWholeText(null)", + expected, domToString(document)); + } + + public void testReplaceWholeTextMiddleWithNull() throws TransformerException { + String original = domToString(document); + assertNull(descriptionText2.replaceWholeText(null)); + String expected = original.replaceFirst(">.*</description>", "/>"); + assertEquals("This implementation doesn't remove adjacent nodes in replaceWholeText(null)", + expected, domToString(document)); + } + + public void testReplaceWholeTextLastWithNull() throws TransformerException { + String original = domToString(document); + assertNull(descriptionText3.replaceWholeText(null)); + String expected = original.replaceFirst(">.*</description>", "/>"); + assertEquals("This implementation doesn't remove adjacent nodes in replaceWholeText(null)", + expected, domToString(document)); + } + + public void testReplaceWholeTextFirstWithEmptyString() throws TransformerException { + String original = domToString(document); + assertNull(descriptionText1.replaceWholeText("")); + String expected = original.replaceFirst(">.*</description>", "/>"); + assertEquals("This implementation doesn't remove adjacent nodes in replaceWholeText(null)", + expected, domToString(document)); + } + + public void testReplaceWholeTextOnlyWithEmptyString() throws TransformerException { + String original = domToString(document); + assertNull(vitamincText.replaceWholeText("")); + String expected = original.replaceFirst(">.*</a:vitaminc>", "/>"); + assertEquals(expected, domToString(document)); + } + + private String domToString(Document document) throws TransformerException { StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); return writer.toString(); |