KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestAttributeDetach


1 /*
2  * Copyright 2001 (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.1 2003/07/07 10:30:29 per_nyfelt Exp $
8  */

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 JavaDoc;
20 import java.util.List JavaDoc;
21
22 /** A test harness to test the detach() method on attributes
23   *
24   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
25   * @version $Revision: 1.1 $
26   */

27 public class TestAttributeDetach extends AbstractTestCase {
28
29     public static void main( String JavaDoc[] args ) {
30         TestRunner.run( suite() );
31     }
32
33     public static Test suite() {
34         return new TestSuite( TestAttributeDetach.class );
35     }
36
37     public TestAttributeDetach(String JavaDoc name) {
38         super(name);
39     }
40
41     // Test case(s)
42
//-------------------------------------------------------------------------
43
public void testDetachAttribute() throws Exception JavaDoc {
44         List JavaDoc attributes = document.selectNodes( "//@name" );
45
46         assertTrue( "Found more than one attribute: ", attributes.size() > 0 );
47
48         for ( Iterator JavaDoc 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 JavaDoc value = attribute.getValue();
62             String JavaDoc 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 /*
82  * Redistribution and use of this software and associated documentation
83  * ("Software"), with or without modification, are permitted provided
84  * that the following conditions are met:
85  *
86  * 1. Redistributions of source code must retain copyright
87  * statements and notices. Redistributions must also contain a
88  * copy of this document.
89  *
90  * 2. Redistributions in binary form must reproduce the
91  * above copyright notice, this list of conditions and the
92  * following disclaimer in the documentation and/or other
93  * materials provided with the distribution.
94  *
95  * 3. The name "DOM4J" must not be used to endorse or promote
96  * products derived from this Software without prior written
97  * permission of MetaStuff, Ltd. For written permission,
98  * please contact dom4j-info@metastuff.com.
99  *
100  * 4. Products derived from this Software may not be called "DOM4J"
101  * nor may "DOM4J" appear in their names without prior written
102  * permission of MetaStuff, Ltd. DOM4J is a registered
103  * trademark of MetaStuff, Ltd.
104  *
105  * 5. Due credit should be given to the DOM4J Project
106  * (http://dom4j.org/).
107  *
108  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
109  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
110  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
111  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
112  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
113  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
114  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
115  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
116  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
117  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
118  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
119  * OF THE POSSIBILITY OF SUCH DAMAGE.
120  *
121  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
122  *
123  * $Id: TestAttributeDetach.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
124  */

125
Popular Tags