📄 testelement.java
字号:
//add some CDATA
element.addContent(new CDATA("the text"));
assertTrue("reported no mixed content when there is", element.hasMixedContent());
element= new Element("element");
//add a PI
element.addContent(new ProcessingInstruction("pi", "the text"));
assertTrue("reported mixed content when there is none", ! element.hasMixedContent());
//add Comment
element.addContent(new Comment("the text"));
assertTrue("reported no mixed content when there is", element.hasMixedContent());
element= new Element("element");
//finally a child element
Element child1= new Element("child1");
element.addContent(child1);
assertTrue("reported mixed content when there is none", ! element.hasMixedContent());
element.addContent("some text");
assertTrue("reported no mixed content when there is", element.hasMixedContent());
*/
}
/**
* Test that an Element can determine if it is a root element
*/
public void test_TCM__boolean_isRootElement() {
Element element = new Element("element");
assertTrue("incorrectly identified element as root", !element.isRootElement());
Document doc = new Document(element);
assertTrue("incorrectly identified element as non root", element.isRootElement());
}
/**
* Test than an Element can remove an Attribute by name
*/
public void test_TCM__boolean_removeAttribute_String() {
Element element = new Element("test");
Attribute att = new Attribute("anAttribute", "test");
element.setAttribute(att);
//make sure it's there
assertNotNull("attribute not found after add", element.getAttribute("anAttribute"));
//and remove it
assertTrue("attribute not removed", element.removeAttribute("anAttribute"));
//make sure it's not there
assertNull("attribute found after remove", element.getAttribute("anAttribute"));
}
/**
* Test removeAttribute with a namespace
*/
public void test_TCM__boolean_removeAttribute_String_OrgJdomNamespace() {
Element element = new Element("test");
Namespace ns = Namespace.getNamespace("x", "urn:test");
Attribute att = new Attribute("anAttribute", "test", ns);
element.setAttribute(att);
//make sure it's there
assertNotNull("attribute not found after add", element.getAttribute("anAttribute", ns));
//and remove it
assertTrue("attribute not removed", element.removeAttribute("anAttribute", ns));
//make sure it's not there
assertNull("attribute found after remove", element.getAttribute("anAttribute", ns));
}
/**
* Test removeAtttribute with a namespace uri.
*/
public void test_TCM__boolean_removeAttribute_String_String() {
/** No op because the method is deprecated
Element element = new Element("test");
Namespace ns = Namespace.getNamespace("x", "urn:test");
Attribute att = new Attribute("anAttribute", "test", ns);
element.setAttribute(att);
//make sure it's there
assertNotNull("attribute not found after add", element.getAttribute("anAttribute", ns));
//and remove it
assertTrue("attribute not removed", element.removeAttribute("anAttribute", "urn:test"));
//make sure it's not there
assertNull("attribute found after remove", element.getAttribute("anAttribute", ns));
*/
}
/**
* Test removeChild by name
*/
public void test_TCM__boolean_removeChild_String() {
Element element = new Element("element");
Element child = new Element("child");
element.addContent(child);
assertTrue("couldn't remove child content", element.removeChild("child"));
assertNull("child not removed", element.getChild("child"));
}
/**
* Test removeChild by name and namespace
*/
public void test_TCM__boolean_removeChild_String_OrgJdomNamespace() {
Namespace ns = Namespace.getNamespace("x", "urn:fudge");
Element element = new Element("element");
Element child = new Element("child", ns);
Element child2 = new Element("child", ns);
element.addContent(child);
assertTrue("couldn't remove child content", element.removeChild("child", ns));
assertNull("child not removed", element.getChild("child", ns));
//now test that only the first child is removed
element.addContent(child);
element.addContent(child2);
assertTrue("couldn't remove child content", element.removeChild("child", ns));
assertNotNull("child not removed", element.getChild("child", ns));
}
/**
* Test removeChildren which removes all child elements
*/
/*
public void test_TCM__boolean_removeChildren() {
Namespace ns = Namespace.getNamespace("x", "urn:fudge");
Element element = new Element("element");
assertTrue("incorrectly returned true when deleting no content", element.removeChildren() == false);
Element child = new Element("child", ns);
Element child2 = new Element("child", ns);
element.addContent(child);
element.addContent(child2);
assertTrue("couldn't remove child content", element.removeChildren());
assertNull("child not removed", element.getContent("child", ns));
}
*/
/**
* Test removeChildren by name.
*/
/*
public void test_TCM__boolean_removeChildren_String() {
Element element = new Element("element");
assertTrue("incorrectly returned true when deleting no content", element.removeChildren() == false);
Element child = new Element("child");
Element child2 = new Element("child");
element.addContent(child);
element.addContent(child2);
assertTrue("incorrect return on bogus child", !element.removeChildren("test"));
assertNotNull("child incorrectly removed", element.getContent("child"));
assertTrue("couldn't remove child content", element.removeChildren("child"));
assertNull("children not removed", element.getContent("child"));
}
*/
/**
* Test removeChildren with a name and namespace
*/
/*
public void test_TCM__boolean_removeChildren_String_OrgJdomNamespace() {
Namespace ns = Namespace.getNamespace("x", "urn:fudge");
Element element = new Element("element");
assertTrue("incorrectly returned true when deleting no content", element.removeChildren() == false);
Element child = new Element("child", ns);
Element child2 = new Element("child", ns);
element.addContent(child);
element.addContent(child2);
assertTrue("incorrect return on bogus child", !element.removeChildren("child"));
assertNotNull("child incorrectly removed", element.getContent("child", ns));
assertTrue("couldn't remove child content", element.removeChildren("child", ns));
assertNull("children not removed", element.getContent("child", ns));
}
*/
/**
* Test removeContent for a Comment
*/
public void test_TCM__boolean_removeContent_OrgJdomComment() {
Element element = new Element("element");
Comment comm = new Comment("a comment");
element.addContent(comm);
assertTrue("couldn't remove comment content", element.removeContent(comm));
assertTrue("didn't remove comment content", element.getContent().equals(Collections.EMPTY_LIST));
}
/**
* Test removeContent for an Element.
*/
public void test_TCM__boolean_removeContent_OrgJdomElement() {
Element element = new Element("element");
Element child = new Element("child");
element.addContent(child);
assertTrue("couldn't remove element content", element.removeContent(child));
assertTrue("didn't remove element content", element.getContent().equals(Collections.EMPTY_LIST));
}
/**
* Test removeContent for entities.
*/
public void test_TCM__boolean_removeContent_OrgJdomEntity() {
Element element = new Element("element");
EntityRef ent = new EntityRef("anEntity");
element.addContent(ent);
assertTrue("couldn't remove entity content", element.removeContent(ent));
assertTrue("didn't remove entity content", element.getContent().equals(Collections.EMPTY_LIST));
}
/**
* Test removeContent for processing instructions.
*/
public void test_TCM__boolean_removeContent_OrgJdomProcessingInstruction() {
Element element = new Element("element");
ProcessingInstruction pi = new ProcessingInstruction("aPi", "something");
element.addContent(pi);
assertTrue("couldn't remove entity content", element.removeContent(pi));
assertTrue("didn't remove entity content", element.getContent().equals(Collections.EMPTY_LIST));
}
/**
* Test hashcode functions.
*/
public void test_TCM__int_hashCode() {
Element element = new Element("test");
//only an exception would be a problem
int i = -1;
try {
i = element.hashCode();
}
catch(Exception e) {
fail("bad hashCode");
}
Element element2 = new Element("test");
//different Elements, same text
int x = element2.hashCode();
assertTrue("Different Elements with same value have same hashcode", x != i);
Element element3 = new Element("test2");
//only an exception would be a problem
int y = element3.hashCode();
assertTrue("Different Elements have same hashcode", y != x);
}
/**
* Test that additionalNamespaces are returned.
*/
public void test_TCM__List_getAdditionalNamespaces() {
Element element = new Element("element");
element.addNamespaceDeclaration(Namespace.getNamespace("x", "urn:foo"));
element.addNamespaceDeclaration(Namespace.getNamespace("y", "urn:bar"));
element.addNamespaceDeclaration(Namespace.getNamespace("z", "urn:baz"));
List list = element.getAdditionalNamespaces();
Namespace ns = (Namespace) list.get(0);
assertTrue("didn't return added namespace", ns.getURI().equals("urn:foo"));
ns = (Namespace) list.get(1);
assertTrue("didn't return added namespace", ns.getURI().equals("urn:bar"));
ns = (Namespace) list.get(2);
assertTrue("didn't return added namespace", ns.getURI().equals("urn:baz"));
}
/**
* Test that getAttribute returns all attributes of this element.
*/
public void test_TCM__List_getAttributes() {
Element element = new Element("test");
Attribute att = new Attribute("anAttribute", "test");
Attribute att2 = new Attribute("anotherAttribute", "test");
Attribute att3 = new Attribute("anotherAttribute", "test", Namespace.getNamespace("x", "urn:JDOM"));
element.setAttribute(att);
element.setAttribute(att2);
element.setAttribute(att3);
List list = element.getAttributes();
assertEquals("incorrect size returned", list.size(), 3);
assertEquals("incorrect attribute returned", list.get(0), att);
assertEquals("incorrect attribute returned", list.get(1), att2);
}
/**
* Test getChildren to return all children from all namespaces.
*/
public void test_TCM__List_getChildren() {
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();
assertEquals("incorrect number of children returned", list.size(), 3);
assertEquals("incorrect child returned", list.get(0), child1);
assertEquals("incorrect child returned", list.get(1), child2);
assertEquals("incorrect child returned", list.get(2), child3);
}
/**
* Test that Element returns a List of children by name
*/
public void test_TCM__List_getChildren_String() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -