📄 testelement.java
字号:
Element element = new Element("el");
assertEquals("did not return Collections.EMPTY_LIST on empty element", Collections.EMPTY_LIST, element.getChildren("child"));
Element child1 = new Element("child");
child1.setAttribute(new Attribute("name", "first"));
Element child2 = new Element("child");
child2.setAttribute(new Attribute("name", "second"));
Element child3 = new Element("child", Namespace.getNamespace("ftp://wombat.stew"));
element.addContent(child1);
element.addContent(child2);
element.addContent(child3);
//should only get the child elements in NO_NAMESPACE
List list = element.getChildren("child");
assertEquals("incorrect number of children returned", list.size(), 2);
assertEquals("incorrect child returned", ((Element) list.get(0)).getAttribute("name").getValue(), "first");
assertEquals("incorrect child returned", ((Element) list.get(1)).getAttribute("name").getValue(), "second");
}
/**
* Test that Element returns a List of children by name and namespace
*/
public void test_TCM__List_getChildren_String_OrgJdomNamespace() {
Element element = new Element("el");
assertEquals("did not return Collections.EMPTY_LIST on empty element", Collections.EMPTY_LIST, element.getChildren("child"));
Namespace ns = Namespace.getNamespace("urn:hogwarts");
Element child1 = new Element("child", ns);
child1.setAttribute(new Attribute("name", "first"));
Element child2 = new Element("child", ns);
child2.setAttribute(new Attribute("name", "second"));
Element child3 = new Element("child", Namespace.getNamespace("ftp://wombat.stew"));
element.addContent(child1);
element.addContent(child2);
element.addContent(child3);
//should only get the child elements in the hogwarts namespace
List list = element.getChildren("child", ns);
assertEquals("incorrect number of children returned", list.size(), 2);
assertEquals("incorrect child returned", ((Element) list.get(0)).getAttribute("name").getValue(), "first");
assertEquals("incorrect child returned", ((Element) list.get(1)).getAttribute("name").getValue(), "second");
}
/**
* Test that getContent returns all the content for the element
*/
public void test_TCM__List_getContent() {
Element element = new Element("el");
assertEquals("did not return Collections.EMPTY_LIST on empty element", Collections.EMPTY_LIST, element.getContent());
Namespace ns = Namespace.getNamespace("urn:hogwarts");
Element child1 = new Element("child", ns);
child1.setAttribute(new Attribute("name", "first"));
Element child2 = new Element("child", ns);
child2.setAttribute(new Attribute("name", "second"));
Element child3 = new Element("child", Namespace.getNamespace("ftp://wombat.stew"));
element.addContent(child1);
element.addContent(child2);
element.addContent(child3);
Comment comment = new Comment("hi");
element.addContent(comment);
CDATA cdata = new CDATA("gotcha");
element.addContent(cdata);
ProcessingInstruction pi = new ProcessingInstruction("tester", "do=something");
element.addContent(pi);
EntityRef entity = new EntityRef("wizards");
element.addContent(entity);
Text text = new Text("finally a new wand!");
element.addContent(text);
List list = element.getContent();
assertEquals("incorrect number of content items", 8, list.size());
assertEquals("wrong child element", child1, list.get(0));
assertEquals("wrong child element", child2, list.get(1));
assertEquals("wrong child element", child3, list.get(2));
assertEquals("wrong comment", comment, list.get(3));
assertEquals("wrong CDATA", cdata, list.get(4));
assertEquals("wrong ProcessingInstruction", pi, list.get(5));
assertEquals("wrong EntityRef", entity, list.get(6));
assertEquals("wrong text", text, list.get(7));
}
/**
* Test that clone returns a disconnected copy of the original element.
* Since clone is a deep copy, that must be tested also
*/
public void test_TCM__Object_clone() {
//first the simple case
Element element = new Element("simple");
Element clone = (Element) element.clone();
assertTrue("clone should not be the same object", element != clone);
assertEquals("clone should not have a parent", null, clone.getParent());
assertEquals("names do not match", element.getName(), clone.getName());
//now do the content tests to 2 levels deep to verify recursion
element = new Element("el");
Namespace ns = Namespace.getNamespace("urn:hogwarts");
element.setAttribute(new Attribute("name", "anElement"));
Element child1 = new Element("child", ns);
child1.setAttribute(new Attribute("name", "first"));
Element child2 = new Element("firstChild", ns);
child2.setAttribute(new Attribute("name", "second"));
Element child3 = new Element("child", Namespace.getNamespace("ftp://wombat.stew"));
child1.addContent(child2);
element.addContent(child1);
element.addContent(child3);
element.addNamespaceDeclaration(Namespace.getNamespace("foo", "http://test1"));
element.addNamespaceDeclaration(Namespace.getNamespace("bar", "http://test2"));
//add mixed content to the nested child2 element
Comment comment = new Comment("hi");
child2.addContent(comment);
CDATA cdata = new CDATA("gotcha");
child2.addContent(cdata);
ProcessingInstruction pi = new ProcessingInstruction("tester", "do=something");
child2.addContent(pi);
EntityRef entity = new EntityRef("wizards");
child2.addContent(entity);
child2.addContent("finally a new wand!");
//a little more for the element
element.addContent("top level element text");
clone = (Element) element.clone();
element = null;
child3 = null;
child2 = null;
child1 = null;
// additional namespaces
List additional = clone.getAdditionalNamespaces();
assertEquals("incorrect deep clone additional namespace",
(Namespace) additional.get(0),
Namespace.getNamespace("foo", "http://test1"));
assertEquals("incorrect deep clone additional namespace",
(Namespace) additional.get(1),
Namespace.getNamespace("bar", "http://test2"));
List list = clone.getContent();
//finally the test
assertEquals("wrong child element", ((Element) list.get(0)).getName(), "child");
assertEquals("wrong child element", ((Element) list.get(1)).getName(), "child");
Element deepClone = ((Element) list.get(0)).getChild("firstChild", Namespace.getNamespace("urn:hogwarts"));
assertEquals("wrong nested element", "firstChild", deepClone.getName());
//comment
assertTrue("deep clone comment not a clone", deepClone.getContent().get(0) != comment);
comment = null;
assertEquals("incorrect deep clone comment", "hi", ((Comment) deepClone.getContent().get(0)).getText());
//CDATA
assertTrue("deep clone CDATA not a clone", ((CDATA) deepClone.getContent().get(1)).getText().equals(cdata.getText()));
cdata = null;
assertEquals("incorrect deep clone CDATA", "gotcha", ((CDATA) deepClone.getContent().get(1)).getText());
//PI
assertTrue("deep clone PI not a clone", deepClone.getContent().get(2) != pi);
pi = null;
assertEquals("incorrect deep clone PI", "do=something", ((ProcessingInstruction) deepClone.getContent().get(2)).getData());
//entity
assertTrue("deep clone Entity not a clone", deepClone.getContent().get(3) != entity);
entity = null;
assertEquals("incorrect deep clone EntityRef", "wizards", ((EntityRef) deepClone.getContent().get(3)).getName());
//text
assertEquals("incorrect deep clone test", "finally a new wand!", ((Text) deepClone.getContent().get(4)).getText());
}
/**
* Test getAttribute by name
*/
public void test_TCM__OrgJdomAttribute_getAttribute_String() {
Element element = new Element("el");
Attribute att = new Attribute("name", "first");
element.setAttribute(att);
assertEquals("incorrect Attribute returned", element.getAttribute("name"), att);
}
/**
* Test getAttribute by name and namespace
*/
public void test_TCM__OrgJdomAttribute_getAttribute_String_OrgJdomNamespace() {
Element element = new Element("el");
Namespace ns = Namespace.getNamespace("x", "urn:foo");
Attribute att = new Attribute("name", "first", ns);
element.setAttribute(att);
element.setAttribute(new Attribute("name", "first"));
assertEquals("incorrect Attribute returned", element.getAttribute("name", ns), att);
}
/**
* Test that an element returns the reference to it's enclosing document
*/
public void test_TCM__OrgJdomDocument_getDocument() {
Element element = new Element("element");
assertNull("incorrectly returned document before there is one", element.getDocument());
Element child = new Element("child");
Element child2 = new Element("child");
element.addContent(child);
element.addContent(child2);
Document doc = new Document(element);
assertEquals("didn't return correct Document", element.getDocument(), doc);
assertEquals("didn't return correct Document", child.getDocument(), doc);
}
/**
* Test addContent for CDATA.
*/
public void test_TCM__OrgJdomElement_addContent_OrgJdomCDATA() {
//positive test is covered in test__List_getContent()
//test with null
Element element = new Element("element");
CDATA cdata = null;
try {
element.addContent(cdata);
fail("didn't catch null CDATA element");
}
catch (IllegalAddException e) {
}
catch (NullPointerException e) {
fail("NullPointer Exception");
}
}
/**
* Test adding comment content.
*/
public void test_TCM__OrgJdomElement_addContent_OrgJdomComment() {
Element element = new Element("element");
Comment comm = new Comment("a comment");
element.addContent(comm);
assertEquals("didn't add comment content", element.getContent().get(0), comm);
try {
comm = null;
element.addContent(comm);
fail("didn't catch null Comment");
}
catch (IllegalAddException e) {
}
catch (NullPointerException e) {
}
}
/**
* Test addContent for Element.
*/
public void test_TCM__OrgJdomElement_addContent_OrgJdomElement() {
//positive test is covered in test__List_getContent()
//test with null
Element element = new Element("element");
try {
Element el = null;
element.addContent(el);
fail("didn't catch null Element");
}
catch (IllegalAddException e) {
}
catch (NullPointerException e) {
}
}
/**
* Test addContent for Entity
*/
public void test_TCM__OrgJdomElement_addContent_OrgJdomEntityRef() {
//positive test is covered in test__List_getContent()
//try with null
Element element = new Element("element");
try {
EntityRef entity = null;
element.addContent(entity);
fail("didn't catch null EntityRef");
}
catch (IllegalAddException e) {
}
catch (NullPointerException e) {
}
}
/**
* Test addContent for ProcessingInstruction.
*/
public void test_TCM__OrgJdomElement_addContent_OrgJdomProcessingInstruction() {
//positive test is covered in test__List_getContent()
//try with null data
Element element = new Element("element");
try {
ProcessingInstruction pi = null;
element.addContent(pi);
fail("didn't catch null ProcessingInstruction");
}
catch (IllegalAddException e) {
}
catch (NullPointerException e) {
}
}
/**
* Test that string based content is added correctly to an Element.
*/
public void test_TCM__OrgJdomElement_addContent_String() {
Element element = new Element("element");
element.addContent("the first part");
assertEquals("incorrect string value", "the first part", element.getText());
element.addContent("the second part");
assertEquals("incorrect string value", "the first partthe second part", element.getText());
//make sure it still works with mixed content
element.addContent(new Element("child"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -