1 8 9 package test.dom4j; 10 11 import junit.framework.Test; 12 import junit.framework.TestSuite; 13 import junit.textui.TestRunner; 14 import org.dom4j.Element; 15 import org.dom4j.Node; 16 17 21 public class TestAddAttribute extends AbstractTestCase { 22 23 public static void main( String [] args ) { 24 TestRunner.run( suite() ); 25 } 26 27 public static Test suite() { 28 return new TestSuite( TestAddAttribute.class ); 29 } 30 31 public TestAddAttribute(String name) { 32 super(name); 33 } 34 35 public void testAddAttributeNormalValue() throws Exception { 38 String testAttributeName = "testAtt"; 39 String testAttributeValue = "testValue"; 40 41 Node authorNode = document.selectSingleNode("//root/author[1]"); 42 43 assertTrue(authorNode instanceof Element); 44 45 Element authorEl = (Element) authorNode; 46 authorEl.addAttribute(testAttributeName, testAttributeValue); 47 48 assertEquals(3, authorEl.attributeCount()); 49 assertEquals(testAttributeValue, authorEl.attributeValue(testAttributeName)); 50 } 51 52 public void testAddAttributeNullValue() throws Exception { 53 String testAttributeName = "location"; 54 String testAttributeValue = null; 55 56 Node authorNode = document.selectSingleNode("//root/author[1]"); 57 58 assertTrue(authorNode instanceof Element); 59 60 Element authorEl = (Element) authorNode; 61 authorEl.addAttribute(testAttributeName, testAttributeValue); 62 63 assertEquals(1, authorEl.attributeCount()); 64 assertNull(authorEl.attributeValue(testAttributeName)); 65 } 66 67 } 68 69 70 71 72 116 | Popular Tags |