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