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.Document; 16 import org.dom4j.Element; 17 import org.dom4j.io.OutputFormat; 18 import org.dom4j.io.XMLWriter; 19 import org.dom4j.util.NodeComparator; 20 21 import java.util.Comparator ; 22 23 28 public class TestClone extends AbstractTestCase { 29 30 private static final boolean VERBOSE = false; 31 32 private Comparator comparator = new NodeComparator(); 33 private XMLWriter writer; 34 35 public static void main( String [] args ) { 36 TestRunner.run( suite() ); 37 } 38 39 public static Test suite() { 40 return new TestSuite( TestClone.class ); 41 } 42 43 public TestClone(String name) throws Exception { 44 super(name); 45 writer = new XMLWriter( 46 System.out, OutputFormat.createPrettyPrint() 47 ); 48 49 } 50 51 public void testDocumentClone() throws Exception { 54 document.setName( "doc1" ); 55 System.out.println("document name set to doc1"); 56 57 Document doc2 = (Document) document.clone(); 58 59 assertTrue( "Returned a new document", document != doc2 ); 60 61 if ( VERBOSE ) { 62 log( "document1" ); 63 writer.write( document ); 64 65 log( "document2" ); 66 writer.write( doc2 ); 67 } 68 69 assertTrue( "Documents are equal", comparator.compare( document, doc2 ) == 0 ); 70 } 71 72 public void testRootElementClone() throws Exception { 73 testElementClone( document.getRootElement() ); 74 } 75 76 public void testAuthorElementClone() throws Exception { 77 testElementClone( (Element) document.selectSingleNode( "//author" ) ); 78 } 79 80 public void testRootCompare1() throws Exception { 81 Document doc2 = (Document) document.clone(); 82 Element author = doc2.getRootElement(); 83 author.addAttribute( "foo", "bar" ); 84 85 assertTrue( "Documents are not equal", comparator.compare( document, doc2 ) != 0 ); 86 } 87 88 public void testRootCompare2() throws Exception { 89 Document doc2 = (Document) document.clone(); 90 Element author = doc2.getRootElement(); 91 92 author.addText( "foo" ); 93 94 assertTrue( "Documents are not equal", comparator.compare( document, doc2 ) != 0 ); 95 } 96 97 public void testAuthorCompare1() throws Exception { 98 Document doc2 = (Document) document.clone(); 99 Element author = (Element) doc2.selectSingleNode( "//author" ); 100 author.addAttribute( "name", "Per Nyfelt" ); 101 102 if ( VERBOSE ) { 103 System.out.println("testAuthorCompare1"); 104 System.out.println("document:"); 105 writer.write(document); 106 System.out.println("doc2:"); 107 writer.write(doc2); 108 } 109 110 assertTrue( "Documents are not equal", comparator.compare( document, doc2 ) != 0 ); 111 } 112 113 public void testAuthorCompare2() throws Exception { 114 Document doc2 = (Document) document.clone(); 115 Element author = (Element) doc2.selectSingleNode( "//author" ); 116 117 author.addText( "foo" ); 118 119 if ( VERBOSE ) { 120 System.out.println("testAuthorCompare2"); 121 System.out.println("document:"); 122 writer.write(document); 123 System.out.println("doc2:"); 124 writer.write(doc2); 125 } 126 127 assertTrue( "Documents are not equal", comparator.compare( document, doc2 ) != 0 ); 128 } 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 assertTrue( "New element has no parent", element2.getParent() == null ); 136 assertTrue( "New element has no Document", element2.getDocument() == null ); 137 138 assertTrue( "Element fragments are equal", comparator.compare( element, element2 ) == 0 ); 139 } 140 141 } 142 143 144 145 146 190 | Popular Tags |