1 9 10 package test.dom4j; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 import org.dom4j.Branch; 16 import org.dom4j.Document; 17 import org.dom4j.Element; 18 import org.dom4j.Node; 19 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 20 21 26 public class TestSetContent extends AbstractTestCase { 27 28 public static void main( String [] args ) { 29 TestRunner.run( suite() ); 30 } 31 32 public static Test suite() { 33 return new TestSuite( TestSetContent.class ); 34 } 35 36 public TestSetContent(String name) { 37 super(name); 38 } 39 40 public void testDocument() throws Exception { 43 document.setName( "doc1" ); 44 45 Element oldRoot = document.getRootElement(); 46 47 assertTrue( "Current root has document", oldRoot.getDocument() == document ); 48 49 Document doc2 = O3DocumentHelper.createDocument(); 50 doc2.setName( "doc2" ); 51 52 assertTrue( "Doc2 has no root element", doc2.getRootElement() == null ); 53 54 doc2.setContent( document.content() ); 55 56 Element newRoot = doc2.getRootElement(); 57 58 assertTrue( "Current root has document", oldRoot.getDocument() == document ); 59 60 assertTrue( "Doc2 has now has root element", newRoot != null ); 61 assertTrue( "Doc2 has different root element", newRoot != oldRoot ); 62 assertTrue( "Root element now has document", newRoot.getDocument() == doc2 ); 63 64 testParent( doc2.getRootElement() ); 65 testDocument( doc2, doc2 ); 66 67 doc2.clearContent(); 68 69 assertTrue( "New Doc has no root", doc2.getRootElement() == null ); 70 assertTrue( "New root has no document", newRoot.getDocument() == null ); 71 } 72 73 74 public void testRootElement() throws Exception { 75 Document doc3 = O3DocumentHelper.createDocument(); 76 doc3.setName( "doc3" ); 77 Element root = doc3.addElement( "root" ); 78 Element oldElement = root.addElement( "old" ); 79 80 assertTrue( "Doc3 has root element", root != null ); 81 82 root.setContent( document.getRootElement().content() ); 83 84 assertTrue( "Doc3's root now has content", root.nodeCount() > 0 ); 85 assertTrue( "Old element has no parent now", oldElement.getParent() == null ); 86 assertTrue( "Old element has no document now", oldElement.getDocument() == null ); 87 88 testParent( root ); 89 testDocument( root, doc3 ); 90 } 91 92 93 protected void testParent( Branch parent ) { 94 for ( int i = 0, size = parent.nodeCount(); i < size; i++ ) { 95 Node node = parent.node(i); 96 assertTrue( "Child node of root has parent of root", node.getParent() == parent ); 97 } 98 } 99 100 101 protected void testDocument( Branch branch, Document doc ) { 102 for ( int i = 0, size = branch.nodeCount(); i < size; i++ ) { 103 Node node = branch.node(i); 104 assertTrue( "Node has correct document", node.getDocument() == doc ); 105 } 106 } 107 } 108 109 110 111 112 156 | Popular Tags |