1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    * 
4    * This software is open source. 
5    * See the bottom of this file for the licence.
6    * 
7    * $Id: TestAddAttribute.java,v 1.4 2004/06/25 08:03:47 maartenc Exp $
8    */
9   
10  package org.dom4j;
11  
12  import junit.framework.Test;
13  import junit.framework.TestSuite;
14  import junit.textui.TestRunner;
15  
16  /*** A test harness to test the addAttribute() methods on attributes
17    *
18    * @author <a href="mailto:maartenc@users.sourceforge.net">Maarten Coene</a>
19    */
20  public class TestAddAttribute extends AbstractTestCase {
21  
22      public static void main( String[] args ) {
23          TestRunner.run( suite() );
24      }
25      
26      public static Test suite() {
27          return new TestSuite( TestAddAttribute.class );
28      }
29      
30      public TestAddAttribute(String name) {
31          super(name);
32      }
33  
34      // Test case(s)
35      //-------------------------------------------------------------------------                    
36      public void testAddAttributeNormalValue() throws Exception {
37          String testAttributeName = "testAtt";
38          String testAttributeValue = "testValue";
39          
40          Node authorNode = document.selectSingleNode("//root/author[1]"); 
41  
42          assertTrue(authorNode instanceof Element);
43          
44          Element authorEl = (Element) authorNode;
45          authorEl.addAttribute(testAttributeName, testAttributeValue);
46          
47          assertEquals(3, authorEl.attributeCount());
48          assertEquals(testAttributeValue, authorEl.attributeValue(testAttributeName));
49      }
50      
51      public void testAddAttributeNullValue() throws Exception {
52          String testAttributeName = "location";
53          String testAttributeValue = null;
54          
55          Node authorNode = document.selectSingleNode("//root/author[1]"); 
56  
57          assertTrue(authorNode instanceof Element);
58          
59          Element authorEl = (Element) authorNode;
60          authorEl.addAttribute(testAttributeName, testAttributeValue);
61          
62          assertEquals(1, authorEl.attributeCount());
63          assertNull(authorEl.attributeValue(testAttributeName));
64      }
65      
66  }
67  
68  
69  
70  
71  /*
72   * Redistribution and use of this software and associated documentation
73   * ("Software"), with or without modification, are permitted provided
74   * that the following conditions are met:
75   *
76   * 1. Redistributions of source code must retain copyright
77   *    statements and notices.  Redistributions must also contain a
78   *    copy of this document.
79   *
80   * 2. Redistributions in binary form must reproduce the
81   *    above copyright notice, this list of conditions and the
82   *    following disclaimer in the documentation and/or other
83   *    materials provided with the distribution.
84   *
85   * 3. The name "DOM4J" must not be used to endorse or promote
86   *    products derived from this Software without prior written
87   *    permission of MetaStuff, Ltd.  For written permission,
88   *    please contact dom4j-info@metastuff.com.
89   *
90   * 4. Products derived from this Software may not be called "DOM4J"
91   *    nor may "DOM4J" appear in their names without prior written
92   *    permission of MetaStuff, Ltd. DOM4J is a registered
93   *    trademark of MetaStuff, Ltd.
94   *
95   * 5. Due credit should be given to the DOM4J Project - 
96   *    http://www.dom4j.org
97   *
98   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
99   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
100  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
101  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
102  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
103  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
104  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
105  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
106  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
107  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
108  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
109  * OF THE POSSIBILITY OF SUCH DAMAGE.
110  *
111  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
112  *
113  * $Id: TestAddAttribute.java,v 1.4 2004/06/25 08:03:47 maartenc Exp $
114  */