1 7 8 package org.dom4j; 9 10 import junit.textui.TestRunner; 11 12 import java.util.Comparator ; 13 14 import org.dom4j.dom.DOMDocument; 15 import org.dom4j.dom.DOMDocumentFactory; 16 import org.dom4j.util.NodeComparator; 17 18 24 public class CloneTest extends AbstractTestCase { 25 private Comparator comparator = new NodeComparator(); 26 27 public static void main(String [] args) { 28 TestRunner.run(CloneTest.class); 29 } 30 31 public void testBug1148333() { 34 DOMDocumentFactory factory = (DOMDocumentFactory) DOMDocumentFactory 35 .getInstance(); 36 DOMDocument doc = (DOMDocument) factory.createDocument(); 37 Element el = doc.addElement("root"); 38 el.addNamespace("pref2", "uri2"); 39 40 DOMDocument clone = (DOMDocument) doc.cloneNode(true); 41 42 assertNotSame(doc, clone); 43 assertNodesEqual(doc, clone); 44 } 45 46 public void testElementWithNamespaceClone() { 47 Element element = DocumentFactory.getInstance() 48 .createElement("element"); 49 element.addNamespace("prefix", "uri"); 50 Element clone = (Element) element.clone(); 51 52 assertNotSame(element, clone); 53 assertNodesEqual(element, clone); 54 } 55 56 public void testDocumentClone() throws Exception { 57 document.setName("doc1"); 58 59 Document doc2 = (Document) document.clone(); 60 61 assertNotSame(document, doc2); 62 assertNodesEqual(document, doc2); 63 } 64 65 public void testAddCloneToOtherElement() { 66 DocumentFactory factory = DocumentFactory.getInstance(); 67 Document doc = factory.createDocument(); 68 Element root = doc.addElement("root"); 69 Element parent1 = root.addElement("parent"); 70 Element child1 = parent1.addElement("child"); 71 72 Element parent2 = (Element) parent1.clone(); 73 root.add(parent2); 74 75 assertSame("parent not correct", root, parent2.getParent()); 76 assertSame("document not correct", doc, parent2.getDocument()); 77 78 Element child2 = parent2.element("child"); 79 80 assertNotSame("child not cloned", child1, child2); 81 assertSame("parent not correct", parent2, child2.getParent()); 82 assertSame("document not correct", doc, child2.getDocument()); 83 } 84 85 public void testRootElementClone() throws Exception { 86 testElementClone(document.getRootElement()); 87 } 88 89 public void testAuthorElementClone() throws Exception { 90 testElementClone((Element) document.selectSingleNode("//author")); 91 } 92 93 public void testRootCompare1() throws Exception { 94 Document doc2 = (Document) document.clone(); 95 Element author = doc2.getRootElement(); 96 author.addAttribute("foo", "bar"); 97 98 assertTrue("Documents are not equal", comparator 99 .compare(document, doc2) != 0); 100 } 101 102 public void testRootCompare2() throws Exception { 103 Document doc2 = (Document) document.clone(); 104 Element author = doc2.getRootElement(); 105 106 author.addText("foo"); 107 108 assertTrue("Documents are not equal", comparator 109 .compare(document, doc2) != 0); 110 } 111 112 public void testAuthorCompare1() throws Exception { 113 Document doc2 = (Document) document.clone(); 114 Element author = (Element) doc2.selectSingleNode("//author"); 115 author.addAttribute("name", "James Strachan"); 116 117 assertTrue("Documents are not equal", comparator 118 .compare(document, doc2) != 0); 119 } 120 121 public void testAuthorCompare2() throws Exception { 122 Document doc2 = (Document) document.clone(); 123 Element author = (Element) doc2.selectSingleNode("//author"); 124 125 author.addText("foo"); 126 127 assertTrue("Documents are not equal", comparator 128 .compare(document, doc2) != 0); 129 } 130 131 protected void testElementClone(Element element) throws Exception { 132 Element element2 = (Element) element.clone(); 133 134 assertTrue("Returned a new Element", element2 != element); 135 assertNull("New element has no parent", element2.getParent()); 136 assertNull("New element has no Document", element2.getDocument()); 137 138 assertTrue("Element fragments are equal", comparator.compare(element, 139 element2) == 0); 140 } 141 } 142 143 179
| Popular Tags
|