1 7 8 package org.dom4j; 9 10 import junit.textui.TestRunner; 11 12 17 public class AddAttributeTest extends AbstractTestCase { 18 public static void main(String [] args) { 19 TestRunner.run(AddAttributeTest.class); 20 } 21 22 public void testAddAttributeNormalValue() throws Exception { 25 String testAttributeName = "testAtt"; 26 String testAttributeValue = "testValue"; 27 28 Node authorNode = document.selectSingleNode("//root/author[1]"); 29 30 assertTrue(authorNode instanceof Element); 31 32 Element authorEl = (Element) authorNode; 33 authorEl.addAttribute(testAttributeName, testAttributeValue); 34 35 assertEquals(3, authorEl.attributeCount()); 36 assertEquals(testAttributeValue, authorEl 37 .attributeValue(testAttributeName)); 38 } 39 40 public void testAddAttributeNullValue() throws Exception { 41 String testAttributeName = "location"; 42 String testAttributeValue = null; 43 44 Node authorNode = document.selectSingleNode("//root/author[1]"); 45 46 assertTrue(authorNode instanceof Element); 47 48 Element authorEl = (Element) authorNode; 49 authorEl.addAttribute(testAttributeName, testAttributeValue); 50 51 assertEquals(1, authorEl.attributeCount()); 52 assertNull(authorEl.attributeValue(testAttributeName)); 53 } 54 } 55 56 92 | Popular Tags |