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.Attribute; 16 import org.dom4j.Element; 17 import org.dom4j.Node; 18 19 import java.util.List ; 20 21 26 public class TestCopy 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( TestCopy.class ); 34 } 35 36 public TestCopy(String name) { 37 super(name); 38 } 39 40 public void testRoot() throws Exception { 43 document.setName( "doc1" ); 44 45 Element root = document.getRootElement(); 46 List authors = root.elements( "author" ); 47 48 assertTrue( "Should be at least 2 authors", authors.size() == 2 ); 49 50 Element author1 = (Element) authors.get(0); 51 Element author2 = (Element) authors.get(1); 52 53 testCopy( root ); 54 testCopy( author1 ); 55 testCopy( author2 ); 56 } 57 58 59 protected void testCopy(Element element) throws Exception { 60 assertTrue( "Not null", element != null ); 61 62 int attributeCount = element.attributeCount(); 63 int nodeCount = element.nodeCount(); 64 65 Element copy = element.createCopy(); 66 67 assertTrue( "Same node size after copy", element.nodeCount() == nodeCount ); 68 assertTrue( "Same attribute size after copy", element.attributeCount() == attributeCount ); 69 70 assertTrue( "Copy has same node size", copy.nodeCount() == nodeCount ); 71 assertTrue( "Copy has same attribute size", copy.attributeCount() == attributeCount ); 72 73 for (int i = 0; i < attributeCount; i++ ) { 74 Attribute attr1 = element.attribute(i); 75 Attribute attr2 = copy.attribute(i); 76 77 assertTrue( "Attribute: " + i + " name is equal", attr1.getName().equals( attr2.getName() ) ); 78 assertTrue( "Attribute: " + i + " value is equal", attr1.getValue().equals( attr2.getValue() ) ); 79 } 80 81 for (int i = 0; i < nodeCount; i++ ) { 82 Node node1 = element.node(i); 83 Node node2 = copy.node(i); 84 85 assertTrue( "Node: " + i + " type is equal", node1.getNodeType() == node2.getNodeType() ); 86 assertTrue( "Node: " + i + " value is equal", node1.getText().equals( node2.getText() ) ); 87 } 88 } 89 } 90 91 92 93 94 138 | Popular Tags |