diff options
author | Jesse Wilson <jessewilson@google.com> | 2010-03-08 17:00:19 -0800 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2010-03-08 17:00:19 -0800 |
commit | a48356a4fc2cacdf8d466680bc456ae69215e027 (patch) | |
tree | 0b2048ffb393546b2244c9da2331027b14e56fb6 /xml/src/test | |
parent | 089764723bc525c48ebf6b228ef2c62c552f9e30 (diff) | |
parent | ec02394f92b3ff460543dad5c57bc2ee6474683f (diff) | |
download | libcore-a48356a4fc2cacdf8d466680bc456ae69215e027.zip libcore-a48356a4fc2cacdf8d466680bc456ae69215e027.tar.gz libcore-a48356a4fc2cacdf8d466680bc456ae69215e027.tar.bz2 |
am 631bbbff: Merge "Adding support for getUserData() and setUserData() to DOM nodes."
Merge commit '631bbbff684e9fe41a5a08ffa3e13fa3bed01212' into dalvik-dev
* commit '631bbbff684e9fe41a5a08ffa3e13fa3bed01212':
Adding support for getUserData() and setUserData() to DOM nodes.
Diffstat (limited to 'xml/src/test')
-rw-r--r-- | xml/src/test/java/tests/xml/DomTest.java | 90 |
1 files changed, 89 insertions, 1 deletions
diff --git a/xml/src/test/java/tests/xml/DomTest.java b/xml/src/test/java/tests/xml/DomTest.java index 69e8b37..0bb27dc 100644 --- a/xml/src/test/java/tests/xml/DomTest.java +++ b/xml/src/test/java/tests/xml/DomTest.java @@ -31,6 +31,7 @@ import org.w3c.dom.Node; import org.w3c.dom.Notation; import org.w3c.dom.ProcessingInstruction; import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; import org.xml.sax.InputSource; import javax.xml.parsers.DocumentBuilder; @@ -44,7 +45,11 @@ import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; + +import static org.w3c.dom.UserDataHandler.NODE_CLONED; /** * Construct a DOM and then interrogate it. @@ -87,6 +92,7 @@ public class DomTest extends TestCase { private Element name; private Attr standard; private Attr deluxe; + private Text waffles; private Element description; private Text descriptionText1; private CDATASection descriptionText2; @@ -128,6 +134,7 @@ public class DomTest extends TestCase { name = (Element) item.getChildNodes().item(1); standard = name.getAttributeNode("a:standard"); deluxe = name.getAttributeNode("deluxe"); + waffles = (Text) name.getChildNodes().item(0); description = (Element) item.getChildNodes().item(3); descriptionText1 = (Text) description.getChildNodes().item(0); descriptionText2 = (CDATASection) description.getChildNodes().item(1); @@ -153,7 +160,7 @@ public class DomTest extends TestCase { } allNodes.addAll(Arrays.asList(document, doctype, menu, item, itemXmlns, - itemXmlnsA, name, standard, deluxe, description, + itemXmlnsA, name, standard, deluxe, waffles, description, descriptionText1, descriptionText2, descriptionText3, option1, option2, option2Reference, wafflemaker, nutrition, vitamins, vitaminsXmlnsA, comment, vitaminc, vitamincText)); @@ -740,6 +747,87 @@ public class DomTest extends TestCase { assertEquals(expected, domToString(document)); } + public void testUserDataAttachments() { + Object a = new Object(); + Object b = new Object(); + for (Node node : allNodes) { + node.setUserData("a", a, null); + node.setUserData("b", b, null); + } + for (Node node : allNodes) { + assertSame(a, node.getUserData("a")); + assertSame(b, node.getUserData("b")); + assertEquals(null, node.getUserData("c")); + assertEquals(null, node.getUserData("A")); + } + } + + public void testUserDataRejectsNullKey() { + try { + menu.setUserData(null, "apple", null); + fail(); + } catch (NullPointerException e) { + } + try { + menu.getUserData(null); + fail(); + } catch (NullPointerException e) { + } + } + + /** + * A shallow clone requires cloning the attributes but not the child nodes. + */ + public void testUserDataHandlerNotifiedOfShallowClones() { + RecordingHandler handler = new RecordingHandler(); + name.setUserData("a", "apple", handler); + name.setUserData("b", "banana", handler); + standard.setUserData("c", "cat", handler); + waffles.setUserData("d", "dog", handler); + + Element clonedName = (Element) name.cloneNode(false); + Attr clonedStandard = clonedName.getAttributeNode("a:standard"); + + Set<String> expected = new HashSet<String>(); + expected.add(notification(NODE_CLONED, "a", "apple", name, clonedName)); + expected.add(notification(NODE_CLONED, "b", "banana", name, clonedName)); + expected.add(notification(NODE_CLONED, "c", "cat", standard, clonedStandard)); + assertEquals(expected, handler.calls); + } + + /** + * A deep clone requires cloning both the attributes and the child nodes. + */ + public void testUserDataHandlerNotifiedOfDeepClones() { + RecordingHandler handler = new RecordingHandler(); + name.setUserData("a", "apple", handler); + name.setUserData("b", "banana", handler); + standard.setUserData("c", "cat", handler); + waffles.setUserData("d", "dog", handler); + + Element clonedName = (Element) name.cloneNode(true); + Attr clonedStandard = clonedName.getAttributeNode("a:standard"); + Text clonedWaffles = (Text) clonedName.getChildNodes().item(0); + + Set<String> expected = new HashSet<String>(); + expected.add(notification(NODE_CLONED, "a", "apple", name, clonedName)); + expected.add(notification(NODE_CLONED, "b", "banana", name, clonedName)); + expected.add(notification(NODE_CLONED, "c", "cat", standard, clonedStandard)); + expected.add(notification(NODE_CLONED, "d", "dog", waffles, clonedWaffles)); + assertEquals(expected, handler.calls); + } + + private class RecordingHandler implements UserDataHandler { + final Set<String> calls = new HashSet<String>(); + public void handle(short operation, String key, Object data, Node src, Node dst) { + calls.add(notification(operation, key, data, src, dst)); + } + } + + private String notification(short operation, String key, Object data, Node src, Node dst) { + return "op:" + operation + " key:" + key + " data:" + data + " src:" + src + " dst:" + dst; + } + private String domToString(Document document) throws TransformerException { StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); |