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.QName; 18 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 27 public class TestAttributeDetach extends AbstractTestCase { 28 29 public static void main( String [] args ) { 30 TestRunner.run( suite() ); 31 } 32 33 public static Test suite() { 34 return new TestSuite( TestAttributeDetach.class ); 35 } 36 37 public TestAttributeDetach(String name) { 38 super(name); 39 } 40 41 public void testDetachAttribute() throws Exception { 44 List attributes = document.selectNodes( "//@name" ); 45 46 assertTrue( "Found more than one attribute: ", attributes.size() > 0 ); 47 48 for ( Iterator iter = attributes.iterator(); iter.hasNext(); ) { 49 Attribute attribute = (Attribute) iter.next(); 50 Element element = attribute.getParent(); 51 52 assertTrue( 53 "Attribute: " + attribute + " has parent: " + element, 54 attribute.getParent() == element 55 ); 56 57 QName qname = attribute.getQName(); 58 59 Attribute attribute2 = element.attribute( qname ); 60 61 String value = attribute.getValue(); 62 String value2 = element.attributeValue( qname ); 63 64 assertEquals( "Attribute and Element have same attrbute value", value, value2 ); 65 66 attribute.detach(); 67 68 attribute2 = element.attribute( qname ); 69 value2 = element.attributeValue( qname ); 70 71 assertTrue( "Element now has no value: " + value2, value2 == null ); 72 assertTrue( "Element now has no attribute: " + attribute2, attribute2 == null ); 73 } 74 } 75 76 } 77 78 79 80 81 125 | Popular Tags |