diff options
author | Jesse Wilson <jessewilson@google.com> | 2010-02-23 15:23:26 -0800 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2010-02-23 17:56:48 -0800 |
commit | bda224da00c0372a7752b1304aeda98e2930c4af (patch) | |
tree | a28550992209058749a605e25ee35a48cd459964 /xml/src/test/java | |
parent | ea6435b142df4aaaf8854b3200b9f442b331f143 (diff) | |
download | libcore-bda224da00c0372a7752b1304aeda98e2930c4af.zip libcore-bda224da00c0372a7752b1304aeda98e2930c4af.tar.gz libcore-bda224da00c0372a7752b1304aeda98e2930c4af.tar.bz2 |
Implementing still more DOM API for text nodes.
- Text.isElementContentWhitespace()
- Text.getWholeText()
- Text.replaceWholeText()
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 6b10a0d..ea7abed 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 @@ -562,24 +562,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 { @@ -587,10 +581,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 @@ -599,8 +589,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 @@ -608,8 +596,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 ca7cf71..e6d6481 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 @@ -677,8 +677,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 89da364..597e35e 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(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(); |